root / elixir / trunk / tests / test_dict.py @ 322

Revision 322, 2.7 kB (checked in by ged, 5 years ago)

- Added two new methods on the base entity: from_dict and to_dict, which can

be used to create (or output) a whole hierarchy of instances from (to) a
simple JSON-like dictionary notation (patch from Paul Johnston,
closes ticket #40).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1"""
2    test the deep-set functionality
3"""
4
5import sqlalchemy as sa, elixir as el
6
7def setup():
8    el.metadata.bind = 'sqlite:///'
9    global Table1, Table2, Table3
10    class Table1(el.Entity):
11        name = el.Field(el.String(30))
12        tbl2s = el.OneToMany('Table2')
13        tbl3 = el.OneToOne('Table3')
14    class Table2(el.Entity):
15        name = el.Field(el.String(30))
16        tbl1 = el.ManyToOne(Table1)
17    class Table3(el.Entity):
18        name = el.Field(el.String(30))
19        tbl1 = el.ManyToOne(Table1)
20    el.setup_all()
21    el.create_all()
22
23def test_set_attr():
24    t1 = Table1()
25    t1.from_dict(dict(name='test1'))
26    assert t1.name == 'test1'
27
28def test_nonset_attr():
29    t1 = Table1(name='test2')
30    t1.from_dict({})
31    assert t1.name == 'test2'
32
33def test_set_rel():
34    t1 = Table1()
35    t1.from_dict(dict(tbl3={'name':'bob'}))
36    assert t1.tbl3.name == 'bob'
37
38def test_remove_rel():
39    t1 = Table1()
40    t1.tbl3 = Table3()
41    t1.from_dict(dict(tbl3=None))
42    assert t1.tbl3 is None
43
44def test_update_rel():
45    t1 = Table1()
46    t1.tbl3 = Table3(name='fred')
47    t1.from_dict(dict(tbl3={'name':'bob'}))
48    assert t1.tbl3.name == 'bob'
49
50def test_extend_list():
51    t1 = Table1()
52    t1.from_dict(dict(tbl2s=[{'name':'test3'}]))
53    assert len(t1.tbl2s) == 1
54    assert t1.tbl2s[0].name == 'test3'
55
56def test_truncate_list():
57    t1 = Table1()
58    t2 = Table2()
59    t1.tbl2s.append(t2)
60    el.session.flush()
61    t1.from_dict(dict(tbl2s=[]))
62    assert len(t1.tbl2s) == 0
63
64def test_update_list_item():
65    t1 = Table1()
66    t2 = Table2()
67    t1.tbl2s.append(t2)
68    el.session.flush()
69    t1.from_dict(dict(tbl2s=[{'id':t2.id, 'name':'test4'}]))
70    assert len(t1.tbl2s) == 1
71    assert t1.tbl2s[0].name == 'test4'
72
73def test_invalid_update():
74    t1 = Table1()
75    t2 = Table2()
76    t1.tbl2s.append(t2)
77    el.session.flush()
78    try:
79        t1.from_dict(dict(tbl2s=[{'id':t2.id+1}]))
80        assert False
81    except sa.exceptions.ArgumentError:
82        pass
83
84def test_to():
85    t1 = Table1(id=50, name='test1')
86    assert t1.to_dict() == {'id':50, 'name':'test1'}
87
88def test_to_deep():
89    t1 = Table1(id=51, name='test2')
90    assert t1.to_dict(deep={'tbl2s':{}}) == \
91            {'id':51, 'name':'test2', 'tbl2s':[]}
92
93def test_to_deep2():
94    t1 = Table1(id=52, name='test3')
95    t2 = Table2(id=50, name='test4')
96    t1.tbl2s.append(t2)
97    el.session.flush()
98    assert t1.to_dict(deep={'tbl2s':{}}) == \
99            {'id':52, 'name':'test3', 'tbl2s':[{'id':50, 'name':'test4'}]}
100
101def test_to_deep3():
102    t1 = Table1(id=53, name='test2')
103    t1.tbl3 = Table3(id=50, name='wobble')
104    el.session.flush()
105    assert t1.to_dict(deep={'tbl3':{}}) == \
106            {'id':53, 'name':'test2', 'tbl3':{'id':50,'name':'wobble'}}
Note: See TracBrowser for help on using the browser.