| 1 | """ |
|---|
| 2 | test having entities using a custom base class |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from elixir import * |
|---|
| 6 | |
|---|
| 7 | def setup(): |
|---|
| 8 | metadata.bind = 'sqlite:///' |
|---|
| 9 | |
|---|
| 10 | global MyBase |
|---|
| 11 | |
|---|
| 12 | class MyBase(object): |
|---|
| 13 | __metaclass__ = EntityMeta |
|---|
| 14 | |
|---|
| 15 | def __init__(self, **kwargs): |
|---|
| 16 | for key, value in kwargs.items(): |
|---|
| 17 | setattr(self, key, value) |
|---|
| 18 | |
|---|
| 19 | class TestCustomBase(object): |
|---|
| 20 | def teardown(self): |
|---|
| 21 | cleanup_all(True) |
|---|
| 22 | |
|---|
| 23 | def test_simple(self): |
|---|
| 24 | class A(MyBase): |
|---|
| 25 | name = Field(String(30)) |
|---|
| 26 | |
|---|
| 27 | setup_all(True) |
|---|
| 28 | |
|---|
| 29 | a1 = A(name="a1") |
|---|
| 30 | |
|---|
| 31 | session.commit() |
|---|
| 32 | session.clear() |
|---|
| 33 | |
|---|
| 34 | a = A.query.filter_by(name="a1").one() |
|---|
| 35 | |
|---|
| 36 | assert a.name == 'a1' |
|---|
| 37 | |
|---|
| 38 | def test_inherit(self): |
|---|
| 39 | class A(MyBase): |
|---|
| 40 | name = Field(String(30)) |
|---|
| 41 | |
|---|
| 42 | class B(A): |
|---|
| 43 | data = Field(String(30)) |
|---|
| 44 | |
|---|
| 45 | setup_all(True) |
|---|
| 46 | |
|---|
| 47 | a1 = A(name="a1") |
|---|
| 48 | b1 = B(name="b1", data="-b1-") |
|---|
| 49 | |
|---|
| 50 | session.commit() |
|---|
| 51 | session.clear() |
|---|
| 52 | |
|---|
| 53 | b = A.query.filter_by(name="b1").one() |
|---|
| 54 | |
|---|
| 55 | assert b.data == '-b1-' |
|---|
| 56 | |
|---|
| 57 | def test_non_object_base(self): |
|---|
| 58 | class BaseParent(object): |
|---|
| 59 | def test(self): |
|---|
| 60 | return "success" |
|---|
| 61 | |
|---|
| 62 | class InheritedBase(BaseParent): |
|---|
| 63 | __metaclass__ = EntityMeta |
|---|
| 64 | |
|---|
| 65 | class A(InheritedBase): |
|---|
| 66 | name = Field(String(30)) |
|---|
| 67 | |
|---|
| 68 | setup_all(True) |
|---|
| 69 | |
|---|
| 70 | a1 = A() |
|---|
| 71 | a1.name = "a1" |
|---|
| 72 | |
|---|
| 73 | session.commit() |
|---|
| 74 | session.clear() |
|---|
| 75 | |
|---|
| 76 | a = A.query.filter_by(name="a1").one() |
|---|
| 77 | |
|---|
| 78 | assert a.name == 'a1' |
|---|
| 79 | assert a.test() == "success" |
|---|
| 80 | |
|---|
| 81 | def test_base_with_fields(self): |
|---|
| 82 | class FieldBase(object): |
|---|
| 83 | __metaclass__ = EntityMeta |
|---|
| 84 | |
|---|
| 85 | common = Field(String(32)) |
|---|
| 86 | |
|---|
| 87 | class A(FieldBase): |
|---|
| 88 | name = Field(String(32)) |
|---|
| 89 | |
|---|
| 90 | class B(FieldBase): |
|---|
| 91 | pass |
|---|
| 92 | |
|---|
| 93 | setup_all(True) |
|---|
| 94 | |
|---|
| 95 | assert 'name' in A.table.columns.keys() |
|---|
| 96 | assert 'common' in A.table.columns.keys() |
|---|
| 97 | assert 'common' in B.table.columns.keys() |
|---|
| 98 | |
|---|
| 99 | def test_base_with_fields_in_parent(self): |
|---|
| 100 | class BaseParent(object): |
|---|
| 101 | common1 = Field(String(32)) |
|---|
| 102 | |
|---|
| 103 | class FieldBase(BaseParent): |
|---|
| 104 | __metaclass__ = EntityMeta |
|---|
| 105 | |
|---|
| 106 | common2 = Field(String(32)) |
|---|
| 107 | |
|---|
| 108 | class A(FieldBase): |
|---|
| 109 | name = Field(String(32)) |
|---|
| 110 | |
|---|
| 111 | class B(FieldBase): |
|---|
| 112 | pass |
|---|
| 113 | |
|---|
| 114 | setup_all(True) |
|---|
| 115 | |
|---|
| 116 | assert 'name' in A.table.columns.keys() |
|---|
| 117 | assert 'common1' in A.table.columns.keys() |
|---|
| 118 | assert 'common1' in B.table.columns.keys() |
|---|
| 119 | assert 'common2' in A.table.columns.keys() |
|---|
| 120 | assert 'common2' in B.table.columns.keys() |
|---|
| 121 | |
|---|
| 122 | def test_base_with_options(self): |
|---|
| 123 | import re |
|---|
| 124 | |
|---|
| 125 | def camel_to_underscore(entity): |
|---|
| 126 | return re.sub(r'(.+?)([A-Z])+?', r'\1_\2', entity.__name__).lower() |
|---|
| 127 | |
|---|
| 128 | class OptionBase(object): |
|---|
| 129 | __metaclass__ = EntityMeta |
|---|
| 130 | |
|---|
| 131 | using_options_defaults(tablename=camel_to_underscore) |
|---|
| 132 | |
|---|
| 133 | class TestA(OptionBase): |
|---|
| 134 | name = Field(String(32)) |
|---|
| 135 | |
|---|
| 136 | class SuperTestB(OptionBase): |
|---|
| 137 | pass |
|---|
| 138 | |
|---|
| 139 | setup_all(True) |
|---|
| 140 | |
|---|
| 141 | assert TestA.table.name == 'test_a' |
|---|
| 142 | assert SuperTestB.table.name == 'super_test_b' |
|---|