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); //Act //Assert await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.GetSaleByIDAsync(invalidSaleID)); } }
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 GetAllWarehousesWhenOneIsRegistred() { //Arrange var GetAllWarehousesWhenOneIsRegistred = System.Reflection.MethodBase.GetCurrentMethod().Name; var options = Utils.GetOptions(GetAllWarehousesWhenOneIsRegistred); Utils.SeedDatabase(options); using (var arrangeContext = new StoreSystemDbContext(options)) { var warehousesList = arrangeContext.Warehouses.ToList(); int cnt = 0; foreach (var item in warehousesList) { if (0 != cnt) { arrangeContext.Warehouses.Remove(item); } else { cnt = 1; } } await arrangeContext.SaveChangesAsync(); } using (var context = new StoreSystemDbContext(options)) { var sut = new WarehouseService(context); //Act var warehousesList = await sut.GetAllWarehousesAsync(); //Assert Assert.AreEqual(1, warehousesList.Count); } }
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); } }
public async Task ThrowsArgumentExceptionWhenWarehouseNameAlreadyExists(string warehouseName, int cityID, int countryID, int addressID, bool toSave) { //Arrange var ThrowsArgumentExceptionWhenWarehouseNameExists = System.Reflection.MethodBase.GetCurrentMethod().Name; var options = Utils.GetOptions(ThrowsArgumentExceptionWhenWarehouseNameExists); Utils.SeedDatabase(options); using (var arrangeContext = new StoreSystemDbContext(options)) { arrangeContext.Warehouses.Add(new Warehouse() { Name = warehouseName }); arrangeContext.Countries.Add(new Country() { CountryID = countryID }); arrangeContext.Cities.Add(new City() { CityID = cityID }); arrangeContext.Addresses.Add(new Address() { AddressID = addressID }); await arrangeContext.SaveChangesAsync(); } using (var context = new StoreSystemDbContext(options)) { var sut = new WarehouseService(context); //Act & Assert await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.CreateWarehouseAsync(warehouseName, countryID, cityID, addressID, toSave)); } }
public async Task AddPurchaseWhenPurchaseIDAndWarehouseIDExist(int validWarehouseID, int validPurchaseID) { //Arrange var AddPurchaseWhenPurchaseIDAndWarehouseIDExist = System.Reflection.MethodBase.GetCurrentMethod().Name; var options = Utils.GetOptions(AddPurchaseWhenPurchaseIDAndWarehouseIDExist); Utils.SeedDatabase(options); using (var arrangeContext = new StoreSystemDbContext(options)) { arrangeContext.Warehouses.Add(new Warehouse() { WarehouseID = validWarehouseID }); arrangeContext.Purchases.Add(new Purchase() { PurchaseID = validPurchaseID }); await arrangeContext.SaveChangesAsync(); } using (var context = new StoreSystemDbContext(options)) { var sut = new WarehouseService(context); //Act var isAdded = await sut.AddPurchaseToWarehouseAsync(validWarehouseID, validPurchaseID); var addedPurchase = context.Warehouses.Find(validWarehouseID).Purchases .Where(x => x.PurchaseID == validPurchaseID) .FirstOrDefault(); //Assert Assert.AreEqual(true, isAdded); Assert.AreEqual(validPurchaseID, addedPurchase.PurchaseID); } }
public async Task UpdateWarehouseWithWhiteSpaceNameWhenInvalidID(string warehouseName, int cityID, int countryID, int addressID, bool toSave) { //Arrange var UpdateWarehouseWithWhiteSpaceNameWhenInvalidID = System.Reflection.MethodBase.GetCurrentMethod().Name; var options = Utils.GetOptions(UpdateWarehouseWithWhiteSpaceNameWhenInvalidID); Utils.SeedDatabase(options); using (var arrangeContext = new StoreSystemDbContext(options)) { arrangeContext.Countries.Add(new Country() { CountryID = countryID }); arrangeContext.Cities.Add(new City() { CityID = cityID }); arrangeContext.Addresses.Add(new Address() { AddressID = addressID }); await arrangeContext.SaveChangesAsync(); } using (var context = new StoreSystemDbContext(options)) { var sut = new WarehouseService(context); //Act var actualWarehouse = await sut.CreateWarehouseAsync(warehouseName, countryID, cityID, addressID, toSave); //Assert await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.UpdateWarehouseAsync(-actualWarehouse.WarehouseID, " ", countryID, cityID, addressID, toSave)); } }
public async Task ThrowsArgumentException_WhenInvalidOfferIdIsPassed() { //Arrange var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name; var options = DbSeed.GetOptions(databaseName); DbSeed.SeedDatabase(options); int invalidOfferId = 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 offerDate = new DateTime(2019, 5, 1); using (var context = new StoreSystemDbContext(options)) { var dateTimeNowProvider = new Mock <IDateTimeNowProvider>(); var sut = new OfferService(context, dateTimeNowProvider.Object); var errorText = string.Format( Consts.ObjectIDNotExist, nameof(Offer), invalidOfferId); //Act //Assert Assert.AreEqual( errorText, (await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.UpdateOfferAsync(invalidOfferId, client, discount, offerDate, (deadline - offerDate).TotalDays, deliveryDate, address, city, country))).Message ); } }
public async Task ShouldReturnTotalSumOfFiltredOffers_WhenValidOfferIdIsPassed() { //Arrange var databaseName = System.Reflection.MethodBase.GetCurrentMethod().Name; var options = DbSeed.GetOptions(databaseName); DbSeed.SeedDatabase(options); var validOfferId = 1; decimal expectedTotal = (decimal)((1 * 1 + 1 * 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(offerID : validOfferId); //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); } }
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 SaleService(StoreSystemDbContext context, IDateTimeNowProvider dateNow) { this.context = context ?? throw new ArgumentNullException(nameof(context)); this.dateNow = dateNow ?? throw new ArgumentNullException(nameof(dateNow)); }
static void Main(string[] args) { var context = new StoreSystemDbContext(); var dateNow = new DateTimeNowProvider(); var addressService = new AddressService(context); var cityService = new CityService(context); var countryService = new CountryService(context); var saleService = new SaleService(context, new DateTimeNowProvider()); var clientService = new ClientService(context); var productService = new ProductService(context); var offerService = new OfferService(context, dateNow); var supplierService = new SupplierService(context); var purchaseService = new PurchaseService(context, new DateTimeNowProvider()); var warehouseService = new WarehouseService(context); addressService.GetListOfAllClientsbyID(1); cityService.GetListOfAllClientsbyName("Sofia"); countryService.GetListOfAllClientsbyName("Bulgaria"); clientService.FindClientWithAddress(1); clientService.GetAllClients(0, 100, "tash"); clientService.UpdateClient(1, null, "321654987", null, null, null, null, null); clientService.GetClientSales(4); try { //var a = clientService.CreateClient("Pesho", "123456789", "*****@*****.**", "0888500050", // new Address() { Name = "Nova Strasse" }, new City() { Name = "Berlin" }, new Country() { Name = "Germany" }); //Console.WriteLine(a?.ClientID + " " +a?.Name); //addressService.CreateAddress("Malinova dolina 2"); //addressService.CreateAddress("Ovcha kupel otzad"); //addressService.CreateAddress("Krasna polqna bai cig"); //addressService.CreateAddress("ul. Krasnorech"); //addressService.CreateAddress("jk. Liulin 11"); //addressService.CreateAddress("Cheroshova gradina 5"); //cityService.CreateCity("Sofia"); //cityService.CreateCity("Dolno Uino"); //cityService.CreateCity("Pelnik"); //productService.CreateProduct("Piron 1.2", "pcs", 100, 1.50m, 1.80m); //productService.CreateProduct("Nailon 2mk", "pcs", 1200, 2.60m, 3.80m); //productService.CreateProduct("Konop", "pcs", 45, 3.50m, 5.80m); //productService.CreateProduct("Lager 6.19", "pcs", 55, 8.20m, 10.70m); //productService.CreateProduct("Bager JCB", "pcs", 5, 50000.00m, 60000.00m); //productService.CreateProduct("Fadroma Liebherr", "pcs", 2, 61000.00m, 66000.00m); //productService.CreateProduct("Lopata prava", "pcs", 35, 10.30m, 13.80m); //productService.CreateProduct("Chuk kofrajen", "pcs", 190, 18.90m, 22.80m); //saleService.CreateSale( // clientService.FindClientByName("Pesho"), // 7, // addressService.FindAddressByName("Nova Strasse"), // cityService.FindCityByName("Berlin"), // countryService.FindCountryByName("Germany") // ); //saleService.AddProductsToSale(1, new KeyValuePair<string, decimal>("Konop", 10), // new KeyValuePair<string, decimal>("Lager 6.19", 20), // new KeyValuePair<string, decimal>("Lopata prava", 8)); //var a =saleService.GetSaleQuantityByDate(new DateTime(2018, 1, 1), new DateTime(2019, 12, 1)); //Console.WriteLine(a.ToString("f2")); //var (a, b) = Spliter(Console.ReadLine()); //Console.WriteLine(a); //Console.WriteLine(String.Join(", ",b)); saleService.AddProductsToSale(4, new ProductQuantityDto("pironche", 1), new ProductQuantityDto("lopata prava", 1)); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public CountryService(StoreSystemDbContext context, IDatabaseJSONConnectivity <string> JSONConnectivityService) { this.context = context ?? throw new ArgumentNullException(nameof(context)); jSONConnectivityService = JSONConnectivityService; }
internal static bool SeedDatabase(DbContextOptions <StoreSystemDbContext> options) { using (var seedContext = new StoreSystemDbContext(options)) { seedContext.Addresses.AddRange( new Address { Name = "valid address 1" }, new Address { Name = "valid address 2" } ); seedContext.Cities.AddRange( new City { Name = "valid city 1" }, new City { Name = "valid city 2" } ); seedContext.Countries.AddRange( new Country { Name = "valid country 1" }, new Country { Name = "valid country 2" } ); seedContext.Clients.AddRange( new Client { Name = "valid client 1", AddressID = 1, CityID = 1, CountryID = 1, UIN = "123456789", }, new Client { Name = "valid client 2", AddressID = 2, CityID = 3, CountryID = 2, UIN = "987654321", } ); seedContext.Suppliers.AddRange( new Supplier { Name = "valid client 1", AddressID = 1, CityID = 1, CountryID = 1, UIN = "123456789", }, new Supplier { Name = "valid client 2", AddressID = 2, CityID = 2, CountryID = 2, UIN = "987654321", } ); seedContext.Products.AddRange( new Product { Name = "valid product 1", Measure = "v1", Quantity = 1, BuyPrice = 1, RetailPrice = 1 }, new Product { Name = "valid product 2", Measure = "v2", Quantity = 2, BuyPrice = 2, RetailPrice = 2 }, new Product { Name = "valid product 3", Measure = "v3", Quantity = 3, BuyPrice = 3, RetailPrice = 3 } ); seedContext.Warehouses.AddRange( new Warehouse { Name = "valid warehouse 1", AddressID = 1, CityID = 1, CountryID = 1, }, new Warehouse { Name = "valid warehouse 2", AddressID = 2, CityID = 2, CountryID = 2, } ); seedContext.Sales.Add( new Sale { ClientID = 1, AddressID = 1, CityID = 1, CountryID = 1, OfferID = 1, ProductDiscount = 0.1m, DeadlineDate = new DateTime(2019, 1, 1), DeliveryDate = new DateTime(1, 1, 1), OrderDate = new DateTime(2019, 1, 1), ProductsInSale = new List <ProductSale> { new ProductSale { ProductID = 1, Quantity = 1 }, new ProductSale { ProductID = 2, Quantity = 1 } } } ); seedContext.Sales.Add( new Sale { ClientID = 1, AddressID = 1, CityID = 1, CountryID = 1, OfferID = 1, ProductDiscount = 0.1m, DeadlineDate = new DateTime(2019, 2, 2), DeliveryDate = new DateTime(2019, 2, 2), OrderDate = new DateTime(2019, 2, 2), ProductsInSale = new List <ProductSale> { new ProductSale { ProductID = 3, Quantity = 1 }, } } ); seedContext.Sales.Add( new Sale { ClientID = 2, AddressID = 2, CityID = 2, CountryID = 2, OfferID = 2, ProductDiscount = 0.1m, DeadlineDate = new DateTime(2019, 3, 3), DeliveryDate = new DateTime(2019, 3, 3), OrderDate = new DateTime(2019, 3, 3), } ); seedContext.Sales.Add( new Sale { ClientID = 2, AddressID = 2, CityID = 2, CountryID = 2, OfferID = 2, ProductDiscount = 0.1m, DeadlineDate = new DateTime(2019, 4, 4), DeliveryDate = new DateTime(2019, 4, 4), OrderDate = new DateTime(2019, 4, 4), ProductsInSale = new List <ProductSale> { new ProductSale { ProductID = 1, Quantity = 1 }, new ProductSale { ProductID = 3, Quantity = 1 } } } ); seedContext.Offers.AddRange( new Offer { ClientID = 1, AddressID = 1, CityID = 1, CountryID = 1, ProductDiscount = 0.1m, ExpiredDate = new DateTime(2019, 1, 1), DeliveryDate = new DateTime(2019, 1, 1), OfferDate = new DateTime(2019, 1, 1), ProductsInOffer = new List <ProductOffer> { new ProductOffer { ProductID = 1, Quantity = 1 }, } }, new Offer { ClientID = 2, AddressID = 2, CityID = 2, CountryID = 2, ProductDiscount = 0.2m, ExpiredDate = new DateTime(2019, 2, 2), DeliveryDate = new DateTime(2019, 2, 2), OfferDate = new DateTime(2019, 2, 2), ProductsInOffer = new List <ProductOffer> { new ProductOffer { ProductID = 2, Quantity = 1 } } } ); seedContext.Purchases.AddRange( new Purchase { SupplierID = 1, WarehouseID = 1, DeadlineDate = new DateTime(2019, 1, 1), DeliveryDate = new DateTime(2019, 1, 1), PurchaseDate = new DateTime(2019, 1, 1), ProductsТоPurchase = new List <ProductPurchase> { new ProductPurchase { ProductID = 1, ProductPrice = 1, ProductQty = 1 }, new ProductPurchase { ProductID = 2, ProductPrice = 2, ProductQty = 2 } } } ); var entityTracked = seedContext.SaveChanges(); return(entityTracked > 0 ? true : false); } }
public ProductService(StoreSystemDbContext context) { this.context = context ?? throw new ArgumentNullException(nameof(context)); }
public ManageService(StoreSystemDbContext context, IDatabaseService databaseService) { this.context = context ?? throw new ArgumentNullException(nameof(context)); this.databaseService = databaseService; }