From a868e1889f6f8ccb8644b2fb6fac64a542080da3 Mon Sep 17 00:00:00 2001 From: Donkie Date: Sat, 6 Jan 2024 20:09:25 +0100 Subject: [PATCH] Added integration tests for using extra fields --- .../tests/fields/test_utilize.py | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests_integration/tests/fields/test_utilize.py diff --git a/tests_integration/tests/fields/test_utilize.py b/tests_integration/tests/fields/test_utilize.py new file mode 100644 index 0000000..f7a370c --- /dev/null +++ b/tests_integration/tests/fields/test_utilize.py @@ -0,0 +1,156 @@ +"""Integration tests for the custom extra fields system.""" + +import json + +import httpx + +from ..conftest import assert_httpx_success + +URL = "http://spoolman:8000" + + +def test_add_vendor_with_extra_field(): + """Test adding a vendor with a custom field.""" + result = httpx.post( + f"{URL}/api/v1/field/vendor/mytextfield", + json={ + "name": "My text field", + "field_type": "text", + "default_value": json.dumps("Hello World"), + }, + ) + assert_httpx_success(result) + + result = httpx.post( + f"{URL}/api/v1/vendor", + json={ + "name": "My Vendor", + "extra": { + "mytextfield": '"My Value"', + }, + }, + ) + assert_httpx_success(result) + + # Verify + result = httpx.get(f"{URL}/api/v1/vendor/{result.json()['id']}") + assert_httpx_success(result) + vendor = result.json() + assert vendor["name"] == "My Vendor" + assert vendor["extra"] == {"mytextfield": '"My Value"'} + + # Clean up + result = httpx.delete(f"{URL}/api/v1/field/vendor/mytextfield") + assert_httpx_success(result) + + result = httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}") + assert_httpx_success(result) + + +def test_add_vendor_with_invalid_extra_field(): + """Test adding a vendor with an invalid custom field.""" + result = httpx.post( + f"{URL}/api/v1/vendor", + json={ + "name": "My Vendor", + "extra": { + "somefield": 42, + }, + }, + ) + assert result.status_code == 400 + assert "somefield" in result.json()["message"].lower() + + +def test_add_vendor_with_extra_field_then_delete(): + """Test adding a vendor with an extra field, then delete the field. + + Vendor GET response should then not contain the extra field. + """ + result = httpx.post( + f"{URL}/api/v1/field/vendor/mytextfield", + json={ + "name": "My text field", + "field_type": "text", + "default_value": json.dumps("Hello World"), + }, + ) + assert_httpx_success(result) + + result = httpx.post( + f"{URL}/api/v1/vendor", + json={ + "name": "My Vendor", + "extra": { + "mytextfield": '"My Value"', + }, + }, + ) + assert_httpx_success(result) + + # Verify + result = httpx.get(f"{URL}/api/v1/vendor/{result.json()['id']}") + assert_httpx_success(result) + vendor = result.json() + assert vendor["name"] == "My Vendor" + assert vendor["extra"] == {"mytextfield": '"My Value"'} + + # Remove field + result = httpx.delete(f"{URL}/api/v1/field/vendor/mytextfield") + assert_httpx_success(result) + + # Verify + result = httpx.get(f"{URL}/api/v1/vendor/{vendor['id']}") + assert_httpx_success(result) + vendor = result.json() + assert vendor["name"] == "My Vendor" + assert "extra" not in vendor or vendor["extra"] == {} + + result = httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}") + assert_httpx_success(result) + + +def test_update_existing_vendor_with_new_extra_field(): + """Test updating an existing vendor with a new extra field.""" + result = httpx.post( + f"{URL}/api/v1/vendor", + json={ + "name": "My Vendor", + }, + ) + assert_httpx_success(result) + vendor_id = result.json()["id"] + + result = httpx.post( + f"{URL}/api/v1/field/vendor/mytextfield", + json={ + "name": "My text field", + "field_type": "text", + "default_value": json.dumps("Hello World"), + }, + ) + assert_httpx_success(result) + + result = httpx.patch( + f"{URL}/api/v1/vendor/{vendor_id}", + json={ + "extra": { + "mytextfield": '"My Value"', + }, + }, + ) + assert_httpx_success(result) + + # Verify + result = httpx.get(f"{URL}/api/v1/vendor/{vendor_id}") + assert_httpx_success(result) + vendor = result.json() + assert vendor["name"] == "My Vendor" + assert vendor["extra"] == {"mytextfield": '"My Value"'} + + # Clean up + result = httpx.delete(f"{URL}/api/v1/field/vendor/mytextfield") + assert_httpx_success(result) + + result = httpx.delete(f"{URL}/api/v1/vendor/{vendor_id}") + assert_httpx_success(result)