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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
#
# Copyright (c) 2006, 2007 Canonical
#
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
#
# This file is part of Storm Object Relational Mapper.
#
# Storm is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# Storm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from weakref import ref
import gc
from storm.exceptions import ClassInfoError
from storm.properties import Property
from storm.variables import Variable
from storm.expr import Undef, Select, compile
from storm.info import *
from tests.helper import TestHelper, run_this
class Wrapper(object):
def __init__(self, obj):
self.obj = obj
__storm_object_info__ = property(lambda self:
self.obj.__storm_object_info__)
class GetTest(TestHelper):
def setUp(self):
TestHelper.setUp(self)
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=True)
self.Class = Class
self.obj = Class()
def test_get_cls_info(self):
cls_info = get_cls_info(self.Class)
self.assertTrue(isinstance(cls_info, ClassInfo))
self.assertTrue(cls_info is get_cls_info(self.Class))
def test_get_obj_info(self):
obj_info = get_obj_info(self.obj)
self.assertTrue(isinstance(obj_info, ObjectInfo))
self.assertTrue(obj_info is get_obj_info(self.obj))
def test_get_obj_info_on_obj_info(self):
obj_info = get_obj_info(self.obj)
self.assertTrue(get_obj_info(obj_info) is obj_info)
def test_set_obj_info(self):
obj_info1 = get_obj_info(self.obj)
obj_info2 = ObjectInfo(self.obj)
self.assertEquals(get_obj_info(self.obj), obj_info1)
set_obj_info(self.obj, obj_info2)
self.assertEquals(get_obj_info(self.obj), obj_info2)
class ClassInfoTest(TestHelper):
def setUp(self):
TestHelper.setUp(self)
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=True)
prop2 = Property("column2")
self.Class = Class
self.cls_info = get_cls_info(Class)
def test_invalid_class(self):
class Class(object): pass
self.assertRaises(ClassInfoError, ClassInfo, Class)
def test_cls(self):
self.assertEquals(self.cls_info.cls, self.Class)
def test_columns(self):
self.assertEquals(self.cls_info.columns,
(self.Class.prop1, self.Class.prop2))
def test_table(self):
self.assertEquals(self.cls_info.table, "table")
def test_primary_key(self):
# Can't use == for props.
self.assertTrue(self.cls_info.primary_key[0] is self.Class.prop1)
self.assertEquals(len(self.cls_info.primary_key), 1)
def test_primary_key_with_attribute(self):
class SubClass(self.Class):
__storm_primary__ = "prop2"
cls_info = get_cls_info(SubClass)
self.assertTrue(cls_info.primary_key[0] is SubClass.prop2)
self.assertEquals(len(self.cls_info.primary_key), 1)
def test_primary_key_composed(self):
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=2)
prop2 = Property("column2", primary=1)
cls_info = ClassInfo(Class)
# Can't use == for props, since they're columns.
self.assertTrue(cls_info.primary_key[0] is Class.prop2)
self.assertTrue(cls_info.primary_key[1] is Class.prop1)
self.assertEquals(len(cls_info.primary_key), 2)
def test_primary_key_composed_with_attribute(self):
class Class(object):
__storm_table__ = "table"
__storm_primary__ = "prop2", "prop1"
# Define primary=True to ensure that the attribute
# has precedence.
prop1 = Property("column1", primary=True)
prop2 = Property("column2")
cls_info = ClassInfo(Class)
# Can't use == for props, since they're columns.
self.assertTrue(cls_info.primary_key[0] is Class.prop2)
self.assertTrue(cls_info.primary_key[1] is Class.prop1)
self.assertEquals(len(cls_info.primary_key), 2)
def test_primary_key_composed_duplicated(self):
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=True)
prop2 = Property("column2", primary=True)
self.assertRaises(ClassInfoError, ClassInfo, Class)
def test_primary_key_missing(self):
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1")
prop2 = Property("column2")
self.assertRaises(ClassInfoError, ClassInfo, Class)
def test_primary_key_attribute_missing(self):
class Class(object):
__storm_table__ = "table"
__storm_primary__ = ()
prop1 = Property("column1", primary=True)
prop2 = Property("column2")
self.assertRaises(ClassInfoError, ClassInfo, Class)
def test_primary_key_pos(self):
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=2)
prop2 = Property("column2")
prop3 = Property("column3", primary=1)
cls_info = ClassInfo(Class)
self.assertEquals(cls_info.primary_key_pos, (2, 0))
class ObjectInfoTest(TestHelper):
def setUp(self):
TestHelper.setUp(self)
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=True)
prop2 = Property("column2")
self.Class = Class
self.obj = Class()
self.obj_info = get_obj_info(self.obj)
self.cls_info = get_cls_info(Class)
self.variable1 = self.obj_info.variables[Class.prop1]
self.variable2 = self.obj_info.variables[Class.prop2]
def test_hashing(self):
self.assertEquals(hash(self.obj_info), hash(self.obj_info))
def test_equals(self):
obj_info1 = self.obj_info
obj_info2 = get_obj_info(self.Class())
self.assertFalse(obj_info1 == obj_info2)
def test_not_equals(self):
obj_info1 = self.obj_info
obj_info2 = get_obj_info(self.Class())
self.assertTrue(obj_info1 != obj_info2)
def test_dict_subclass(self):
self.assertTrue(isinstance(self.obj_info, dict))
def test_variables(self):
self.assertTrue(isinstance(self.obj_info.variables, dict))
for column in self.cls_info.columns:
variable = self.obj_info.variables.get(column)
self.assertTrue(isinstance(variable, Variable))
self.assertTrue(variable.column is column)
self.assertEquals(len(self.obj_info.variables),
len(self.cls_info.columns))
def test_variable_has_validator_object_factory(self):
args = []
def validator(obj, attr, value):
args.append((obj, attr, value))
class Class(object):
__storm_table__ = "table"
prop = Property(primary=True,
variable_kwargs={"validator": validator})
obj = Class()
get_obj_info(obj).variables[Class.prop].set(123)
self.assertEquals(args, [(obj, "prop", 123)])
def test_primary_vars(self):
self.assertTrue(isinstance(self.obj_info.primary_vars, tuple))
for column, variable in zip(self.cls_info.primary_key,
self.obj_info.primary_vars):
self.assertEquals(self.obj_info.variables.get(column),
variable)
self.assertEquals(len(self.obj_info.primary_vars),
len(self.cls_info.primary_key))
def test_checkpoint(self):
self.obj.prop1 = 10
self.obj_info.checkpoint()
self.assertEquals(self.obj.prop1, 10)
self.assertEquals(self.variable1.has_changed(), False)
self.obj.prop1 = 20
self.assertEquals(self.obj.prop1, 20)
self.assertEquals(self.variable1.has_changed(), True)
self.obj_info.checkpoint()
self.assertEquals(self.obj.prop1, 20)
self.assertEquals(self.variable1.has_changed(), False)
self.obj.prop1 = 20
self.assertEquals(self.obj.prop1, 20)
self.assertEquals(self.variable1.has_changed(), False)
def test_add_change_notification(self):
changes1 = []
changes2 = []
def object_changed1(obj_info, variable, old_value, new_value, fromdb):
changes1.append((1, obj_info, variable,
old_value, new_value, fromdb))
def object_changed2(obj_info, variable, old_value, new_value, fromdb):
changes2.append((2, obj_info, variable,
old_value, new_value, fromdb))
self.obj_info.checkpoint()
self.obj_info.event.hook("changed", object_changed1)
self.obj_info.event.hook("changed", object_changed2)
self.obj.prop2 = 10
self.obj.prop1 = 20
self.assertEquals(changes1,
[(1, self.obj_info, self.variable2, Undef, 10, False),
(1, self.obj_info, self.variable1, Undef, 20, False)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable2, Undef, 10, False),
(2, self.obj_info, self.variable1, Undef, 20, False)])
del changes1[:]
del changes2[:]
self.obj.prop1 = None
self.obj.prop2 = None
self.assertEquals(changes1,
[(1, self.obj_info, self.variable1, 20, None, False),
(1, self.obj_info, self.variable2, 10, None, False)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable1, 20, None, False),
(2, self.obj_info, self.variable2, 10, None, False)])
del changes1[:]
del changes2[:]
del self.obj.prop1
del self.obj.prop2
self.assertEquals(changes1,
[(1, self.obj_info, self.variable1, None, Undef, False),
(1, self.obj_info, self.variable2, None, Undef, False)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable1, None, Undef, False),
(2, self.obj_info, self.variable2, None, Undef, False)])
def test_add_change_notification_with_arg(self):
changes1 = []
changes2 = []
def object_changed1(obj_info, variable,
old_value, new_value, fromdb, arg):
changes1.append((1, obj_info, variable,
old_value, new_value, fromdb, arg))
def object_changed2(obj_info, variable,
old_value, new_value, fromdb, arg):
changes2.append((2, obj_info, variable,
old_value, new_value, fromdb, arg))
self.obj_info.checkpoint()
obj = object()
self.obj_info.event.hook("changed", object_changed1, obj)
self.obj_info.event.hook("changed", object_changed2, obj)
self.obj.prop2 = 10
self.obj.prop1 = 20
self.assertEquals(changes1,
[(1, self.obj_info, self.variable2, Undef, 10, False, obj),
(1, self.obj_info, self.variable1, Undef, 20, False, obj)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable2, Undef, 10, False, obj),
(2, self.obj_info, self.variable1, Undef, 20, False, obj)])
del changes1[:]
del changes2[:]
self.obj.prop1 = None
self.obj.prop2 = None
self.assertEquals(changes1,
[(1, self.obj_info, self.variable1, 20, None, False, obj),
(1, self.obj_info, self.variable2, 10, None, False, obj)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable1, 20, None, False, obj),
(2, self.obj_info, self.variable2, 10, None, False, obj)])
del changes1[:]
del changes2[:]
del self.obj.prop1
del self.obj.prop2
self.assertEquals(changes1,
[(1, self.obj_info, self.variable1, None, Undef, False, obj),
(1, self.obj_info, self.variable2, None, Undef, False, obj)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable1, None, Undef, False, obj),
(2, self.obj_info, self.variable2, None, Undef, False, obj)])
def test_remove_change_notification(self):
changes1 = []
changes2 = []
def object_changed1(obj_info, variable, old_value, new_value, fromdb):
changes1.append((1, obj_info, variable,
old_value, new_value, fromdb))
def object_changed2(obj_info, variable, old_value, new_value, fromdb):
changes2.append((2, obj_info, variable,
old_value, new_value, fromdb))
self.obj_info.checkpoint()
self.obj_info.event.hook("changed", object_changed1)
self.obj_info.event.hook("changed", object_changed2)
self.obj_info.event.unhook("changed", object_changed1)
self.obj.prop2 = 20
self.obj.prop1 = 10
self.assertEquals(changes1, [])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable2, Undef, 20, False),
(2, self.obj_info, self.variable1, Undef, 10, False)])
def test_remove_change_notification_with_arg(self):
changes1 = []
changes2 = []
def object_changed1(obj_info, variable,
old_value, new_value, fromdb, arg):
changes1.append((1, obj_info, variable,
old_value, new_value, fromdb, arg))
def object_changed2(obj_info, variable,
old_value, new_value, fromdb, arg):
changes2.append((2, obj_info, variable,
old_value, new_value, fromdb, arg))
self.obj_info.checkpoint()
obj = object()
self.obj_info.event.hook("changed", object_changed1, obj)
self.obj_info.event.hook("changed", object_changed2, obj)
self.obj_info.event.unhook("changed", object_changed1, obj)
self.obj.prop2 = 20
self.obj.prop1 = 10
self.assertEquals(changes1, [])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable2, Undef, 20, False, obj),
(2, self.obj_info, self.variable1, Undef, 10, False, obj)])
def test_auto_remove_change_notification(self):
changes1 = []
changes2 = []
def object_changed1(obj_info, variable, old_value, new_value, fromdb):
changes1.append((1, obj_info, variable,
old_value, new_value, fromdb))
return False
def object_changed2(obj_info, variable, old_value, new_value, fromdb):
changes2.append((2, obj_info, variable,
old_value, new_value, fromdb))
return False
self.obj_info.checkpoint()
self.obj_info.event.hook("changed", object_changed1)
self.obj_info.event.hook("changed", object_changed2)
self.obj.prop2 = 20
self.obj.prop1 = 10
self.assertEquals(changes1,
[(1, self.obj_info, self.variable2, Undef, 20, False)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable2, Undef, 20, False)])
def test_auto_remove_change_notification_with_arg(self):
changes1 = []
changes2 = []
def object_changed1(obj_info, variable,
old_value, new_value, fromdb, arg):
changes1.append((1, obj_info, variable,
old_value, new_value, fromdb, arg))
return False
def object_changed2(obj_info, variable,
old_value, new_value, fromdb, arg):
changes2.append((2, obj_info, variable,
old_value, new_value, fromdb, arg))
return False
self.obj_info.checkpoint()
obj = object()
self.obj_info.event.hook("changed", object_changed1, obj)
self.obj_info.event.hook("changed", object_changed2, obj)
self.obj.prop2 = 20
self.obj.prop1 = 10
self.assertEquals(changes1,
[(1, self.obj_info, self.variable2, Undef, 20, False, obj)])
self.assertEquals(changes2,
[(2, self.obj_info, self.variable2, Undef, 20, False, obj)])
def test_get_obj(self):
self.assertTrue(self.obj_info.get_obj() is self.obj)
def test_get_obj_reference(self):
"""
We used to assign the get_obj() manually. This breaks stored
references to the method (IOW, what we do in the test below).
It was a bit faster, but in exchange for the danger of introducing
subtle bugs which are super hard to debug.
"""
get_obj = self.obj_info.get_obj
self.assertTrue(get_obj() is self.obj)
another_obj = self.Class()
self.obj_info.set_obj(another_obj)
self.assertTrue(get_obj() is another_obj)
def test_set_obj(self):
obj = self.Class()
self.obj_info.set_obj(obj)
self.assertTrue(self.obj_info.get_obj() is obj)
def test_weak_reference(self):
obj = self.Class()
obj_info = get_obj_info(obj)
del obj
self.assertEquals(obj_info.get_obj(), None)
def test_object_deleted_notification(self):
obj = self.Class()
obj_info = get_obj_info(obj)
obj_info["tainted"] = True
deleted = []
def object_deleted(obj_info):
deleted.append(obj_info)
obj_info.event.hook("object-deleted", object_deleted)
del obj_info
del obj
self.assertEquals(len(deleted), 1)
self.assertTrue("tainted" in deleted[0])
def test_object_deleted_notification_after_set_obj(self):
obj = self.Class()
obj_info = get_obj_info(obj)
obj_info["tainted"] = True
obj = self.Class()
obj_info.set_obj(obj)
deleted = []
def object_deleted(obj_info):
deleted.append(obj_info)
obj_info.event.hook("object-deleted", object_deleted)
del obj_info
del obj
self.assertEquals(len(deleted), 1)
self.assertTrue("tainted" in deleted[0])
class ClassAliasTest(TestHelper):
def setUp(self):
TestHelper.setUp(self)
class Class(object):
__storm_table__ = "table"
prop1 = Property("column1", primary=True)
self.Class = Class
self.ClassAlias = ClassAlias(self.Class, "alias")
def test_cls_info_cls(self):
cls_info = get_cls_info(self.ClassAlias)
self.assertEquals(cls_info.cls, self.Class)
self.assertEquals(cls_info.table, "alias")
self.assertEquals(self.ClassAlias.prop1.name, "column1")
self.assertEquals(self.ClassAlias.prop1.table, self.ClassAlias)
def test_compile(self):
statement = compile(self.ClassAlias)
self.assertEquals(statement, "alias")
def test_compile_with_reserved_keyword(self):
Alias = ClassAlias(self.Class, "select")
statement = compile(Alias)
self.assertEquals(statement, '"select"')
def test_compile_in_select(self):
expr = Select(self.ClassAlias.prop1, self.ClassAlias.prop1 == 1,
self.ClassAlias)
statement = compile(expr)
self.assertEquals(statement,
'SELECT alias.column1 FROM "table" AS alias '
'WHERE alias.column1 = ?')
def test_compile_in_select_with_reserved_keyword(self):
Alias = ClassAlias(self.Class, "select")
expr = Select(Alias.prop1, Alias.prop1 == 1, Alias)
statement = compile(expr)
self.assertEquals(statement,
'SELECT "select".column1 FROM "table" AS "select" '
'WHERE "select".column1 = ?')
def test_crazy_metaclass(self):
"""We don't want metaclasses playing around when we build an alias."""
TestHelper.setUp(self)
class MetaClass(type):
def __new__(meta_cls, name, bases, dict):
cls = type.__new__(meta_cls, name, bases, dict)
cls.__storm_table__ = "HAH! GOTCH YA!"
return cls
class Class(object):
__metaclass__ = MetaClass
__storm_table__ = "table"
prop1 = Property("column1", primary=True)
Alias = ClassAlias(Class, "USE_THIS")
self.assertEquals(Alias.__storm_table__, "USE_THIS")
def test_cached_aliases(self):
"""
Class aliases are cached such that multiple invocations of
C{ClassAlias} return the same object.
"""
alias1 = ClassAlias(self.Class, "something_unlikely")
alias2 = ClassAlias(self.Class, "something_unlikely")
self.assertIdentical(alias1, alias2)
alias3 = ClassAlias(self.Class, "something_unlikely2")
self.assertNotIdentical(alias1, alias3)
alias4 = ClassAlias(self.Class, "something_unlikely2")
self.assertIdentical(alias3, alias4)
def test_unnamed_aliases_not_cached(self):
alias1 = ClassAlias(self.Class)
alias2 = ClassAlias(self.Class)
self.assertNotIdentical(alias1, alias2)
def test_alias_cache_is_per_class(self):
"""
The cache of class aliases is not as bad as it once was.
"""
class LocalClass(self.Class):
pass
alias = ClassAlias(self.Class, "something_unlikely")
alias2 = ClassAlias(LocalClass, "something_unlikely")
self.assertNotIdentical(alias, alias2)
def test_aliases_only_last_as_long_as_class(self):
"""
The cached ClassAliases only last for as long as the class is alive.
"""
class LocalClass(self.Class):
pass
alias = ClassAlias(LocalClass, "something_unlikely3")
alias_ref = ref(alias)
class_ref = ref(LocalClass)
del alias
del LocalClass
gc.collect(); gc.collect(); gc.collect()
self.assertIdentical(class_ref(), None)
self.assertIdentical(alias_ref(), None)
class TypeCompilerTest(TestHelper):
def test_nested_classes(self):
"""Convoluted case checking that the model is right."""
class Class1(object):
__storm_table__ = "class1"
id = Property(primary=True)
class Class2(object):
__storm_table__ = Class1
id = Property(primary=True)
statement = compile(Class2)
self.assertEquals(statement, "class1")
alias = ClassAlias(Class2, "alias")
statement = compile(Select(alias.id))
self.assertEquals(statement, "SELECT alias.id FROM class1 AS alias")
|