| 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(True) |
|---|
| 29 | |
|---|
| 30 | def teardown(): |
|---|
| 31 | cleanup_all(True) |
|---|
| 32 | |
|---|
| 33 | class TestDeepSet(object): |
|---|
| 34 | def test_set_attr(self): |
|---|
| 35 | t1 = Table1() |
|---|
| 36 | t1.from_dict(dict(name='test1')) |
|---|
| 37 | assert t1.name == 'test1' |
|---|
| 38 | |
|---|
| 39 | def test_nonset_attr(self): |
|---|
| 40 | t1 = Table1(name='test2') |
|---|
| 41 | t1.from_dict({}) |
|---|
| 42 | assert t1.name == 'test2' |
|---|
| 43 | |
|---|
| 44 | def test_set_rel(self): |
|---|
| 45 | t1 = Table1() |
|---|
| 46 | t1.from_dict(dict(tbl3={'name': 'bob'})) |
|---|
| 47 | assert t1.tbl3.name == 'bob' |
|---|
| 48 | |
|---|
| 49 | def test_remove_rel(self): |
|---|
| 50 | t1 = Table1() |
|---|
| 51 | t1.tbl3 = Table3() |
|---|
| 52 | t1.from_dict(dict(tbl3=None)) |
|---|
| 53 | assert t1.tbl3 is None |
|---|
| 54 | |
|---|
| 55 | def test_update_rel(self): |
|---|
| 56 | t1 = Table1() |
|---|
| 57 | t1.tbl3 = Table3(name='fred') |
|---|
| 58 | t1.from_dict(dict(tbl3={'name': 'bob'})) |
|---|
| 59 | assert t1.tbl3.name == 'bob' |
|---|
| 60 | |
|---|
| 61 | def test_extend_list(self): |
|---|
| 62 | t1 = Table1() |
|---|
| 63 | t1.from_dict(dict(tbl2s=[{'name': 'test3'}])) |
|---|
| 64 | assert len(t1.tbl2s) == 1 |
|---|
| 65 | assert t1.tbl2s[0].name == 'test3' |
|---|
| 66 | |
|---|
| 67 | def test_truncate_list(self): |
|---|
| 68 | t1 = Table1() |
|---|
| 69 | t2 = Table2() |
|---|
| 70 | t1.tbl2s.append(t2) |
|---|
| 71 | session.commit() |
|---|
| 72 | t1.from_dict(dict(tbl2s=[])) |
|---|
| 73 | assert len(t1.tbl2s) == 0 |
|---|
| 74 | |
|---|
| 75 | def test_update_list_item(self): |
|---|
| 76 | t1 = Table1() |
|---|
| 77 | t2 = Table2() |
|---|
| 78 | t1.tbl2s.append(t2) |
|---|
| 79 | session.commit() |
|---|
| 80 | t1.from_dict(dict(tbl2s=[{'t2id': t2.t2id, 'name': 'test4'}])) |
|---|
| 81 | assert len(t1.tbl2s) == 1 |
|---|
| 82 | assert t1.tbl2s[0].name == 'test4' |
|---|
| 83 | |
|---|
| 84 | def test_invalid_update(self): |
|---|
| 85 | t1 = Table1() |
|---|
| 86 | t2 = Table2() |
|---|
| 87 | t1.tbl2s.append(t2) |
|---|
| 88 | session.commit() |
|---|
| 89 | try: |
|---|
| 90 | t1.from_dict(dict(tbl2s=[{'t2id':t2.t2id+1}])) |
|---|
| 91 | assert False |
|---|
| 92 | except: |
|---|
| 93 | pass |
|---|
| 94 | |
|---|
| 95 | def test_to(self): |
|---|
| 96 | t1 = Table1(t1id=50, name='test1') |
|---|
| 97 | assert t1.to_dict() == {'t1id': 50, 'name': 'test1'} |
|---|
| 98 | |
|---|
| 99 | def test_to_deep(self): |
|---|
| 100 | t1 = Table1(t1id=51, name='test2') |
|---|
| 101 | assert t1.to_dict(deep={'tbl2s':{}}) == \ |
|---|
| 102 | {'t1id': 51, 'name': 'test2', 'tbl2s': []} |
|---|
| 103 | |
|---|
| 104 | def test_to_deep2(self): |
|---|
| 105 | t1 = Table1(t1id=52, name='test3') |
|---|
| 106 | t2 = Table2(t2id=50, name='test4') |
|---|
| 107 | t1.tbl2s.append(t2) |
|---|
| 108 | session.commit() |
|---|
| 109 | assert t1.to_dict(deep={'tbl2s':{}}) == \ |
|---|
| 110 | {'t1id': 52, |
|---|
| 111 | 'name': 'test3', |
|---|
| 112 | 'tbl2s': [{'t2id': 50, 'name': 'test4'}]} |
|---|
| 113 | |
|---|
| 114 | def test_to_deep3(self): |
|---|
| 115 | t1 = Table1(t1id=53, name='test2') |
|---|
| 116 | t1.tbl3 = Table3(t3id=50, name='wobble') |
|---|
| 117 | session.commit() |
|---|
| 118 | assert t1.to_dict(deep={'tbl3':{}}) == \ |
|---|
| 119 | {'t1id': 53, |
|---|
| 120 | 'name': 'test2', |
|---|
| 121 | 'tbl3': {'t3id': 50, 'name': 'wobble'}} |
|---|
| 122 | |
|---|
| 123 | class TestSetOnAliasedColumn(object): |
|---|
| 124 | def setup(self): |
|---|
| 125 | metadata.bind = 'sqlite:///' |
|---|
| 126 | session.clear() |
|---|
| 127 | |
|---|
| 128 | def teardown(self): |
|---|
| 129 | cleanup_all(True) |
|---|
| 130 | |
|---|
| 131 | def test_set_on_aliased_column(self): |
|---|
| 132 | class A(Entity): |
|---|
| 133 | name = Field(String(60), colname='strName') |
|---|
| 134 | |
|---|
| 135 | setup_all(True) |
|---|
| 136 | |
|---|
| 137 | a = A() |
|---|
| 138 | a.set(name='Aye') |
|---|
| 139 | |
|---|
| 140 | assert a.name == 'Aye' |
|---|
| 141 | session.commit() |
|---|
| 142 | session.clear() |
|---|