From 45a7e56a438e57af45fe8fd5dcece0394523b1f0 Mon Sep 17 00:00:00 2001 From: Adel Atzouza <1032542@hr.nl> Date: Tue, 21 Jan 2025 02:56:01 +0100 Subject: [PATCH] fix tests --- Tests/test_clients.py | 2 +- Tests/test_item_groups.py | 11 ++- Tests/test_item_lines.py | 11 ++- Tests/test_item_types.py | 91 +++++++++++++++++++ Tests/test_items.py | 45 +++++++++ .../{Test_locations.py => test_locations.py} | 0 Tests/{Test_orders.py => test_orders.py} | 2 +- .../{Test_shipments.py => test_shipments.py} | 0 UnitTest/ItemStorageTests.cs | 30 ------ 9 files changed, 150 insertions(+), 42 deletions(-) create mode 100644 Tests/test_item_types.py create mode 100644 Tests/test_items.py rename Tests/{Test_locations.py => test_locations.py} (100%) rename Tests/{Test_orders.py => test_orders.py} (99%) rename Tests/{Test_shipments.py => test_shipments.py} (100%) diff --git a/Tests/test_clients.py b/Tests/test_clients.py index adbe1a4..9c198d8 100644 --- a/Tests/test_clients.py +++ b/Tests/test_clients.py @@ -50,7 +50,7 @@ def test_get_all_clients(): # Test to retrieve a specific client by ID def test_get_client_by_id(): - client_id = 5 # Replace with a valid ID for your test + client_id = 1 # Replace with a valid ID for your test response = requests.get(f"{BASE_URL}/{client_id}", headers=headers) assert response.status_code == 200 client = response.json() diff --git a/Tests/test_item_groups.py b/Tests/test_item_groups.py index 62e4ddd..f05a0c7 100644 --- a/Tests/test_item_groups.py +++ b/Tests/test_item_groups.py @@ -30,7 +30,7 @@ def test_Post(): assert generated_id != 0 # get the same entity that has been posted - get_response = requests.get(BASE_URL + f"/{generated_id}") + get_response = requests.get(BASE_URL + f"/{generated_id}", headers=HEADERS) json = get_response.json() assert get_response.status_code == 200 assert json["name"] == VALID_ITEM_GROUP["Name"] @@ -39,7 +39,7 @@ def test_Post(): assert "updated_at" in json, "updated_at key was not created" delete_response = requests.delete( - BASE_URL+f"/{generated_id}") + BASE_URL+f"/{generated_id}", headers=HEADERS) assert delete_response.status_code == 200 @@ -62,6 +62,7 @@ def test_put(): generated_id = test_helper.get_integer_from_json_string(json) assert generated_id != 0 # modify the added item group + UPDATED_ITEM_GROUP["id"] = generated_id put_response = requests.put( BASE_URL + f"/{generated_id}", headers=HEADERS, json=UPDATED_ITEM_GROUP) assert put_response.status_code == 200 @@ -74,16 +75,16 @@ def test_put_Item_group_that_does_not_exist(): def test_delete(): - post_response = requests.post(BASE_URL, json=VALID_ITEM_GROUP) + post_response = requests.post(BASE_URL, json=VALID_ITEM_GROUP, headers=HEADERS) 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}") + BASE_URL+f"/{generated_id}", headers=HEADERS) assert delete_response.status_code == 200 - get_response = requests.get(BASE_URL + str(generated_id)) + get_response = requests.get(BASE_URL + str(generated_id), headers=HEADERS) assert get_response.status_code == 404 diff --git a/Tests/test_item_lines.py b/Tests/test_item_lines.py index de2c888..e1b2236 100644 --- a/Tests/test_item_lines.py +++ b/Tests/test_item_lines.py @@ -30,7 +30,7 @@ def test_Post(): assert generated_id != 0 # get the same entity that has been posted - get_response = requests.get(BASE_URL + f"/{generated_id}") + get_response = requests.get(BASE_URL + f"/{generated_id}", headers=HEADERS) json = get_response.json() assert get_response.status_code == 200 assert json["name"] == VALID_ITEM_LINE["Name"] @@ -39,7 +39,7 @@ def test_Post(): assert "updated_at" in json, "updated_at key was not created" delete_response = requests.delete( - BASE_URL+f"/{generated_id}") + BASE_URL+f"/{generated_id}", headers=HEADERS) assert delete_response.status_code == 200 @@ -62,6 +62,7 @@ def test_put(): generated_id = test_helper.get_integer_from_json_string(json) assert generated_id != 0 # modify the added item line + UPDATED_ITEM_LINE["id"] = generated_id put_response = requests.put( BASE_URL + f"/{generated_id}", headers=HEADERS, json=UPDATED_ITEM_LINE) assert put_response.status_code == 200 @@ -74,16 +75,16 @@ def test_put_Item_line_that_does_not_exist(): def test_delete(): - post_response = requests.post(BASE_URL, json=VALID_ITEM_LINE) + post_response = requests.post(BASE_URL, json=VALID_ITEM_LINE, headers=HEADERS) 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}") + BASE_URL+f"/{generated_id}", headers=HEADERS) assert delete_response.status_code == 200 - get_response = requests.get(BASE_URL + str(generated_id)) + get_response = requests.get(BASE_URL + str(generated_id), headers=HEADERS) assert get_response.status_code == 404 diff --git a/Tests/test_item_types.py b/Tests/test_item_types.py new file mode 100644 index 0000000..372f986 --- /dev/null +++ b/Tests/test_item_types.py @@ -0,0 +1,91 @@ +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", 'APIKEY': "4125a7b2-7ef8-4c4f-9ff9-3386c0dbcb5c"} + +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}", headers=HEADERS) + 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}", headers=HEADERS) + + 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 + UPDATED_ITEM_TYPE["id"] = generated_id + put_response = requests.put( + BASE_URL + f"/{generated_id}", headers=HEADERS, json=UPDATED_ITEM_TYPE) + assert put_response.status_code == 200 + requests.delete(BASE_URL + f"/{generated_id}") + + +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, headers=HEADERS) + 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}", headers=HEADERS) + + assert delete_response.status_code == 200 + + get_response = requests.get(BASE_URL + str(generated_id), headers=HEADERS) + assert get_response.status_code == 404 \ No newline at end of file diff --git a/Tests/test_items.py b/Tests/test_items.py new file mode 100644 index 0000000..0fbe9df --- /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", 'APIKEY': "4125a7b2-7ef8-4c4f-9ff9-3386c0dbcb5c"} + +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 \ No newline at end of file diff --git a/Tests/Test_locations.py b/Tests/test_locations.py similarity index 100% rename from Tests/Test_locations.py rename to Tests/test_locations.py diff --git a/Tests/Test_orders.py b/Tests/test_orders.py similarity index 99% rename from Tests/Test_orders.py rename to Tests/test_orders.py index 8314a4b..a708c0e 100644 --- a/Tests/Test_orders.py +++ b/Tests/test_orders.py @@ -255,4 +255,4 @@ def test_delete_order(setup_environment): assert delete_response.status_code == 200, f"Failed to delete order: {delete_response.text}" get_response = requests.get(f"{BASE_URL}/{order_id}", headers=HEADERS) - assert get_response.status_code == 404, f"Deleted order still exists: {get_response.text}" + assert get_response.status_code == 404, f"Deleted order still exists: {get_response.text}" \ No newline at end of file diff --git a/Tests/Test_shipments.py b/Tests/test_shipments.py similarity index 100% rename from Tests/Test_shipments.py rename to Tests/test_shipments.py diff --git a/UnitTest/ItemStorageTests.cs b/UnitTest/ItemStorageTests.cs index 81aa81d..0c0e6eb 100644 --- a/UnitTest/ItemStorageTests.cs +++ b/UnitTest/ItemStorageTests.cs @@ -85,34 +85,4 @@ public async Task TestGetItemsForItemGroup() 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 - } } \ No newline at end of file