diff --git a/UnitTest/Test_Location.cs b/UnitTest/Test_Location.cs index 9d55dd7..725e958 100644 --- a/UnitTest/Test_Location.cs +++ b/UnitTest/Test_Location.cs @@ -1,125 +1,125 @@ -// using Microsoft.VisualStudio.TestTools.UnitTesting; -// using Microsoft.EntityFrameworkCore; -// using System.Collections.Generic; -// using System.Threading.Tasks; -// using CargoHub.Models; -// using CargoHub.Services; - -// namespace CargoHub.Tests -// { -// [TestClass] -// public class LocationStorageServiceTests -// { -// private LocationStorageService _locationService; -// private AppDbContext _dbContext; - -// [TestInitialize] // Setup method that runs before each test -// public void Setup() -// { -// var options = new DbContextOptionsBuilder() -// .UseInMemoryDatabase(databaseName: "TestDatabase") // Create an in-memory database -// .Options; - -// _dbContext = new AppDbContext(options); -// _locationService = new LocationStorageService(_dbContext); -// } - -// [TestCleanup] -// public void Cleanup() -// { -// _dbContext.Database.EnsureDeleted(); -// _dbContext.Dispose(); -// } - -// [TestMethod] -// public async Task GetLocationsInWarehouse_ShouldReturnCorrectLocations() -// { -// // Arrange -// _dbContext.Locations.AddRange(new List -// { -// new Location { Name = "Location 1", Code = "LOC001", WarehouseId = 1 }, -// new Location { Name = "Location 2", Code = "LOC002", WarehouseId = 2 }, -// new Location { Name = "Location 3", Code = "LOC003", WarehouseId = 1 } -// }); -// await _dbContext.SaveChangesAsync(); - -// // Act -// var result = await _locationService.GetLocationsInWarehouse(1); - -// // Assert -// Assert.AreEqual(2, result.Count); // There should be 2 locations in warehouse 1 -// Assert.IsTrue(result.Exists(l => l.Code == "LOC001")); -// Assert.IsTrue(result.Exists(l => l.Code == "LOC003")); -// } - -// [TestMethod] -// public async Task AddRow_ShouldAddLocationToDatabase() -// { -// // Arrange -// var newLocation = new Location -// { -// Name = "New Location", -// Code = "LOC004", -// WarehouseId = 3 -// }; - -// // Act -// var result = await _locationService.AddRow(newLocation); - -// // Assert -// Assert.IsNotNull(result); -// var location = await _dbContext.Locations.FindAsync(result); -// Assert.IsNotNull(location); -// Assert.AreEqual("New Location", location.Name); -// } - -// [TestMethod] -// public async Task GetRow_ShouldReturnCorrectLocation() -// { -// // Arrange -// var location = new Location { Name = "Test Location", Code = "LOC005", WarehouseId = 4 }; -// await _locationService.AddRow(location); - -// // Act -// var result = await _locationService.GetRow(location.Id); - -// // Assert -// Assert.IsNotNull(result); -// Assert.AreEqual("Test Location", result.Name); -// } - -// [TestMethod] -// public async Task UpdateRow_ShouldUpdateExistingLocation() -// { -// // Arrange -// var location = new Location { Name = "Old Name", Code = "LOC006", WarehouseId = 5 }; -// await _locationService.AddRow(location); - -// var updatedLocation = new Location { Id = location.Id, Name = "New Name", Code = "LOC006", WarehouseId = 5 }; - -// // Act -// var result = await _locationService.UpdateRow(location.Id, updatedLocation); - -// // Assert -// Assert.IsTrue(result); -// var updated = await _dbContext.Locations.FindAsync(location.Id); -// Assert.AreEqual("New Name", updated.Name); -// } - -// [TestMethod] -// public async Task DeleteRow_ShouldRemoveLocationFromDatabase() -// { -// // Arrange -// var location = new Location { Name = "Location to Delete", Code = "LOC007", WarehouseId = 6 }; -// await _locationService.AddRow(location); - -// // Act -// var result = await _locationService.DeleteRow(location.Id); - -// // Assert -// Assert.IsTrue(result); -// var deletedLocation = await _dbContext.Locations.FindAsync(location.Id); -// Assert.IsNull(deletedLocation); -// } -// } -// } +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Threading.Tasks; +using CargoHub.Models; +using CargoHub.Services; + +namespace CargoHub.Tests +{ + [TestClass] + public class LocationStorageServiceTests + { + private LocationStorageService _locationService; + private AppDbContext _dbContext; + + [TestInitialize] // Setup method that runs before each test + public void Setup() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestDatabase") // Create an in-memory database + .Options; + + _dbContext = new AppDbContext(options); + _locationService = new LocationStorageService(_dbContext); + } + + [TestCleanup] + public void Cleanup() + { + _dbContext.Database.EnsureDeleted(); + _dbContext.Dispose(); + } + + [TestMethod] + public async Task GetLocationsInWarehouse_ShouldReturnCorrectLocations() + { + // Arrange + _dbContext.Locations.AddRange(new List + { + new Location { Name = "Location 1", Code = "LOC001", WarehouseId = 1 }, + new Location { Name = "Location 2", Code = "LOC002", WarehouseId = 2 }, + new Location { Name = "Location 3", Code = "LOC003", WarehouseId = 1 } + }); + await _dbContext.SaveChangesAsync(); + + // Act + var result = await _locationService.GetLocationsInWarehouse(1); + + // Assert + Assert.AreEqual(2, result.Count); // There should be 2 locations in warehouse 1 + Assert.IsTrue(result.Exists(l => l.Code == "LOC001")); + Assert.IsTrue(result.Exists(l => l.Code == "LOC003")); + } + + [TestMethod] + public async Task AddRow_ShouldAddLocationToDatabase() + { + // Arrange + var newLocation = new Location + { + Name = "New Location", + Code = "LOC004", + WarehouseId = 3 + }; + + // Act + var result = await _locationService.AddRow(newLocation); + + // Assert + Assert.IsNotNull(result); + var location = await _dbContext.Locations.FindAsync(result); + Assert.IsNotNull(location); + Assert.AreEqual("New Location", location.Name); + } + + [TestMethod] + public async Task GetRow_ShouldReturnCorrectLocation() + { + // Arrange + var location = new Location { Name = "Test Location", Code = "LOC005", WarehouseId = 4 }; + await _locationService.AddRow(location); + + // Act + var result = await _locationService.GetRow(location.Id); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual("Test Location", result.Name); + } + + [TestMethod] + public async Task UpdateRow_ShouldUpdateExistingLocation() + { + // Arrange + var location = new Location { Name = "Old Name", Code = "LOC006", WarehouseId = 5 }; + await _locationService.AddRow(location); + + var updatedLocation = new Location { Id = location.Id, Name = "New Name", Code = "LOC006", WarehouseId = 5 }; + + // Act + var result = await _locationService.UpdateRow(location.Id, updatedLocation); + + // Assert + Assert.IsTrue(result); + var updated = await _dbContext.Locations.FindAsync(location.Id); + Assert.AreEqual("New Name", updated.Name); + } + + [TestMethod] + public async Task DeleteRow_ShouldRemoveLocationFromDatabase() + { + // Arrange + var location = new Location { Name = "Location to Delete", Code = "LOC007", WarehouseId = 6 }; + await _locationService.AddRow(location); + + // Act + var result = await _locationService.DeleteRow(location.Id); + + // Assert + Assert.IsTrue(result); + var deletedLocation = await _dbContext.Locations.FindAsync(location.Id); + Assert.IsNull(deletedLocation); + } + } +}