示例#1
0
        public async Task GivenAccountRepository_WhenCallCreate_ShouldSuccess()
        {
            using (var context = await InMemoryDbContextFactory.CreateUserContext())
            {
                // assign
                var testData = (memebrId : 0, accountId : 0, memebrAccountId : 1);
                var member   = MemberDataBuilder.CreateMember(testData.memebrId, new List <MemberAccount>(), null, null);
                await context.Members.AddAsync(member);

                await context.SaveChangesAsync();

                var memberAccount = MemberDataBuilder.CreateMemberAccount(testData.memebrAccountId, testData.accountId, testData.memebrId);
                var account       = AccountDataBuilder.CreateAccount(testData.accountId, new List <MemberAccount> {
                    memberAccount
                });

                // act
                var repo = new AccountRepository(context);
                await repo.Create(account);

                await context.SaveChangesAsync();

                // assert
                account.Id.ShouldBeGreaterThan(0);
            }
        }
示例#2
0
        public void GivenMemberMapper_WhenMapMemberToMemberModel_ShouldMapSuccessful(long id,
                                                                                     decimal monthlyExpense, decimal monthlySalary,
                                                                                     string name, string emailAddress)
        {
            // assign
            var memberAccounts = new List <MemberAccount>
            {
                MemberDataBuilder.CreateMemberAccount(1, id, 2),
            };
            var memberExpense = MemberDataBuilder.CreateMemberExpense(1, id, FrequencyIds.Month, monthlyExpense);
            var memberSalary  = MemberDataBuilder.CreateMemberSalary(1, id, FrequencyIds.Month, monthlySalary);
            var member        = MemberDataBuilder.CreateMember(id, memberAccounts, memberExpense,
                                                               memberSalary, name, emailAddress);

            // act
            var memberModel = _mapper.Map <MemberModel>(member);

            // assert
            memberModel.ShouldSatisfyAllConditions(
                () => memberModel.ShouldNotBeNull(),
                () => memberModel.Name.ShouldBe(member.Name),
                () => memberModel.Email.ShouldBe(member.Email),
                () => memberModel.Id.ShouldBe(member.Id),
                () => memberModel.MonthlySalary.ShouldBe(member.MemberSalary.GetMonthlySalary()),
                () => memberModel.MonthlyExpense.ShouldBe(member.MemberExpense.GetMonthlyExpense())
                );
        }
示例#3
0
        private static async Task <Member> CreateMember(UserContext context)
        {
            var member = MemberDataBuilder.CreateMember(1, new List <MemberAccount>(), null, null);
            await context.Members.AddAsync(member);

            await context.SaveChangesAsync();

            return(member);
        }
示例#4
0
        public async Task GivenMemberService_WhenCallSignUpMember_IfMemberIdIsNotZero_ShouldRaiseException()
        {
            // assign
            var member = MemberDataBuilder.CreateMember(1, null, null, null);

            // act
            var exception = await Should.ThrowAsync <BusinessException>(() => _memberService.SignUpMember(member));

            // assert
            exception.IsCritical.ShouldBeTrue();
            exception.BusinessErrorMessage.ShouldContain("Member is not marked as new");
            await _memberDataAccessor.DidNotReceive().Create(member);
        }
示例#5
0
        public async Task GivenMemberService_WhenCallGetMemberByEmail_ShouldReturn()
        {
            // assign
            var member = MemberDataBuilder.CreateMember(1, null, null, null);

            _memberDataAccessor.GetByEmail(member.Email).Returns(member);

            // act
            var actualMember = await _memberService.GetMemberByEmail(member.Email);

            // assert
            actualMember.ShouldBe(member);
        }
示例#6
0
        public void GivenMemberEntity_WhenCallGetMonthNetIncome_ShouldReturnValue()
        {
            // assign
            var memberId        = 1;
            var memberExpensive = MemberDataBuilder.CreateMemberExpense(1, memberId, FrequencyIds.Month, 2000);
            var memberSalary    = MemberDataBuilder.CreateMemberSalary(1, memberId, FrequencyIds.Month, 5000);
            var member          = MemberDataBuilder.CreateMember(1, new List <MemberAccount>(), memberExpensive, memberSalary);

            // act
            var result = member.GetMonthNetIncome();

            //  assert
            result.ShouldBe(3000);
        }
示例#7
0
        public async Task GivenMemberService_WhenCallSignUpMember_IfMemberNotExisted_ShouldCreateMember()
        {
            // assign
            var member = MemberDataBuilder.CreateMember(0, null, null, null);

            _memberDataAccessor.GetByEmail(member.Email).Returns(default(Member));

            // act
            var signedUpMember = await _memberService.SignUpMember(member);

            // assert
            signedUpMember.ShouldNotBeNull();
            signedUpMember.ShouldBe(member);
            await _memberDataAccessor.Received(1).Create(member);
        }
        public void GivenAccountSignUpService_WhenCallMethodEvaluate_IfNoGuardFailed_ShouldReturnSuccessResult()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.TryAddEnumerable(ServiceDescriptor.Singleton <IAccountSignUpGuard, AlwaysSuccessGuard>());

            using (var provider = serviceCollection.BuildServiceProvider())
            {
                var accountSignUpService = new AccountSignUpService(provider.GetService <IEnumerable <IAccountSignUpGuard> >());
                var member   = MemberDataBuilder.CreateMember(1, new List <MemberAccount>(), null, null);
                var strategy = AccountSignUpStrategyDataBuilder.CreateAccountSignUpStrategy(1);
                var result   = accountSignUpService.Evaluate(member, strategy);
                result.IsSuccess.ShouldBeTrue();
            }
        }
示例#9
0
        public async Task GivenMemberService_WhenCallSignUpMember_IfMemberExisted_ShouldRaiseException()
        {
            // assign
            var member = MemberDataBuilder.CreateMember(0, null, null, null);

            _memberDataAccessor.GetByEmail(member.Email).Returns(member);

            // act
            var exception = await Should.ThrowAsync <BusinessException>(() => _memberService.SignUpMember(member));

            // assert
            exception.IsBadRequest.ShouldBeTrue();
            exception.BusinessErrorMessage.ShouldContain($"Cannot sign up member for email {member.Email}");
            await _memberDataAccessor.DidNotReceive().Create(member);
        }
示例#10
0
        public void GivenMonthNetIncomeGuard_WhenCallCanSignUp_ShouldReturnResult(decimal monthlySalary, decimal monthlyExpense, decimal monthNetIncomeLimit, bool canSignUp)
        {
            // assign

            var memberId        = 1;
            var memberExpensive = MemberDataBuilder.CreateMemberExpense(1, memberId, FrequencyIds.Month, monthlyExpense);
            var memberSalary    = MemberDataBuilder.CreateMemberSalary(1, memberId, FrequencyIds.Month, monthlySalary);
            var member          = MemberDataBuilder.CreateMember(1, new List <MemberAccount>(), memberExpensive, memberSalary);
            var strategy        = AccountSignUpStrategyDataBuilder.CreateAccountSignUpStrategy(1, true, monthNetIncomeLimit);

            // act
            var result = _accountSignUpGuard.CanSignUp(member, strategy);

            // assert
            result.IsSuccess.ShouldBe(canSignUp);
        }
示例#11
0
        public async Task GivenMemberRepository_WhenCallCreate_ShouldSuccess()
        {
            using (var context = await InMemoryDbContextFactory.CreateUserContext())
            {
                // assign
                var member = MemberDataBuilder.CreateMember(1, new List <MemberAccount>(), null, null);

                // act
                var repo = new MemberRepository(context);
                await repo.Create(member);

                await context.SaveChangesAsync();

                // assert
                member.Id.ShouldBeGreaterThan(0);
            }
        }
示例#12
0
        public async Task GivenAccountService_WhenCallSignUpAccount_IfMemberIsNoQualifiedToSignUp_ShouldRaiseException()
        {
            // assign
            var member          = MemberDataBuilder.CreateMember(1, null, null, null);
            var evaluatedResult = AccountSignUpResult.Fail("Month net income is too low.");

            MockAccountSignUpStrategyResult(evaluatedResult, member);
            MockAccountSequenceNumber(100000001);

            // act
            var exception = await Should.ThrowAsync <BusinessException>(() => _accountService.SignUpAccount(member));

            // assert
            exception.IsBadRequest.ShouldBeTrue();
            exception.BusinessErrorMessage.ShouldContain(evaluatedResult.ReasonPhase);
            await _accountDataAccessor.DidNotReceive().Create(Arg.Any <Account>());
        }
示例#13
0
        private static async Task <Account> CreateOneAccountWithOneMember(int accountId, int memberId, UserContext context)
        {
            var memberAccount = MemberDataBuilder.CreateMemberAccount(1, accountId, memberId);
            var member        = MemberDataBuilder.CreateMember(1, new List <MemberAccount> {
                memberAccount
            }, null, null);
            var account = AccountDataBuilder.CreateAccount(1, new List <MemberAccount> {
                memberAccount
            });

            await context.Accounts.AddAsync(account);

            await context.Members.AddAsync(member);

            await context.SaveChangesAsync();

            return(account);
        }
示例#14
0
        public async Task GivenGetMemberByEmailHandler_WhenCallHandle_ShouldReturn()
        {
            // assign
            var query = new GetMemberByEmailQuery()
            {
                Email = "*****@*****.**"
            };
            var member = MemberDataBuilder.CreateMember(1, null, null, null);

            _memberService.GetMemberByEmail(query.Email).Returns(member);

            // act
            var response = await _getMemberByEmailHandler.Handle(query, CancellationToken.None);

            // assert

            response.ShouldNotBeNull();
            response.Data.ShouldBe(member);
        }
示例#15
0
        public async Task GivenAccountService_WhenCallSignUpAccount_IfMemberIsQualifiedToSignUp_ShouldRerunAccount()
        {
            // assign
            var member          = MemberDataBuilder.CreateMember(1, null, null, null);
            var evaluatedResult = AccountSignUpResult.Success();
            var account         = AccountDataBuilder.CreateAccount(1, null);

            MockAccountSignUpStrategyResult(evaluatedResult, member);
            MockAccountSequenceNumber(100000001);
            _accountFactory.Create("ZIP0100000001", member.Id).Returns(account);

            // act
            var signedUpAccount = await _accountService.SignUpAccount(member);

            // assert
            await _accountDataAccessor.Received(1).Create(signedUpAccount);

            signedUpAccount.ShouldBe(account);
        }
示例#16
0
        [Fact] public async Task GivenMemberRepository_WhenCallListAll_ShouldSuccess()
        {
            using (var context = await InMemoryDbContextFactory.CreateUserContext())
            {
                // assign
                var member1 = MemberDataBuilder.CreateMember(1, new List <MemberAccount>(), null, null, "Rom", "*****@*****.**");
                var member2 = MemberDataBuilder.CreateMember(2, new List <MemberAccount>(), null, null, "Shawn", "*****@*****.**");
                var member3 = MemberDataBuilder.CreateMember(3, new List <MemberAccount>(), null, null, "Owen", "*****@*****.**");

                await context.AddRangeAsync(new[] { member1, member2, member3 });

                await context.SaveChangesAsync();

                // act
                var repo    = new MemberRepository(context);
                var members = await repo.ListAll(new PaginationRequest { PageSize = 1, PageNumber = 1 }, "n");

                // assert
                members.ShouldNotBeNull();
                members.TotalPageNumber.ShouldBe(2);
                members.Data.ShouldContain(m => m.Id == member3.Id);

                // act
                members = await repo.ListAll(new PaginationRequest { PageSize = 1, PageNumber = 2 }, "n");

                // assert
                members.ShouldNotBeNull();
                members.TotalPageNumber.ShouldBe(2);
                members.Data.ShouldContain(m => m.Id == member2.Id);

                // act
                members = await repo.ListAll(new PaginationRequest { PageSize = 2, PageNumber = 1 }, "n");

                // assert
                members.ShouldNotBeNull();
                members.TotalPageNumber.ShouldBe(1);
                members.Data.ShouldContain(m => m.Id == member2.Id);
                members.Data.ShouldContain(m => m.Id == member3.Id);
            }
        }
        public async Task GivenSignUpAccountHandler_WhenCallHandle_IfMemberFound_ShouldSignUpAccount()
        {
            // assign
            var query = new SignUpAccountCommand
            {
                MemberId = 1
            };
            var member  = MemberDataBuilder.CreateMember(1, null, null, null);
            var account = AccountDataBuilder.CreateAccount(1, null);

            _memberService.GetMemberById(query.MemberId).Returns(member);
            _accountService.SignUpAccount(member).Returns(account);
            _accountService.GetAccountById(account.Id).Returns(account);

            // act
            var response = await _signUpAccountHandler.Handle(query, CancellationToken.None);

            // assert
            await _unitOfWork.Received(1).SaveChangesAsync(CancellationToken.None);

            response.ShouldNotBeNull();
            response.Data.ShouldBe(account);
        }
示例#18
0
        public async Task GivenSignUpMemberHandler_WhenCallHandle_IfMemberFound_ShouldSignUpMember()
        {
            // assign
            var query = new SignUpMemberCommand
            {
                Name           = "jack",
                Email          = "*****@*****.**",
                MonthlySalary  = 1000,
                MonthlyExpense = 3000
            };
            var member = MemberDataBuilder.CreateMember(1, null, null, null);

            _memberFactory.Create(query.Email, query.Name, query.MonthlyExpense, query.MonthlySalary).Returns(member);
            _memberService.SignUpMember(Arg.Any <Member>()).Returns(member);
            _memberService.GetMemberById(Arg.Any <long>()).Returns(member);
            // act
            var response = await _signUpMemberHandler.Handle(query, CancellationToken.None);

            // assert
            await _unitOfWork.Received(1).SaveChangesAsync(CancellationToken.None);

            response.ShouldNotBeNull();
            response.Data.ShouldBe(member);
        }