mirror of
https://github.com/Eledio/superfaktura-client.git
synced 2025-11-01 08:28:25 +01:00
Compare commits
6 Commits
feature/li
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adc2fa4aac | ||
|
|
f40b015beb | ||
|
|
964882bc5d | ||
|
|
c861f35ad2 | ||
|
|
133702dc1d | ||
|
|
765334ed92 |
24
.github/workflows/mypy.yml
vendored
24
.github/workflows/mypy.yml
vendored
@@ -1,24 +0,0 @@
|
|||||||
name: MyPy Type Checking
|
|
||||||
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
type-check:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Python
|
|
||||||
uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: '3.11'
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: |
|
|
||||||
python -m pip install --upgrade pip
|
|
||||||
pip install -r requirements_dev.txt
|
|
||||||
|
|
||||||
- name: Run MyPy
|
|
||||||
run: mypy .
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
# type: ignore
|
|
||||||
# Configuration file for the Sphinx documentation builder.
|
# Configuration file for the Sphinx documentation builder.
|
||||||
#
|
#
|
||||||
# For the full list of built-in configuration values, see the documentation:
|
# For the full list of built-in configuration values, see the documentation:
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
requests~=2.32.3
|
requests~=2.32.3
|
||||||
python-dotenv~=1.0.1
|
python-dotenv~=1.0.1
|
||||||
pytest~=8.3.5
|
pytest~=8.3.5
|
||||||
pytest-cov~=6.0.0
|
pytest-cov
|
||||||
mypy~=1.9.0
|
|
||||||
types-requests~=2.31
|
|
||||||
types-setuptools~=68.2
|
|
||||||
|
|||||||
69
test/test_client_contact.py
Normal file
69
test/test_client_contact.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import pytest # type: ignore
|
||||||
|
from unittest.mock import patch
|
||||||
|
from superfaktura.client_contacts import (
|
||||||
|
ClientContactModel,
|
||||||
|
ClientContact,
|
||||||
|
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():
|
||||||
|
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 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)
|
||||||
@@ -10,21 +10,27 @@ from superfaktura.superfaktura_api import (
|
|||||||
SuperFakturaAPIMissingCredentialsException,
|
SuperFakturaAPIMissingCredentialsException,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def api():
|
def api():
|
||||||
with patch.dict(os.environ, {
|
with patch.dict(
|
||||||
"SUPERFAKTURA_API_KEY": "test_key",
|
os.environ,
|
||||||
"SUPERFAKTURA_API_URL": "https://api.superfaktura.cz",
|
{
|
||||||
"SUPERFAKTURA_API_EMAIL": "test_email",
|
"SUPERFAKTURA_API_KEY": "test_key",
|
||||||
"SUPERFAKTURA_API_COMPANY_ID": "test_company_id"
|
"SUPERFAKTURA_API_URL": "https://api.superfaktura.cz",
|
||||||
}):
|
"SUPERFAKTURA_API_EMAIL": "test_email",
|
||||||
|
"SUPERFAKTURA_API_COMPANY_ID": "test_company_id",
|
||||||
|
},
|
||||||
|
):
|
||||||
return SuperFakturaAPI()
|
return SuperFakturaAPI()
|
||||||
|
|
||||||
|
|
||||||
def test_missing_credentials():
|
def test_missing_credentials():
|
||||||
with patch.dict(os.environ, {}, clear=True):
|
with patch.dict(os.environ, {}, clear=True):
|
||||||
with pytest.raises(SuperFakturaAPIMissingCredentialsException):
|
with pytest.raises(SuperFakturaAPIMissingCredentialsException):
|
||||||
SuperFakturaAPI()
|
SuperFakturaAPI()
|
||||||
|
|
||||||
|
|
||||||
def test_get(api):
|
def test_get(api):
|
||||||
with patch("requests.get") as mock_get:
|
with patch("requests.get") as mock_get:
|
||||||
mock_get.return_value.status_code = 200
|
mock_get.return_value.status_code = 200
|
||||||
@@ -32,12 +38,14 @@ def test_get(api):
|
|||||||
response = api.get("test_endpoint")
|
response = api.get("test_endpoint")
|
||||||
assert response == {"data": "test"}
|
assert response == {"data": "test"}
|
||||||
|
|
||||||
|
|
||||||
def test_get_failure(api):
|
def test_get_failure(api):
|
||||||
with patch("requests.get") as mock_get:
|
with patch("requests.get") as mock_get:
|
||||||
mock_get.return_value.status_code = 404
|
mock_get.return_value.status_code = 404
|
||||||
with pytest.raises(SuperFakturaAPIException):
|
with pytest.raises(SuperFakturaAPIException):
|
||||||
api.get("test_endpoint")
|
api.get("test_endpoint")
|
||||||
|
|
||||||
|
|
||||||
def test_download(api):
|
def test_download(api):
|
||||||
with patch("requests.get") as mock_get:
|
with patch("requests.get") as mock_get:
|
||||||
mock_get.return_value.status_code = 200
|
mock_get.return_value.status_code = 200
|
||||||
@@ -47,6 +55,7 @@ def test_download(api):
|
|||||||
api.download("test_endpoint", f)
|
api.download("test_endpoint", f)
|
||||||
mock_file().write.assert_called_once_with(b"test_content")
|
mock_file().write.assert_called_once_with(b"test_content")
|
||||||
|
|
||||||
|
|
||||||
def test_download_failure(api):
|
def test_download_failure(api):
|
||||||
with patch("requests.get") as mock_get:
|
with patch("requests.get") as mock_get:
|
||||||
mock_get.return_value.status_code = 404
|
mock_get.return_value.status_code = 404
|
||||||
@@ -55,6 +64,7 @@ def test_download_failure(api):
|
|||||||
with pytest.raises(SuperFakturaAPIException):
|
with pytest.raises(SuperFakturaAPIException):
|
||||||
api.download("test_endpoint", f)
|
api.download("test_endpoint", f)
|
||||||
|
|
||||||
|
|
||||||
def test_post(api):
|
def test_post(api):
|
||||||
with patch("requests.post") as mock_post:
|
with patch("requests.post") as mock_post:
|
||||||
mock_post.return_value.status_code = 200
|
mock_post.return_value.status_code = 200
|
||||||
@@ -62,6 +72,7 @@ def test_post(api):
|
|||||||
response = api.post("test_endpoint", '{"name": "Example"}')
|
response = api.post("test_endpoint", '{"name": "Example"}')
|
||||||
assert response == {"data": "test"}
|
assert response == {"data": "test"}
|
||||||
|
|
||||||
|
|
||||||
def test_post_failure(api):
|
def test_post_failure(api):
|
||||||
with patch("requests.post") as mock_post:
|
with patch("requests.post") as mock_post:
|
||||||
mock_post.return_value.status_code = 404
|
mock_post.return_value.status_code = 404
|
||||||
@@ -69,15 +80,21 @@ def test_post_failure(api):
|
|||||||
with pytest.raises(SuperFakturaAPIException):
|
with pytest.raises(SuperFakturaAPIException):
|
||||||
api.post("test_endpoint", '{"name": "Example"}')
|
api.post("test_endpoint", '{"name": "Example"}')
|
||||||
|
|
||||||
|
|
||||||
def test_get_invalid_json(api):
|
def test_get_invalid_json(api):
|
||||||
with patch("requests.get") as mock_get:
|
with patch("requests.get") as mock_get:
|
||||||
mock_response = MagicMock()
|
mock_response = MagicMock()
|
||||||
mock_response.status_code = 200
|
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
|
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")
|
api.get("test_endpoint")
|
||||||
|
|
||||||
|
|
||||||
def test_download_not_writable_descriptor(api):
|
def test_download_not_writable_descriptor(api):
|
||||||
with patch("requests.get") as mock_get:
|
with patch("requests.get") as mock_get:
|
||||||
mock_get.return_value.status_code = 200
|
mock_get.return_value.status_code = 200
|
||||||
|
|||||||
Reference in New Issue
Block a user