Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alihan Items/ItemTypes tests, Validate Items. #48

Merged
merged 40 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e3b0705
unittests items
Alihanaslan75 Jan 17, 2025
f50fdf4
Merge branch 'development' into Alihan2
Alihanaslan75 Jan 17, 2025
5eb0064
small change
Alihanaslan75 Jan 17, 2025
2a1a8c0
test helper new version
Alihanaslan75 Jan 17, 2025
549312d
Merge branch 'development' into Alihan2
Alihanaslan75 Jan 17, 2025
aaf2910
unittest item types
Alihanaslan75 Jan 17, 2025
1d3486d
unittest itemtypes
Alihanaslan75 Jan 17, 2025
89a406d
Unittest itemtype
Alihanaslan75 Jan 17, 2025
5ba44cb
unittest itemtype
Alihanaslan75 Jan 17, 2025
0f4555f
Unittest itemType
Alihanaslan75 Jan 17, 2025
2ca2643
Unittest Items
Alihanaslan75 Jan 17, 2025
4981ba7
Unittest TestGetAllItems
Alihanaslan75 Jan 17, 2025
831eea3
Unittest TestPostItem
Alihanaslan75 Jan 17, 2025
87097ed
Unittest TestPostNullItem
Alihanaslan75 Jan 17, 2025
f3569d5
Unittest TestPutItem
Alihanaslan75 Jan 17, 2025
8163336
Unittest
Alihanaslan75 Jan 17, 2025
7ee08de
Unittest Items
Alihanaslan75 Jan 17, 2025
5423331
Items validation
Alihanaslan75 Jan 17, 2025
8398979
ItemStorageService unittest 1.0
Alihanaslan75 Jan 18, 2025
0bb33be
ItemStorageTests TestAddRowUid 1.1
Alihanaslan75 Jan 18, 2025
2b08043
Unittest ItemServiceTests
Alihanaslan75 Jan 18, 2025
4a89ac5
Before merge met sidney branch
Alihanaslan75 Jan 18, 2025
d7a95a0
Items controller small change
Alihanaslan75 Jan 19, 2025
3c8f9d4
GetItemsForItemLine
Alihanaslan75 Jan 20, 2025
56c0729
GetItemsForItemLine
Alihanaslan75 Jan 20, 2025
95956a9
GetItemforItemLine
Alihanaslan75 Jan 20, 2025
519f12e
GetItemsforItemType
Alihanaslan75 Jan 20, 2025
79a5b4a
TestGetItemsForItemLine
Alihanaslan75 Jan 20, 2025
4ee7664
TestGetItemsForItemType
Alihanaslan75 Jan 20, 2025
ea8837f
TestGetItemsForItemGroup
Alihanaslan75 Jan 20, 2025
0334435
Validation tests
Alihanaslan75 Jan 20, 2025
5541cd3
Validation tests 1.1
Alihanaslan75 Jan 20, 2025
9603969
Validation tests 1.2
Alihanaslan75 Jan 20, 2025
278951f
Merge branch 'development' into Alihan2
Alihanaslan75 Jan 20, 2025
52034de
Merge branch 'development' into Alihan2
Alihanaslan75 Jan 20, 2025
3f3e093
Unittesten
Alihanaslan75 Jan 20, 2025
07d96a6
Unittests na merge met main
Alihanaslan75 Jan 20, 2025
3fd847e
Integrations tests items
Alihanaslan75 Jan 20, 2025
a7d2ed7
Merge branch 'development' into Alihan2
Alihanaslan75 Jan 20, 2025
c54843b
ItemStorageService
Alihanaslan75 Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions CargoHub.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 11 additions & 0 deletions CargoHub/Controllers/ItemGroupsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ public async Task<IActionResult> GetItemsForSupplier(int itemGroupId)
var items = await itemStorageService.GetItemsforSupplier(itemGroupId);
return Ok(items);
}

[HttpGet("itemgroup/{itemGroupId}")]
public async Task<IActionResult> 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);
}
}
}
12 changes: 11 additions & 1 deletion CargoHub/Controllers/ItemLinesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public async Task<IActionResult> GetItemsForSupplier(int itemLineId)
var items = await itemStorageService.GetItemsforSupplier(itemLineId);
return Ok(items);
}
}

[HttpGet("itemline/{itemLineId}")]
public async Task<IActionResult> 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);
}
}
}
11 changes: 11 additions & 0 deletions CargoHub/Controllers/ItemTypesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ public async Task<IActionResult> GetItemsForSupplier(int itemTypeId)
var items = await itemStorageService.GetItemsforSupplier(itemTypeId);
return Ok(items);
}

[HttpGet("itemtype/{itemTypeId}")]
public async Task<IActionResult> 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);
}
}
}
6 changes: 3 additions & 3 deletions CargoHub/Controllers/ItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item>(storage, error)
{
public override async Task<IActionResult> PostRow([FromBody] Item row)
Expand All @@ -18,11 +18,11 @@ public override async Task<IActionResult> 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<IActionResult> GetInventoriesForItem(string itemId)
{
Expand Down
17 changes: 16 additions & 1 deletion CargoHub/Services/ItemStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ private static string IdToUid(int id)

public async Task<string?> AddRowUid<T>(T row) where T : Item
{
var isValid = await ValidateItems(row.ItemType, row.ItemGroup, row.ItemLine);
if (!isValid)
{
return null;
}

row.Id = appDbContext.Set<T>().Any() ? appDbContext.Set<T>().Max(_ => _.Id) + 1 : 1;
row.Uid = IdToUid(row.Id);

if (await base.AddRow(row) is null)
return null;

return row.Uid;
}

Expand Down Expand Up @@ -65,5 +71,14 @@ public async Task<List<Item>> GetItemsforSupplier(int supplierId)

return items;
}

public async Task<bool> 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;
}
}
}
15 changes: 6 additions & 9 deletions Tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def get_integer_from_json_string(json_string):
}

template_item = {
"uid": "",
"code": "",
"description": "",
"short_description": "",
Expand All @@ -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,
Expand All @@ -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,
Expand Down
89 changes: 89 additions & 0 deletions Tests/test_item_types.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions Tests/test_items.py
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading