def test_collection(historian): def identity(x): return x coll = frontend.EntriesCollection(historian, historian.archive.objects, entry_factory=identity) p1 = testing.Person('martin', 35) p1.save() p2 = testing.Person('john', 5) p2.save() p1.age = 36 p1.save() # Find by obj ID records = coll.find(obj_id=p1.obj_id).one() assert isinstance(records, dict) assert records['obj_id'] == p1.obj_id # Find using multiple obj IDs records = list(coll.find(obj_id=[p1.obj_id, p2.obj_id])) assert len(records) == 2 assert {records[0]['obj_id'], records[1]['obj_id']} == {p1.obj_id, p2.obj_id} c1 = testing.Car() c1.save() records = list( coll.find(obj_type=[testing.Car.TYPE_ID, testing.Person.TYPE_ID])) assert len(records) == 3 assert {records[0]['type_id'], records[1]['type_id'], records[2]['type_id']} == \ {testing.Car.TYPE_ID, testing.Person.TYPE_ID}
def test_find_arg_types(historian: mincepy.Historian): """Test the argument types accepted by the historian find() method""" red_ferrari = testing.Car(colour='red', make='ferrari') green_ferrari = testing.Car(colour='green', make='ferrari') red_honda = testing.Car(colour='red', make='honda') martin = testing.Person(name='martin', age=35, car=red_honda) red_ferrari_id, green_ferrari_id, red_honda_id = \ historian.save(red_ferrari, green_ferrari, red_honda) martin_id = martin.save() # Test different possibilities for object ids being passed list(historian.find(obj_id=red_ferrari_id)) list( historian.find( obj_id=[red_ferrari_id, green_ferrari_id, martin_id, red_honda_id ])) list( historian.find(obj_id=(red_ferrari_id, green_ferrari_id, martin_id, red_honda_id))) list(historian.find(obj_id=str(red_ferrari_id))) # Test object types list(historian.find(obj_type=testing.Person)) list(historian.find(obj_type=[testing.Person, testing.Car])) list(historian.find(obj_type=(testing.Person, testing.Car))) list(historian.find(obj_type=testing.Person.TYPE_ID)) list( historian.find(obj_type=[testing.Person.TYPE_ID, testing.Car.TYPE_ID]))
def test_delete_referenced_by(historian: mincepy.Historian): car = testing.Car() person = testing.Person('martin', 35, car) person.save() with pytest.raises(mincepy.ReferenceError): historian.delete(car) # Check you can delete it in a transaction with historian.transaction(): historian.delete(car) historian.delete(person)
def test_simple_helper(historian: mincepy.Historian): historian.register_type(BoatHelper()) jenneau = Boat('jenneau', 38.9) jenneau_id = historian.save(jenneau) del jenneau jenneau = historian.load(jenneau_id) assert jenneau.make == 'jenneau' assert jenneau.length == 38.9 # Now check that references work martin = testing.Person('martin', 35) jenneau.owner = martin historian.save(jenneau) del jenneau jenneau = historian.load(jenneau_id) assert jenneau.owner is martin
def test_subclass_helper(historian: mincepy.Historian): historian.register_type(PowerboatHelper()) quicksilver = Powerboat('quicksilver', length=7.0, horsepower=115) quicksilver_id = historian.save(quicksilver) del quicksilver quicksilver = historian.load(quicksilver_id) assert quicksilver.make == 'quicksilver' assert quicksilver.length == 7. assert quicksilver.horsepower == 115 martin = testing.Person('martin', 35) quicksilver.owner = martin historian.save(quicksilver) del quicksilver quicksilver = historian.load(quicksilver_id) assert quicksilver.owner is martin
def test_migrating_live_object(historian: mincepy.Historian): """Test that a migration including a live object works fine""" class V1(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('8b1620f6-dd6d-4d39-b8b1-4433dc2a54df') ref = mincepy.field() def __init__(self, obj): super().__init__() self.ref = obj car = testing.Car() car.save() class V2(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('8b1620f6-dd6d-4d39-b8b1-4433dc2a54df') ref = mincepy.field(ref=True) class V1toV2(mincepy.ObjectMigration): VERSION = 1 @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader'): # Create a reference to the live car object saved_state['ref'] = mincepy.ObjRef(car) return saved_state LATEST_MIGRATION = V1toV2 martin = testing.Person('martin', 35) my_obj = V1(martin) my_obj_id = my_obj.save() del my_obj # Now change my mind historian.register_type(V2) assert len(historian.migrations.migrate_all()) == 1 migrated = historian.load(my_obj_id) assert migrated.ref is car