def test_handle_path(self): cases = [ (Handle(None, "root", None), "root"), (Handle(None, "root", "1"), "root[1]"), (Handle(Handle(None, "root", None), "child", None), "root/child"), (Handle(Handle(None, "root", "1"), "child", "2"), "root[1]/child[2]"), ] for handle, path in cases: self.assertEqual(str(handle), path) self.assertEqual(Handle.from_path(path), handle)
def test_handle_attrs_readonly(self): handle = Handle(None, 'kind', 'key') with self.assertRaises(AttributeError): handle.parent = 'foo' with self.assertRaises(AttributeError): handle.kind = 'foo' with self.assertRaises(AttributeError): handle.key = 'foo' with self.assertRaises(AttributeError): handle.path = 'foo'
def test_restore_unknown(self): framework = self.create_framework() class Foo(Object): pass handle = Handle(None, "a_foo", "some_key") framework.register_type(Foo, None, handle.kind) try: framework.load_snapshot(handle) except NoSnapshotError as e: self.assertEqual(e.handle_path, str(handle)) self.assertEqual( str(e), "no snapshot data found for a_foo[some_key] object") else: self.fail("exception NoSnapshotError not raised")
def test_snapshot_saving_restricted_to_simple_types(self): # this can not be saved, as it has not simple types! to_be_saved = {"bar": TestFramework} class FooEvent(EventSetBase): def snapshot(self): return to_be_saved handle = Handle(None, "a_foo", "some_key") event = FooEvent() framework = self.create_framework() framework.register_type(FooEvent, None, handle.kind) with self.assertRaises(ValueError) as cm: framework.save_snapshot(event) expected = ( "unable to save the data for FooEvent, it must contain only simple types: " "{'bar': <class 'test.test_framework.TestFramework'>}") self.assertEqual(str(cm.exception), expected)
def test_snapshot_roundtrip(self): class Foo: def __init__(self, handle, n): self.handle = handle self.my_n = n def snapshot(self): return {"My N!": self.my_n} def restore(self, snapshot): self.my_n = snapshot["My N!"] + 1 handle = Handle(None, "a_foo", "some_key") event = Foo(handle, 1) framework1 = self.create_framework() framework1.register_type(Foo, None, handle.kind) framework1.save_snapshot(event) framework1.commit() framework2 = self.create_framework() framework2.register_type(Foo, None, handle.kind) event2 = framework2.load_snapshot(handle) self.assertEqual(event2.my_n, 2) framework2.save_snapshot(event2) del event2 gc.collect() event3 = framework2.load_snapshot(handle) self.assertEqual(event3.my_n, 3) framework2.drop_snapshot(event.handle) framework2.commit() framework3 = self.create_framework() framework3.register_type(Foo, None, handle.kind) self.assertRaises(NoSnapshotError, framework1.load_snapshot, handle) self.assertRaises(NoSnapshotError, framework2.load_snapshot, handle) self.assertRaises(NoSnapshotError, framework3.load_snapshot, handle)
def test_defer_and_reemit(self): framework = self.create_framework() class MyEvent(EventBase): pass class MyNotifier1(Object): a = EventSource(MyEvent) b = EventSource(MyEvent) class MyNotifier2(Object): c = EventSource(MyEvent) class MyObserver(Object): def __init__(self, parent, key): super().__init__(parent, key) self.seen = [] self.done = {} def on_any(self, event): self.seen.append(event.handle.kind) if not self.done.get(event.handle.kind): event.defer() pub1 = MyNotifier1(framework, "1") pub2 = MyNotifier2(framework, "1") obs1 = MyObserver(framework, "1") obs2 = MyObserver(framework, "2") framework.observe(pub1.a, obs1.on_any) framework.observe(pub1.b, obs1.on_any) framework.observe(pub1.a, obs2.on_any) framework.observe(pub1.b, obs2.on_any) framework.observe(pub2.c, obs2.on_any) pub1.a.emit() pub1.b.emit() pub2.c.emit() # Events remain stored because they were deferred. ev_a_handle = Handle(pub1, "a", "1") framework.load_snapshot(ev_a_handle) ev_b_handle = Handle(pub1, "b", "2") framework.load_snapshot(ev_b_handle) ev_c_handle = Handle(pub2, "c", "3") framework.load_snapshot(ev_c_handle) # make sure the objects are gone before we reemit them gc.collect() framework.reemit() obs1.done["a"] = True obs2.done["b"] = True framework.reemit() framework.reemit() obs1.done["b"] = True obs2.done["a"] = True framework.reemit() obs2.done["c"] = True framework.reemit() framework.reemit() framework.reemit() self.assertEqual(" ".join(obs1.seen), "a b a b a b b b") self.assertEqual(" ".join(obs2.seen), "a b c a b c a b c a c a c c") # Now the event objects must all be gone from storage. self.assertRaises(NoSnapshotError, framework.load_snapshot, ev_a_handle) self.assertRaises(NoSnapshotError, framework.load_snapshot, ev_b_handle) self.assertRaises(NoSnapshotError, framework.load_snapshot, ev_c_handle)