~free.ekanayaka/storm/any-expr

« back to all changes in this revision

Viewing changes to tests/store/base.py

  • Committer: James Henstridge
  • Date: 2010-02-08 11:30:44 UTC
  • mfrom: (354 trunk)
  • mto: This revision was merged to the branch mainline in revision 355.
  • Revision ID: james@jamesh.id.au-20100208113044-e9les8qvvt7h4flx
merge from trunk, fixing conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
from storm.properties import PropertyPublisherMeta, Decimal
31
31
from storm.variables import PickleVariable
32
32
from storm.expr import (
33
 
    Asc, Desc, Select, Func, LeftJoin, SQL, Count, Sum, Avg, And, Or, Eq,
34
 
    Lower)
 
33
    Asc, Desc, Select, LeftJoin, SQL, Count, Sum, Avg, And, Or, Eq, Lower)
35
34
from storm.variables import Variable, UnicodeVariable, IntVariable
36
35
from storm.info import get_obj_info, ClassAlias
37
36
from storm.exceptions import *
147
146
 
148
147
    def test_wb_default_cache_size(self):
149
148
        store = Store(DummyDatabase())
150
 
        self.assertEquals(store._cache._size, 100)
 
149
        self.assertEquals(store._cache._size, 1000)
 
150
 
 
151
 
 
152
class StoreDatabaseTest(TestHelper):
 
153
 
 
154
    def test_store_has_reference_to_its_database(self):
 
155
        database = DummyDatabase()
 
156
        store = Store(database)
 
157
        self.assertIdentical(store.get_database(), database)
151
158
 
152
159
 
153
160
class StoreTest(object):
446
453
        self.store.flush()
447
454
        self.assertEquals(self.store._event._hooks["flush"], set())
448
455
 
449
 
 
450
456
    def test_obj_info_with_deleted_object_with_get(self):
451
457
        # Same thing, but using get rather than find.
452
458
 
2605
2611
        bar.foo_id = SQL("20")
2606
2612
        self.assertEquals(bar.foo.id, 20)
2607
2613
 
 
2614
    def test_reference_remote_leak_on_flush_with_changed(self):
 
2615
        """
 
2616
        "changed" events only hold weak references to remote infos object, thus
 
2617
        not creating a leak when unhooked.
 
2618
        """
 
2619
        self.get_cache(self.store).set_size(0)
 
2620
        bar = self.store.get(Bar, 100)
 
2621
        bar.foo.title = u"Changed title"
 
2622
        bar_ref = weakref.ref(get_obj_info(bar))
 
2623
        foo = bar.foo
 
2624
        del bar
 
2625
        self.store.flush()
 
2626
        gc.collect()
 
2627
        self.assertEquals(bar_ref(), None)
 
2628
 
 
2629
    def test_reference_remote_leak_on_flush_with_removed(self):
 
2630
        """
 
2631
        "removed" events only hold weak references to remote infos objects,
 
2632
        thus not creating a leak when unhooked.
 
2633
        """
 
2634
        self.get_cache(self.store).set_size(0)
 
2635
        class MyFoo(Foo):
 
2636
            bar = Reference(Foo.id, Bar.foo_id, on_remote=True)
 
2637
 
 
2638
        foo = self.store.get(MyFoo, 10)
 
2639
        foo.bar.title = u"Changed title"
 
2640
        foo_ref = weakref.ref(get_obj_info(foo))
 
2641
        bar = foo.bar
 
2642
        del foo
 
2643
        self.store.flush()
 
2644
        gc.collect()
 
2645
        self.assertEquals(foo_ref(), None)
 
2646
 
2608
2647
    def test_reference_break_on_remote_diverged_by_lazy(self):
2609
2648
        class MyBar(Bar):
2610
2649
            pass
4493
4532
        self.store.invalidate()
4494
4533
 
4495
4534
        obj_info = get_obj_info(pickle_blob)
4496
 
        variable =  obj_info.variables[PickleBlob.bin]
 
4535
        variable = obj_info.variables[PickleBlob.bin]
4497
4536
        var_ref = weakref.ref(variable)
4498
4537
        del variable, blob, pickle_blob, obj_info
4499
4538
        gc.collect()
4532
4571
        self.store.invalidate()
4533
4572
 
4534
4573
        obj_info = get_obj_info(pickle_blob)
4535
 
        variable =  obj_info.variables[PickleBlob.bin]
 
4574
        variable = obj_info.variables[PickleBlob.bin]
4536
4575
        var_ref = weakref.ref(variable)
4537
4576
        del variable, blob, pickle_blob, obj_info, foo
4538
4577
        gc.collect()
4985
5024
        obj_info = get_obj_info(foo)
4986
5025
        self.assertEquals(obj_info.variables[Foo.title].get_lazy(), AutoReload)
4987
5026
 
 
5027
    def test_primary_key_reference(self):
 
5028
        """
 
5029
        When an object references another one using its primary key, it
 
5030
        correctly checks for the invalidated state after the store has been
 
5031
        committed, detecting if the referenced object has been removed behind
 
5032
        its back.
 
5033
        """
 
5034
        class BarOnRemote(object):
 
5035
            __storm_table__ = "bar"
 
5036
            foo_id = Int(primary=True)
 
5037
            foo = Reference(foo_id, Foo.id, on_remote=True)
 
5038
        foo = self.store.get(Foo, 10)
 
5039
        bar = self.store.get(BarOnRemote, 10)
 
5040
        self.assertEqual(bar.foo, foo)
 
5041
        self.store.execute("DELETE FROM foo WHERE id = 10")
 
5042
        self.store.commit()
 
5043
        self.assertEqual(bar.foo, None)
 
5044
 
4988
5045
    def test_invalidate_and_get_object(self):
4989
5046
        foo = self.store.get(Foo, 20)
4990
5047
        self.store.invalidate(foo)
5829
5886
        self.assertEquals(self.empty.intersection(self.empty), self.empty)
5830
5887
        self.assertEquals(self.empty.intersection(self.result), self.empty)
5831
5888
        self.assertEquals(self.result.intersection(self.empty), self.empty)
5832