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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
from datetime import datetime, date, time
from storm.properties import *
from storm.kinds import *
from storm.expr import Column, compile
from tests.helpers import TestHelper
class DecorateKind(object):
def to_python(self, value):
return "to", value
def from_python(self, value):
return "from", value
class PropertyTest(TestHelper):
def setUp(self):
TestHelper.setUp(self)
self.kind = DecorateKind()
class Class(object):
__table__ = "table", "prop1"
prop1 = Property("column1", self.kind)
prop2 = Property()
class SubClass(Class):
__table__ = "subtable", "prop1"
self.Class = Class
self.SubClass = SubClass
def test_column(self):
self.assertTrue(isinstance(self.Class.prop1, Column))
def test_cls(self):
self.assertEquals(self.Class.prop1.cls, self.Class)
self.assertEquals(self.Class.prop2.cls, self.Class)
self.assertEquals(self.SubClass.prop1.cls, self.SubClass)
self.assertEquals(self.SubClass.prop2.cls, self.SubClass)
self.assertEquals(self.Class.prop1.cls, self.Class)
self.assertEquals(self.Class.prop2.cls, self.Class)
def test_cls_reverse(self):
self.assertEquals(self.SubClass.prop1.cls, self.SubClass)
self.assertEquals(self.SubClass.prop2.cls, self.SubClass)
self.assertEquals(self.Class.prop1.cls, self.Class)
self.assertEquals(self.Class.prop2.cls, self.Class)
self.assertEquals(self.SubClass.prop1.cls, self.SubClass)
self.assertEquals(self.SubClass.prop2.cls, self.SubClass)
def test_name(self):
self.assertEquals(self.Class.prop1.name, "column1")
def test_auto_name(self):
self.assertEquals(self.Class.prop2.name, "prop2")
def test_auto_table(self):
self.assertEquals(self.Class.prop1.table, "table")
self.assertEquals(self.Class.prop2.table, "table")
def test_auto_table_subclass(self):
self.assertEquals(self.Class.prop1.table, "table")
self.assertEquals(self.Class.prop2.table, "table")
self.assertEquals(self.SubClass.prop1.table, "subtable")
self.assertEquals(self.SubClass.prop2.table, "subtable")
def test_auto_table_subclass_reverse_initialization(self):
self.assertEquals(self.SubClass.prop1.table, "subtable")
self.assertEquals(self.SubClass.prop2.table, "subtable")
self.assertEquals(self.Class.prop1.table, "table")
self.assertEquals(self.Class.prop2.table, "table")
def test_kind(self):
self.assertEquals(self.Class.prop1.kind, self.kind)
self.assertTrue(isinstance(self.Class.prop2.kind, AnyKind))
self.assertEquals(self.SubClass.prop1.kind, self.kind)
self.assertTrue(isinstance(self.SubClass.prop2.kind, AnyKind))
def test_set_get(self):
obj = self.Class()
obj.prop1 = 10
obj.prop2 = 20
self.assertEquals(obj.prop1, ("to", ("from", 10)))
self.assertEquals(obj.prop2, 20)
def test_set_get_subclass(self):
obj = self.SubClass()
obj.prop1 = 10
obj.prop2 = 20
self.assertEquals(obj.prop1, ("to", ("from", 10)))
self.assertEquals(obj.prop2, 20)
def test_set_get_explicitly(self):
obj = self.Class()
prop1 = self.Class.prop1
prop2 = self.Class.prop2
prop1.__set__(obj, 10)
prop2.__set__(obj, 20)
self.assertEquals(prop1.__get__(obj), ("to", ("from", 10)))
self.assertEquals(prop2.__get__(obj), 20)
def test_set_get_explicitly_default(self):
obj = self.Class()
prop1 = self.SubClass.prop1
prop2 = self.SubClass.prop2
self.assertEquals(prop1.__get__(obj, default=30), 30)
self.assertEquals(prop2.__get__(obj, default=30), 30)
prop1.__set__(obj, 10)
prop2.__set__(obj, 20)
self.assertEquals(prop1.__get__(obj, default=30), ("to", ("from", 10)))
self.assertEquals(prop2.__get__(obj, default=30), 20)
def test_set_get_subclass_explicitly(self):
obj = self.SubClass()
prop1 = self.SubClass.prop1
prop2 = self.SubClass.prop2
prop1.__set__(obj, 10)
prop2.__set__(obj, 20)
self.assertEquals(prop1.__get__(obj), ("to", ("from", 10)))
self.assertEquals(prop2.__get__(obj), 20)
def test_set_get_subclass_explicitly_default(self):
obj = self.SubClass()
prop1 = self.SubClass.prop1
prop2 = self.SubClass.prop2
self.assertEquals(prop1.__get__(obj, default=30), 30)
self.assertEquals(prop2.__get__(obj, default=30), 30)
prop1.__set__(obj, 10)
prop2.__set__(obj, 20)
self.assertEquals(prop1.__get__(obj, default=30), ("to", ("from", 10)))
self.assertEquals(prop2.__get__(obj, default=30), 20)
def test_delete(self):
obj = self.Class()
obj.prop1 = 10
obj.prop2 = 20
del obj.prop1
del obj.prop2
self.assertEquals(obj.prop1, None)
self.assertEquals(obj.prop2, None)
def test_delete_subclass(self):
obj = self.SubClass()
obj.prop1 = 10
obj.prop2 = 20
del obj.prop1
del obj.prop2
self.assertEquals(obj.prop1, None)
self.assertEquals(obj.prop2, None)
def test_delete_explicitly(self):
obj = self.Class()
obj.prop1 = 10
obj.prop2 = 20
self.Class.prop1.__delete__(obj)
self.Class.prop2.__delete__(obj)
self.assertEquals(obj.prop1, None)
self.assertEquals(obj.prop2, None)
def test_delete_subclass_explicitly(self):
obj = self.SubClass()
obj.prop1 = 10
obj.prop2 = 20
self.SubClass.prop1.__delete__(obj)
self.SubClass.prop2.__delete__(obj)
self.assertEquals(obj.prop1, None)
self.assertEquals(obj.prop2, None)
def test_comparable_expr(self):
prop1 = self.Class.prop1
prop2 = self.Class.prop2
expr = (prop1 == "value1") & (prop2 == "value2")
statement, parameters = compile(expr)
self.assertEquals(statement, "table.column1 = ? AND "
"table.prop2 = ?")
self.assertEquals(parameters, ["value1", "value2"])
def test_comparable_expr_subclass(self):
prop1 = self.SubClass.prop1
prop2 = self.SubClass.prop2
expr = (prop1 == "value1") & (prop2 == "value2")
statement, parameters = compile(expr)
self.assertEquals(statement, "subtable.column1 = ? AND "
"subtable.prop2 = ?")
self.assertEquals(parameters, ["value1", "value2"])
class PropertyKindsTest(TestHelper):
def setup(self, property):
class Class(object):
__table__ = "table", "column1"
prop1 = property("column1")
prop2 = property()
class SubClass(Class):
pass
self.Class = Class
self.SubClass = SubClass
self.obj = SubClass()
return self.SubClass.prop1, self.SubClass.prop2
def test_bool(self):
prop1, prop2 = self.setup(Bool)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, BoolKind))
self.assertTrue(isinstance(prop2.kind, BoolKind))
self.obj.prop1 = 1
self.assertTrue(self.obj.prop1 is True)
self.obj.prop1 = 0
self.assertTrue(self.obj.prop1 is False)
def test_int(self):
prop1, prop2 = self.setup(Int)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, IntKind))
self.assertTrue(isinstance(prop2.kind, IntKind))
self.obj.prop1 = False
self.assertEquals(self.obj.prop1, 0)
self.obj.prop1 = True
self.assertEquals(self.obj.prop1, 1)
def test_float(self):
prop1, prop2 = self.setup(Float)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, FloatKind))
self.assertTrue(isinstance(prop2.kind, FloatKind))
self.obj.prop1 = 1
self.assertTrue(isinstance(self.obj.prop1, float))
def test_str(self):
prop1, prop2 = self.setup(Str)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, StrKind))
self.assertTrue(isinstance(prop2.kind, StrKind))
self.obj.prop1 = u"str"
self.assertTrue(isinstance(self.obj.prop1, str))
def test_unicode(self):
prop1, prop2 = self.setup(Unicode)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, UnicodeKind))
self.assertTrue(isinstance(prop2.kind, UnicodeKind))
self.obj.prop1 = "unicode"
self.assertTrue(isinstance(self.obj.prop1, unicode))
def test_datetime(self):
prop1, prop2 = self.setup(DateTime)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, DateTimeKind))
self.assertTrue(isinstance(prop2.kind, DateTimeKind))
self.obj.prop1 = 0.0
self.assertEquals(self.obj.prop1, datetime.utcfromtimestamp(0))
self.obj.prop1 = datetime(2006, 1, 1, 12, 34)
self.assertEquals(self.obj.prop1, datetime(2006, 1, 1, 12, 34))
self.assertRaises(TypeError, setattr, self.obj, "prop1", object())
def test_date(self):
prop1, prop2 = self.setup(Date)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, DateKind))
self.assertTrue(isinstance(prop2.kind, DateKind))
self.obj.prop1 = datetime(2006, 1, 1, 12, 34, 56)
self.assertEquals(self.obj.prop1, date(2006, 1, 1))
self.obj.prop1 = date(2006, 1, 1)
self.assertEquals(self.obj.prop1, date(2006, 1, 1))
self.assertRaises(TypeError, setattr, self.obj, "prop1", object())
def test_time(self):
prop1, prop2 = self.setup(Time)
self.assertTrue(isinstance(prop1, Column))
self.assertTrue(isinstance(prop2, Column))
self.assertEquals(prop1.name, "column1")
self.assertEquals(prop1.table, "table")
self.assertEquals(prop2.name, "prop2")
self.assertEquals(prop2.table, "table")
self.assertTrue(isinstance(prop1.kind, TimeKind))
self.assertTrue(isinstance(prop2.kind, TimeKind))
self.obj.prop1 = datetime(2006, 1, 1, 12, 34, 56)
self.assertEquals(self.obj.prop1, time(12, 34, 56))
self.obj.prop1 = time(12, 34, 56)
self.assertEquals(self.obj.prop1, time(12, 34, 56))
self.assertRaises(TypeError, setattr, self.obj, "prop1", object())
|