Skip to content

Commit

Permalink
update test locations
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenvanMuiden committed Jan 21, 2025
1 parent 62eec83 commit 5066a76
Showing 1 changed file with 125 additions and 125 deletions.
250 changes: 125 additions & 125 deletions UnitTest/Test_Location.cs
Original file line number Diff line number Diff line change
@@ -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<AppDbContext>()
// .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<Location>
// {
// 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>(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>(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<AppDbContext>()
.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<Location>
{
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>(location.Id);

Check failure on line 84 in UnitTest/Test_Location.cs

View workflow job for this annotation

GitHub Actions / unit-tests

The non-generic method 'BaseStorageService<Location>.GetRow(int)' cannot be used with type arguments

// 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>(location.Id);

// Assert
Assert.IsTrue(result);
var deletedLocation = await _dbContext.Locations.FindAsync(location.Id);
Assert.IsNull(deletedLocation);
}
}
}

0 comments on commit 5066a76

Please sign in to comment.