Esempio n. 1
0
        public void Can_return_price_from_many_prices_with_start_and_end_date()
        {
            var evalContext = new PriceEvaluationContext
            {
                ProductIds   = new[] { "ProductId" },
                PricelistIds = new[] { "List1" }
            };

            var mockPrices = new PriceEntity[] {
                // Unbounded past.
                new PriceEntity {
                    Id = "1", List = 1, EndDate = new DateTime(2018, 09, 10), PricelistId = "List1", ProductId = "ProductId"
                },
                // Bounded past.
                new PriceEntity {
                    Id = "2", List = 2, StartDate = new DateTime(2018, 09, 15), EndDate = new DateTime(2018, 09, 17), PricelistId = "List1", ProductId = "ProductId"
                },

                // Bounded future.
                new PriceEntity {
                    Id = "3", List = 3, StartDate = new DateTime(2018, 09, 26), EndDate = new DateTime(2018, 09, 29), PricelistId = "List1", ProductId = "ProductId"
                },
                // Unbounded future.
                new PriceEntity {
                    Id = "4", List = 4, StartDate = new DateTime(2018, 10, 1), PricelistId = "List1", ProductId = "ProductId"
                },

                // Default unfiltered price.
                new PriceEntity {
                    Id = "10", List = 10, PricelistId = "List1", ProductId = "ProductId"
                },
            }.AsQueryable().BuildMock();

            var mockRepository = new Mock <IPricingRepository>();

            mockRepository.SetupGet(x => x.Prices).Returns(mockPrices.Object);

            var service = new PricingServiceImpl(() => mockRepository.Object, null, null, null, null,
                                                 new DefaultPricingPriorityFilterPolicy());

            // Eval with date and no matches, this should result in default price.
            evalContext.CertainDate = new DateTime(2018, 09, 20);
            var prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();

            Assert.Equal(10, prices.Single().List);

            // Eval with date falling in bounded future.
            evalContext.CertainDate = new DateTime(2018, 09, 27);
            prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();
            Assert.Equal(3, prices.Single().List);

            // Eval with date falling in unbounded future.
            evalContext.CertainDate = new DateTime(2118, 10, 2);
            prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();
            Assert.Equal(4, prices.Single().List);

            // Eval with date falling in bounded past.
            evalContext.CertainDate = new DateTime(2018, 9, 16);
            prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();
            Assert.Equal(2, prices.Single().List);

            // Eval with date falling in unbounded past.
            evalContext.CertainDate = new DateTime(2018, 8, 1);
            prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();
            Assert.Equal(1, prices.Single().List);

            // Eval with current date, should result in unbounded future price.
            evalContext.CertainDate = DateTime.UtcNow;
            prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();
            Assert.Equal(4, prices.Single().List);

            // Eval without date, should result in unbounded future price.
            // This is also a backwards compatibilty test.
            // CertainDate was not used in previous price evaluation. Default to 'now' behaviour.
            evalContext.CertainDate = null;
            prices = service.EvaluateProductPricesAsync(evalContext).GetAwaiter().GetResult();
            Assert.Equal(4, prices.Single().List);
        }