root / elixir / tags / 0.7.0 / tests / test_autosetup.py

Revision 485, 4.1 kB (checked in by ged, 3 years ago)

fix a few cosmetic issues reported by pyflakes (mostly unused imports)

Line 
1"""
2Autosetup tests. Test that all setup triggers actually work.
3"""
4
5from sqlalchemy import Table
6from elixir import *
7import elixir
8
9def setup():
10    metadata.bind = 'sqlite://'
11
12def teardown():
13    cleanup_all()
14
15class TestSetup(object):
16    def teardown(self):
17        cleanup_all()
18
19    def test_autosetup_manual_setup_all(self):
20        class Person(Entity):
21            name = Field(String(30))
22            using_options(autosetup=True, tablename='person')
23
24        # check that we have a fake table installed
25        assert 'person' in metadata.tables
26        assert isinstance(metadata.tables['person'],
27                          elixir.entity.TriggerProxy)
28
29        setup_all()
30
31        assert isinstance(metadata.tables['person'], Table)
32
33    def test_cleanup_before_setup(self):
34        class Person(Entity):
35            name = Field(String(30))
36            using_options(autosetup=True, tablename='person')
37
38        # check that we have a fake table installed
39        assert 'person' in metadata.tables
40        assert isinstance(metadata.tables['person'],
41                          elixir.entity.TriggerProxy)
42
43        cleanup_all()
44
45        assert 'person' not in metadata.tables
46
47    def test_drop_create_drop(self):
48        class User(Entity):
49            using_options(tablename='users')
50            some_field = Field(Boolean, default=False)
51
52        metadata.bind = 'sqlite://'
53
54        drop_all()
55        create_all()
56
57        class MockUser(User):
58            pass
59
60        drop_all()
61
62    def test_no_autosetup(self):
63        class Person(Entity):
64            name = Field(String(30))
65            using_options(autosetup=False, tablename='person')
66
67        assert 'person' not in metadata.tables
68        assert Person.table is None
69
70        # check that accessing the table didn't trigger the setup
71        assert 'person' not in metadata.tables
72
73        setup_all()
74
75        assert isinstance(metadata.tables['person'], Table)
76
77    def test_call(self):
78        class Person(Entity):
79            name = Field(String(30))
80            using_options(autosetup=True, tablename='person')
81
82        assert 'person' in metadata.tables
83        homer = Person(name="Homer Simpson")
84        assert isinstance(metadata.tables['person'], Table)
85
86    def test_getattr(self):
87        class Person(Entity):
88            name = Field(String(30))
89            using_options(autosetup=True, tablename='person')
90
91        tablename = Person.table.name
92        assert tablename == 'person'
93        assert isinstance(metadata.tables['person'], Table)
94
95    def test_createall(self):
96        assert 'person' not in metadata.tables
97
98        # note that if this test included a ManyToMany relationship, it would
99        # probably fail on SA 0.5.x and later, because the monkey-patched
100        # iterator is never called (but the TriggerProxy is).
101        class Person(Entity):
102            name = Field(String(30))
103            using_options(autosetup=True, tablename='person')
104
105        assert isinstance(metadata.tables['person'],
106                          elixir.entity.TriggerProxy)
107
108        create_all()
109
110        assert isinstance(metadata.tables['person'], Table)
111
112    def test_setupall(self):
113        class Person(Entity):
114            name = Field(String(30))
115            using_options(autosetup=True, tablename='person')
116
117        setup_all()
118        assert isinstance(metadata.tables['person'], Table)
119
120    def test_query(self):
121        class Person(Entity):
122            name = Field(String(30))
123            using_options(autosetup=True, tablename='person')
124
125        q = Person.query
126        assert isinstance(metadata.tables['person'], Table)
127
128    # This test doesn't work with Elixir 0.4.1 and further (because of changes
129    # in SA 0.4.2).
130#    def test_mapper(self):
131        # we want to hit the mapper directly (without hitting any of the
132        # other triggers first). We do so by getting a query object using a
133        # manual session.
134#        class Person(Entity):
135#            name = Field(String(30))
136#            using_options(autosetup=True, tablename='person')
137#        sess = create_session()
138#        persons = sess.query(Person).all()
139#        assert isinstance(metadata.tables['person'], Table)
140
141
Note: See TracBrowser for help on using the browser.