public async Task DeleteProperOffer_WhenValidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int validOfferId;
            int totalCountOfOffers;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validOfferId       = getContext.Offers.First().OfferID;
                totalCountOfOffers = getContext.Offers.Count();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var isExecxuted = await sut.DeleteOfferAsync(validOfferId);

                //Assert
                Assert.IsTrue(isExecxuted);
                Assert.IsTrue(context.Offers.Find(validOfferId) == null);
                Assert.AreEqual(totalCountOfOffers - 1, context.Offers.Count());
            }
        }
        public async Task DeleteProperProductOfferRecords_WhenValidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);
            Offer sale;
            int   totalRecordsInProductOffer;

            using (var getContext = new StoreSystemDbContext(options))
            {
                sale = getContext.Offers.Include(x => x.ProductsInOffer).First();
                totalRecordsInProductOffer = getContext.ProductOffers.Count();
            }

            int validOfferId           = sale.OfferID;
            var countOfProductsInOffer = sale.ProductsInOffer.Count();

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var isExecxuted = await sut.DeleteOfferAsync(validOfferId);

                //Assert
                Assert.IsTrue(isExecxuted);
                Assert.AreEqual(totalRecordsInProductOffer - countOfProductsInOffer, context.ProductOffers.Count());
            }
        }
        public async Task ThrowsArgumentException_WhenInvalidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int invalidOfferID = 100;

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var validDate           = new DateTime(2019, 4, 1);
                var sut = new OfferService(context, dateTimeNowProvider.Object);
                ProductIdQuantityDto[] products = new ProductIdQuantityDto[0];
                var errorText = string.Format(
                    Consts.ObjectIDNotExist,
                    nameof(Offer),
                    invalidOfferID);
                //Act
                //Assert
                Assert.AreEqual(
                    errorText,
                    (await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.AddProductsByIdToOfferAsync(invalidOfferID, products))).Message);
            }
        }
        public async Task CreateAndAddNewOfferWithIDsInDB_WhenValidParametersArePassed()
        {
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            using (var context = new StoreSystemDbContext(DbSeed.GetOptions(databaseName)))
            {
                //Arrange
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();

                var validDate = new DateTime(2019, 1, 1);

                dateTimeNowProvider.Setup(x => x.Now).Returns(validDate);
                var sut      = new OfferService(context, dateTimeNowProvider.Object);
                var client   = 1;
                var address  = 1;
                var city     = 1;
                var country  = 1;
                var deadline = new DateTime(2019, 5, 10);
                var discount = 0.10m;

                //Act
                await sut.CreateOfferAsync(client, discount, deadline, address, city, country);

                //Assert
                Assert.AreEqual(1, context.Offers.Count());
                Assert.AreEqual(client, context.Offers.Single(x => x.ClientID == client).ClientID);
            }
        }
Example #5
0
        public async Task ShouldReturnTotalSumOfFiltredOffers_WhenValidDatesArePassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            var     validStartDate = new DateTime(2019, 2, 2);
            var     validEndDate   = new DateTime(2019, 4, 4);
            decimal expectedTotal  = (decimal)((1 * 1 + 1 * 3 * 2) * (1 - 0.1 / 100));

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var actualTotal = await sut.GetOfferQuantityAsync(startDate : validStartDate, endDate : validEndDate);

                //Assert
                Assert.AreEqual(expectedTotal, actualTotal);
            }
        }
        public async Task UpdateOffer_WhenValidParanetersArePassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int      validOfferId;
            DateTime deliveryDate;
            var      client    = 1;
            var      address   = 1;
            var      city      = 1;
            var      country   = 1;
            var      deadline  = new DateTime(2019, 5, 10);
            var      discount  = 0.10m;
            var      offerDate = new DateTime(2019, 5, 1);

            using (var getContext = new StoreSystemDbContext(options))
            {
                var offer = getContext.Offers.First();
                validOfferId = offer.OfferID;
                deliveryDate = offer.OfferDate.AddDays(1);
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var isExecuted = await sut.UpdateOfferAsync(validOfferId, client, discount, offerDate, (deadline - offerDate).TotalDays, deliveryDate, address, city, country);

                //Assert
                Assert.IsTrue(isExecuted);
                Assert.AreEqual(client, context.Offers.Find(validOfferId).ClientID);
                Assert.AreEqual(discount, context.Offers.Find(validOfferId).ProductDiscount);
                Assert.AreEqual(offerDate, context.Offers.Find(validOfferId).OfferDate);
                Assert.AreEqual(deliveryDate, context.Offers.Find(validOfferId).DeliveryDate);
                Assert.AreEqual(deadline, context.Offers.Find(validOfferId).ExpiredDate);
                Assert.AreEqual(address, context.Offers.Find(validOfferId).AddressID);
                Assert.AreEqual(city, context.Offers.Find(validOfferId).CityID);
                Assert.AreEqual(country, context.Offers.Find(validOfferId).CountryID);
            }
        }
        public async Task ThrowsArgumentException_WhenInvalidDateIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int      validOfferId;
            DateTime deliveryDate = new DateTime(2019, 5, 8);
            var      client       = 1;
            var      address      = 1;
            var      city         = 1;
            var      country      = 1;
            var      discount     = 0.10m;
            DateTime offerDate;
            DateTime invalidDeadlineDate;


            using (var getContext = new StoreSystemDbContext(options))
            {
                var offer = getContext.Offers.First();
                validOfferId        = offer.OfferID;
                invalidDeadlineDate = offer.OfferDate.AddDays(-1);
                offerDate           = offer.OfferDate;
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut       = new OfferService(context, dateTimeNowProvider.Object);
                var errorText = string.Format(
                    Consts.DateError,
                    invalidDeadlineDate,
                    offerDate);
                //Act
                //Assert
                Assert.AreEqual(
                    errorText,
                    (await Assert.ThrowsExceptionAsync <ArgumentException>(() =>
                                                                           sut.UpdateOfferAsync(validOfferId, client, discount, offerDate, (invalidDeadlineDate - offerDate).TotalDays, deliveryDate, address, city, country))).Message
                    );
            }
        }
        public async Task ShouldReturnCollectionWithProductInOffer_WhenValidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int validOfferId                    = 1;
            int productsInOfferCount            = 2;
            ICollection <ProductOffer> products = new List <ProductOffer>
            {
                new ProductOffer {
                    ProductID = 1, Quantity = 1
                },
                new ProductOffer {
                    ProductID = 2, Quantity = 1
                }
            };

            //using (var getContext = new StoreSystemDbContext(options))
            //{
            //    var offer = getContext.Offers.Include(x => x.ProductsInOffer).First(x => x.ProductsInOffer.Count > 0);
            //    validOfferId = offer.OfferID;
            //    products = offer.ProductsInOffer;
            //    productsInOfferCount = products.Count;
            //}

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var actualProductsInOffer = await sut.GetAllProductsInOfferAsync(validOfferId);

                //Assert
                Assert.AreEqual(productsInOfferCount, actualProductsInOffer.Count);
                CollectionAssert.AreEquivalent(products.Select(x => x.ProductID).ToList(), actualProductsInOffer.Select(x => x.ProductID).ToList());
            }
        }
        public async Task ShouldThrowsArgumentException_WhenInvalidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int invalidOfferID = 100;

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                //Assert
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetAllProductsInOfferAsync(invalidOfferID));
            }
        }
        public async Task CreateThrowsArgumentExceptionWithProperMessage_WhenInvalidProductQuantityIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int    validOfferID;
            var    validProductId1 = 1;
            string validProductName;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validOfferID     = getContext.Offers.Include(x => x.ProductsInOffer).First(x => x.ProductsInOffer.Count == 0).OfferID;
                validProductName = getContext.Products.Find(validProductId1).Name;
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var validDate           = new DateTime(2019, 4, 1);
                var sut = new OfferService(context, dateTimeNowProvider.Object);
                ProductIdQuantityDto[] products = new[]
                {
                    new ProductIdQuantityDto(validProductId1, 100),
                };
                var errorText = string.Format(
                    Consts.QuantityNotEnough,
                    validProductName,
                    validProductId1);
                //Act
                //Assert
                Assert.AreEqual(
                    errorText,
                    (await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.AddProductsByIdToOfferAsync(validOfferID, products))).Message);
            }
        }
        public async Task AddProductsToOffer_WhenValidParametersArePassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int validOfferID;

            var validProductId1 = 1;
            var validProductId2 = 2;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validOfferID = getContext.Offers.Include(x => x.ProductsInOffer).First(x => x.ProductsInOffer.Count == 0).OfferID;
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var validDate           = new DateTime(2019, 4, 1);
                var sut = new OfferService(context, dateTimeNowProvider.Object);
                ProductIdQuantityDto[] products = new[]
                {
                    new ProductIdQuantityDto(validProductId1, 1),
                    new ProductIdQuantityDto(validProductId2, 1),
                };

                //Act
                var isExecuted = await sut.AddProductsByIdToOfferAsync(validOfferID, products);

                //Assert
                Assert.IsTrue(isExecuted);
                Assert.AreEqual(2, context.Offers.Include(x => x.ProductsInOffer).First(x => x.OfferID == validOfferID).ProductsInOffer.Count);
            }
        }
Example #12
0
        public async Task ShouldReturnTotalSumOfAllOffers_WhenNoParamsArePassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            var     validClientName = "valid client 1";
            decimal expectedTotal   = (decimal)((1 * 1 + 1 * 2 + 1 * 3) * (1 - 0.1 / 100));

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var actualTotal = await sut.GetOfferQuantityAsync(clientName : validClientName);

                //Assert
                Assert.AreEqual(expectedTotal, actualTotal);
            }
        }
Example #13
0
        public async Task FindProperOffer_WhenValidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            var validOfferID = 1;

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var validDate           = new DateTime(2019, 4, 1);
                var sut = new OfferService(context, dateTimeNowProvider.Object);

                //Act
                var actualOffer = await sut.GetOfferByIDAsync(validOfferID);

                //Assert
                Assert.AreEqual(validOfferID, actualOffer.OfferID);
            }
        }