def db(app, account_id, client_id):
    event_store_db.create_all()

    operation_id = UniqueID()
    account_created_event = AccountCreated(operation_id=operation_id.value,
                                           client_id=client_id.value,
                                           account_id=account_id.value,
                                           account_name='test')
    account_credit_event = AccountCredited(operation_id=operation_id.value,
                                           dollars=23,
                                           cents=0,
                                           account_id=account_id.value)

    aggregate = AggregateModel(uuid=account_id.value,
                               version=AGGREGATE_VERSION)

    event1 = EventModel(uuid=str(UniqueID()),
                        aggregate_uuid=account_id.value,
                        name=account_created_event.__class__.__name__,
                        data=asdict(account_created_event))
    event2 = EventModel(uuid=str(UniqueID()),
                        aggregate_uuid=account_id.value,
                        name=account_credit_event.__class__.__name__,
                        data=asdict(account_credit_event))
    # Add aggregate and event to the db.
    event_store_db.session.add(aggregate)
    event_store_db.session.flush()
    event_store_db.session.add(event1)
    event_store_db.session.commit()
    event_store_db.session.add(event2)
    event_store_db.session.commit()

    yield event_store_db  # this is the object that the tests use

    event_store_db.drop_all()
def test_save_events_passing_expected_version_for_new_aggregate_throws_concurrency_error(
        session: Session, postgres_event_store):
    account_id = UniqueID()
    account_created_event = AccountCreated(operation_id=str(UniqueID()),
                                           client_id=str(UniqueID()),
                                           account_id=str(account_id),
                                           account_name='test')
    with pytest.raises(ConcurrencyException):
        postgres_event_store.save_events(account_id, [account_created_event],
                                         expected_version=6)
def test_save_events_tries_to_create_new_aggregate_if_no_expected_version_passed(
        session: Session, postgres_event_store):
    account_id = UniqueID()
    account_created_event = AccountCreated(operation_id=str(UniqueID()),
                                           client_id=str(UniqueID()),
                                           account_id=str(account_id),
                                           account_name='test')
    postgres_event_store.save_events(account_id, [account_created_event])
    found = session.query(AggregateModel).filter(
        AggregateModel.uuid == account_id.value).all()
    assert len(found) == 1
 def wrapped(target_id):
     if account_id == target_id:
         account_created = AccountCreated(
             operation_id=str(UniqueID()),
             account_id=str(UniqueID()),
             client_id=account_id,
             account_name='test'
         )
         account = Account(EventStream([account_created]))
         account.set_maximum_debt(Amount(500, 0), UniqueID())
         return account
     return None
 def wrapped(target_id: UniqueID):
     if account_id == target_id:
         return EventStream([
             AccountCreated(operation_id=str(UniqueID()),
                            client_id=str(UniqueID()),
                            account_id=str(account_id),
                            account_name='test'),
             AccountCredited(dollars=20,
                             cents=0,
                             account_id=account_id.value,
                             operation_id=str(UniqueID))
         ],
                            version=2)
     return None
def test_save_events_adds_related_events_to_new_aggregate(
        session: Session, postgres_event_store):
    account_id = UniqueID()
    account_created_event = AccountCreated(operation_id=str(UniqueID()),
                                           client_id=str(UniqueID()),
                                           account_id=str(account_id),
                                           account_name='test')
    credit_event = AccountCredited(dollars=22,
                                   cents=0,
                                   account_id=str(account_id),
                                   operation_id=str(UniqueID()))
    postgres_event_store.save_events(
        aggregate_id=account_id, events=[account_created_event, credit_event])
    aggregate = session.query(AggregateModel).filter(
        AggregateModel.uuid == account_id.value).one()
    assert len(aggregate.events) == 2
def db(app, account_id: UniqueID, client_id: UniqueID, account_name: str):
    with app.app_context():
        event_store_db.create_all()

        client_create_operation_id = UniqueID()
        client_created_event = ClientCreated(
            client_id=str(client_id),
            ssn=SocialSecurityNumber(123456789).value,
            operation_id=str(client_create_operation_id),
            first_name=FirstName('test').value,
            last_name=LastName('test').value,
            birthdate=Birthdate('27/02/1998').value)
        account_added_to_client = AccountAddedToClient(
            client_id=str(client_id),
            account_id=str(account_id),
            operation_id=str(client_create_operation_id),
            account_name=account_name)

        account_create_operation_id = UniqueID()
        account_created_event = AccountCreated(
            operation_id=account_create_operation_id.value,
            client_id=str(client_id),
            account_id=str(account_id),
            account_name=account_name)
        account_credit_event = AccountCredited(
            operation_id=account_create_operation_id.value,
            dollars=23,
            cents=0,
            account_id=str(account_id))

        client_aggregate = AggregateModel(uuid=str(client_id),
                                          version=AGGREGATE_VERSION)
        client_created_db_event = EventModel(
            uuid=str(UniqueID()),
            aggregate_uuid=str(client_id),
            name=client_created_event.__class__.__name__,
            data=asdict(client_created_event))
        account_added_to_client_db_event = EventModel(
            uuid=str(UniqueID()),
            aggregate_uuid=str(client_id),
            name=account_added_to_client.__class__.__name__,
            data=asdict(account_added_to_client))

        account_aggregate = AggregateModel(uuid=str(account_id),
                                           version=AGGREGATE_VERSION)
        account_created_db_event = EventModel(
            uuid=str(UniqueID()),
            aggregate_uuid=account_id.value,
            name=account_created_event.__class__.__name__,
            data=asdict(account_created_event))
        account_credited_db_event = EventModel(
            uuid=str(UniqueID()),
            aggregate_uuid=account_id.value,
            name=account_credit_event.__class__.__name__,
            data=asdict(account_credit_event))

        event_store_db.session.add(client_aggregate)
        event_store_db.session.add(account_aggregate)
        event_store_db.session.flush()
        event_store_db.session.add(client_created_db_event)
        event_store_db.session.add(account_added_to_client_db_event)
        event_store_db.session.add(account_created_db_event)
        event_store_db.session.add(account_credited_db_event)
        event_store_db.session.commit()

        yield event_store_db  # this is the object that the tests use

        event_store_db.drop_all()
Exemple #8
0
def related_random_account_created_event(new_random_account):
    return AccountCreated(
        client_id=new_random_account.client_id.value,
        account_id=new_random_account.account_id.value,
        operation_id=new_random_account.uncommitted_changes[-1].operation_id,
        account_name='test')