public async void EditAsync_ShouldEditSupplier() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "EditAsync_ShouldEditSupplier") .Options; TechAndToolsDbContext context = new TechAndToolsDbContext(options); await SeedData(context); ISupplierService supplierService = new SupplierService(context); SupplierServiceModel serviceModel = new SupplierServiceModel { Id = 1, Name = "edited", DeliveryTimeInDays = 5, PriceToOffice = 10, PriceToAddress = 10 }; var actualResult = await supplierService.EditAsync(serviceModel); Assert.NotNull(actualResult); Assert.Equal(serviceModel.Name, actualResult.Name); Assert.Equal(serviceModel.DeliveryTimeInDays, actualResult.DeliveryTimeInDays); Assert.Equal(serviceModel.PriceToAddress, actualResult.PriceToAddress); Assert.Equal(serviceModel.PriceToOffice, actualResult.PriceToOffice); }
public async Task <SupplierServiceModel> CreateAsync(SupplierServiceModel supplierServiceModel) { Supplier supplier = supplierServiceModel.To <Supplier>(); await this.context.Suppliers.AddAsync(supplier); await this.context.SaveChangesAsync(); return(supplierServiceModel); }
public IActionResult Create(SupplierCreateInputModel supplierCreateInputModel) { if (!this.ModelState.IsValid) { return(this.View()); } SupplierServiceModel supplierServiceModel = supplierCreateInputModel.To <SupplierServiceModel>(); this.suppliersService.Create(supplierServiceModel); return(Redirect("/")); }
public void Edit_WithCorrectData_ShouldPassSuccessfully() { string errorMessagePrefix = "SuppliersService Edit() method does not work properly."; var context = UniShopDbContextInMemoryFactory.InitializeContext(); SeedData(context); this.suppliersService = new SuppliersService(context); SupplierServiceModel expectedData = context.Suppliers.First().To <SupplierServiceModel>(); bool actualData = this.suppliersService.Edit(expectedData); Assert.True(actualData, errorMessagePrefix); }
public void GetSupplierById_WithNonExistentId_ShouldReturnNull() { string errorMessagePrefix = "SuppliersService GetSupplierById() method does not work properly."; var context = UniShopDbContextInMemoryFactory.InitializeContext(); SeedData(context); this.suppliersService = new SuppliersService(context); int nonExistentId = 5; SupplierServiceModel actualResult = this.suppliersService.GetSupplierById(nonExistentId); Assert.True(actualResult == null, errorMessagePrefix); }
public bool Create(SupplierServiceModel supplierServiceModel) { Supplier supplier = new Supplier { Name = supplierServiceModel.Name, PriceToHome = supplierServiceModel.PriceToHome, PriceToOffice = supplierServiceModel.PriceToOffice }; this.context.Suppliers.Add(supplier); int result = this.context.SaveChanges(); return(result > 0); }
public async void EditAsync_ShouldThrowArgumentNullExceptionWithInvalidData() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "EditAsync_ShouldThrowArgumentNullExceptionWithInvalidData") .Options; TechAndToolsDbContext context = new TechAndToolsDbContext(options); await SeedData(context); ISupplierService supplierService = new SupplierService(context); SupplierServiceModel serviceModel = new SupplierServiceModel(); await Assert.ThrowsAsync <ArgumentNullException>(() => supplierService.EditAsync(serviceModel)); }
public bool Edit(SupplierServiceModel supplierEditInputModel) { var supplier = this.context.Suppliers.FirstOrDefault(s => s.Id == supplierEditInputModel.Id); if (supplier == null) { return(false); } supplier.Name = supplierEditInputModel.Name; supplier.PriceToHome = supplierEditInputModel.PriceToHome; supplier.PriceToOffice = supplierEditInputModel.PriceToOffice; this.context.Update(supplier); int result = this.context.SaveChanges(); return(result > 0); }
public void GetSupplierById_WithExistentId_ShouldReturnCorrectResults() { string errorMessagePrefix = "SuppliersService GetSupplierById() method does not work properly."; var context = UniShopDbContextInMemoryFactory.InitializeContext(); SeedData(context); this.suppliersService = new SuppliersService(context); SupplierServiceModel expectedResults = context.Suppliers.First().To <SupplierServiceModel>(); SupplierServiceModel actualResults = this.suppliersService.GetSupplierById(expectedResults.Id); Assert.True(expectedResults.Id == actualResults.Id, errorMessagePrefix + " " + "Id is not returned properly."); Assert.True(expectedResults.Name == actualResults.Name, errorMessagePrefix + " " + "Name is not returned properly."); Assert.True(expectedResults.PriceToHome == actualResults.PriceToHome, errorMessagePrefix + " " + "PriceToHome is not returned properly."); Assert.True(expectedResults.PriceToOffice == actualResults.PriceToOffice, errorMessagePrefix + " " + "PriceToOffice is not returned properly."); }
public void Create_WithCorrectData_ShouldSuccessfullyCreate() { string errorMessagePrefix = "SuppliersService Create() method does not work properly."; var context = UniShopDbContextInMemoryFactory.InitializeContext(); SeedData(context); this.suppliersService = new SuppliersService(context); SupplierServiceModel testSupplier = new SupplierServiceModel { Name = "Econt", PriceToHome = 7.50M, PriceToOffice = 5.50M }; bool actualResult = this.suppliersService.Create(testSupplier); Assert.True(actualResult, errorMessagePrefix); }
public async Task <SupplierServiceModel> EditAsync(SupplierServiceModel supplierServiceModel) { Supplier supplierFromDb = this.context.Suppliers .Find(supplierServiceModel.Id); if (supplierFromDb == null) { throw new ArgumentNullException(nameof(supplierFromDb)); } supplierFromDb.Name = supplierServiceModel.Name; supplierFromDb.DeliveryTimeInDays = supplierServiceModel.DeliveryTimeInDays; supplierFromDb.PriceToAddress = supplierServiceModel.PriceToAddress; supplierFromDb.PriceToOffice = supplierServiceModel.PriceToOffice; this.context.Suppliers.Update(supplierFromDb); await this.context.SaveChangesAsync(); return(supplierFromDb.To <SupplierServiceModel>()); }
public void Edit_WithCorrectData_ShouldEditSupplierCorrectly() { string errorMessagePrefix = "SuppliersService Edit() method does not work properly."; var context = UniShopDbContextInMemoryFactory.InitializeContext(); SeedData(context); this.suppliersService = new SuppliersService(context); SupplierServiceModel expectedData = context.Suppliers.First().To <SupplierServiceModel>(); expectedData.Name = "Editted_Name"; expectedData.PriceToHome = 3.0M; expectedData.PriceToOffice = 2.0M; this.suppliersService.Edit(expectedData); SupplierServiceModel actualData = context.Suppliers.First().To <SupplierServiceModel>(); Assert.True(actualData.Name == expectedData.Name, errorMessagePrefix + " " + "Name not editted properly."); Assert.True(actualData.PriceToHome == expectedData.PriceToHome, errorMessagePrefix + " " + "PriceToHome not editted properly."); Assert.True(actualData.PriceToOffice == expectedData.PriceToOffice, errorMessagePrefix + " " + "PriceToOffice not editted properly."); }