예제 #1
0
        public async Task CreateAndAddNewSaleWithIDsInDB_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 SaleService(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.CreateSaleAsync(client, discount, deadline, address, city, country);

                //Assert
                Assert.AreEqual(1, context.Sales.Count());
                Assert.AreEqual(client, context.Sales.Single(x => x.ClientID == client).ClientID);
            }
        }
예제 #2
0
        public async Task CreateAndAddNewSaleInDB_WhenValidParametersArePassed()
        {
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

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

                var validDate       = new DateTime(2019, 4, 1);
                var validClientName = "Pesho";

                dateTimeNowProvider.Setup(x => x.Now).Returns(validDate);
                var sut    = new SaleService(context, dateTimeNowProvider.Object);
                var client = new Client()
                {
                    Name = validClientName
                };
                var address = new Address();
                var city    = new City();
                var country = new Country();
                await sut.CreateSaleAsync(client, 0.15m, 3, address, city, country);

                Assert.AreEqual(1, context.Sales.Count());
                Assert.AreEqual(validClientName, context.Sales.Single(x => x.Client == client).Client.Name);
            }
        }
        public async Task ThrowsArgumentException_WhenInvalidSaleIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int invalidSaleID = 100;

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var validDate           = new DateTime(2019, 4, 1);
                var sut = new SaleService(context, dateTimeNowProvider.Object);
                ProductIdQuantityDto[] products = new ProductIdQuantityDto[0];
                var errorText = string.Format(
                    Consts.ObjectIDNotExist,
                    nameof(Sale),
                    invalidSaleID);
                //Act
                //Assert
                Assert.AreEqual(
                    errorText,
                    (await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.AddProductsByIdToSaleAsync(invalidSaleID, products))).Message);
            }
        }
예제 #4
0
        public async Task ShouldReturnTotalSumOfFiltredSales_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 SaleService(context, dateTimeNowProvider.Object);

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

                //Assert
                Assert.AreEqual(expectedTotal, actualTotal);
            }
        }
예제 #5
0
        public async Task DeleteProperSale_WhenValidSaleIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int validSaleId;
            int totalCountOfSales;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validSaleId       = getContext.Sales.First().SaleID;
                totalCountOfSales = getContext.Sales.Count();
            }

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

                //Act
                var isExecxuted = await sut.DeleteSaleAsync(validSaleId);

                //Assert
                Assert.IsTrue(isExecxuted);
                Assert.IsTrue(context.Sales.Find(validSaleId) == null);
                Assert.AreEqual(totalCountOfSales - 1, context.Sales.Count());
            }
        }
        public async Task CreateSaleFromOffer_WhenValidOfferIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            Offer validOffer;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validOffer = getContext.Offers.Include(x => x.Client).First();
            }

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

                //Act
                var saleActual = await sut.CreateSaleByOfferIDAsync(validOffer.OfferID);

                //Assert
                Assert.AreEqual(validOffer.OfferID, saleActual.OfferID);
                Assert.AreEqual(validOffer.ClientID, context.Sales.FirstOrDefault(x => x.OfferID == validOffer.OfferID).ClientID);
                Assert.AreEqual(validOffer.AddressID, context.Sales.FirstOrDefault(x => x.OfferID == validOffer.OfferID).AddressID);
                Assert.AreEqual(validOffer.CityID, context.Sales.FirstOrDefault(x => x.OfferID == validOffer.OfferID).CityID);
                Assert.AreEqual(validOffer.CountryID, context.Sales.FirstOrDefault(x => x.OfferID == validOffer.OfferID).CountryID);
            }
        }
예제 #7
0
        public async Task DeleteProperProductSaleRecords_WhenValidSaleIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);
            Sale sale;
            int  totalRecordsInProductSale;

            using (var getContext = new StoreSystemDbContext(options))
            {
                sale = getContext.Sales.Include(x => x.ProductsInSale).First();
                totalRecordsInProductSale = getContext.ProductSales.Count();
            }

            int validSaleId           = sale.SaleID;
            var countOfProductsInSale = sale.ProductsInSale.Count();

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

                //Act
                var isExecxuted = await sut.DeleteSaleAsync(validSaleId);

                //Assert
                Assert.IsTrue(isExecxuted);
                Assert.AreEqual(totalRecordsInProductSale - countOfProductsInSale, context.ProductSales.Count());
            }
        }
        public void MarkRecordForAdding_WhenValidAddressTextAndSaveFalseIsPassed()
        {
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var options      = DbSeed.GetOptions(databaseName);

            using (var assertContext = new StoreSystemDbContext(options))
            {
                const string validAddressText = "valid address";
                var          sut    = new AddressService(assertContext);
                var          actual = sut.CreateAddressAsync(validAddressText, false).Result;

                Assert.IsTrue(assertContext.Entry <Address>(actual).State == EntityState.Added);
                Assert.IsTrue(assertContext.Addresses.Count() == 0);
            }
        }
        public async Task UpdateSale_WhenValidParanetersArePassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int      validSaleId;
            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      saleDate = new DateTime(2019, 5, 1);

            using (var getContext = new StoreSystemDbContext(options))
            {
                var sale = getContext.Sales.First();
                validSaleId  = sale.SaleID;
                deliveryDate = sale.OrderDate.AddDays(1);
            }

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

                //Act
                var isExecuted = await sut.UpdateSaleAsync(validSaleId, client, discount, saleDate, (deadline - saleDate).TotalDays, deliveryDate, address, city, country);

                //Assert
                Assert.IsTrue(isExecuted);
                Assert.AreEqual(client, context.Sales.Find(validSaleId).ClientID);
                Assert.AreEqual(discount, context.Sales.Find(validSaleId).ProductDiscount);
                Assert.AreEqual(saleDate, context.Sales.Find(validSaleId).OrderDate);
                Assert.AreEqual(deliveryDate, context.Sales.Find(validSaleId).DeliveryDate);
                Assert.AreEqual(deadline, context.Sales.Find(validSaleId).DeadlineDate);
                Assert.AreEqual(address, context.Sales.Find(validSaleId).AddressID);
                Assert.AreEqual(city, context.Sales.Find(validSaleId).CityID);
                Assert.AreEqual(country, context.Sales.Find(validSaleId).CountryID);
            }
        }
        public void ReturnAddressFromDB_WhenExistingAddressTextIsPassed()
        {
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var options      = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);
            using (var assertContext = new StoreSystemDbContext(options))
            {
                const string existingAddressText = "valid address 1";
                var          sut = new AddressService(assertContext);
                var          a   = sut.CreateAddressAsync(existingAddressText);
                Assert.IsTrue(assertContext.Addresses.SingleOrDefault(x => x.Name == existingAddressText) != null);
                var t = assertContext.Addresses.Single(x => x.Name == existingAddressText);
                var v = assertContext.Addresses.Count();
                Assert.IsTrue(assertContext.Addresses.Count() == 2);
            }
        }
        public async Task ThrowsArgumentException_WhenInvalidDateIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

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


            using (var getContext = new StoreSystemDbContext(options))
            {
                var sale = getContext.Sales.First();
                validSaleId         = sale.SaleID;
                invalidDeadlineDate = sale.OrderDate.AddDays(-1);
                saleDate            = sale.OrderDate;
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut       = new SaleService(context, dateTimeNowProvider.Object);
                var errorText = string.Format(
                    Consts.DateError,
                    invalidDeadlineDate,
                    saleDate);
                //Act
                //Assert
                Assert.AreEqual(
                    errorText,
                    (await Assert.ThrowsExceptionAsync <ArgumentException>(() =>
                                                                           sut.UpdateSaleAsync(validSaleId, client, discount, saleDate, (invalidDeadlineDate - saleDate).TotalDays, deliveryDate, address, city, country))).Message
                    );
            }
        }
예제 #12
0
        public async Task ShouldReturnCollectionWithProductInSale_WhenValidSaleIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int validSaleId                    = 1;
            int productsInSaleCount            = 2;
            ICollection <ProductSale> products = new List <ProductSale>
            {
                new ProductSale {
                    ProductID = 1, Quantity = 1
                },
                new ProductSale {
                    ProductID = 2, Quantity = 1
                }
            };

            //using (var getContext = new StoreSystemDbContext(options))
            //{
            //    var sale = getContext.Sales.Include(x => x.ProductsInSale).First(x => x.ProductsInSale.Count > 0);
            //    validSaleId = sale.SaleID;
            //    products = sale.ProductsInSale;
            //    productsInSaleCount = products.Count;
            //}

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

                //Act
                var actualProductsInSale = await sut.GetAllProductsInSaleAsync(validSaleId);

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

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int validSaleID;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validSaleID = getContext.Sales.Include(x => x.ProductsInSale).First(x => x.ProductsInSale.Count == 0).SaleID;
            }

            var     validProductId1      = 1;
            var     validProductId2      = 2;
            decimal expectedQuntityProd1 = 1 - 1;
            decimal expectedQuntityProd2 = 2 - 1;

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

                //Act
                var isExecuted = await sut.AddProductsByIdToSaleAsync(validSaleID, products);

                //Assert
                Assert.IsTrue(isExecuted);
                Assert.AreEqual(expectedQuntityProd1, context.Products.Find(1).Quantity);
                Assert.AreEqual(expectedQuntityProd2, context.Products.Find(2).Quantity);
            }
        }
예제 #14
0
        public async Task ShouldThrowsArgumentException_WhenInvalidSaleIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int invalidSaleID = 100;

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

                //Act
                //Assert
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetAllProductsInSaleAsync(invalidSaleID));
            }
        }
예제 #15
0
        public async Task ReturnAllNotClosedSales_WhenIsInvolkedPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);
            var expectedCountOfNotClosed = 1;

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

                //Act
                var actualSales = await sut.GetNotClosedSalesAsync();

                //Assert
                Assert.AreEqual(expectedCountOfNotClosed, actualSales.Count);
            }
        }
        public async Task CreateThrowsArgumentExceptionWithProperMessage_WhenInvalidProductQuantityIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int    validSaleID;
            var    validProductId1 = 1;
            string validProductName;

            using (var getContext = new StoreSystemDbContext(options))
            {
                validSaleID      = getContext.Sales.Include(x => x.ProductsInSale).First(x => x.ProductsInSale.Count == 0).SaleID;
                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 SaleService(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.AddProductsByIdToSaleAsync(validSaleID, products))).Message);
            }
        }
        public async Task ThrowsArgumentException_WhenInvalidSaleIdIsPassed()
        {
            //Arrange
            var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            int invalidSaleId = 100;
            var client        = 1;
            var address       = 1;
            var city          = 1;
            var country       = 1;
            var deadline      = new DateTime(2019, 5, 10);
            var deliveryDate  = new DateTime(2019, 5, 8);
            var discount      = 0.10m;
            var saleDate      = new DateTime(2019, 5, 1);


            using (var context = new StoreSystemDbContext(options))
            {
                var dateTimeNowProvider = new Mock <IDateTimeNowProvider>();
                var sut       = new SaleService(context, dateTimeNowProvider.Object);
                var errorText = string.Format(
                    Consts.ObjectIDNotExist,
                    nameof(Sale),
                    invalidSaleId);
                //Act
                //Assert
                Assert.AreEqual(
                    errorText,
                    (await Assert.ThrowsExceptionAsync <ArgumentException>(() =>
                                                                           sut.UpdateSaleAsync(invalidSaleId, client, discount, saleDate, (deadline - saleDate).TotalDays, deliveryDate, address, city, country))).Message
                    );
            }
        }
예제 #18
0
        public async Task ShouldReturnTotalSumOfAllSales_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 SaleService(context, dateTimeNowProvider.Object);

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

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

            var options = DbSeed.GetOptions(databaseName);

            DbSeed.SeedDatabase(options);

            var validSaleID = 1;

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

                //Act
                var actualSale = await sut.GetSaleByIDAsync(validSaleID);

                //Assert
                Assert.AreEqual(validSaleID, actualSale.SaleID);
            }
        }