From 765334ed92fc19a337fb292c336596e970cf810f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Wed, 12 Mar 2025 21:49:00 +0100 Subject: [PATCH 1/5] test_client_contact: add tests --- test/test_client_contact.py | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/test_client_contact.py diff --git a/test/test_client_contact.py b/test/test_client_contact.py new file mode 100644 index 0000000..ad360c7 --- /dev/null +++ b/test/test_client_contact.py @@ -0,0 +1,52 @@ +import pytest # type: ignore +from unittest.mock import patch +from superfaktura.client_contacts import ( + ClientContactModel, + ClientContact, + ClientException, +) + + +@pytest.fixture +def client_contact(): + with patch("superfaktura.client_contacts.ClientContact", return_value=None): + return ClientContact() + + +def test_add_contact_success(client_contact): + client = ClientContactModel( + name="John Doe", + ) + + with patch("superfaktura.superfaktura_api.SuperFakturaAPI.post") as mock_post: + mock_post.return_value = {"error_message": "Client created"} + assert client_contact.add_contact(contact=client) + + +def test_add_contact_failed(client_contact): + client = ClientContactModel( + name="John Doe", + ) + + with patch("superfaktura.superfaktura_api.SuperFakturaAPI.post") as mock_post: + mock_post.return_value = {"error_message": "Client creation failed"} + assert not client_contact.add_contact(contact=client) + + +def test_list(client_contact): + with patch("superfaktura.superfaktura_api.SuperFakturaAPI.get") as mock_get: + mock_get.return_value = {"data": "test"} + assert client_contact.list() == {"data": "test"} + + +def test_get_client_exists(client_contact): + with patch("superfaktura.superfaktura_api.SuperFakturaAPI.get") as mock_get: + mock_get.return_value = {"Client": {"name": "John Doe", "id": 1}} + assert client_contact.get_client(client_id=1).name == "John Doe" + + +def test_get_client_not_exists(client_contact): + with patch("superfaktura.superfaktura_api.SuperFakturaAPI.get") as mock_get: + mock_get.return_value = {} + with pytest.raises(ClientException): + client_contact.get_client(client_id=1) From 133702dc1d9091c5bb82a9f2276c30f447ea7f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Wed, 12 Mar 2025 21:49:10 +0100 Subject: [PATCH 2/5] test_superfaktura_api: fix format --- test/test_superfaktura_api.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/test/test_superfaktura_api.py b/test/test_superfaktura_api.py index c4e295e..7255bc3 100644 --- a/test/test_superfaktura_api.py +++ b/test/test_superfaktura_api.py @@ -10,21 +10,27 @@ from superfaktura.superfaktura_api import ( SuperFakturaAPIMissingCredentialsException, ) + @pytest.fixture def api(): - with patch.dict(os.environ, { - "SUPERFAKTURA_API_KEY": "test_key", - "SUPERFAKTURA_API_URL": "https://api.superfaktura.cz", - "SUPERFAKTURA_API_EMAIL": "test_email", - "SUPERFAKTURA_API_COMPANY_ID": "test_company_id" - }): + with patch.dict( + os.environ, + { + "SUPERFAKTURA_API_KEY": "test_key", + "SUPERFAKTURA_API_URL": "https://api.superfaktura.cz", + "SUPERFAKTURA_API_EMAIL": "test_email", + "SUPERFAKTURA_API_COMPANY_ID": "test_company_id", + }, + ): return SuperFakturaAPI() + def test_missing_credentials(): with patch.dict(os.environ, {}, clear=True): with pytest.raises(SuperFakturaAPIMissingCredentialsException): SuperFakturaAPI() + def test_get(api): with patch("requests.get") as mock_get: mock_get.return_value.status_code = 200 @@ -32,12 +38,14 @@ def test_get(api): response = api.get("test_endpoint") assert response == {"data": "test"} + def test_get_failure(api): with patch("requests.get") as mock_get: mock_get.return_value.status_code = 404 with pytest.raises(SuperFakturaAPIException): api.get("test_endpoint") + def test_download(api): with patch("requests.get") as mock_get: mock_get.return_value.status_code = 200 @@ -47,6 +55,7 @@ def test_download(api): api.download("test_endpoint", f) mock_file().write.assert_called_once_with(b"test_content") + def test_download_failure(api): with patch("requests.get") as mock_get: mock_get.return_value.status_code = 404 @@ -55,6 +64,7 @@ def test_download_failure(api): with pytest.raises(SuperFakturaAPIException): api.download("test_endpoint", f) + def test_post(api): with patch("requests.post") as mock_post: mock_post.return_value.status_code = 200 @@ -62,6 +72,7 @@ def test_post(api): response = api.post("test_endpoint", '{"name": "Example"}') assert response == {"data": "test"} + def test_post_failure(api): with patch("requests.post") as mock_post: mock_post.return_value.status_code = 404 @@ -69,15 +80,21 @@ def test_post_failure(api): with pytest.raises(SuperFakturaAPIException): api.post("test_endpoint", '{"name": "Example"}') + def test_get_invalid_json(api): with patch("requests.get") as mock_get: mock_response = MagicMock() mock_response.status_code = 200 - mock_response.json.side_effect = requests.exceptions.JSONDecodeError("msg", "doc", 0) + mock_response.json.side_effect = requests.exceptions.JSONDecodeError( + "msg", "doc", 0 + ) mock_get.return_value = mock_response - with pytest.raises(SuperFakturaAPIException, match="Unable to decode response as JSON"): + with pytest.raises( + SuperFakturaAPIException, match="Unable to decode response as JSON" + ): api.get("test_endpoint") + def test_download_not_writable_descriptor(api): with patch("requests.get") as mock_get: mock_get.return_value.status_code = 200 From c861f35ad2c6b4983c1f2c03db8a48fcc50aa450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Wed, 12 Mar 2025 21:51:55 +0100 Subject: [PATCH 3/5] test_client_contact: add api credinitials --- test/test_client_contact.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/test_client_contact.py b/test/test_client_contact.py index ad360c7..9b59dc3 100644 --- a/test/test_client_contact.py +++ b/test/test_client_contact.py @@ -1,3 +1,5 @@ +import os + import pytest # type: ignore from unittest.mock import patch from superfaktura.client_contacts import ( @@ -9,8 +11,17 @@ from superfaktura.client_contacts import ( @pytest.fixture def client_contact(): - with patch("superfaktura.client_contacts.ClientContact", return_value=None): - return ClientContact() + with patch.dict( + os.environ, + { + "SUPERFAKTURA_API_KEY": "test_key", + "SUPERFAKTURA_API_URL": "https://api.superfaktura.cz", + "SUPERFAKTURA_API_EMAIL": "test_email", + "SUPERFAKTURA_API_COMPANY_ID": "test_company_id", + }, + ): + with patch("superfaktura.client_contacts.ClientContact", return_value=None): + return ClientContact() def test_add_contact_success(client_contact): From 964882bc5de672522f61f79f834737c17567245d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Wed, 12 Mar 2025 22:02:29 +0100 Subject: [PATCH 4/5] test_client_contact: fix pytest.fixture --- test/test_client_contact.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_client_contact.py b/test/test_client_contact.py index 9b59dc3..fd9c928 100644 --- a/test/test_client_contact.py +++ b/test/test_client_contact.py @@ -20,8 +20,7 @@ def client_contact(): "SUPERFAKTURA_API_COMPANY_ID": "test_company_id", }, ): - with patch("superfaktura.client_contacts.ClientContact", return_value=None): - return ClientContact() + return ClientContact() def test_add_contact_success(client_contact): From f40b015beb326c651099ffbd2988288ad47e10a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Wed, 12 Mar 2025 22:03:25 +0100 Subject: [PATCH 5/5] test_client_contact: add docstring --- test/test_client_contact.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_client_contact.py b/test/test_client_contact.py index fd9c928..fdf88bf 100644 --- a/test/test_client_contact.py +++ b/test/test_client_contact.py @@ -8,6 +8,13 @@ from superfaktura.client_contacts import ( ClientException, ) +""" +Tests for the ClientContact class from the superfaktura package. + +These tests validate the functionality of methods for managing client contacts +through the SuperFaktura API. +""" + @pytest.fixture def client_contact():