コード例 #1
0
        public async virtual void ProductPriceRecalculation(ITestOutputHelper output, int productId)
        {
            IPriceEngine engine = new PriceEngine(_pricingRepository);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            var result = _customerDbRepository.ExecutePriceRecalculation(productId, 0, 0, 0, 0);

            sw.Stop();
            output.WriteLine($"Old price recalculation takes {sw.Elapsed.TotalSeconds} seconds.");
            //acknoledge
            sw.Restart();
            var dbPrices = result.ToList();
            var dt       = System.DateTime.UtcNow;
            //act
            var prices = await engine.Recalculation(productId, false, dt);

            sw.Stop();
            output.WriteLine($"New price recalculation takes {sw.Elapsed.TotalSeconds} seconds.");

            //Assert.Equal(dbPrices.Count, prices.Count());

            var priceToUpdate = engine.ComparePrices(dbPrices, prices).ToList();

            Assert.Empty(priceToUpdate);
            var pricesToDelete = engine.ComparePrices(prices, dbPrices).ToList();

            Assert.Empty(pricesToDelete);

            output.WriteLine($"Total Prices {prices.Count() }");
        }
コード例 #2
0
        public void RecalculationStore()
        {
            Mock <IPricingRepository> repo = new Mock <IPricingRepository>();
            int      logIdResult           = 123;
            string   actionExpected        = "Dish Pricing Calculation";
            string   actionExpectedSkipped = "Dish Pricing Calculation Skipped";
            string   actionResult          = string.Empty;
            DateTime dtResult = DateTime.MinValue;
            int      productIdResult;
            DateTime messageTime           = DateTime.UtcNow;
            int      productId             = 1122;
            bool     isSuccessStatusUpdate = false;

            repo.Setup(x => x.GetLastMsmqStartTime(productId)).Returns(() => { return(Task.FromResult <MsmqLog>(new MsmqLog()
                {
                    StartTime = DateTime.UtcNow.AddHours(-1)
                })); });
            repo.Setup(x => x.CreateMsmqLog(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime>())).Callback <string, int, int, int, int, int, DateTime>((string action, int product_Id, int groupid, int pbandId, int setid, int unitId, DateTime dt) => {
                actionResult    = action;
                dtResult        = dt;
                productIdResult = product_Id;
            }).Returns(() => { return(Task.FromResult <int>(logIdResult)); });

            List <ProductGroupPrice> privateItems = new List <ProductGroupPrice>();

            privateItems.Add(new ProductGroupPrice()
            {
                GroupId = 0, Price = 123.321m, ProductId = productId
            });

            List <DbPrice> expectedPrices = new List <DbPrice>();

            expectedPrices.Add(new DbPrice()
            {
                ProductId = productId, Price = 123.321m
            });

            List <Product> products = new List <Product>();

            products.Add(new Product()
            {
                ScopeId = 3, ProductId = productId
            });

            repo.Setup(x => x.GetProducts()).Returns(() => { return(Task.FromResult(products.AsEnumerable())); });

            repo.Setup(x => x.GetProductParts()).Returns(() => { return(Task.FromResult(new List <ProductPart>().AsEnumerable())); });

            var res = new Tuple <IEnumerable <Product>, IEnumerable <ProductPart> >(products, new List <ProductPart>());

            repo.Setup(x => x.GetProductsAndParts(It.IsAny <int>())).Returns(() => { return(Task.FromResult(res)); });


            repo.Setup(x => x.GetGroupProductPricesByProduct(It.IsAny <int>())).Returns(() => { return(Task.FromResult <IEnumerable <ProductGroupPrice> >(privateItems)); });

            repo.Setup(x => x.UpdatePrices(It.IsAny <Dictionary <int, decimal> >(), It.Is <int?>(g => !g.HasValue), It.Is <int>(l => l == 123), It.Is <DateTime>(d => d == messageTime))).Returns(true);

            repo.Setup(x => x.UpdateMsmqLog(It.IsAny <DateTime>(), It.Is <int>(l => l == 123), It.IsAny <bool>())).Callback <DateTime, int, bool>((DateTime dt, int product_Id, bool isSuccess) => {
                isSuccessStatusUpdate = isSuccess;
                dtResult        = dt;
                productIdResult = product_Id;
            }).Returns(() => Task.FromResult <int>(1));

            IPriceEngine engine = new PriceEngine(repo.Object);
            var          result = engine.Recalculation(productId, true, messageTime).Result;

            Assert.Equal(actionExpected, actionResult);
            Assert.NotEmpty(result);
            Assert.Equal(expectedPrices, result);
            Assert.True(isSuccessStatusUpdate);

            repo.Setup(x => x.GetLastMsmqStartTime(productId)).Returns(() => { return(Task.FromResult <MsmqLog>(new MsmqLog()
                {
                    StartTime = DateTime.UtcNow.AddHours(1)
                })); });

            engine = new PriceEngine(repo.Object);
            result = engine.Recalculation(productId, true, messageTime).Result;

            Assert.Equal(actionExpectedSkipped, actionResult);
            Assert.Empty(result);

            repo.Setup(x => x.GetLastMsmqStartTime(productId)).Returns(() => { return(Task.FromResult <MsmqLog>(new MsmqLog()
                {
                    StartTime = DateTime.UtcNow.AddHours(-1)
                })); });
            repo.Setup(x => x.UpdatePrices(It.IsAny <Dictionary <int, decimal> >(), It.Is <int?>(g => !g.HasValue), It.Is <int>(l => l == 123), It.Is <DateTime>(d => d == messageTime))).Returns(false);

            engine = new PriceEngine(repo.Object);
            result = engine.Recalculation(productId, true, messageTime).Result;

            Assert.Equal(actionExpected, actionResult);
            Assert.NotEmpty(result);
            Assert.Equal(expectedPrices, result);
            Assert.False(isSuccessStatusUpdate);
        }