Example #1
0
def test_loading_account_from_created_and_credited_events_adds_to_balance(
        new_random_account, related_random_account_created_event):
    credited_event = get_account_credited_event(new_random_account.account_id,
                                                Amount(50, 42))
    account = Account(
        EventStream([related_random_account_created_event, credited_event]))
    assert account.balance == Amount(50, 42)
Example #2
0
def applyless_aggregate(events: Optional[List[BaseEvent]] = None):
    AggregateRoot.apply = lambda *args, **kwargs: None
    if events:
        aggregate = AggregateRoot(EventStream(events))
    else:
        aggregate = AggregateRoot()
    return aggregate
Example #3
0
def test_apply_event_is_called_for_each_event_passed_when_loading_aggregate():
    with patch.object(AggregateRoot, 'apply_event') as mock:
        AggregateRoot(
            EventStream([
                BaseEvent(operation_id=str(UniqueID())),
                BaseEvent(operation_id=str(UniqueID())),
                BaseEvent(operation_id=str(UniqueID()))
            ]))
    assert mock.call_count == 3
Example #4
0
def test_credit_adds_account_credited_event_to_uncommitted_changes(
        new_random_account, related_random_account_created_event,
        new_random_account_credit_event):
    account = Account(EventStream([related_random_account_created_event]))
    amount = Amount(new_random_account_credit_event.dollars,
                    new_random_account_credit_event.cents)
    account.credit(amount,
                   UniqueID(new_random_account_credit_event.operation_id))
    assert account.uncommitted_changes[-1] == new_random_account_credit_event
Example #5
0
 def create(ssn: SocialSecurityNumber, first_name: FirstName,
            last_name: LastName, birthdate: Birthdate,
            operation_id: UniqueID) -> 'Client':
     created_event = ClientCreated(client_id=str(UniqueID()),
                                   ssn=ssn.value,
                                   first_name=first_name.value,
                                   last_name=last_name.value,
                                   birthdate=birthdate.value,
                                   operation_id=operation_id.value)
     client = Client(EventStream([created_event]))
     client._initialize([created_event])
     return client
Example #6
0
 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
Example #7
0
 def wrapped(target_id):
     if client_id == target_id:
         client_created = ClientCreated(
             operation_id=str(UniqueID()),
             client_id=client_id.value,
             ssn=SocialSecurityNumber(123456789).value,
             first_name=FirstName('asd').value,
             last_name=LastName('asd').value,
             birthdate=Birthdate('22/01/1900').value)
         client = Client(EventStream([client_created]))
         return client
     return None
Example #8
0
    def load_stream(self, aggregate_id: UniqueID) -> EventStream:
        aggregate = self.session.query(AggregateModel).filter(
            AggregateModel.uuid == str(aggregate_id)).first()

        if not aggregate:
            raise NotFoundException(f'No aggregate with id {aggregate_id}')

        # translate all events models to proper event objects (see part 1)
        events_objects = [
            self._event_model_to_core(model) for model in aggregate.events
        ]
        version = aggregate.version

        return EventStream(events_objects, version)
Example #9
0
def test_loading_account_from_created_credited_debited_adds_to_balance(
        new_random_account, related_random_account_created_event,
        new_random_account_credit_event, new_random_account_debit_event):
    account = Account(
        EventStream([
            related_random_account_created_event,
            new_random_account_credit_event, new_random_account_debit_event
        ]))
    amount_credit = Amount(new_random_account_credit_event.dollars,
                           new_random_account_credit_event.cents)
    amount_debit = Amount(new_random_account_debit_event.dollars,
                          new_random_account_debit_event.cents)
    final = amount_credit - amount_debit
    assert account.balance == final
Example #10
0
 def create(client_id: UniqueID, account_id: UniqueID,
            operation_id: UniqueID, account_name: str) -> 'Account':
     """
     This method is used to create new instance of `Account` object.
     The changes to `_changes` and `_version` variables on the new instance
     are so that it will be in an initialized state.
     """
     created_event = AccountCreated(account_id=account_id.value,
                                    client_id=client_id.value,
                                    account_name=account_name,
                                    operation_id=operation_id.value)
     account = Account(EventStream([created_event]))
     account._initialize([created_event])
     return account
 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
Example #12
0
def test_event_stream_immutable():
    event_stream = EventStream([])
    with pytest.raises(FrozenInstanceError):
        event_stream.version = 2
    with pytest.raises(FrozenInstanceError):
        event_stream.events = ['asd']
Example #13
0
def test_loading_aggregate_adds_each_operation_id_to_operations_committed_with_value_of_true(
):
    event1 = BaseEvent(operation_id=str(UniqueID()))
    aggregate = AggregateRoot(EventStream([event1]))
    assert event1.operation_id in aggregate.committed_operations
    assert aggregate.committed_operations[event1.operation_id] is True
Example #14
0
def test_credit_increases_balance(related_random_account_created_event):
    account = Account(EventStream([related_random_account_created_event]))
    account.credit(Amount(120, 65), UniqueID())
    assert account.balance == Amount(120, 65)
Example #15
0
def test_loading_account_with_only_created_event_has_zero_balance(
        related_random_account_created_event):
    account = Account(EventStream([related_random_account_created_event]))
    assert account.balance == Amount(0, 0)
Example #16
0
def test_creating_aggregate_from_stream_sets_version_of_stream_to_aggregate():
    aggregate = AggregateRoot(EventStream([], 5))
    assert aggregate.version == 5
Example #17
0
def test_default_version_on_new_event_stream_is_minus_one():
    event_stream = EventStream([])
    assert event_stream.version == -1