def test_exception_reading(self): """If a file exists but is not the correct format, we expect deserialization to gracefully fail (rather than exploding)""" cp = Checkpointer(tempfile.mkdtemp()) self.assertEqual(1, cp.checkpoint("1", lambda: 1)) with open(cp._filename("1"), "w") as written_file: written_file.write("") cp._reset() # pickle will raise an exception, so we will recompute self.assertEqual(-1, cp.checkpoint("1", lambda: -1))
def test_dont_load_later_elements(self): """If a checkpoint is executed, we should not load any later checkpoints. This allows a user to delete, say step 5, and effectively rebuild from that checkpoint.""" cp = Checkpointer(tempfile.mkdtemp()) self.assertEqual(cp.checkpoint("1", lambda: 1), 1) self.assertEqual(cp.checkpoint("2", lambda: 2), 2) self.assertEqual(cp.checkpoint("3", lambda: 3), 3) cp._reset() self.assertEqual(cp.checkpoint("1", lambda: -1), 1) self.assertEqual(cp.checkpoint("2", lambda: -2, force=True), -2) self.assertEqual(cp.checkpoint("3", lambda: -3), -3)
def test_tree_serialization(self): """Trees have embedded XML, which doesn't serialize well""" tree = Node( text="top", label=["111"], title="Reg 111", children=[ Node(text="inner", label=["111", "1"], source_xml=etree.fromstring("""<tag>Hi</tag>""")) ]) cp = Checkpointer(tempfile.mkdtemp()) cp.checkpoint("a-tag", lambda: tree) # saving cp._reset() loaded = cp.checkpoint("a-tag", None) # would explode if not loaded self.assertEqual(repr(tree), repr(loaded)) self.assertEqual( etree.tostring(tree.children[0].source_xml), etree.tostring(loaded.children[0].source_xml))
def test_tree_serialization(self): """Trees have embedded XML, which doesn't serialize well""" tree = Node(text="top", label=["111"], title="Reg 111", children=[ Node(text="inner", label=["111", "1"], source_xml=etree.fromstring("""<tag>Hi</tag>""")) ]) cp = Checkpointer(tempfile.mkdtemp()) cp.checkpoint("a-tag", lambda: tree) # saving cp._reset() loaded = cp.checkpoint("a-tag", None) # would explode if not loaded self.assertEqual(repr(tree), repr(loaded)) self.assertEqual(etree.tostring(tree.children[0].source_xml), etree.tostring(loaded.children[0].source_xml))