| 1 | """ |
|---|
| 2 | test the deep-set functionality |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import sqlalchemy as sa |
|---|
| 6 | from elixir import * |
|---|
| 7 | |
|---|
| 8 | def setup(): |
|---|
| 9 | metadata.bind = 'sqlite:///' |
|---|
| 10 | |
|---|
| 11 | global Table1, Table2, Table3 |
|---|
| 12 | class Table1(Entity): |
|---|
| 13 | t1id = Field(Integer, primary_key=True) |
|---|
| 14 | name = Field(String(30)) |
|---|
| 15 | tbl2s = OneToMany('Table2') |
|---|
| 16 | tbl3 = OneToOne('Table3') |
|---|
| 17 | |
|---|
| 18 | class Table2(Entity): |
|---|
| 19 | t2id = Field(Integer, primary_key=True) |
|---|
| 20 | name = Field(String(30)) |
|---|
| 21 | tbl1 = ManyToOne(Table1) |
|---|
| 22 | |
|---|
| 23 | class Table3(Entity): |
|---|
| 24 | t3id = Field(Integer, primary_key=True) |
|---|
| 25 | name = Field(String(30)) |
|---|
| 26 | tbl1 = ManyToOne(Table1) |
|---|
| 27 | |
|---|
| 28 | setup_all() |
|---|
| 29 | |
|---|
| 30 | def teardown(): |
|---|
| 31 | cleanup_all() |
|---|
| 32 | |
|---|
| 33 | class TestDeepSet(object): |
|---|
| 34 | def setup(self): |
|---|
| 35 | create_all() |
|---|
| 36 | |
|---|
| 37 | def teardown(self): |
|---|
| 38 | session.close() |
|---|
| 39 | drop_all() |
|---|
| 40 | |
|---|
| 41 | def test_set_attr(self): |
|---|
| 42 | t1 = Table1() |
|---|
| 43 | t1.from_dict(dict(name='test1')) |
|---|
| 44 | assert t1.name == 'test1' |
|---|
| 45 | |
|---|
| 46 | def test_nonset_attr(self): |
|---|
| 47 | t1 = Table1(name='test2') |
|---|
| 48 | t1.from_dict({}) |
|---|
| 49 | assert t1.name == 'test2' |
|---|
| 50 | |
|---|
| 51 | def test_set_rel(self): |
|---|
| 52 | t1 = Table1() |
|---|
| 53 | t1.from_dict(dict(tbl3={'name': 'bob'})) |
|---|
| 54 | assert t1.tbl3.name == 'bob' |
|---|
| 55 | |
|---|
| 56 | def test_remove_rel(self): |
|---|
| 57 | t1 = Table1() |
|---|
| 58 | t1.tbl3 = Table3() |
|---|
| 59 | t1.from_dict(dict(tbl3=None)) |
|---|
| 60 | assert t1.tbl3 is None |
|---|
| 61 | |
|---|
| 62 | def test_update_rel(self): |
|---|
| 63 | t1 = Table1() |
|---|
| 64 | t1.tbl3 = Table3(name='fred') |
|---|
| 65 | t1.from_dict(dict(tbl3={'name': 'bob'})) |
|---|
| 66 | assert t1.tbl3.name == 'bob' |
|---|
| 67 | |
|---|
| 68 | def test_extend_list(self): |
|---|
| 69 | t1 = Table1() |
|---|
| 70 | t1.from_dict(dict(tbl2s=[{'name': 'test3'}])) |
|---|
| 71 | assert len(t1.tbl2s) == 1 |
|---|
| 72 | assert t1.tbl2s[0].name == 'test3' |
|---|
| 73 | |
|---|
| 74 | def test_truncate_list(self): |
|---|
| 75 | t1 = Table1() |
|---|
| 76 | t2 = Table2() |
|---|
| 77 | t1.tbl2s.append(t2) |
|---|
| 78 | session.commit() |
|---|
| 79 | t1.from_dict(dict(tbl2s=[])) |
|---|
| 80 | assert len(t1.tbl2s) == 0 |
|---|
| 81 | |
|---|
| 82 | def test_update_list_item(self): |
|---|
| 83 | t1 = Table1() |
|---|
| 84 | t2 = Table2() |
|---|
| 85 | t1.tbl2s.append(t2) |
|---|
| 86 | session.commit() |
|---|
| 87 | t1.from_dict(dict(tbl2s=[{'t2id': t2.t2id, 'name': 'test4'}])) |
|---|
| 88 | assert len(t1.tbl2s) == 1 |
|---|
| 89 | assert t1.tbl2s[0].name == 'test4' |
|---|
| 90 | |
|---|
| 91 | def test_invalid_update(self): |
|---|
| 92 | t1 = Table1() |
|---|
| 93 | t2 = Table2() |
|---|
| 94 | t1.tbl2s.append(t2) |
|---|
| 95 | session.commit() |
|---|
| 96 | try: |
|---|
| 97 | t1.from_dict(dict(tbl2s=[{'t2id': t2.t2id+1}])) |
|---|
| 98 | assert False |
|---|
| 99 | except: |
|---|
| 100 | pass |
|---|
| 101 | |
|---|
| 102 | def test_to(self): |
|---|
| 103 | t1 = Table1(t1id=50, name='test1') |
|---|
| 104 | assert t1.to_dict() == {'t1id': 50, 'name': 'test1'} |
|---|
| 105 | |
|---|
| 106 | def test_to_deep_m2o(self): |
|---|
| 107 | t1 = Table1(t1id=1, name='test1') |
|---|
| 108 | t2 = Table2(t2id=1, name='test2', tbl1=t1) |
|---|
| 109 | session.flush() |
|---|
| 110 | assert t2.to_dict(deep={'tbl1': {}}) == \ |
|---|
| 111 | {'t2id': 1, 'name': 'test2', 'tbl1_t1id': 1, |
|---|
| 112 | 'tbl1': {'name': 'test1'}} |
|---|
| 113 | |
|---|
| 114 | def test_to_deep_m2o_none(self): |
|---|
| 115 | t2 = Table2(t2id=1, name='test2') |
|---|
| 116 | session.flush() |
|---|
| 117 | assert t2.to_dict(deep={'tbl1': {}}) == \ |
|---|
| 118 | {'t2id': 1, 'name': 'test2', 'tbl1_t1id': None, 'tbl1': None} |
|---|
| 119 | |
|---|
| 120 | def test_to_deep(self): |
|---|
| 121 | t1 = Table1(t1id=51, name='test2') |
|---|
| 122 | assert t1.to_dict(deep={'tbl2s': {}}) == \ |
|---|
| 123 | {'t1id': 51, 'name': 'test2', 'tbl2s': []} |
|---|
| 124 | |
|---|
| 125 | def test_to_deep2(self): |
|---|
| 126 | t1 = Table1(t1id=52, name='test3') |
|---|
| 127 | t2 = Table2(t2id=50, name='test4') |
|---|
| 128 | t1.tbl2s.append(t2) |
|---|
| 129 | session.commit() |
|---|
| 130 | assert t1.to_dict(deep={'tbl2s':{}}) == \ |
|---|
| 131 | {'t1id': 52, |
|---|
| 132 | 'name': 'test3', |
|---|
| 133 | 'tbl2s': [{'t2id': 50, 'name': 'test4'}]} |
|---|
| 134 | |
|---|
| 135 | def test_to_deep3(self): |
|---|
| 136 | t1 = Table1(t1id=53, name='test2') |
|---|
| 137 | t1.tbl3 = Table3(t3id=50, name='wobble') |
|---|
| 138 | session.commit() |
|---|
| 139 | assert t1.to_dict(deep={'tbl3': {}}) == \ |
|---|
| 140 | {'t1id': 53, |
|---|
| 141 | 'name': 'test2', |
|---|
| 142 | 'tbl3': {'t3id': 50, 'name': 'wobble'}} |
|---|
| 143 | |
|---|
| 144 | class TestSetOnAliasedColumn(object): |
|---|
| 145 | def setup(self): |
|---|
| 146 | metadata.bind = 'sqlite:///' |
|---|
| 147 | session.clear() |
|---|
| 148 | |
|---|
| 149 | def teardown(self): |
|---|
| 150 | cleanup_all(True) |
|---|
| 151 | |
|---|
| 152 | def test_set_on_aliased_column(self): |
|---|
| 153 | class A(Entity): |
|---|
| 154 | name = Field(String(60), colname='strName') |
|---|
| 155 | |
|---|
| 156 | setup_all(True) |
|---|
| 157 | |
|---|
| 158 | a = A() |
|---|
| 159 | a.set(name='Aye') |
|---|
| 160 | |
|---|
| 161 | assert a.name == 'Aye' |
|---|
| 162 | session.commit() |
|---|
| 163 | session.clear() |
|---|