-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41bcf75
commit e80a2e0
Showing
5 changed files
with
502 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CargoHub.Models; | ||
using CargoHub.Services; | ||
using CargoHub; | ||
|
||
[TestClass] | ||
public class SupplierServiceTests | ||
{ | ||
private AppDbContext _context; | ||
private BaseStorageService<Supplier> _service; | ||
|
||
[TestInitialize] | ||
public void Setup() | ||
{ | ||
var options = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
.Options; | ||
|
||
_context = new AppDbContext(options); | ||
_service = new BaseStorageService<Supplier>(_context); | ||
|
||
SeedDatabase(); | ||
} | ||
|
||
private void SeedDatabase() | ||
{ | ||
_context.Suppliers.Add(new Supplier | ||
{ | ||
Name = "Supplier 1", | ||
Code = "W1", | ||
Address = "123 Main St", | ||
AddressExtra = "Suite 100", | ||
City = "Anytown", | ||
ZipCode = "12345", | ||
Province = "ProvinceName", | ||
Country = "CountryName", | ||
ContactName = "John Doe", | ||
Phonenumber = "123-456-7890", | ||
Reference = "Ref123" | ||
}); | ||
|
||
_context.Suppliers.Add(new Supplier | ||
{ | ||
Name = "Supplier 2", | ||
Code = "W1", | ||
Address = "123 Main St", | ||
AddressExtra = "Suite 100", | ||
City = "Anytown", | ||
ZipCode = "12345", | ||
Province = "ProvinceName", | ||
Country = "CountryName", | ||
ContactName = "John Doe", | ||
Phonenumber = "123-456-7890", | ||
Reference = "Ref123" | ||
}); | ||
|
||
|
||
|
||
_context.SaveChanges(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetRow_ReturnsEntity_WhenEntityExists() | ||
{ | ||
int entityId = 1; | ||
|
||
var result = await _service.GetRow(entityId); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.AreEqual(entityId, result.Id); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetRow_ReturnsNull_WhenEntityDoesNotExist() | ||
{ | ||
int entityId = 999; // Non-existing ID | ||
|
||
var result = await _service.GetRow(entityId); | ||
|
||
Assert.IsNull(result); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetAllRows_ReturnsAllEntities() | ||
{ | ||
var result = await _service.GetAllRows(); | ||
|
||
Assert.AreEqual(2, result.Count); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AddRow_AddsEntity_WhenEntityIsValid() | ||
{ | ||
var newSupplier = new Supplier { Name = "Supplier 4", | ||
Code = "W4", | ||
Address = "123 Main St", | ||
AddressExtra = "Suite 100", | ||
City = "Anytown", | ||
ZipCode = "12345", | ||
Province = "ProvinceName", | ||
Country = "CountryName", | ||
ContactName = "John Doe", | ||
Phonenumber = "123-456-7890", | ||
Reference = "Ref123" }; | ||
|
||
var resultId = await _service.AddRow(newSupplier); | ||
|
||
Assert.IsNotNull(resultId); | ||
Assert.AreEqual(3, resultId); // Assuming IDs are sequential | ||
var addedSupplier = await _service.GetRow(resultId.Value); | ||
Assert.IsNotNull(addedSupplier); | ||
Assert.AreEqual(newSupplier.Name, addedSupplier.Name); | ||
} | ||
|
||
[TestMethod] | ||
public async Task UpdateRow_UpdatesEntity_WhenEntityExists() | ||
{ | ||
var updatedSupplier = new Supplier { Id=1, | ||
Name = "Updated Supplier 1", | ||
Code = "W1", | ||
Address = "123 Main St", | ||
AddressExtra = "Suite 100", | ||
City = "Anytown", | ||
ZipCode = "12345", | ||
Province = "ProvincessName", | ||
Country = "CountryName", | ||
ContactName = "John Doe", | ||
Phonenumber = "123-456-7890", | ||
Reference = "Ref123" }; | ||
|
||
var result = await _service.UpdateRow(1, updatedSupplier); | ||
|
||
Assert.IsTrue(result); | ||
var updatedEntity = await _service.GetRow(1); | ||
Assert.AreEqual("Updated Supplier 1", updatedEntity.Name); | ||
} | ||
|
||
[TestMethod] | ||
public async Task UpdateRow_ReturnsFalse_WhenEntityDoesNotExist() | ||
{ | ||
var updatedSupplier = new Supplier { Name = "Supplier 999", | ||
Code = "W999", | ||
Address = "123 Main St", | ||
AddressExtra = "Suite 100", | ||
City = "Anytown", | ||
ZipCode = "12345", | ||
Province = "ProvinceName", | ||
Country = "CountryName", | ||
ContactName = "John Doe", | ||
Phonenumber = "123-456-7890", | ||
Reference = "Ref123" }; | ||
|
||
var result = await _service.UpdateRow(999, updatedSupplier); | ||
|
||
Assert.IsFalse(result); | ||
} | ||
|
||
[TestMethod] | ||
public async Task DeleteRow_ReturnsTrue_WhenEntityExists() | ||
{ | ||
int entityId = 1; | ||
|
||
var result = await _service.DeleteRow(entityId); | ||
|
||
Assert.IsTrue(result); | ||
var deletedEntity = await _service.GetRow(entityId); | ||
Assert.IsNull(deletedEntity); | ||
} | ||
|
||
[TestMethod] | ||
public async Task DeleteRow_ReturnsFalse_WhenEntityDoesNotExist() | ||
{ | ||
int entityId = 999; // Non-existing ID | ||
|
||
var result = await _service.DeleteRow(entityId); | ||
|
||
Assert.IsFalse(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CargoHub.Models; | ||
using CargoHub.Services; | ||
using CargoHub; | ||
|
||
[TestClass] | ||
public class TransferServiceTests | ||
{ | ||
private AppDbContext _context; | ||
private BaseStorageService<Transfer> _service; | ||
private Transfer TransferTemp; | ||
|
||
[TestInitialize] | ||
public void Setup() | ||
{ | ||
var options = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
.Options; | ||
|
||
_context = new AppDbContext(options); | ||
_service = new BaseStorageService<Transfer>(_context); | ||
|
||
SeedDatabase(); | ||
} | ||
|
||
private void SeedDatabase() | ||
{ | ||
_context.Transfers.Add(new Transfer { Reference = "Transfer 1", TransferFrom = 2, TransferTo = 1 }); | ||
_context.Transfers.Add(new Transfer { Reference = "Transfer 2", TransferFrom = 2, TransferTo = 1 }); | ||
_context.SaveChanges(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetRow_ReturnsEntity_WhenEntityExists() | ||
{ | ||
int entityId = 1; | ||
|
||
var result = await _service.GetRow(entityId); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.AreEqual(entityId, result.Id); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetRow_ReturnsNull_WhenEntityDoesNotExist() | ||
{ | ||
int entityId = 999; // Non-existing ID | ||
|
||
var result = await _service.GetRow(entityId); | ||
|
||
Assert.IsNull(result); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetAllRows_ReturnsAllEntities() | ||
{ | ||
var result = await _service.GetAllRows(); | ||
|
||
Assert.AreEqual(2, result.Count); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AddRow_AddsEntity_WhenEntityIsValid() | ||
{ | ||
var newTransfer = new Transfer { Reference = "Transfer 3", TransferFrom = 2, TransferTo = 1 }; | ||
|
||
var resultId = await _service.AddRow(newTransfer); | ||
|
||
Assert.IsNotNull(resultId); | ||
Assert.AreEqual(3, resultId); // Assuming IDs are sequential | ||
var addedTransfer = await _service.GetRow(resultId.Value); | ||
Assert.IsNotNull(addedTransfer); | ||
Assert.AreEqual(newTransfer.Reference, addedTransfer.Reference); | ||
} | ||
|
||
[TestMethod] | ||
public async Task UpdateRow_UpdatesEntity_WhenEntityExists() | ||
{ | ||
var updatedTransfer = new Transfer { Id = 1, Reference = "Updated Transfer 1", TransferFrom = 2, TransferTo = 1 }; | ||
|
||
var result = await _service.UpdateRow(1, updatedTransfer); | ||
|
||
Assert.IsTrue(result); | ||
var updatedEntity = await _service.GetRow(1); | ||
Assert.AreEqual("Updated Transfer 1", updatedEntity.Reference); | ||
} | ||
|
||
[TestMethod] | ||
public async Task UpdateRow_ReturnsFalse_WhenEntityDoesNotExist() | ||
{ | ||
var updatedTransfer = new Transfer { Id = 999, Reference = "Non-existing Transfer", TransferFrom = 2, TransferTo = 1}; | ||
|
||
var result = await _service.UpdateRow(999, updatedTransfer); | ||
|
||
Assert.IsFalse(result); | ||
} | ||
|
||
[TestMethod] | ||
public async Task DeleteRow_ReturnsTrue_WhenEntityExists() | ||
{ | ||
int entityId = 1; | ||
|
||
var result = await _service.DeleteRow(entityId); | ||
|
||
Assert.IsTrue(result); | ||
var deletedEntity = await _service.GetRow(entityId); | ||
Assert.IsNull(deletedEntity); | ||
} | ||
|
||
[TestMethod] | ||
public async Task DeleteRow_ReturnsFalse_WhenEntityDoesNotExist() | ||
{ | ||
int entityId = 999; // Non-existing ID | ||
|
||
var result = await _service.DeleteRow(entityId); | ||
|
||
Assert.IsFalse(result); | ||
} | ||
} |
Oops, something went wrong.