Exemplo n.º 1
0
def test_snapshot_id_eq():
    sid1 = mincepy.SnapshotId('sid', 1)
    sid1_again = mincepy.SnapshotId('sid', 1)
    sid2 = mincepy.SnapshotId('sid', 2)

    assert sid1 == sid1_again
    assert sid1 != sid2
    assert sid1_again != sid2
Exemplo n.º 2
0
def test_load(historian: mincepy.Historian):
    archive: mincepy.mongo.MongoArchive = historian.archive
    with pytest.raises(TypeError):
        archive.load('invalid')

    with pytest.raises(mincepy.NotFound):
        archive.load(mincepy.SnapshotId(bson.ObjectId(), 0))
Exemplo n.º 3
0
def test_purge(historian: mincepy.Historian):
    car = Car()
    garage = testing.Garage(mincepy.ObjRef(car))
    car_id, _ = historian.save(car, garage)

    # Now update the car
    car.colour = 'blue'
    car.save()

    records_count = historian.records.find().count()

    # This should not perge anything as v0 of the car has a reference from the garage, while
    # v1 is the current, live, version
    res = historian.purge(dry_run=False)
    assert not res.deleted_purged
    assert not res.unreferenced_purged
    assert records_count == historian.records.find().count()

    # Now mutate the car, save and purge again
    car.colour = 'white'
    car.save()

    # This time, version 1 is unreferenced by anything and so can be safely deleted
    res = historian.purge(dry_run=False)
    assert not res.deleted_purged
    assert res.unreferenced_purged == {mincepy.SnapshotId(car_id, 1)}
    assert records_count == historian.records.find().count()
Exemplo n.º 4
0
def test_purge(historian: mincepy.Historian):
    """Test that snapshots.purge() removes snapshots corresponding to deleted objects"""
    assert historian.snapshots.purge().deleted_purged == set()

    car = Car()
    car_id = car.save()
    car.colour = 'yellow'
    car.save()

    # Insert something else just to check that it doesn't get accidentally purged
    car2 = Car()
    car2.save()

    historian.delete(car)
    records_count = historian.records.find().count()
    res = historian.snapshots.purge(dry_run=False)

    assert records_count == historian.records.find().count()
    assert res.deleted_purged == {
        mincepy.SnapshotId(car_id, 0),
        mincepy.SnapshotId(car_id, 1),
        mincepy.SnapshotId(car_id, 2)  # The deleted record
    }
Exemplo n.º 5
0
def test_migrating_snapshot(historian: mincepy.Historian, caplog):
    """Test migrating an out of date snapshot"""
    caplog.set_level(logging.INFO)

    car = CarV0('yellow', 'bugatti')
    car_id = car.save()  # Version 0

    car.colour = 'brown'
    car.save()  # Version 1
    del car

    historian.register_type(CarV2)  # And update the car definition
    snapshot = historian.load_snapshot(mincepy.SnapshotId(car_id,
                                                          0))  # Load version 0

    assert snapshot.colour == 'yellow'
    assert snapshot.make == 'bugatti'

    # Now load the current version
    current = historian.load(car_id)
    assert current.colour == 'brown'
    assert current.make == 'bugatti'