Beispiel #1
0
async def test_eventstream_should_raise_version_error_when_dependency_not_found():
    # arrange
    owner_changed = OwnerChanged(new_owner="Jane Doe")
    # act and assert
    with pytest.raises(DependencyDoesNotExist) as e:
        event_stream = EventStream()
        event_stream.add(owner_changed)
Beispiel #2
0
async def test_eventstream_should_append_new_event():
    # arrange
    bank_account_created = BankAccountCreated(
        id="052c21b6-aab9-4311-b954-518cd04f704c", owner="John Doe"
    )
    # act
    event_stream = EventStream()
    event_stream.add(bank_account_created)
    # assert
    assert len(event_stream) == 1
    assert event_stream.current_version == 0
    assert list(event_stream)[0] == bank_account_created
Beispiel #3
0
async def test_save_should_create_event_store(dbsession, eventsourcing):
    # arrange
    aggregate_id = '052c21b6-aab9-4311-b954-518cd04f704c'
    events = EventStream(
        [BankAccountCreated(id=aggregate_id, owner='John Doe')])
    # act
    async with eventsourcing.open('event_store') as eventstore:
        await eventstore.append_to_stream(aggregate_id, events)
        result = await eventstore.get_stream(aggregate_id)

    # assert
    async with dbsession.cursor() as cursor:
        stmt = """
        SELECT event_store.id, event_store.data, event_store.created_at
        FROM event_store WHERE event_store.id = %(id)s
        """
        result = await cursor.execute(stmt, {'id': aggregate_id})
        event_store = await cursor.fetchone()
        assert event_store is not None
        assert cursor.rowcount == 1
        # id
        assert event_store[0] == aggregate_id
        assert event_store[1][0]['$type'] == 'BankAccountCreated'
        assert event_store[1][0]['$version'] == 0
        assert event_store[1][0]['id'] == aggregate_id
        assert event_store[1][0]['owner'] == 'John Doe'
        assert event_store[2] is not None
Beispiel #4
0
async def test_eventstore_should_raise_version_error(dbsession, eventsourcing):
    # arrange
    aggregate_id = "f2283f9d-9ed2-4385-a614-53805725cbac"
    events_base = EventStream(
        [BankAccountCreated(id=aggregate_id, owner="John Doe")])
    events = EventStream([DepositPerformed(amount=20)])
    async with eventsourcing.open("event_store") as eventstore:
        await eventstore.append_to_stream(aggregate_id, events_base)

    # act
    async with dbsession.cursor() as cursor:
        stmt = """
        UPDATE event_store SET version=%(version)s
        WHERE id = %(id)s;
        """
        await cursor.execute(stmt, {"id": aggregate_id, "version": 10})
    # assert
    with pytest.raises(VersionError):
        async with eventsourcing.open("event_store") as eventstore:
            await eventstore.append_to_stream(aggregate_id, events)
Beispiel #5
0
async def test_eventstream_should_raise_stream_exists_when_stream_exists():
    # arrange
    bank_account_created_1 = BankAccountCreated(
        id="052c21b6-aab9-4311-b954-518cd04f704c", owner="John Doe"
    )
    bank_account_created_2 = BankAccountCreated(
        id="052c21b6-aab9-4311-b954-518cd04f704c", owner="John Doe"
    )
    # act
    event_stream = EventStream()
    event_stream.add(bank_account_created_1)
    # assert
    with pytest.raises(StreamExists):
        event_stream.add(bank_account_created_2)