forked from open-formulieren/open-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[maykinmedia/open-producten#24] Add open producten client & command t…
…ests
- Loading branch information
Showing
5 changed files
with
209 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from django.test import TestCase | ||
|
||
import requests_mock | ||
from zgw_consumers.test.factories import ServiceFactory | ||
|
||
from openforms.contrib.open_producten.client import ( | ||
NoServiceConfigured, | ||
OpenProductenClient, | ||
get_open_producten_client, | ||
) | ||
from openforms.contrib.open_producten.models import OpenProductenConfig | ||
|
||
|
||
class TestOpenProductenClient(TestCase): | ||
def setUp(self): | ||
self.client = OpenProductenClient(base_url="https://test") | ||
self.requests_mock = requests_mock.Mocker() | ||
self.requests_mock.start() | ||
self.addCleanup(self.requests_mock.stop) | ||
|
||
def test_get_current_prices(self): | ||
|
||
self.requests_mock.get( | ||
json={ | ||
"id": "f714ce89-0c98-4249-9e03-9fc929a29a09", | ||
"name": "test", | ||
"upl_name": "UPL-naam nog niet beschikbaar", | ||
"upl_uri": "http://standaarden.overheid.nl/owms/terms/UPL-naam_nog_niet_beschikbaar", | ||
"current_price": { | ||
"id": "1ec25528-4c6b-4bbf-84f7-d0a3efdbe314", | ||
"options": [ | ||
{ | ||
"id": "1c3cc677-b643-46f7-8f30-cbc18e297cad", | ||
"amount": "24.00", | ||
"description": "normaal", | ||
} | ||
], | ||
"valid_from": "2024-10-23", | ||
}, | ||
}, | ||
status_code=200, | ||
url="https://test/producttypes/current-prices", | ||
) | ||
|
||
product_type = self.client.get_current_prices() | ||
self.assertEqual(product_type.name, "test") | ||
self.assertEqual(product_type.id, "f714ce89-0c98-4249-9e03-9fc929a29a09") | ||
self.assertEqual( | ||
product_type.current_price.id, "1ec25528-4c6b-4bbf-84f7-d0a3efdbe314" | ||
) | ||
self.assertEqual( | ||
product_type.current_price.options[0].id, | ||
"1c3cc677-b643-46f7-8f30-cbc18e297cad", | ||
) | ||
self.assertEqual(product_type.current_price.options[0].amount, "24.00") | ||
|
||
def test_get_open_producten_client_without_service(self): | ||
OpenProductenConfig.objects.create() | ||
|
||
with self.assertRaisesMessage( | ||
NoServiceConfigured, "No open producten service configured!" | ||
): | ||
get_open_producten_client() | ||
|
||
def test_get_open_producten_client_with_service(self): | ||
service = ServiceFactory(api_root="http://test") | ||
OpenProductenConfig.objects.create(producten_service=service) | ||
|
||
client = get_open_producten_client() | ||
self.assertEqual(client.base_url, "http://test/") |
61 changes: 61 additions & 0 deletions
61
src/openforms/contrib/open_producten/tests/test_command.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from io import StringIO | ||
from unittest.mock import patch | ||
from uuid import uuid4 | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from zgw_consumers.test.factories import ServiceFactory | ||
|
||
from openforms.contrib.open_producten.models import OpenProductenConfig | ||
from openforms.contrib.open_producten.tests.factories import ( | ||
PriceFactory, | ||
PriceOptionFactory, | ||
) | ||
from openforms.products.tests.factories import ProductFactory | ||
|
||
|
||
class TestImportPrices(TestCase): | ||
|
||
def call_command(self, *args, **kwargs): | ||
out = StringIO() | ||
call_command( | ||
"import_prices", | ||
*args, | ||
stdout=out, | ||
stderr=StringIO(), | ||
**kwargs, | ||
) | ||
return out.getvalue() | ||
|
||
def test_import_prices_without_config(self): | ||
out = self.call_command() | ||
self.assertEqual( | ||
out, "Please define the OpenProductenConfig before running this command.\n" | ||
) | ||
|
||
@patch( | ||
"openforms.contrib.open_producten.management.commands.import_prices.get_open_producten_client" | ||
) | ||
@patch( | ||
"openforms.contrib.open_producten.management.commands.import_prices.PriceImporter.import_product_types" | ||
) | ||
def test_import_prices(self, mock_import_product_types, mock_client): | ||
OpenProductenConfig.objects.create(producten_service=ServiceFactory.create()) | ||
|
||
uuid = uuid4() | ||
|
||
mock_import_product_types.return_value = ( | ||
[ | ||
ProductFactory.create(uuid=uuid), | ||
PriceFactory.create(uuid=uuid), | ||
PriceOptionFactory.create(uuid=uuid), | ||
], | ||
[], | ||
) | ||
out = self.call_command() | ||
|
||
self.assertEqual( | ||
out, | ||
f"updated 0 exising product type(s)\ncreated 3 new product type(s):\nProduct: {uuid}\nPrice: {uuid}\nPriceOption: {uuid}\n", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters