mirror of
https://github.com/Eledio/superfaktura-client.git
synced 2025-11-01 00:18:25 +01:00
test: introduce basic tests
This commit is contained in:
3
requirements_dev.txt
Normal file
3
requirements_dev.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
requests~=2.32.3
|
||||||
|
python-dotenv~=1.0.1
|
||||||
|
pytest~=8.3.5
|
||||||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
68
test/test_superfaktura_api.py
Normal file
68
test/test_superfaktura_api.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
from unittest.mock import patch, mock_open
|
||||||
|
from superfaktura.superfaktura_api import (
|
||||||
|
SuperFakturaAPI,
|
||||||
|
SuperFakturaAPIException,
|
||||||
|
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"
|
||||||
|
}):
|
||||||
|
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
|
||||||
|
mock_get.return_value.json.return_value = {"data": "test"}
|
||||||
|
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
|
||||||
|
mock_get.return_value.content = b"test_content"
|
||||||
|
with patch("builtins.open", mock_open()) as mock_file:
|
||||||
|
with open("test_file", "wb") as f:
|
||||||
|
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
|
||||||
|
with patch("builtins.open", mock_open()) as mock_file:
|
||||||
|
with open("test_file", "wb") as f:
|
||||||
|
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
|
||||||
|
mock_post.return_value.json.return_value = {"data": "test"}
|
||||||
|
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
|
||||||
|
mock_post.return_value.json.return_value = {"error": "not found"}
|
||||||
|
with pytest.raises(SuperFakturaAPIException):
|
||||||
|
api.post("test_endpoint", '{"name": "Example"}')
|
||||||
Reference in New Issue
Block a user