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_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_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) 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 = Event(MyEvent) b = Event(MyEvent) class MyNotifier2(Object): c = Event(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 = framework.load_snapshot(Handle(pub1, "a", "1")) ev_b = framework.load_snapshot(Handle(pub1, "b", "2")) ev_c = framework.load_snapshot(Handle(pub2, "c", "3")) 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)