예제 #1
0
            public async Task CanGetItem_PostNewItem()
            {
                var repository     = new InMemoryItemRepository();
                var readRepository = new InMemoryItemReadRepository();

                repository.EventOccurred += async entity => await new ItemReadModelHandler(readRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();

                var postNewItemDto = new PostRequestDto
                {
                    Code = "lettuce"
                };

                await itemController.Post(postNewItemDto);

                var anotherItemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();
                var result = await anotherItemController.GetByItemCode("lettuce");

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);

                var itemDto = (ItemDto)result.Value;

                Assert.Equal("lettuce", itemDto.Code);
            }
예제 #2
0
            public async Task ReturnsOk_SetsDiscountOnItem()
            {
                var repository     = new InMemoryItemRepository();
                var readRepository = new InMemoryItemReadRepository();

                repository.EventOccurred += async entity => await new ItemReadModelHandler(readRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postNewPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postNewPotatoResult.Value;

                var postSetDiscountDto = new SetDiscountRequestDto
                {
                    PercentOff = 10
                };

                var result = await itemController.SetDiscount(potatoDto.Id, postSetDiscountDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);

                var potatoItemDto = (ItemDto)result.Value;

                Assert.Equal(10, potatoItemDto.PercentOff);
            }
예제 #3
0
            public async Task RespondsWithCreated_PostNewItem()
            {
                var repository     = new InMemoryItemRepository();
                var readRepository = new InMemoryItemReadRepository();

                repository.EventOccurred += async entity => await new ItemReadModelHandler(readRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();

                var itemTypeId = Guid.NewGuid();

                var postNewItemDto = new PostRequestDto
                {
                    Code       = "lettuce",
                    ItemTypeId = itemTypeId
                };

                var result = await itemController.Post(postNewItemDto);

                Assert.Equal((int)HttpStatusCode.Created, result.StatusCode);

                var itemDto = (ItemDto)result.Value;

                Assert.Equal("lettuce", itemDto.Code);
                Assert.Equal(itemTypeId, itemDto.ItemTypeId);
            }
            public async Task CostIsZero_NoShoppingItems()
            {
                var itemRepository       = new InMemoryItemRepository();
                var calculatorController = new CalculatorController(itemRepository, new InMemoryCouponReadRepository())
                                           .BootstrapForTests();

                var result = await calculatorController.ComputeCost(new CalculatorComputeCostRequestDto());

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(0, dto.TotalCost);
            }
예제 #5
0
            public async Task Returns404_GetNonExistentItem()
            {
                var repository     = new InMemoryItemRepository();
                var readRepository = new InMemoryItemReadRepository();

                repository.EventOccurred += entity => readRepository.UpdateAsync(new ItemReadModel());

                var itemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();

                var result = await itemController.GetByItemCode("unknown");

                Assert.Equal((int)HttpStatusCode.NotFound, result.StatusCode);
                Assert.Equal("unknown", result.Value);
            }
            public async Task ReturnsTotalCost_SingleItem_SingleQuantity()
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var calculatorController = new CalculatorController(itemRepository, new InMemoryCouponReadRepository())
                                           .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(30, dto.TotalCost);
            }
예제 #7
0
            public async Task CanGetItems_PostMultipleItems()
            {
                var repository     = new InMemoryItemRepository();
                var readRepository = new InMemoryItemReadRepository();

                repository.EventOccurred += async entity => await new ItemReadModelHandler(readRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                await itemController.Post(postNewPotatoItemDto);

                var postNewLettuceItemDto = new PostRequestDto
                {
                    Code  = "lettuce",
                    Price = 50
                };

                await itemController.Post(postNewLettuceItemDto);

                var potatoResult = await itemController.GetByItemCode("potato");

                Assert.Equal((int)HttpStatusCode.OK, potatoResult.StatusCode);

                var potatoItemDto = (ItemDto)potatoResult.Value;

                Assert.Equal("potato", potatoItemDto.Code);
                Assert.Equal(30, potatoItemDto.Price);

                var lettuceResult = await itemController.GetByItemCode("lettuce");

                Assert.Equal((int)HttpStatusCode.OK, lettuceResult.StatusCode);

                var lettuceItemDto = (ItemDto)lettuceResult.Value;

                Assert.Equal("lettuce", lettuceItemDto.Code);
                Assert.Equal(50, lettuceItemDto.Price);
            }
예제 #8
0
            public async Task GetReturnsTheSameItem_PostsTheSameItemTwiceButUsesDataOfTheFirst()
            {
                var repository     = new InMemoryItemRepository();
                var readRepository = new InMemoryItemReadRepository();

                repository.EventOccurred += async entity => await new ItemReadModelHandler(readRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(repository, readRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                await itemController.Post(postNewPotatoItemDto);

                var postNewLettuceItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 50
                };

                await itemController.Post(postNewLettuceItemDto);

                var potatoResult = await itemController.GetByItemCode("potato");

                Assert.Equal((int)HttpStatusCode.OK, potatoResult.StatusCode);

                var potatoItemDto = (ItemDto)potatoResult.Value;

                Assert.Equal("potato", potatoItemDto.Code);
                Assert.Equal(30, potatoItemDto.Price);
            }
예제 #9
0
        static void Main(string[] args)
        {
            IItemRepository <ItemData> itemRepository = new InMemoryItemRepository();
            var inventoryManager = new InventoryManager();

            inventoryManager.AddInventoryCommand(new CreateItemCommand(itemRepository, new ItemData("Book01", 10.50M, 13.79M)));
            inventoryManager.AddInventoryCommand(new CreateItemCommand(itemRepository, new ItemData("Food01", 1.47M, 3.98M)));
            inventoryManager.AddInventoryCommand(new CreateItemCommand(itemRepository, new ItemData("Med01", 30.63M, 34.29M)));
            inventoryManager.AddInventoryCommand(new CreateItemCommand(itemRepository, new ItemData("Tab01", 57.00M, 84.98M)));

            inventoryManager.AddInventoryCommand(new UpdateBuyItemCommand(itemRepository, "Tab01", 100));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Tab01", 2));
            inventoryManager.AddInventoryCommand(new UpdateBuyItemCommand(itemRepository, "Food01", 500));
            inventoryManager.AddInventoryCommand(new UpdateBuyItemCommand(itemRepository, "Book01", 100));

            inventoryManager.AddInventoryCommand(new UpdateBuyItemCommand(itemRepository, "Med01", 100));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Food01", 1));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Food01", 1));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Tab01", 2));

            inventoryManager.AddInventoryCommand(new ReportItemCommand(itemRepository));

            inventoryManager.AddInventoryCommand(new DeleteItemCommand(itemRepository, "Book01"));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Tab01", 5));
            inventoryManager.AddInventoryCommand(new CreateItemCommand(itemRepository, new ItemData("Mobile01", 10.51M, 44.56M)));
            inventoryManager.AddInventoryCommand(new UpdateBuyItemCommand(itemRepository, "Mobile01", 250));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Food01", 5));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Mobile01", 4));
            inventoryManager.AddInventoryCommand(new UpdateSellItemCommand(itemRepository, "Med01", 10));

            inventoryManager.AddInventoryCommand(new ReportItemCommand(itemRepository));

            inventoryManager.ProcessInventoryCommands();

            System.Console.Read();
        }
예제 #10
0
 public RepositoryTest()
 {
     _itemRepository = new InMemoryItemRepository();
 }
            public async Task ApplyHighestDiscount_TwoItems_DiscountedOneItemByFixedAmountAndPercentage()
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var postNewLettuceItemDto = new PostRequestDto
                {
                    Code  = "lettuce",
                    Price = 50
                };

                var postLettuceResult = await itemController.Post(postNewLettuceItemDto);

                var lettuceDto = (ItemDto)postLettuceResult.Value;

                var setDiscountOnPotatoDto = new SetDiscountRequestDto
                {
                    AmountOff = 5
                };

                await itemController.SetDiscount(potatoDto.Id, setDiscountOnPotatoDto);

                var calculatorController = new CalculatorController(itemRepository, new InMemoryCouponReadRepository())
                                           .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 3
                        },
                        new ShoppingItemDto
                        {
                            Id       = lettuceDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(125, dto.TotalCost);
            }
            public async Task ApplyHighestDiscount_HasAmountOffHasPercentOffHasCoupon(
                string testName,
                double itemPercentOff,
                decimal itemAmountOff,
                double couponPercentOff,
                decimal expectedTotalCost)
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 50
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var setDiscountOnPotatoDto = new SetDiscountRequestDto
                {
                    PercentOff = itemPercentOff,
                    AmountOff  = itemAmountOff
                };

                await itemController.SetDiscount(potatoDto.Id, setDiscountOnPotatoDto);

                var couponRepository     = new InMemoryCouponRepository();
                var couponReadRepository = new InMemoryCouponReadRepository();

                couponRepository.EventOccurred += async entity =>
                                                  await new CouponReadModelHandler(couponReadRepository).Handle(entity.NewEvents.ToList <object>());

                var couponController = new CouponController(couponRepository)
                                       .BootstrapForTests();

                var postRequestDto = new ShoppingCartApi.Controllers.Coupon.PostRequestDto
                {
                    Code       = "GRAND_SALE",
                    PercentOff = couponPercentOff
                };

                await couponController.Post(postRequestDto);

                var calculatorController =
                    new CalculatorController(itemRepository, couponReadRepository)
                    .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    CouponCode    = "GRAND_SALE",
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 2
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(expectedTotalCost, dto.TotalCost);
            }
            public async Task ApplyDiscount_FromCouponOnlyForSelectItemType()
            {
                var itemTypeRepository     = new InMemoryItemTypeRepository();
                var itemTypeReadRepository = new InMemoryItemTypeReadRepository();

                itemTypeRepository.EventOccurred += async entity => await new ItemTypeReadModelHandler(itemTypeReadRepository).Handle(entity.NewEvents.ToList <object>());
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());
                var couponRepository     = new InMemoryCouponRepository();
                var couponReadRepository = new InMemoryCouponReadRepository();

                couponRepository.EventOccurred += async entity =>
                                                  await new CouponReadModelHandler(couponReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemTypeController = new ItemTypeController(itemTypeRepository, itemTypeReadRepository)
                                         .BootstrapForTests();

                var postFruitResult = await itemTypeController.Post(
                    new ShoppingCartApi.Controllers.ItemType.PostRequestDto
                {
                    Code = "fruit"
                });

                var fruitItemTypeDto = (ItemTypeDto)postFruitResult.Value;

                var postVegetableResult = await itemTypeController.Post(
                    new ShoppingCartApi.Controllers.ItemType.PostRequestDto
                {
                    Code = "vegetable"
                });

                var vegetableItemTypeDto = (ItemTypeDto)postVegetableResult.Value;

                var itemController = new ItemController(itemRepository, itemReadRepository)
                                     .BootstrapForTests();

                var postPotatoResult = await itemController.Post(new PostRequestDto
                {
                    Code       = "potato",
                    Price      = 30,
                    ItemTypeId = vegetableItemTypeDto.Id
                });

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var postAppleResult = await itemController.Post(new PostRequestDto
                {
                    Code       = "apple",
                    Price      = 50,
                    ItemTypeId = fruitItemTypeDto.Id
                });

                var appleDto = (ItemDto)postAppleResult.Value;

                var couponController = new CouponController(couponRepository)
                                       .BootstrapForTests();
                var postRequestDto = new ShoppingCartApi.Controllers.Coupon.PostRequestDto
                {
                    Code          = "HALF_OFF_FRUIT",
                    PercentOff    = 50,
                    ForItemTypeId = fruitItemTypeDto.Id
                };
                await couponController.Post(postRequestDto);

                var calculatorController =
                    new CalculatorController(itemRepository, couponReadRepository)
                    .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    CouponCode    = "HALF_OFF_FRUIT",
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 3
                        },
                        new ShoppingItemDto
                        {
                            Id       = appleDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(115, dto.TotalCost);
            }
            public async Task ApplyDiscount_FromCoupon()
            {
                var itemRepository     = new InMemoryItemRepository();
                var itemReadRepository = new InMemoryItemReadRepository();

                itemRepository.EventOccurred += async entity => await new ItemReadModelHandler(itemReadRepository).Handle(entity.NewEvents.ToList <object>());

                var itemController =
                    new ItemController(itemRepository, itemReadRepository)
                    .BootstrapForTests();

                var postNewPotatoItemDto = new PostRequestDto
                {
                    Code  = "potato",
                    Price = 30
                };

                var postPotatoResult = await itemController.Post(postNewPotatoItemDto);

                var potatoDto = (ItemDto)postPotatoResult.Value;

                var postNewLettuceItemDto = new PostRequestDto
                {
                    Code  = "lettuce",
                    Price = 50
                };

                var postLettuceResult = await itemController.Post(postNewLettuceItemDto);

                var lettuceDto = (ItemDto)postLettuceResult.Value;

                var couponRepository     = new InMemoryCouponRepository();
                var couponReadRepository = new InMemoryCouponReadRepository();

                couponRepository.EventOccurred += async entity =>
                                                  await new CouponReadModelHandler(couponReadRepository).Handle(entity.NewEvents.ToList <object>());

                var couponController = new CouponController(couponRepository)
                                       .BootstrapForTests();

                var postRequestDto = new ShoppingCartApi.Controllers.Coupon.PostRequestDto
                {
                    Code       = "GRAND_SALE",
                    PercentOff = 20
                };

                await couponController.Post(postRequestDto);

                var calculatorController =
                    new CalculatorController(itemRepository, couponReadRepository)
                    .BootstrapForTests();

                var calculatorComputeCostRequestDto =
                    new CalculatorComputeCostRequestDto
                {
                    CouponCode    = "GRAND_SALE",
                    ShoppingItems =
                        new List <ShoppingItemDto>
                    {
                        new ShoppingItemDto
                        {
                            Id       = potatoDto.Id,
                            Quantity = 3
                        },
                        new ShoppingItemDto
                        {
                            Id       = lettuceDto.Id,
                            Quantity = 1
                        }
                    }
                };

                var result = await calculatorController.ComputeCost(calculatorComputeCostRequestDto);

                Assert.Equal((int)HttpStatusCode.OK, result.StatusCode);
                Assert.NotNull(result.Value);

                var dto = (CalculatorComputeCostDto)result.Value;

                Assert.Equal(112, dto.TotalCost);
            }