def test_get_object_graph(historian: mincepy.Historian): """Try getting the reference graph for live objects""" car = Car() garage = Garage(mincepy.ObjRef(car)) gid = garage.save() garage_graph = historian.archive.get_obj_ref_graph(gid) assert len(garage_graph.edges) == 1 assert (gid, car.obj_id) in garage_graph.edges
def test_get_snapshot_graph_simple(historian: mincepy.Historian): car = Car() garage = Garage(mincepy.ObjRef(car)) garage.save() garage_sid = historian.get_snapshot_id(garage) garage_graph = historian.archive.get_snapshot_ref_graph(garage_sid) assert len(garage_graph.edges) == 1 assert (garage_sid, historian.get_snapshot_id(car)) in garage_graph.edges
def test_get_obj_graph_current(historian: mincepy.Historian): """Test that when changing a reference the reference graph is returned correctly""" car = Car() garage = Garage(mincepy.ObjRef(car)) gid = garage.save() garage_graph = historian.archive.get_obj_ref_graph(gid) assert len(garage_graph.edges) == 1 assert (gid, car.obj_id) in garage_graph.edges # Now, modify the garage car2 = Car() garage.car = mincepy.ObjRef(car2) garage.save() # Check that the reference graph is correct garage_graph = historian.archive.get_obj_ref_graph(gid) assert len(garage_graph.edges) == 1 assert (gid, car2.obj_id) in garage_graph.edges # Finally, set the reference to None garage.car = mincepy.ObjRef() garage.save() # Check that the reference graph is correct garage_graph = historian.archive.get_obj_ref_graph(gid) assert len(garage_graph.edges) == 0 assert len(garage_graph.nodes) == 1
def test_nested_references(historian: mincepy.Historian): car = Car() garage = Garage(car) car_id = historian.save(car) garage_id = historian.save(garage) # Now change the car car.make = 'fiat' car.colour = 'white' historian.save(car) # Try loading while the object is still alive loaded_garage = historian.load(garage_id) assert loaded_garage is garage # Now delete and load del garage del car loaded_garage2 = historian.load(garage_id) # Should be the last version fo the car assert loaded_garage2.car.make == 'fiat' assert len(historian.history(car_id)) == 2 # The following may seem counter intuitive that we only have one history # entry for garage. But above we only saved it once. It's just that when # we load the garage again we get the 'latest' version it's contents i.e. # the newer version of the car assert len(historian.history(garage_id)) == 1
def test_encode_nested(historian: mincepy.Historian): class CarDelegate(mincepy.SimpleSavable): TYPE_ID = uuid.UUID('c0148a43-c0c0-4d2b-9262-ed1c8c6ab2fc') ATTRS = ('car',) def __init__(self, car): super().__init__() self.car = car def save_instance_state(self, saver): return {'car': self.car} def load_instance_state(self, saved_state, _loader): self.car = saved_state['car'] historian.register_type(CarDelegate) car = Car() delegate = CarDelegate(car) garage = Garage(delegate) garage_id = historian.save(garage) del garage historian.load(garage_id)
def test_get_snapshot_graph_twice(historian: mincepy.Historian): """Check for a bug that arise when asking for references twice""" car = Car() garage = Garage(mincepy.ObjRef(car)) garage.save() garage_sid = historian.get_snapshot_id(garage) def make_checks(graph): assert len(graph.edges) == 1 assert (garage_sid, historian.get_snapshot_id(car)) in graph.edges ref_graphs = historian.archive.get_snapshot_ref_graph(garage_sid) make_checks(ref_graphs) # Check again ref_graphs = historian.archive.get_snapshot_ref_graph(garage_sid) make_checks(ref_graphs)
def test_replace_invalid(historian: mincepy.Historian): honda = Car('honda', 'yellow') historian.save(honda) with pytest.raises(TypeError): historian.replace(honda, Garage())
def test_get_obj_referencing_simple(historian: mincepy.Historian): car = Car() garage = Garage(mincepy.ObjRef(car)) gid = garage.save() car_graph = historian.archive.get_obj_ref_graph(car.obj_id, direction=mincepy.INCOMING) assert len(car_graph.edges) == 1 assert len(car_graph.nodes) == 2 assert (gid, car.obj_id) in car_graph.edges # Now, create a new garage garage2 = Garage(mincepy.ObjRef(car)) g2id = garage2.save() # Check that the reference graph is correct car_graph = historian.archive.get_obj_ref_graph(car.obj_id, direction=mincepy.INCOMING) assert len(car_graph.nodes) == 3 assert len(car_graph.edges) == 2 assert (gid, car.obj_id) in car_graph.edges assert (g2id, car.obj_id) in car_graph.edges # Finally, set the references to None garage.car = mincepy.ObjRef() garage.save() garage2.car = mincepy.ObjRef() garage2.save() # Check that the reference graph is correct car_graph = historian.archive.get_obj_ref_graph(gid) assert len(car_graph.edges) == 0 assert len(car_graph.nodes) == 1