From b66745ce6fa8f24d8299f42584c1b70d531551b1 Mon Sep 17 00:00:00 2001 From: Floris272 Date: Fri, 25 Oct 2024 15:26:56 +0200 Subject: [PATCH] [maykinmedia/open-producten#38] add client tests --- .../open_producten/tests/test_client.py | 44 +++++++++++++++++++ .../tests/test_generate_form.py | 6 +++ 2 files changed, 50 insertions(+) diff --git a/src/openforms/contrib/open_producten/tests/test_client.py b/src/openforms/contrib/open_producten/tests/test_client.py index 440fb92826..133d2ba404 100644 --- a/src/openforms/contrib/open_producten/tests/test_client.py +++ b/src/openforms/contrib/open_producten/tests/test_client.py @@ -10,6 +10,7 @@ get_open_producten_client, ) from openforms.contrib.open_producten.models import OpenProductenConfig +from openforms.products.tests.factories import ProductFactory class TestOpenProductenClient(TestCase): @@ -79,3 +80,46 @@ def test_get_open_producten_client_with_service(self): client = get_open_producten_client() self.assertEqual(client.base_url, "http://test/") + + def test_product_type_fields(self): + product = ProductFactory() + self.requests_mock.get( + json={ + "count": 1, + "next": None, + "previous": None, + "results": [ + { + "id": "d4fd8fb8-8b7a-45bf-8b93-969c23573af9", + "name": "text", + "description": "text", + "type": "textfield", + "is_required": None, + "choices": [], + } + ], + }, + status_code=200, + url=f"https://test/producttypes/{product.uuid}/fields", + ) + + fields = self.client.get_product_type_fields(product.uuid) + + self.assertEqual(len(fields), 1) + + field = fields[0] + + self.assertEqual(field.name, "text") + self.assertEqual(field.type, "textfield") + + def test_get_product_type_fields_with_request_exception(self): + product = ProductFactory() + + self.requests_mock.get( + status_code=404, + json={"error": "not found"}, + url=f"https://test/producttypes/{product.uuid}/fields", + ) + + with self.assertRaises(requests.RequestException): + self.client.get_product_type_fields(product.uuid) diff --git a/src/openforms/contrib/open_producten/tests/test_generate_form.py b/src/openforms/contrib/open_producten/tests/test_generate_form.py index ccb892dc7d..443727daae 100644 --- a/src/openforms/contrib/open_producten/tests/test_generate_form.py +++ b/src/openforms/contrib/open_producten/tests/test_generate_form.py @@ -154,6 +154,12 @@ def test_generate_configuration_with_radio(self): }, ) + def test_generate_form_configuration_with_unknown_type(self): + field = _create_field(id=uuid4(), type="unknown") + + with self.assertRaisesMessage(ValidationError, "Unknown field type unknown"): + _generate_configuration([field]) + @patch("openforms.contrib.open_producten.generate_form.get_open_producten_client") def test_generate_form(self, mock_get_client):