Exemplo n.º 1
0
def test_transaction_snapshots(historian: mincepy.Historian):
    class ThirdPartyPerson:
        """A class from a third party library"""
        def __init__(self, name):
            self.name = name

    class PersonHelper(mincepy.TypeHelper):
        TYPE_ID = uuid.UUID('62d8c767-14bc-4437-a9a3-ca5d0ce65d9b')
        TYPE = ThirdPartyPerson
        INJECT_CREATION_TRACKING = True

        def yield_hashables(self, obj, hasher):
            yield from hasher.yield_hashables(obj.name)

        def eq(self, one, other) -> bool:
            return one.name == other.name

        def save_instance_state(self, obj, saver):
            return obj.name

        def load_instance_state(self, obj, saved_state, loader):
            obj.name = saved_state.name

    person_helper = PersonHelper()
    historian.register_type(person_helper)

    person_maker = mincepy.Process('person maker')

    with person_maker.running():
        martin = ThirdPartyPerson('Martin')

    historian.save(martin)
    assert historian.created_by(martin) == historian.get_obj_id(person_maker)
Exemplo n.º 2
0
def test_basic_save_process(historian: mincepy.Historian):
    proc = mincepy.Process('test_basic_save')
    pid = historian.save(proc)
    with proc.running():
        car = testing.Car('nissan', 'white')
        car_id = historian.save(car)
    assert historian.created_by(car) == pid
    assert historian.get_creator(car) is proc
    assert historian.get_creator(car_id) is proc

    second_car = testing.Car('ford')
    historian.save(second_car)
    assert historian.created_by(second_car) is None

    # Now check we can get the creator id from the object id
    del car
    assert historian.created_by(car_id) == pid
    assert historian.get_creator(car_id) is proc
Exemplo n.º 3
0
def test_save_after_creation(historian: mincepy.Historian):
    """
    Test saving an object that was created inside a process context but then saved
    outside it.  The creator should still be correctly set
    """
    proc = mincepy.Process('test_delayed_save')
    proc.save()
    with proc.running():
        # Create the car
        car = testing.Car('nissan', 'white')

    # Save it
    historian.save(car)
    created_in = historian.created_by(car)
    assert created_in is not None
    assert created_in == historian.get_current_record(proc).obj_id