def test(self): storage = Storage({}) t = storage.makeRef(Person('Tom', 3)) d = storage.makeRef(Person('Dick', 3)) storage['c'] = c = Collection(storage) c.add(t) c.add(d) self.assertEqual(len(c.values()), 2) del c self.assertEqual(storage.cache.values(), []) t.setAge(4) c = storage['c'] # The info for Tom in c has been updated. self.assertEqual(c.count(QTerm(':age', 'eq', 4)), 1) ti = c.values(QTerm(':name', 'eq', 'Tom'))[0] self.assertEqual(ti['age'], 4) c.discard(t) self.assertEqual(c.count(), 1) self.assertEqual(len(t.subscribers('*')), 0) d = c.open(QTerm(':name', 'eq', 'Dick')) self.assertEqual(d.age, 3)
def testFacetRef(self): vat = Storage({}) main_ref = vat.makeRef(TestData(24)) public = vat.makeRef(main_ref._getFacet('public')) self.assertEqual(public.get(), 24) self.assertRaises(AttributeError, getattr, public, 'set') # main_ref references a TestData instance self.assertEqual(type(main_ref._get()), TestData) # public references a facet Ref self.assertEqual(type(public._get()), Ref) self.assertEqual(public._get()._facet, 'public') # the facet Ref references an obj instance self.assertEqual(type(public._get()._get()), obj) # check facet is not lost in serialization. public._close() self.assertEqual(public._get()._facet, 'public')
def testSubscribe(self): storage = Storage({}) t = storage.makeRef(Person('Tom', 3)) storage['c'] = c = Collection(storage) c.add(t) storage['s'] = s = Subscriber([]) c.subscribe('change', s.on) t.setAge(4) self.assertEqual(len(s.events), 1)
def testNames(self): vat = Storage({}) foo = vat.makeRef(Obj({})) ns = NameStore(vat, {}) foo['x'] = 'bar' ns.setn('foo', foo) foo1 = ns.getn('foo') self.assertEqual(foo1['x'], 'bar') # Can do it all in one.. ns.setn('time', Time()) self.assertEqual(ns.getn('time').time(), 'Tea-time') ns.getn('time')._erase() ns.deln('time')