Beispiel #1
0
        public async Task CheckoutProcessReturns_Correct_Total(List <string> productCodes, decimal actualPrice)
        {
            // Arrange
            var context = new Context(new DbContextOptionsBuilder <Context>().UseInMemoryDatabase("stage").Options);

            context.AddRange(CheckoutProcessTestData.Products);
            context.SaveChanges();
            var repository = new Repository <Product>(context);

            var productRuleBinder = new ProductRuleBinder();

            productRuleBinder.Add(new Product {
                Code = "TICKET", DefaultPrice = 5.00M, Name = "Triggerise Ticket"
            }, new TwoForOnePromotionRule());
            productRuleBinder.Add(new Product {
                Code = "HOODIE", DefaultPrice = 5.00M, Name = "Triggerise Hoodie"
            }, new BulkPurchasePriceRule(3, 19.00M));

            // Act
            var checkoutProcess = new Checkout(repository, productRuleBinder);

            foreach (var code in productCodes)
            {
                await checkoutProcess.Scan(code);
            }

            var total = checkoutProcess.GetTotal();

            // Assert
            Assert.Equal(actualPrice, total);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Triggerise BE Challenge!");

            var services = new ServiceCollection();

            services.AddDbContext <Context>(opt => opt.UseInMemoryDatabase("stage"));

            var products = new List <Product>
            {
                new Product {
                    Id = Guid.NewGuid(), Code = "TICKET", Name = "Triggerise Ticket", DefaultPrice = 5.00M
                },
                new Product {
                    Id = Guid.NewGuid(), Code = "HOODIE", Name = "Triggerise Hoodie", DefaultPrice = 20.00M
                },
                new Product {
                    Id = Guid.NewGuid(), Code = "HAT", Name = "Triggerise Hat", DefaultPrice = 7.50M
                }
            };
            var productrepository = new Repository <Product>(services.BuildServiceProvider().GetService <Context>());

            productrepository.AddRangeAsync(products).Wait();
            productrepository.SaveChanges();

            services.AddScoped(typeof(ProductRuleBinder), s =>
            {
                var _productRuleBinder = new ProductRuleBinder();
                var ticketproduct      = productrepository.Get(filter: x => x.Code.Equals("TICKET", StringComparison.OrdinalIgnoreCase)).First();
                var hoodieproduct      = productrepository.Get(filter: x => x.Code.Equals("HOODIE", StringComparison.OrdinalIgnoreCase)).First();

                _productRuleBinder.Add(ticketproduct, new TwoForOnePromotionRule());
                _productRuleBinder.Add(hoodieproduct, new BulkPurchasePriceRule(3, 19.00M));

                return(_productRuleBinder);
            });

            var productRuleBinder = services.BuildServiceProvider().GetService <ProductRuleBinder>();
            var checkout          = new Checkout(productrepository, productRuleBinder);

            checkout.Scan("TICKET").Wait();
            checkout.Scan("HOODIE").Wait();
            checkout.Scan("HAT").Wait();

            var total = checkout.GetTotal();

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"Total = $ {total}");
            Console.ReadKey();
        }