diff --git a/CargoHub.sln b/CargoHub.sln index 704c86b..5b429ad 100644 --- a/CargoHub.sln +++ b/CargoHub.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CargoHub", "CargoHub\CargoHub.csproj", "{DF5C2A7E-1DF9-42A8-B3B9-D6552BDB3BE3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CargoHub", "CargoHub\CargoHub.csproj", "{D287ADFD-E9BA-4787-9B63-F0EAACE63FE9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CargoHub.Test", "UnitTest\CargoHub.Test.csproj", "{2565E8A5-EBB2-4773-AE8C-C14387ACB2A0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CargoHub.Test", "UnitTest\CargoHub.Test.csproj", "{537592E6-925A-45C2-AF47-B1F001267F77}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -16,17 +16,13 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DF5C2A7E-1DF9-42A8-B3B9-D6552BDB3BE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DF5C2A7E-1DF9-42A8-B3B9-D6552BDB3BE3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DF5C2A7E-1DF9-42A8-B3B9-D6552BDB3BE3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DF5C2A7E-1DF9-42A8-B3B9-D6552BDB3BE3}.Release|Any CPU.Build.0 = Release|Any CPU - {08CCD5C8-5AE7-4908-843D-884E2BAC774D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {08CCD5C8-5AE7-4908-843D-884E2BAC774D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {08CCD5C8-5AE7-4908-843D-884E2BAC774D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {08CCD5C8-5AE7-4908-843D-884E2BAC774D}.Release|Any CPU.Build.0 = Release|Any CPU - {2565E8A5-EBB2-4773-AE8C-C14387ACB2A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2565E8A5-EBB2-4773-AE8C-C14387ACB2A0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2565E8A5-EBB2-4773-AE8C-C14387ACB2A0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2565E8A5-EBB2-4773-AE8C-C14387ACB2A0}.Release|Any CPU.Build.0 = Release|Any CPU + {D287ADFD-E9BA-4787-9B63-F0EAACE63FE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D287ADFD-E9BA-4787-9B63-F0EAACE63FE9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D287ADFD-E9BA-4787-9B63-F0EAACE63FE9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D287ADFD-E9BA-4787-9B63-F0EAACE63FE9}.Release|Any CPU.Build.0 = Release|Any CPU + {537592E6-925A-45C2-AF47-B1F001267F77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {537592E6-925A-45C2-AF47-B1F001267F77}.Debug|Any CPU.Build.0 = Debug|Any CPU + {537592E6-925A-45C2-AF47-B1F001267F77}.Release|Any CPU.ActiveCfg = Release|Any CPU + {537592E6-925A-45C2-AF47-B1F001267F77}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/CargoHub/Controllers/ItemGroupsController.cs b/CargoHub/Controllers/ItemGroupsController.cs index 05e084a..1b1d72e 100644 --- a/CargoHub/Controllers/ItemGroupsController.cs +++ b/CargoHub/Controllers/ItemGroupsController.cs @@ -14,5 +14,16 @@ public async Task GetItemsForSupplier(int itemGroupId) var items = await itemStorageService.GetItemsforSupplier(itemGroupId); return Ok(items); } + + [HttpGet("itemgroup/{itemGroupId}")] + public async Task GetItemsforItemGroup(int itemGroupId) + { + var items = await itemStorageService.GetItemsforItemGroup(itemGroupId); + if (items == null || items.Count == 0) + { + return NotFound($"No items found for this ItemGroup ID {itemGroupId}"); + } + return Ok(items); + } } } \ No newline at end of file diff --git a/CargoHub/Controllers/ItemLinesController.cs b/CargoHub/Controllers/ItemLinesController.cs index 6a96635..b87f683 100644 --- a/CargoHub/Controllers/ItemLinesController.cs +++ b/CargoHub/Controllers/ItemLinesController.cs @@ -15,6 +15,16 @@ public async Task GetItemsForSupplier(int itemLineId) var items = await itemStorageService.GetItemsforSupplier(itemLineId); return Ok(items); } - } + [HttpGet("itemline/{itemLineId}")] + public async Task GetItemsForItemLine(int itemLineId) + { + var items = await itemStorageService.GetItemsforItemLine(itemLineId); + if (items == null || items.Count == 0) + { + return NotFound($"No items found for this ItemLine ID {itemLineId}"); + } + return Ok(items); + } + } } \ No newline at end of file diff --git a/CargoHub/Controllers/ItemTypesController.cs b/CargoHub/Controllers/ItemTypesController.cs index b8e6e09..c4cbfd7 100644 --- a/CargoHub/Controllers/ItemTypesController.cs +++ b/CargoHub/Controllers/ItemTypesController.cs @@ -14,5 +14,16 @@ public async Task GetItemsForSupplier(int itemTypeId) var items = await itemStorageService.GetItemsforSupplier(itemTypeId); return Ok(items); } + + [HttpGet("itemtype/{itemTypeId}")] + public async Task GetItemsforItemType(int itemTypeId) + { + var items = await itemStorageService.GetItemsforItemType(itemTypeId); + if (items == null || items.Count == 0) + { + return NotFound($"No items found for this ItemLine ID {itemTypeId}"); + } + return Ok(items); + } } } \ No newline at end of file diff --git a/CargoHub/Controllers/ItemsController.cs b/CargoHub/Controllers/ItemsController.cs index 5f1018e..b02cd2f 100644 --- a/CargoHub/Controllers/ItemsController.cs +++ b/CargoHub/Controllers/ItemsController.cs @@ -5,7 +5,7 @@ namespace CargoHub.Controllers { - [Route("api/v2/[Controller]")] + [Route("api/v2/[Controller]")] public class ItemsController(ItemStorageService storage, InventoryStorageService inventoryStorageService, ErrorHandler error) : BaseController(storage, error) { public override async Task PostRow([FromBody] Item row) @@ -18,11 +18,11 @@ public override async Task PostRow([FromBody] Item row) var rowId = await storage.AddRowUid(row); return CreatedAtAction(nameof(GetRow), new { id = rowId }, rowId); } - + return error.ModelInvalid(base.Name, ModelState); } - + [HttpGet("{itemId}/inventory")] public async Task GetInventoriesForItem(string itemId) { diff --git a/CargoHub/Services/ItemStorageService.cs b/CargoHub/Services/ItemStorageService.cs index 5dcf943..6f93241 100644 --- a/CargoHub/Services/ItemStorageService.cs +++ b/CargoHub/Services/ItemStorageService.cs @@ -20,12 +20,18 @@ private static string IdToUid(int id) public async Task AddRowUid(T row) where T : Item { + var isValid = await ValidateItems(row.ItemType, row.ItemGroup, row.ItemLine); + if (!isValid) + { + return null; + } + row.Id = appDbContext.Set().Any() ? appDbContext.Set().Max(_ => _.Id) + 1 : 1; row.Uid = IdToUid(row.Id); if (await base.AddRow(row) is null) return null; - + return row.Uid; } @@ -65,5 +71,14 @@ public async Task> GetItemsforSupplier(int supplierId) return items; } + + public async Task ValidateItems(int itemTypeId, int itemGroupId, int itemLineId) + { + bool isItemTypeValid = await appDbContext.ItemTypes.AnyAsync(it => it.Id == itemTypeId); + bool isItemGroupValid = await appDbContext.ItemGroups.AnyAsync(ig => ig.Id == itemGroupId); + bool isItemLineValid = await appDbContext.ItemLines.AnyAsync(il => il.Id == itemLineId); + + return isItemTypeValid && isItemGroupValid && isItemLineValid; + } } } \ No newline at end of file diff --git a/Tests/test_helper.py b/Tests/test_helper.py index cbd65f0..b4acfbe 100644 --- a/Tests/test_helper.py +++ b/Tests/test_helper.py @@ -45,7 +45,6 @@ def get_integer_from_json_string(json_string): } template_item = { - "uid": "", "code": "", "description": "", "short_description": "", @@ -64,16 +63,15 @@ def get_integer_from_json_string(json_string): } item_1 = { - "uid": "P000001", "code": "sjQ23408K", "description": "Face-to-face clear-thinking complexity", "short_description": "must", "upc_code": "6523540947122", "model_number": "63-OFFTq0T", "commodity_code": "oTo304", - "item_line": 11, - "item_group": 73, - "item_type": 14, + "item_line": 1, + "item_group": 1, + "item_type": 1, "unit_purchase_quantity": 47, "unit_order_quantity": 13, "pack_order_quantity": 11, @@ -83,16 +81,15 @@ def get_integer_from_json_string(json_string): } item_1_updated = { - "uid": "P000001", "code": "sjQ23408K", "description": "Changed", "short_description": "must", "upc_code": "6523540947122", "model_number": "63-OFFTq0T", "commodity_code": "oTo304", - "item_line": 11, - "item_group": 73, - "item_type": 14, + "item_line": 1, + "item_group": 1, + "item_type": 1, "unit_purchase_quantity": 47, "unit_order_quantity": 13, "pack_order_quantity": 11, diff --git a/Tests/test_item_types.py b/Tests/test_item_types.py new file mode 100644 index 0000000..8de3689 --- /dev/null +++ b/Tests/test_item_types.py @@ -0,0 +1,89 @@ +import requests +import test_helper + + +BASE_URL = "http://localhost:3000/api/v2/ItemTypes" + +VALID_ITEM_TYPE = { + "Name": "Test Name", + "Description": "Test Description" +} + +INVALID_ITEM_TYPE_1 = {} +HEADERS = {"Content-Type": "application/json"} + +UPDATED_ITEM_TYPE = { + "id": 1, + "Name": "Updated test name", + "Description": "Updated test description" +} + + +def test_Post(): + # post the test body + post_response = requests.post( + BASE_URL, headers=HEADERS, json=VALID_ITEM_TYPE) + # convert the json response to a string and look for the id of the created Entity + json = str(post_response.json()) + # look here for an integer (digit) + + generated_id = test_helper.get_integer_from_json_string(json) + assert generated_id != 0 + + # get the same entity that has been posted + get_response = requests.get(BASE_URL + f"/{generated_id}") + json = get_response.json() + assert get_response.status_code == 200 + assert json["name"] == VALID_ITEM_TYPE["Name"] + assert json["description"] == VALID_ITEM_TYPE["Description"] + assert "created_at" in json, "created_at key was not created" + assert "updated_at" in json, "updated_at key was not created" + + delete_response = requests.delete( + BASE_URL+f"/{generated_id}") + + assert delete_response.status_code == 200 + + +def test_invalid_post(): + # post an item TYPE with an empty body + post_response = requests.post( + BASE_URL, headers=HEADERS, json=INVALID_ITEM_TYPE_1) + # the response should return a bad request + assert post_response.status_code == 400 + + +# def test_put(): +# # post an item TYPE +# post_response = requests.post( +# BASE_URL, headers=HEADERS, json=VALID_ITEM_TYPE) +# # convert the json response to a string and look for the id of the created Entity +# json = str(post_response.json()) +# # look here for an integer (digit) +# generated_id = test_helper.get_integer_from_json_string(json) +# assert generated_id != 0 +# # modify the added item TYPE +# put_response = requests.put( +# BASE_URL + f"/{generated_id}", headers=HEADERS, json=UPDATED_ITEM_TYPE) +# assert put_response.status_code == 200 + + +def test_put_Item_type_that_does_not_exist(): + put_response = requests.put( + BASE_URL + "999999999999", json=VALID_ITEM_TYPE) + assert put_response.status_code == 404 + + +def test_delete(): + post_response = requests.post(BASE_URL, json=VALID_ITEM_TYPE) + json = str(post_response.json()) + generated_id = test_helper.get_integer_from_json_string(json) + assert generated_id != 0 + + delete_response = requests.delete( + BASE_URL+f"/{generated_id}") + + assert delete_response.status_code == 200 + + get_response = requests.get(BASE_URL + str(generated_id)) + assert get_response.status_code == 404 diff --git a/Tests/test_items.py b/Tests/test_items.py new file mode 100644 index 0000000..7f598c9 --- /dev/null +++ b/Tests/test_items.py @@ -0,0 +1,45 @@ +import requests +import test_helper + + +BASE_URL = "http://localhost:3000/api/v2/items" + +VALID_ITEM = { + "Name": "Test Name", + "Description": "Test Description" +} + +INVALID_ITEM_1 = {} +HEADERS = {"Content-Type": "application/json"} + +UPDATED_ITEM = { + "code": "XLE77785i", + "description": "Focused human-resource implementation", + "short_description": "offer", + "upc_code": "7946503676171", + "model_number": "fM-605648-lbu", + "commodity_code": "qB-2533", + "item_line": 1, + "item_group": 1, + "item_type": 1, + "unit_purchase_quantity": 27, + "unit_order_quantity": 20, + "pack_order_quantity": 16, + "supplier_id": 45, + "supplier_code": "SUP127", + "supplier_part_number": "Ay-062669-VVl" +} + + +def test_invalid_post(): + # post an item TYPE with an empty body + post_response = requests.post( + BASE_URL, headers=HEADERS, json=INVALID_ITEM_1) + # the response should return a bad request + assert post_response.status_code == 400 + + +def test_put_Item_that_does_not_exist(): + put_response = requests.put( + BASE_URL + "999999999999", json=VALID_ITEM) + assert put_response.status_code == 404 diff --git a/UnitTest/BaseItemStorageTests.cs b/UnitTest/BaseItemStorageTests.cs new file mode 100644 index 0000000..500ea92 --- /dev/null +++ b/UnitTest/BaseItemStorageTests.cs @@ -0,0 +1,151 @@ +namespace CargoHub.Test; + +[TestClass] +public class BaseItemStorageTests +{ + private BaseStorageService _baseStorageService; + private AppDbContext _dbContext; + + [TestInitialize] + public void Setup() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestDatabase") + .Options; + + _dbContext = new AppDbContext(options); + _baseStorageService = new BaseStorageService(_dbContext); + } + + [TestCleanup] + public void Cleanup() + { + _dbContext.Database.EnsureDeleted(); + _dbContext.Dispose(); + } + + [TestMethod] + public async Task TestGetRowItems() + { + Item item = TestHelper.TestItemGet; + + _dbContext.Set().Add(item); + await _dbContext.SaveChangesAsync(); + + var result = await _baseStorageService.GetRow(item.Id); + Assert.AreEqual("P000001", result.Uid); + Assert.AreEqual("XLE77785i", result.Code); + Assert.AreEqual("test item", result.Description); + Assert.AreEqual("short test", result.ShortDescription); + Assert.AreEqual("7946503676171", result.UpcCode); + Assert.AreEqual("fM-605648-lbu", result.ModelNumber); + Assert.AreEqual("qB-2533", result.CommodityCode); + Assert.AreEqual(1, result.ItemLine); + Assert.AreEqual(1, result.ItemGroup); + Assert.AreEqual(1, result.ItemType); + Assert.AreEqual(23, result.UnitPurchaseQuantity); + Assert.AreEqual(43, result.UnitOrderQuantity); + Assert.AreEqual(65, result.PackOrderQuantity); + Assert.AreEqual(65, result.SupplierId); + Assert.AreEqual("SUP127", result.SupplierCode); + Assert.AreEqual("Ay-062669-VVl", result.SupplierPartNumber); + } + + [TestMethod] + public async Task TestGetRowNotExistItems() + { + Item result = await _baseStorageService.GetRow(999); + Assert.IsNull(result); + } + + [TestMethod] + public async Task TestGetAllItems() + { + Item item1 = TestHelper.TestItemGetAll1; + Item item2 = TestHelper.TestItemGetAll2; + + _dbContext.Items.AddRange(new List { item1, item2 }); + await _dbContext.SaveChangesAsync(); + + var result = await _baseStorageService.GetAllRows(); + Assert.AreEqual(2, result.Count); + } + + + [TestMethod] + public async Task TestPostItem() + { + Item newItem = TestHelper.TestItem1; + var result = await _baseStorageService.AddRow(newItem); + + Assert.IsNotNull(result); + } + + [TestMethod] + public async Task TestPostNullItem() + { + var result = await _baseStorageService.AddRow(null); + Assert.IsNull(result); + } + + [TestMethod] + public async Task TestPutItem() + { + Item item = TestHelper.TestItemPut; + await _baseStorageService.AddRow(item); + + Item updatedItem = TestHelper.TestUpdatedItem; + updatedItem.Id = item.Id; + + var result = await _baseStorageService.UpdateRow(item.Id, updatedItem); + Assert.IsTrue(result); + + var updated = await _baseStorageService.GetRow(item.Id); + Assert.AreEqual("P000001", updated.Uid); + Assert.AreEqual("XLE77785i", updated.Code); + Assert.AreEqual("test Updated", updated.Description); + Assert.AreEqual("short Updated", updated.ShortDescription); + Assert.AreEqual("7946503676171", updated.UpcCode); + Assert.AreEqual("fM-605648-lbu", updated.ModelNumber); + Assert.AreEqual("qB-2533", updated.CommodityCode); + Assert.AreEqual(92, updated.ItemLine); + Assert.AreEqual(82, updated.ItemGroup); + Assert.AreEqual(23, updated.ItemType); + Assert.AreEqual(23, updated.UnitPurchaseQuantity); + Assert.AreEqual(43, updated.UnitOrderQuantity); + Assert.AreEqual(65, updated.SupplierId); + Assert.AreEqual("SUP127", updated.SupplierCode); + Assert.AreEqual("Ay-062669-VVl", updated.SupplierPartNumber); + } + + + [TestMethod] + public async Task TestPutNotExist() + { + Item updatedItem = TestHelper.TestUpdatedItem; + var result = await _baseStorageService.UpdateRow(999, updatedItem); + Assert.IsFalse(result); + } + + [TestMethod] + public async Task TestDeleteRow() + { + // Arrange + Item deleteItem = TestHelper.TestItem1; + + var addedId = await _baseStorageService.AddRow(deleteItem); + Assert.IsNotNull(addedId); + deleteItem.Id = (int)addedId; + + var result = await _baseStorageService.DeleteRow(deleteItem.Id); + + Assert.IsTrue(result); + } + + [TestMethod] + public async Task TestDeleteRowNotExist() + { + var item = await _baseStorageService.DeleteRow(999); + Assert.IsFalse(item); + } +} \ No newline at end of file diff --git a/UnitTest/ItemStorageTests.cs b/UnitTest/ItemStorageTests.cs new file mode 100644 index 0000000..792a5e6 --- /dev/null +++ b/UnitTest/ItemStorageTests.cs @@ -0,0 +1,118 @@ +namespace CargoHub.Test; + +[TestClass] +public class ItemStorageServiceTests +{ + private ItemStorageService _itemStorageService; + private AppDbContext _dbContext; + + [TestInitialize] + public void Setup() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestDatabase") + .Options; + + _dbContext = new AppDbContext(options); + _itemStorageService = new ItemStorageService(_dbContext); + } + + [TestCleanup] + public void Cleanup() + { + _dbContext.Database.EnsureDeleted(); + _dbContext.Dispose(); + } + + [TestMethod] + public async Task TestAddRowUid() + { + // Arrange + Item firstItem = TestHelper.TestItem1; + Item secondItem = TestHelper.TestItem2; + + await _dbContext.ItemTypes.AddAsync(TestHelper.TestItemType1WithId); + await _dbContext.ItemGroups.AddAsync(TestHelper.TestItemGroup1WithId); + await _dbContext.ItemLines.AddAsync(TestHelper.TestItemLine1WithId); + await _dbContext.SaveChangesAsync(); + + // Act + var firstUid = await _itemStorageService.AddRowUid(firstItem); + var secondUid = await _itemStorageService.AddRowUid(secondItem); + + // Assert + Assert.IsNotNull(firstUid); + Assert.IsNotNull(secondUid); + Assert.AreEqual("P000001", firstUid); + Assert.AreEqual("P000002", secondUid); + } + + [TestMethod] + public async Task TestGetItemsForItemLine() + { + await _dbContext.AddRangeAsync(TestHelper.TestGetItemLTG1, TestHelper.TestGetItemLTG2); + await _dbContext.SaveChangesAsync(); + + var result = await _itemStorageService.GetItemsforItemLine(1); + + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Count); + Assert.IsTrue(result.All(item => item.ItemLine == 1)); + } + + [TestMethod] + public async Task TestGetItemsForItemType() + { + await _dbContext.AddRangeAsync(TestHelper.TestGetItemLTG1, TestHelper.TestGetItemLTG2); + await _dbContext.SaveChangesAsync(); + + var result = await _itemStorageService.GetItemsforItemType(3); + + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Count); + Assert.IsTrue(result.All(item => item.ItemType == 3)); + } + + [TestMethod] + public async Task TestGetItemsForItemGroup() + { + await _dbContext.AddRangeAsync(TestHelper.TestGetItemLTG1, TestHelper.TestGetItemLTG2); + await _dbContext.SaveChangesAsync(); + + var result = await _itemStorageService.GetItemsforItemGroup(2); + + Assert.IsNotNull(result); + Assert.AreEqual(2, result.Count); + Assert.IsTrue(result.All(item => item.ItemGroup == 2)); + } + + [TestMethod] + public async Task TestValidateItems_AllValid() + { + // Arrange + await _dbContext.ItemTypes.AddAsync(TestHelper.TestItemType1WithId); + await _dbContext.ItemGroups.AddAsync(TestHelper.TestItemGroup1WithId); + await _dbContext.ItemLines.AddAsync(TestHelper.TestItemLine1WithId); + await _dbContext.SaveChangesAsync(); + + // Act + var isValid = await _itemStorageService.ValidateItems(1, 1, 1); + + // Assert + Assert.IsTrue(isValid); + } + + [TestMethod] + public async Task ValidateItems_False() + { + await _dbContext.ItemTypes.AddAsync(TestHelper.TestItemType1WithId); + await _dbContext.ItemGroups.AddAsync(TestHelper.TestItemGroup1WithId); + await _dbContext.ItemLines.AddAsync(TestHelper.TestItemLine1WithId); + await _dbContext.SaveChangesAsync(); + + Assert.IsFalse(await _itemStorageService.ValidateItems(1, 1, 2)); //itemLine + Assert.IsFalse(await _itemStorageService.ValidateItems(1, 2, 1)); //itemGroup + Assert.IsFalse(await _itemStorageService.ValidateItems(2, 1, 1)); //itemType + Assert.IsFalse(await _itemStorageService.ValidateItems(2, 2, 2)); //All fail + } +} diff --git a/UnitTest/ItemTypesServiceTests.cs b/UnitTest/ItemTypesServiceTests.cs new file mode 100644 index 0000000..7ec6f6c --- /dev/null +++ b/UnitTest/ItemTypesServiceTests.cs @@ -0,0 +1,132 @@ +namespace CargoHub.Test; + +[TestClass] +public class ItemTypesServiceTests +{ + private BaseStorageService _baseStorageService; + private AppDbContext _dbContext; + + [TestInitialize] + public void Setup() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestDatabase") + .Options; + + _dbContext = new AppDbContext(options); + _baseStorageService = new BaseStorageService(_dbContext); + } + + [TestCleanup] + public void Cleanup() + { + _dbContext.Database.EnsureDeleted(); + _dbContext.Dispose(); + } + + [TestMethod] + public async Task TestGetRowItemType() + { + // Arrange + ItemType itemType = TestHelper.TestItemTypeGet; + await _dbContext.ItemTypes.AddAsync(itemType); + await _dbContext.SaveChangesAsync(); + + // Act + ItemType? result = await _baseStorageService.GetRow(itemType.Id); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(itemType.Name, result.Name); + Assert.AreEqual(itemType.Description, result.Description); + } + + [TestMethod] + public async Task TestGetRowNotExist() + { + var result = await _baseStorageService.GetRow(999); + Assert.IsNull(result); + } + + [TestMethod] + public async Task TestGetAllItemTypes() + { + ItemType itemType = TestHelper.TestItemType1Getall; + ItemType itemType2 = TestHelper.TestItemType2Getall; + + _dbContext.ItemTypes.AddRange(new List + { + itemType, itemType2 + }); + await _dbContext.SaveChangesAsync(); + + var result = await _baseStorageService.GetAllRows(); + + Assert.AreEqual(2, result.Count); + } + + [TestMethod] + public async Task TestGetAllMax() + { + for (int i = 0; i < 300; i++) + { + ItemType randomType = TestHelper.CreateRandomItemType(); + await _dbContext.ItemTypes.AddAsync(randomType); + } + await _dbContext.SaveChangesAsync(); + + var result = await _baseStorageService.GetAllRows(1, 100); + Assert.AreEqual(100, result.Count); + Assert.IsTrue(result.All(type => type.Id <= 100)); + } + + [TestMethod] + public async Task TestPostItemTypes() + { + ItemType newItemType = TestHelper.TestItemType1; + var result = await _baseStorageService.AddRow(newItemType); + + Assert.IsNotNull(result); + } + + [TestMethod] + public async Task TestPostNullItemTypes() + { + var result = await _baseStorageService.AddRow(null); + Assert.IsNull(result); + } + + [TestMethod] + public async Task TestDeleteItemType() + { + // Try deleting an entity that doesn't exist + bool result1 = await _baseStorageService.DeleteRow(1); + Assert.IsFalse(result1); + + // Add an ItemType to the database + ItemType itemType = TestHelper.TestItemType1; + await _dbContext.ItemTypes.AddAsync(itemType); + await _dbContext.SaveChangesAsync(); + + // Try deleting the added ItemType + bool result2 = await _baseStorageService.DeleteRow(itemType.Id); // No type argument needed + Assert.IsTrue(result2); + } + + + + [TestMethod] + public async Task TestPutNotExist() + { + ItemType updatedItem = TestHelper.TestupdateItemType; + var result = await _baseStorageService.UpdateRow(999, updatedItem); + Assert.IsFalse(result); + } + + [TestMethod] + public async Task TestDeleteRowNotExist() + { + var itemType = await _baseStorageService.DeleteRow(999); + Assert.IsFalse(itemType); + } +} diff --git a/UnitTest/TestHelper.cs b/UnitTest/TestHelper.cs index 5611d1e..5232913 100644 --- a/UnitTest/TestHelper.cs +++ b/UnitTest/TestHelper.cs @@ -80,5 +80,249 @@ public static ItemLine CreateRandomItemLine() Description = null }; + public static ItemType TestItemType1 = new ItemType + { + Name = "Test Name", + Description = "Test Description" + }; + + public static ItemType TestItemType2 = new ItemType + { + Name = "Test Name2", + Description = "Test Description2" + }; + + public static ItemType TestItemType1Getall = new ItemType + { + Name = "Test Name", + Description = "Test Description" + }; + + public static ItemType TestItemType2Getall = new ItemType + { + Name = "Test Name2", + Description = "Test Description2" + }; + + public static ItemType TestupdateItemType = new ItemType + { + Name = "updated Name", + Description = "updated Description" + }; + + public static Item TestItem1 = new Item + { + Uid = "P000001", + Code = "XLE77785i", + Description = "test item", + ShortDescription = "short test", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 1, + ItemType = 1, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static Item TestItem2 = new Item + { + Uid = "P000002", + Code = "XLE77785i", + Description = "test item2", + ShortDescription = "short test2", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 1, + ItemType = 1, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static Item TestUpdatedItem = new Item + { + Uid = "P000001", + Code = "XLE77785i", + Description = "test Updated", + ShortDescription = "short Updated", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 92, + ItemGroup = 82, + ItemType = 23, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static Item TestGetItemLTG1 = new Item + { + Uid = "P000001", + Code = "XLE77785i", + Description = "test Updated", + ShortDescription = "short Updated", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 2, + ItemType = 3, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static Item TestGetItemLTG2 = new Item + { + Uid = "P000002", + Code = "XLE77785i", + Description = "test Updated", + ShortDescription = "short Updated", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 2, + ItemType = 3, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static ItemType TestItemType1WithId = new ItemType + { + Id = 1, + Name = "Test Name", + Description = "Test Description" + }; + + public static ItemGroup TestItemGroup1WithId = new ItemGroup + { + Id = 1, + Name = "Test Group", + Description = "Group Description" + }; + + public static ItemLine TestItemLine1WithId = new ItemLine + { + Id = 1, + Name = "Test Line", + Description = "Line Description" + }; + public static ItemType CreateRandomItemType() + { + return new ItemType + { + Name = Guid.NewGuid().ToString(), + Description = Guid.NewGuid().ToString() + }; + } + + public static Item TestItemGet = new Item + { + Uid = "P000001", + Code = "XLE77785i", + Description = "test item", + ShortDescription = "short test", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 1, + ItemType = 1, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static ItemType TestItemTypeGet = new ItemType + { + Name = "Test Name", + Description = "Test Description" + }; + + public static Item TestItemGetAll1 = new Item + { + Uid = "P000001", + Code = "XLE77785i", + Description = "test item", + ShortDescription = "short test", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 1, + ItemType = 1, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static Item TestItemGetAll2 = new Item + { + Uid = "P000002", + Code = "XLE77785i", + Description = "test item", + ShortDescription = "short test", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 1, + ItemType = 1, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; + + public static Item TestItemPut = new Item + { + Uid = "P000001", + Code = "XLE77785i", + Description = "test item", + ShortDescription = "short test", + UpcCode = "7946503676171", + ModelNumber = "fM-605648-lbu", + CommodityCode = "qB-2533", + ItemLine = 1, + ItemGroup = 1, + ItemType = 1, + UnitPurchaseQuantity = 23, + UnitOrderQuantity = 43, + PackOrderQuantity = 65, + SupplierId = 65, + SupplierCode = "SUP127", + SupplierPartNumber = "Ay-062669-VVl", + }; } \ No newline at end of file diff --git a/UnitTest/Test_ItemTypes.cs b/UnitTest/Test_ItemTypes.cs deleted file mode 100644 index a22656b..0000000 --- a/UnitTest/Test_ItemTypes.cs +++ /dev/null @@ -1,140 +0,0 @@ -// 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 ItemTypeServiceTests -// { -// private BaseStorageService _baseStorageService; -// private AppDbContext _dbContext; - -// [TestInitialize] -// public void Setup() -// { -// var options = new DbContextOptionsBuilder() -// .UseInMemoryDatabase(databaseName: "TestDatabase") -// .Options; - -// _dbContext = new AppDbContext(options); -// _baseStorageService = new BaseStorageService(_dbContext); -// } - -// [TestCleanup] -// public void Cleanup() -// { -// _dbContext.Database.EnsureDeleted(); -// _dbContext.Dispose(); -// } - -// [TestMethod] -// public async Task TestGetRowItemType() -// { -// // Arrange -// var itemType = new ItemType { Name = "Test Item", Description = "Description" }; -// _dbContext.Set().Add(itemType); -// await _dbContext.SaveChangesAsync(); - -// // Act -// var result = await _baseStorageService.GetRow(itemType.Id); - -// // Assert -// Assert.IsNotNull(result); -// Assert.AreEqual("Test Item", result.Name); -// Assert.AreEqual("Description", result.Description); -// } - -// [TestMethod] // checkt null if does not exst -// public async Task TestGetRowNotExist() -// { -// var result = await _baseStorageService.GetRow(999); -// Assert.IsNull(result); -// } - -// [TestMethod] -// public async Task TestGetAllItemTypes() -// { -// _dbContext.ItemTypes.AddRange(new List -// { -// new ItemType { Name = "Test ItemType 1", Description = "Test ItemType 1"}, -// new ItemType { Name = "Test ItemType 2", Description = "Test ItemType 2"} -// }); -// await _dbContext.SaveChangesAsync(); - -// var result = await _baseStorageService.GetAllRows(); - -// Assert.AreEqual(2, result.Count); -// } - -// [TestMethod] -// public async Task TestPostItemTypes() -// { -// var newItemType = new ItemType { Name = "Test ItemType", Description = "Test ItemType" }; -// var result = await _baseStorageService.AddRow(newItemType); - -// Assert.IsNotNull(result); -// } - -// [TestMethod] -// public async Task TestPostRowNotExist() -// { -// var result = await _baseStorageService.AddRow(null); -// Assert.IsNull(result); -// } - -// [TestMethod] -// public async Task TestPutItemType() -// { -// // Arrange -// var itemType = new ItemType { Name = "Old Name", Description = "Old Description" }; -// _dbContext.Set().Add(itemType); -// await _dbContext.SaveChangesAsync(); - -// var updatedItem = new ItemType { Id = itemType.Id, Name = "New Name", Description = "New Description" }; - -// // Act -// var result = await _baseStorageService.UpdateRow(itemType.Id, updatedItem); - -// // Assert -// Assert.IsTrue(result); - -// var updated = await _dbContext.Set().FindAsync(itemType.Id); -// Assert.AreEqual("New Name", updated.Name); -// Assert.AreEqual("New Description", updated.Description); -// } - -// [TestMethod] -// public async Task TestPutNotExist() -// { -// var updatedItem = new ItemType { Name = "New Name", Description = "New Description" }; -// var result = await _baseStorageService.UpdateRow(999, updatedItem); -// Assert.IsFalse(result); -// } - -// [TestMethod] -// public async Task TestDeleteRow() -// { -// var deleteItemType = new ItemType { Name = "Delete name", Description = "Delete Description" }; -// _dbContext.Set().Add(deleteItemType); -// await _dbContext.SaveChangesAsync(); - -// var result = await _baseStorageService.DeleteRow(deleteItemType.Id); - -// Assert.IsTrue(result); - -// var deletedItem = await _dbContext.Set().FindAsync(deleteItemType.Id); -// Assert.IsNull(deletedItem); -// } - -// [TestMethod] -// public async Task TestDeleteRowNotExist() -// { -// var itemType = await _baseStorageService.GetRow(999); -// Assert.IsNull(itemType); -// } -// } -// } \ No newline at end of file diff --git a/UnitTest/Test_Items.cs b/UnitTest/Test_Items.cs deleted file mode 100644 index a9a3bad..0000000 --- a/UnitTest/Test_Items.cs +++ /dev/null @@ -1,88 +0,0 @@ -// 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 ItemStorageServiceTest -// { -// private BaseStorageService _baseStorageService; -// private AppDbContext _dbContext; - -// [TestInitialize] -// public void Setup() -// { -// var options = new DbContextOptionsBuilder() -// .UseInMemoryDatabase(databaseName: "TestDatabase") -// .Options; - -// _dbContext = new AppDbContext(options); -// _baseStorageService = new BaseStorageService(_dbContext); -// } - -// [TestCleanup] -// public void Cleanup() -// { -// _dbContext.Database.EnsureDeleted(); -// _dbContext.Dispose(); -// } - -// [TestMethod] -// public async Task TestGetRowItems() -// { - -// } - -// [TestMethod] // checkt null if does not exst -// public async Task TestGetRowNotExistItems() -// { - -// } - -// [TestMethod] -// public async Task TestGetAllItems() -// { - -// } - -// [TestMethod] -// public async Task TestPostItems() -// { - -// } - -// [TestMethod] -// public async Task TestPostRowNotExistItems() -// { - -// } - -// [TestMethod] -// public async Task TestPutItems() -// { - -// } - -// [TestMethod] -// public async Task TestPutNotExistItems() -// { - -// } - -// [TestMethod] -// public async Task TestDeleteRowItems() -// { - -// } - -// [TestMethod] -// public async Task TestDeleteRowNotExistItems() -// { - -// } -// } -// }