Пример #1
0
        public void Raise_when_buynowonly_throws()
        {
            var user    = AuctionTestUtils.CreateUser();
            var auction = AuctionTestUtils.CreateBuyNowOnlyAuction();

            Assert.Throws <DomainException>(() => auction.Raise(user, 12));
        }
Пример #2
0
        public void CancelBid_when_buynowOnly_throws()
        {
            var auction = AuctionTestUtils.CreateBuyNowOnlyAuction();
            var user    = AuctionTestUtils.CreateUser();

            Assert.Throws <DomainException>(() => auction.CancelBid(user, new Bid(auction.AggregateId, user.UserIdentity, 12, DateTime.UtcNow)));
        }
Пример #3
0
        public void Raise_generates_valid_pending_events_and_state()
        {
            var user = AuctionTestUtils.CreateUser();

            auction.Raise(user, 21);

            auction.PendingEvents.Count.Should()
            .Be(2);
            var bid         = auction.Bids.Last();
            var raisedEvent = auction.PendingEvents.Last() as AuctionRaised;

            raisedEvent.Should()
            .BeOfType(typeof(AuctionRaised));
            raisedEvent.Bid.Should()
            .Be(bid);
            auction.ActualPrice.Should()
            .Be(21);
            bid.UserIdentity.Should().BeEquivalentTo(user.UserIdentity);
            bid.AuctionId.Should().Be(auction.AggregateId);
            bid.DateCreated.Kind.Should().Be(DateTimeKind.Utc);

            user.Credits.Should().Be(1000 - bid.Price);
            user.PendingEvents.Should().HaveCount(1);
            user.PendingEvents.First().Should().BeOfType <CreditsWithdrawn>();
        }
Пример #4
0
        public void CancelBid_when_bid_exists_changes_currentPrice_and_returns_credits_to_user()
        {
            var user1 = AuctionTestUtils.CreateUser();
            var user2 = AuctionTestUtils.CreateUser();

            auction.Raise(user1, 10);
            var firstBid = auction.Bids.Last();

            auction.Raise(user2, 20);
            var secondBid = auction.Bids.Last();

            auction.MarkPendingEventsAsHandled();

            auction.CancelBid(user2, secondBid);

            auction.ActualPrice.Should()
            .Be(10);
            auction.Bids.First().Should().BeEquivalentTo(firstBid);
            auction.PendingEvents.Count.Should()
            .Be(1);
            auction.PendingEvents.First()
            .Should()
            .BeOfType <BidCanceled>();

            user2.PendingEvents.Should().HaveCount(2);
            user2.PendingEvents.First().Should().BeOfType <CreditsWithdrawn>();
            user2.PendingEvents.Last().Should().BeOfType <CreditsReturned>();
            user2.Credits.Should().Be(1000);
        }
Пример #5
0
        public void BuyNow_generates_valid_events_and_state()
        {
            var user = AuctionTestUtils.CreateUser();

            auction.MarkPendingEventsAsHandled();

            auction.BuyNow(user);

            auction.PendingEvents.Count.Should()
            .Be(1);
            auction.Buyer.Should()
            .Be(user.UserIdentity);
            auction.Completed.Should()
            .BeTrue();
            var ev = auction.PendingEvents.First() as AuctionBought;

            ev.AuctionId.Should()
            .Be(auction.AggregateId);
            ev.UserIdentity.Should()
            .Be(user.UserIdentity);

            user.Credits.Should().Be(1000 - auction.BuyNowPrice);
            user.PendingEvents.Count.Should().Be(1);
            user.PendingEvents.First().Should().BeOfType <CreditsWithdrawn>();
        }
Пример #6
0
        public void Raise_when_invalid_parameters_throws(decimal price)
        {
            var user = AuctionTestUtils.CreateUser();

            auction.Raise(user, 91);

            Assert.Throws <DomainException>(() => auction.Raise(user, price));
        }
Пример #7
0
        public void Auction_FromEvents_builds_auction_from_pending_events()
        {
            var user = AuctionTestUtils.CreateUser();

            auction.Raise(user, 21);
            auction.EndAuction();

            var built = Auction.FromEvents(auction.PendingEvents);

            auction.MarkPendingEventsAsHandled();

            built.Should()
            .BeEquivalentTo(auction);
        }
Пример #8
0
        public void SetUp()
        {
            auction = AuctionTestUtils.CreateAuction();
            for (int i = 0; i < users.Length; i++)
            {
                users[i] = AuctionTestUtils.CreateUser();
            }


            var mockRepo = new Mock <IUserRepository>();

            mockRepo.Setup(r => r.FindUser(It.IsAny <UserIdentity>())).Returns <UserIdentity>(identity =>
            {
                return(auction.Bids.Where(bid => bid.UserIdentity.UserId.Equals(identity.UserId))
                       .Select(bid => bid.UserIdentity)
                       .Select(userIdentity =>
                               users.First(user => user.UserIdentity.UserId.Equals(userIdentity.UserId)))
                       .FirstOrDefault());
            });

            service = new BuyNowService(mockRepo.Object);
        }
Пример #9
0
        public void RemoveBid_when_bid_exists_removes_it()
        {
            var user1 = AuctionTestUtils.CreateUser();
            var user2 = AuctionTestUtils.CreateUser();

            auction.Raise(user1, 10);
            var firstBid = auction.Bids.Last();

            auction.Raise(user2, 20);
            var secondBid = auction.Bids.Last();

            auction.MarkPendingEventsAsHandled();

            auction.RemoveBid(firstBid);

            auction.ActualPrice.Should()
            .Be(20);
            auction.PendingEvents.Count.Should()
            .Be(1);
            auction.PendingEvents.First()
            .Should()
            .BeOfType <BidRemoved>();
        }
Пример #10
0
        public void EndAuction_when_has_bids_generates_valid_event_and_state()
        {
            var user = AuctionTestUtils.CreateUser();

            auction.Raise(user, 91);
            auction.MarkPendingEventsAsHandled();

            auction.EndAuction();

            auction.PendingEvents.Count.Should()
            .Be(1);
            auction.Buyer.Should()
            .Be(user.UserIdentity);
            auction.Completed.Should()
            .BeTrue();


            var ev = auction.PendingEvents.First() as AuctionCompleted;

            ev.AuctionId.Should()
            .Be(auction.AggregateId);
            ev.WinningBid.Should()
            .Be(auction.Bids.Last());
        }
Пример #11
0
        public void Raise_when_user_does_not_have_enough_credits_throws()
        {
            var user = AuctionTestUtils.CreateUser(0);

            Assert.Throws <DomainException>(() => auction.Raise(user, 91));
        }