From d2cbbd059685145d8387d1f87e8c4beaa8140dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Tue, 11 Mar 2025 21:09:32 +0100 Subject: [PATCH] test_superfaktura_api: add test invalid json as repsonse from the server --- test/test_superfaktura_api.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/test_superfaktura_api.py b/test/test_superfaktura_api.py index 2bf96b5..dd034fe 100644 --- a/test/test_superfaktura_api.py +++ b/test/test_superfaktura_api.py @@ -1,7 +1,9 @@ import os import pytest +from unittest.mock import patch, mock_open, MagicMock + import requests -from unittest.mock import patch, mock_open + from superfaktura.superfaktura_api import ( SuperFakturaAPI, SuperFakturaAPIException, @@ -48,7 +50,7 @@ def test_download(api): def test_download_failure(api): with patch("requests.get") as mock_get: mock_get.return_value.status_code = 404 - with patch("builtins.open", mock_open()) as mock_file: + with patch("builtins.open", mock_open()): with open("test_file", "wb") as f: with pytest.raises(SuperFakturaAPIException): api.download("test_endpoint", f) @@ -65,4 +67,13 @@ def test_post_failure(api): mock_post.return_value.status_code = 404 mock_post.return_value.json.return_value = {"error": "not found"} with pytest.raises(SuperFakturaAPIException): - api.post("test_endpoint", '{"name": "Example"}') \ No newline at end of file + 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_get.return_value = mock_response + with pytest.raises(SuperFakturaAPIException, match="Unable to decode response as JSON"): + api.get("test_endpoint")