-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
213 lines (183 loc) · 8.06 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import shutil
import tempfile
import unittest
import sqlalchemy as sa
import sqlalchemy.ext.declarative as declarative
import sqlahelper
class DBInfo(object):
def __init__(self, dir, filename):
self.file = os.path.join(dir, filename)
self.url = "sqlite:///" + self.file
class SQLAHelperTestCase(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
self.db1 = DBInfo(self.dir, "db1.sqlite")
self.db2 = DBInfo(self.dir, "db2.sqlite")
self.db3 = DBInfo(self.dir, "db3.sqlite")
def tearDown(self):
sqlahelper.reset()
shutil.rmtree(self.dir, True)
if not hasattr(unittest.TestCase, "assertIsInstance"): # pragma: no cover
def assertIsInstance(self, obj, classes):
if not isinstance(obj, classes):
typ = type(obj)
if isinstance(classes, (list, tuple)):
classes_str = ", ".join(x.__name__ for x in classes)
classes_str = "[%s]" % classes_str
else:
classes_str = classes.__name__
msg = "%s is not an instance of %s" % (typ, classes_str)
raise AssertionError(msg)
def assertIs(self, a, b):
if a is not b:
raise AssertionError("%r is not %r" % (a, b))
class TestAddEngine(SQLAHelperTestCase):
def test_one_engine(self):
e = sa.create_engine(self.db1.url)
sqlahelper.add_engine(e)
retrieved = sqlahelper.get_engine()
self.assertIs(retrieved, e)
def test_multiple_engines(self):
default = sa.create_engine(self.db1.url)
stats = sa.create_engine(self.db2.url)
sqlahelper.add_engine(default)
sqlahelper.add_engine(stats, "stats")
# Can we retrieve the engines?
self.assertIs(sqlahelper.get_engine(), default)
self.assertIs(sqlahelper.get_engine("default"), default)
self.assertIs(sqlahelper.get_engine("stats"), stats)
# Are the session binding and base binding set correctly?
self.assertIs(sqlahelper.get_session().bind, default)
self.assertIs(sqlahelper.get_base().metadata.bind, default)
def test_multiple_engines_without_default(self):
db1 = sa.create_engine(self.db1.url)
db2 = sa.create_engine(self.db2.url)
sqlahelper.add_engine(db1, "db1")
sqlahelper.add_engine(db2, "db2")
# Can we retrieve the engines?
self.assertIs(sqlahelper.get_engine("db1"), db1)
self.assertIs(sqlahelper.get_engine("db2"), db2)
# There should be no default engine
self.assertIsNone(sqlahelper.get_session().bind)
self.assertIsNone(sqlahelper.get_base().metadata.bind)
self.assertIsNone(sqlahelper.get_engine())
def test_add_engine_twice(self):
db1 = sa.create_engine(self.db1.url)
db2 = sa.create_engine(self.db2.url)
sqlahelper.add_engine(db1)
self.assertIs(sqlahelper.get_session().bind, db1)
sqlahelper.add_engine(db2)
self.assertIs(sqlahelper.get_session().bind, db2)
self.assertIs(sqlahelper.get_session().bind, sqlahelper.sessions.default.registry.registry.value.bind)
class TestDeclarativeBase(SQLAHelperTestCase):
def test1(self):
import transaction
Base = sqlahelper.get_base()
class Person(Base):
__tablename__ = "people"
id = sa.Column(sa.Integer, primary_key=True)
first_name = sa.Column(sa.Unicode(100), nullable=False)
last_name = sa.Column(sa.Unicode(100), nullable=False)
engine = sa.create_engine(self.db1.url)
sqlahelper.add_engine(engine)
Base.metadata.create_all()
fred = Person(id=1, first_name=u"Fred", last_name=u"Flintstone")
wilma = Person(id=2, first_name=u"Wilma", last_name=u"Flintstone")
barney = Person(id=3, first_name=u"Barney", last_name=u"Rubble")
betty = Person(id=4, first_name=u"Betty", last_name=u"Rubble")
Session = sqlahelper.get_session()
sess = Session()
sess.add_all([fred, wilma, barney, betty])
transaction.commit()
sess.expunge_all()
del fred, wilma, barney, betty
# Can we get back a record?
barney2 = sess.query(Person).get(3)
self.assertEqual(barney2.id, 3)
self.assertEqual(barney2.first_name, u"Barney")
self.assertEqual(barney2.last_name, u"Rubble")
sa.select([Person.first_name])
# Can we iterate the first names in reverse alphabetical order?
q = sess.query(Person.first_name).order_by(Person.first_name.desc())
result = [x.first_name for x in q]
control = [u"Wilma", u"Fred", u"Betty", u"Barney"]
self.assertEqual(result, control)
def test1_without_transaction_manager(self):
Base = sqlahelper.get_base()
class Person(Base):
__tablename__ = "people"
id = sa.Column(sa.Integer, primary_key=True)
first_name = sa.Column(sa.Unicode(100), nullable=False)
last_name = sa.Column(sa.Unicode(100), nullable=False)
engine = sa.create_engine(self.db1.url)
sqlahelper.add_engine(engine)
Base.metadata.create_all()
fred = Person(id=1, first_name=u"Fred", last_name=u"Flintstone")
wilma = Person(id=2, first_name=u"Wilma", last_name=u"Flintstone")
barney = Person(id=3, first_name=u"Barney", last_name=u"Rubble")
betty = Person(id=4, first_name=u"Betty", last_name=u"Rubble")
Session = sqlahelper.get_session()
Session.configure(extension=None) # XXX Kludge for SQLAlchemy/ZopeTransactionExtension bug
sess = Session()
sess.add_all([fred, wilma, barney, betty])
sess.commit()
sess.expunge_all()
del fred, wilma, barney, betty
# Can we get back a record?
barney2 = sess.query(Person).get(3)
self.assertEqual(barney2.id, 3)
self.assertEqual(barney2.first_name, u"Barney")
self.assertEqual(barney2.last_name, u"Rubble")
sa.select([Person.first_name])
# Can we iterate the first names in reverse alphabetical order?
q = sess.query(Person.first_name).order_by(Person.first_name.desc())
result = [x.first_name for x in q]
control = [u"Wilma", u"Fred", u"Betty", u"Barney"]
self.assertEqual(result, control)
class TestSetBase(SQLAHelperTestCase):
def test1(self):
base = sqlahelper.get_base()
my_base = declarative.declarative_base()
sqlahelper.set_base(my_base)
base2 = sqlahelper.get_base()
try:
self.assertIsNot(base2, base)
self.assertIs(base2, my_base)
except AttributeError: # Python < 2.7
self.assertNotEqual(base2, base)
self.assertEqual(base2, my_base)
class IncludeMeTests(unittest.TestCase):
def setUp(self):
from pyramid import testing
self.config = testing.setUp()
def tearDown(self):
from pyramid import testing
testing.tearDown()
def _callFUT(self, *args, **kwargs):
from sqlahelper import includeme
return includeme(*args, **kwargs)
def test_default_engine(self):
engine_settings = {
'sqlalchemy.url': 'sqlite:///',
'sqlalchemy.echo': 'true',
}
self.config.registry.settings.update(engine_settings)
self._callFUT(self.config)
import sqlahelper
self.assertIsNotNone(sqlahelper.engines.default)
self.assertEqual(str(sqlahelper.engines.default.url), 'sqlite:///')
self.assertTrue(sqlahelper.engines.default.echo)
def test_another_engines(self):
engine_settings = {
'sqlahelper.other.url': 'sqlite:///',
'sqlahelper.other.echo': 'true',
}
self.config.registry.settings.update(engine_settings)
self._callFUT(self.config)
import sqlahelper
self.assertIsNotNone(sqlahelper.engines.other)
self.assertEqual(str(sqlahelper.engines.other.url), 'sqlite:///')
self.assertTrue(sqlahelper.engines.other.echo)
if __name__ == "__main__":
unittest.main()