Exemplo n.º 1
0
        public async Task ShouldCloseBet()
        {
            var      betId                = Guid.NewGuid();
            var      memberId             = new MemberId(Guid.Parse("1311e8c9-d993-4346-94c1-1907e3ce65d3"));
            var      creator              = new Member(memberId, "name", 300);
            BetState betState             = GenerateBet(betId, creator);
            var      domainEventsListener = new DomainEventsAccessor();
            var      repository           = new InMemoryBetRepository(domainEventsListener, betState);
            var      command              = new CloseBetCommand(betId, true);
            var      dateTimeClosed       = new DateTime(2021, 4, 1);
            var      dateTimeProvider     = new FakeDateTimeProvider(dateTimeClosed);
            var      handler              = new CloseBetCommandHandler(repository, dateTimeProvider, new InMemoryAuthenticationGateway(true, memberId.Value));

            await handler.Handle(command, default);

            var bet = await repository.GetByIdAsync(new(betId));

            Assert.True(bet.State.IsSuccess);
            Assert.Equal(dateTimeClosed, bet.State.CloseDate);
            var betClosedEvent = domainEventsListener.GetDomainEvents()
                                 .SingleOrDefault(x => x.GetType() == typeof(BetClosed)) as BetClosed;
            var betUpdatedEvent = domainEventsListener.GetDomainEvents()
                                  .SingleOrDefault(x => x.GetType() == typeof(BetUpdated)) as BetUpdated;

            Assert.NotNull(betClosedEvent);
            Assert.NotNull(betUpdatedEvent);
            Assert.Equal(betId, betClosedEvent.BetId);
            Assert.True(betClosedEvent.IsSuccess);
            Assert.Equal(betId, betUpdatedEvent.BetId);
        }
Exemplo n.º 2
0
 public RegisterUserHandlerTest()
 {
     domainEventsAccessor   = new DomainEventsAccessor();
     userRepository         = new InMemoryUserRepository(domainEventsAccessor: domainEventsAccessor);
     fakeDateTimeProvider   = new FakeDateTimeProvider(new DateTime(2021, 9, 22));
     inMemoryTokenGenerator = new InMemoryTokenGenerator(JWT_TOKEN);
 }
Exemplo n.º 3
0
        public async Task ShouldDecreaseWalletCreatorAndIncreaseParticipants()
        {
            var betId        = Guid.NewGuid();
            var creator      = new Member(new(Guid.NewGuid()), "creator", 200);
            var participant  = new Member(new(Guid.NewGuid()), "participant", 100);
            var participant2 = new Member(new(Guid.NewGuid()), "participant2", 500);
            var betState     = new BetState(betId,
                                            creator,
                                            new DateTime(2021, 12, 3),
                                            "description",
                                            50,
                                            new DateTime(2020, 3, 3),
                                            new List <AnswerState>()
            {
                new AnswerState(participant, true, new DateTime(2021, 3, 4)),
                new AnswerState(participant2, true, new DateTime(2021, 3, 4))
            }, new DateTime(2021, 3, 3), false);
            var               domainEventListener = new DomainEventsAccessor();
            IBetRepository    betRepository       = new InMemoryBetRepository(domainEventListener, betState);
            IMemberRepository memberRepository    = new InMemoryMemberRepository(new() { creator, participant, participant2 });
            var               command             = new UpdateWalletMembersCommand(betId);
            var               handler             = new UpdateWalletMembersCommandHandler(betRepository, memberRepository);

            await handler.Handle(command, default);

            var creatorUpdated = await memberRepository.GetByIdAsync(creator.Id);

            var participantUpdated = await memberRepository.GetByIdAsync(participant.Id);

            var participantUpdated2 = await memberRepository.GetByIdAsync(participant2.Id);

            Assert.Equal(150, creatorUpdated.Wallet);
            Assert.Equal(125, participantUpdated.Wallet);
            Assert.Equal(525, participantUpdated2.Wallet);
        }
Exemplo n.º 4
0
        public async Task ShouldCreateBet()
        {
            //arrange
            IDateTimeProvider dtNow  = new FakeDateTimeProvider(new DateTime(2021, 5, 6, 0, 0, 0));
            var endDate              = new DateTime(2021, 5, 7, 0, 0, 0);
            var command              = new LaunchBetCommand(_betId, endDate, coins, description);
            var domainEventsListener = new DomainEventsAccessor();
            var betRepository        = new InMemoryBetRepository(domainEventsListener);
            var member           = new Member(new MemberId(_creatorId), "name", 25);
            var memberRepository = new InMemoryMemberRepository(new List <Member>()
            {
                member
            });
            var      handler     = new LaunchBetCommandHandler(betRepository, memberRepository, new InMemoryAuthenticationGateway(true, _creatorId), dtNow);
            BetState expectedBet = new(_betId, member, endDate, description, coins, dtNow.Now, new List <AnswerState>(), null, null);

            //act
            await handler.Handle(command, default);

            //assert
            Bet actualBet = await betRepository.GetByIdAsync(new(_betId));

            IDomainEvent domainEvent = domainEventsListener.GetDomainEvents()
                                       .SingleOrDefault(x => x.GetType() == typeof(BetCreated));

            Assert.Equal(expectedBet.BetId, actualBet.State.BetId);
            Assert.Equal(expectedBet.Description, actualBet.State.Description);
            Assert.Equal(expectedBet.EndDate, actualBet.State.EndDate);
            Assert.Equal(expectedBet.Creator.Id, actualBet.State.Creator.Id);
            Assert.Equal(expectedBet.Creator.Name, actualBet.State.Creator.Name);
            Assert.Equal(expectedBet.Creator.Wallet, actualBet.State.Creator.Wallet);
            Assert.Equal(expectedBet.Coins, actualBet.State.Coins);
            Assert.Equal(expectedBet.CreationDate, actualBet.State.CreationDate);
            Assert.False(actualBet.IsClosed());
            Assert.True(expectedBet.EndDate > actualBet.State.CreationDate);
            Assert.True(actualBet.GetEndDateToAnswer().CompareTo(new DateTime(2021, 5, 6, 6, 0, 0)) == 0);
            Assert.NotNull(domainEvent);
            Assert.Equal(_betId, ((BetCreated)domainEvent).BetId.Value);
            Assert.Equal(_creatorId, ((BetCreated)domainEvent).CreatorId.Value);
        }
Exemplo n.º 5
0
        public async Task ShouldNotModifyWalletCreatorWhenThereAreNoParticipants()
        {
            var betId    = Guid.NewGuid();
            var creator  = new Member(new(Guid.NewGuid()), "creator", 200);
            var betState = new BetState(betId,
                                        creator,
                                        new DateTime(2021, 12, 3),
                                        "description",
                                        50,
                                        new DateTime(2020, 3, 3),
                                        new List <AnswerState>(),
                                        new DateTime(2021, 3, 3), true);
            var domainEventListener = new DomainEventsAccessor();
            var betRepository       = new InMemoryBetRepository(domainEventListener, betState);
            var memberRepository    = new InMemoryMemberRepository(new() { creator });
            var command             = new UpdateWalletMembersCommand(betId);
            var handler             = new UpdateWalletMembersCommandHandler(betRepository, memberRepository);

            await handler.Handle(command, default);

            Assert.Equal(200, creator.Wallet);
        }
        public async Task ShouldAddSubscription()
        {
            var subscriptionId    = Guid.NewGuid();
            var memberId          = Guid.NewGuid();
            var memberToSubscribe = new Member(new(subscriptionId), "member2", 300);
            var member            = new Member(new(memberId), "member1", 300);
            var domainListener    = new DomainEventsAccessor();
            var memberReposiory   = new InMemoryMemberRepository(new() { member, memberToSubscribe }, domainListener);
            var handler           = new SubscribeMemberCommanderHandler(memberReposiory, new InMemoryAuthenticationGateway(true, memberId));
            var command           = new SubscribeMemberCommand(subscriptionId);

            await handler.Handle(command, default);

            Assert.Collection(member.Subscriptions, x =>
            {
                Assert.Equal(subscriptionId, x.MemberId.Value);
            });
            Assert.Collection(domainListener.GetDomainEvents(), x =>
            {
                Assert.IsType <MemberSubscribed>(x);
                Assert.Equal(memberId, (x as MemberSubscribed).MemberId);
                Assert.Equal(subscriptionId, (x as MemberSubscribed).SubscriptionId);
            });
        }