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_metadata_using_object_instance(historian: mincepy.Historian): car = Car() historian.save((car, {'reg': 'VD395'})) # Check that we get back what we just set assert historian.meta.get(car) == {'reg': 'VD395'} car.make = 'fiat' historian.save(car) # Check that the metadata is shared assert historian.meta.get(car) == {'reg': 'VD395'} historian.meta.set(car, {'reg': 'N317'}) assert historian.meta.get(car) == {'reg': 'N317'}
def test_get_latest(historian: mincepy.Historian): # Save the car car = Car() car_id = historian.save(car) # Change it and save getting a snapshot car.make = 'fiat' car.colour = 'white' historian.save(car) fiat_sid = historian.get_snapshot_id(car) assert car_id != fiat_sid # Change it again... car.make = 'honda' car.colour = 'wine red' historian.save(car) honda_sid = historian.get_snapshot_id(car) assert honda_sid != fiat_sid assert honda_sid != car_id # Now delete and reload del car latest = historian.load(car_id) assert latest == historian.load_snapshot(honda_sid)
def test_metadata_simple(historian: mincepy.Historian): car = Car() ferrari_id = historian.save((car, {'reg': 'VD395'})) # Check that we get back what we just set assert historian.meta.get(ferrari_id) == {'reg': 'VD395'} car.make = 'fiat' red_fiat_id = historian.save(car) # Check that the metadata is shared assert historian.meta.get(red_fiat_id) == {'reg': 'VD395'} historian.meta.set(ferrari_id, {'reg': 'N317'}) # Check that this saves the metadata on the object level i.e. both are changed assert historian.meta.get(ferrari_id) == {'reg': 'N317'} assert historian.meta.get(red_fiat_id) == {'reg': 'N317'}
def test_save_snapshot_change_load(historian: mincepy.Historian): car = Car() # Saving twice without changing should produce the same snapshot id historian.save(car) ferrari_sid = historian.get_snapshot_id(car) historian.save(car) assert ferrari_sid == historian.get_snapshot_id(car) car.make = 'fiat' car.color = 'white' historian.save(car) fiat_sid = historian.get_snapshot_id(car) assert fiat_sid != ferrari_sid ferrari = historian.load_snapshot(ferrari_sid) assert ferrari.make == 'ferrari' assert ferrari.colour == 'red'