def test_create_happy_path(self, ctx, mock_member_repository: MagicMock,
                               sample_mutation_request: FullMutationRequest,
                               member_manager: MemberManager):
        # Given that there is not user in the DB (user will be created).
        mock_member_repository.search_member_by = MagicMock(return_value=([],
                                                                          0))

        # When...
        member_manager.update_or_create(ctx, TEST_USERNAME,
                                        sample_mutation_request)

        # Expect...
        mock_member_repository.create_member.assert_called_once_with(
            ctx, **asdict(sample_mutation_request))
    def test_create_username_mismatch(
            self, ctx, mock_member_repository: MagicMock,
            sample_mutation_request: FullMutationRequest,
            member_manager: MemberManager):
        # Given a request that contains a different username than the one in the first argument.
        req = sample_mutation_request
        req.username = "******"  # something different than the username provided in the 'username' argument

        # Given that there is not user in the DB (user will be created).
        mock_member_repository.search_member_by = MagicMock(return_value=([],
                                                                          0))

        # When...
        with raises(UsernameMismatchError):
            member_manager.update_or_create(ctx, TEST_USERNAME, req)

        # Expect...
        mock_member_repository.create_member.assert_not_called()
        mock_member_repository.update_member.assert_not_called()
    def test_update_without_required_field(
            self, ctx, mock_member_repository: MagicMock,
            sample_mutation_request: FullMutationRequest,
            sample_member: Member, field: str, member_manager: MemberManager):
        # Given that there is a user in the DB (user will be updated).
        mock_member_repository.search_member_by = MagicMock(
            return_value=([sample_member], 1))

        # Given a request that updates some fields.
        req = sample_mutation_request
        req = FullMutationRequest(**{**asdict(req), **{field: None}})

        # When...
        with raises(MissingRequiredField):
            member_manager.update_or_create(ctx, TEST_USERNAME, req)

        # Expect...
        mock_member_repository.update_member.assert_not_called()
        mock_member_repository.create_member.assert_not_called(
        )  # Do not create any member!
    def test_update_happy_path(self, ctx, mock_member_repository: MagicMock,
                               sample_mutation_request: FullMutationRequest,
                               sample_member: Member,
                               member_manager: MemberManager):
        # Given that there is a user in the DB (user will be updated).
        mock_member_repository.search_member_by = MagicMock(
            return_value=([sample_member], 1))

        # Given a request that updates some fields.
        req = sample_mutation_request
        req.comment = "Updated comment."
        req.first_name = "George"
        req.last_name = "Dupuis"

        # When...
        member_manager.update_or_create(ctx, TEST_USERNAME, req)

        # Expect...
        mock_member_repository.update_member.assert_called_once_with(
            ctx, TEST_USERNAME, **asdict(req))
        mock_member_repository.create_member.assert_not_called(
        )  # Do not create any member!