Esempio n. 1
0
        public void TestBasketExistsShouldBeSafedAfterModification()
        {
            var basket = new Basket("*****@*****.**");

            basket.Articles.Add(new Article {
                Price = 19.99M, Quantity = 1
            });
            var inMemoryBasketRepository = new InMemoryBasketRepository(basket);

            var interactor = new AddArticleInteractor <IViewModel>(new TestAddArticlePresenter(), inMemoryBasketRepository);

            interactor.ExecuteAsync(new AddArticleRequest {
                Email = "*****@*****.**", Price = 9.99M, Quantity = 2
            });

            var response = ((TestAddArticlePresenter)interactor.Presenter).GetResponse();

            Assert.AreEqual(2, response.ArticleCount);
            Assert.AreEqual(39.97M, response.BasketValue);

            var savedBasket = inMemoryBasketRepository.GetBasket(string.Empty);

            Assert.AreEqual(2, savedBasket.Articles.Count);
            Assert.AreEqual(39.97M, savedBasket.Value);
        }
        public void GetBasket_ReturnsNullWhenBasketDoesntExistsForUser()
        {
            var sut    = new InMemoryBasketRepository();
            var basket = sut.GetBasket(new UserId("ryan"));

            Assert.Null(basket);
        }
        public async Task RemovedItemShouldNotBePresentInBasketsItemsList()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      = new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);

            await basketService.AddItemToTheBasketAsync("item1", 1, _testUserId1);

            await basketService.AddItemToTheBasketAsync("item2", 1, _testUserId1);

            await basketService.AddItemToTheBasketAsync("item3", 1, _testUserId1);

            await basketService.AddItemToTheBasketAsync("item4", 1, _testUserId1);

            var basket = await basketService.GetOrCreateBasketforUserAsync(_testUserId1);

            basket.Items.Count.ShouldBe(4);


            basket = await basketService.RemoveItemFromtheBasketAsync("item1", _testUserId1);

            basket.Items.Count.ShouldBe(3);
            basket.Items.Any(x => x.ItemId == "item1").ShouldBe(false);

            basket = await basketService.RemoveItemFromtheBasketAsync("item2", _testUserId1);

            basket.Items.Count.ShouldBe(2);

            basket.Items.Any(x => x.ItemId == "item2").ShouldBe(false);
        }
        public async Task ChangeQuantityoftheBasketItemAsync_ShouldThrowfQuantityisLessThanOne()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      = new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);

            await Assert.ThrowsAsync <ArgumentException>(() =>
                                                         basketService.ChangeQuantityoftheBasketItemAsync(_testItemId1, 0, _testUserId1));
        }
        public async Task ChangeQuantityoftheBasketShouldReturnNullIfNoItemIsPresentInBasket()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      = new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);
            var result             =
                await basketService.ChangeQuantityoftheBasketItemAsync(_testItemId1, _testQuantity1, _testUserId1);

            result.ShouldBeNull();
        }
        public void CanSaveAndRetrieveBasket()
        {
            UserId userId = new UserId("foo");
            var    sut    = new InMemoryBasketRepository();
            Basket basket = new Basket(userId, creationDate, this.inventory);

            sut.Save(basket);
            var actual = sut.GetBasket(userId);

            Assert.Same(basket, actual);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var productSystem = new PretendPurchaseSystem();

            var lordOfTheRings = new ProductId(10001);
            var theHobbit      = new ProductId(10002);
            var gameOfThrones  = new ProductId(20001);
            var breakingBad    = new ProductId(20110);

            var inventory = new Inventory(productSystem);

            inventory.Add(lordOfTheRings, 10, 10m);
            inventory.Add(theHobbit, 12, 5m);
            inventory.Add(gameOfThrones, 8, 9m);
            inventory.Add(breakingBad, 14, 7m);
            inventory.Print(Console.Out);

            var john = new UserId("john");
            var kim  = new UserId("kim");

            var basketRepository = new InMemoryBasketRepository();
            var basketFactory    = new BasketFactory(new SystemClock(),
                                                     Console.Out,
                                                     inventory);
            var shoppingBasketService = new ShoppingBasketService(
                basketRepository,
                basketFactory,
                inventory);

            // No discounts
            shoppingBasketService.AddItem(john, lordOfTheRings, 1);
            shoppingBasketService.AddItem(john, theHobbit, 1);

            shoppingBasketService.AddItem(kim, breakingBad, 1);

            var johnsCart = shoppingBasketService.BasketFor(john);
            var kimsCart  = shoppingBasketService.BasketFor(kim);

            Console.WriteLine($"John's basket total: {johnsCart.Total:c}");
            Console.WriteLine($"Kim's basket total: {kimsCart.Total:c}");

            var johnsPayment      = new PaymentDetails();
            var orderService      = new OrderService(new OrderIdGenerator(), basketRepository);
            var paymentGateway    = new PretendPaymentGateway();
            var orderConfirmation = new PretendOrderConfirmation();
            var paymentService    = new PaymentService(orderService,
                                                       paymentGateway,
                                                       inventory,
                                                       orderConfirmation);

            paymentService.MakePayment(john, johnsCart.Id, johnsPayment);

            inventory.Print(Console.Out);
        }
        public void CanStoreMultipleUserBaskets()
        {
            UserId userId = new UserId("foo");
            var    sut    = new InMemoryBasketRepository();
            Basket basket = new Basket(userId, creationDate, this.inventory);

            sut.Save(basket);
            sut.Save(new Basket(new UserId("bar"), creationDate, this.inventory));
            var actual = sut.GetBasket(userId);

            Assert.Same(basket, actual);
        }
        public void Save_SameUsersBasketOverwrites()
        {
            var    sut  = new InMemoryBasketRepository();
            UserId john = new UserId("john");

            sut.Save(new Basket(john, creationDate, this.inventory));
            Basket basket = new Basket(john, creationDate, this.inventory);

            basket.Add(new BasketItem(new ProductId(20001), 5));
            sut.Save(basket);
            var actual = sut.GetBasket(john);

            Assert.Same(basket, actual);
        }
        public async Task WithFalseIdNothingShouldBeDeleted()
        {
            var basketRepository = new InMemoryBasketRepository(null);

            var newBasket = new Basket()
            {
                UserId = _testUserId,
                Id     = _testBasketId
            };

            await basketRepository.Add(newBasket);

            await Assert.ThrowsAsync <ArgumentNullException>(() => basketRepository.Delete(0, ""));
        }
        public async Task ShouldNotAddBasketItem()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      =
                new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);

            await basketService.GetOrCreateBasketforUserAsync(_testUserId1);

            await basketService.AddItemToTheBasketAsync(null, 0, _testUserId1);

            var usersBasket = await basketService.GetOrCreateBasketforUserAsync(_testUserId1);

            usersBasket.Items.Count.ShouldBe(0);
        }
        public async Task ShouldCreateAndReturnaBasketForUserIfNotABasketPresent()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      = new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);

            await basketService.GetOrCreateBasketforUserAsync(_testUserId1);

            var basketinRepo =
                (await inMemoryBasketRepo.Find(new BasketFilterObject {
                UserId = _testUserId1
            })).First();

            basketinRepo.UserId.ShouldBe(_testUserId1);
        }
        public async Task WithTrueValuesBasketShouldBeDeleted()
        {
            var basketRepository = new InMemoryBasketRepository(null);

            var newBasket = new Basket()
            {
                UserId = _testUserId,
                Id     = _testBasketId
            };

            await basketRepository.Add(newBasket);

            var deleteResult = await basketRepository.Delete(_testBasketId, _testUserId);

            deleteResult.ShouldBe(true);
        }
        public async Task ShouldCreateMultipleBaskets()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      = new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);

            await basketService.GetOrCreateBasketforUserAsync("user1");

            await basketService.GetOrCreateBasketforUserAsync("user2");

            await basketService.GetOrCreateBasketforUserAsync("user3");

            var basketCount =
                (await inMemoryBasketRepo.Find(new BasketFilterObject {
            })).Count;

            basketCount.ShouldBe(3);
        }
        public async Task NewlyAddedBasketShouldBeinRepository()
        {
            var basketRepository = new InMemoryBasketRepository(null);

            var newBasket = new Basket
            {
                UserId = _testUserId,
                Id     = _testBasketId
            };

            //Add a new basket to in memory list and fetch and check
            await basketRepository.Add(newBasket);

            var basketInfo = (await basketRepository.Find(new BasketFilterObject())).First();

            basketInfo.UserId.ShouldBe(_testUserId);
            basketInfo.Id.ShouldBe(_testBasketId);

            //get by id an check values
            var basketGetById = (await basketRepository.Get(_testBasketId));

            basketGetById.UserId.ShouldBe(_testUserId);
            basketGetById.Id.ShouldBe(_testBasketId);

            var newBasket2 = new Basket()
            {
                UserId = _testUserId1,
                Id     = _testBasketId1
            };

            //add a new Basket- There should be two baskets now.
            await basketRepository.Add(newBasket2);

            var basketFounSearchInfo = (await basketRepository.Find(new BasketFilterObject {
                BasketId = _testBasketId1
            })).First();

            basketFounSearchInfo.UserId.ShouldBe(_testUserId1);
            basketFounSearchInfo.Id.ShouldBe(_testBasketId1);

            var basketCount = (await basketRepository.Find(new BasketFilterObject())).Count();

            basketCount.ShouldBe(2);
        }
        public async Task ChangeQuantityoftheBasketShouldSaveNewQuantityIfItemIsPresentInBasket()
        {
            var inMemoryBasketRepo = new InMemoryBasketRepository(_mockBasketLogger.Object);
            var basketService      =
                new BasketService(inMemoryBasketRepo, _catalogService, _mockServiceLogger.Object);
            var basket = await basketService.GetOrCreateBasketforUserAsync(_testUserId1);

            var updatedBasket =
                await basketService.ChangeQuantityoftheBasketItemAsync(_testItemId1, _testQuantity1, _testUserId1);

            updatedBasket.ShouldBeNull();

            basket.AddItem(_testItemId1, 1, 1, null);

            updatedBasket =
                await basketService.ChangeQuantityoftheBasketItemAsync(_testItemId1, _testQuantity1, _testUserId1);

            updatedBasket.Items.First().Quantity.ShouldBe(_testQuantity1);
        }
        public void ImplementsIBasketRepository()
        {
            var sut = new InMemoryBasketRepository();

            Assert.IsAssignableFrom <IBasketRepository>(sut);
        }