mirror of
https://github.com/Eledio/superfaktura-client.git
synced 2025-11-01 00:18:25 +01:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adc2fa4aac | ||
|
|
3d4400db2d | ||
|
|
f40b015beb | ||
|
|
964882bc5d | ||
|
|
c861f35ad2 | ||
|
|
133702dc1d | ||
|
|
765334ed92 | ||
|
|
b41bf28a34 | ||
|
|
8d3b2589b0 | ||
|
|
d2cbbd0596 | ||
|
|
45259f8f27 | ||
|
|
5b50f2c3eb | ||
|
|
16cb1b39ba | ||
|
|
e2f89cf754 | ||
|
|
886004a20a | ||
|
|
e668f76b4c |
2
.github/workflows/pylint.yml
vendored
2
.github/workflows/pylint.yml
vendored
@@ -20,7 +20,7 @@ jobs:
|
|||||||
pip install pylint
|
pip install pylint
|
||||||
- name: pylint
|
- name: pylint
|
||||||
run: |
|
run: |
|
||||||
pylint $(git ls-files '*.py') --fail-under=9.0 --output-format=json > pylint-report.json
|
pylint $(git ls-files '*.py' | grep -v '^test/') --fail-under=9.0 --output-format=json > pylint-report.json
|
||||||
|
|
||||||
- name: Generate Pylint Score
|
- name: Generate Pylint Score
|
||||||
id: pylint_score
|
id: pylint_score
|
||||||
|
|||||||
29
.github/workflows/pytest.yml
vendored
Normal file
29
.github/workflows/pytest.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
name: PyTest
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v5
|
||||||
|
with:
|
||||||
|
python-version: '3.10'
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements_dev.txt
|
||||||
|
- name: Test with pytest
|
||||||
|
run: |
|
||||||
|
mkdir -p junit
|
||||||
|
pytest test/ --doctest-modules --junitxml=junit/test-results.xml --cov=superfaktura --cov-report=xml --cov-report=html
|
||||||
|
- name: Upload pytest test results
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: pytest-results
|
||||||
|
path: junit/test-results.xml
|
||||||
|
# Use always() to always run this step to publish test results when there are test failures
|
||||||
|
if: ${{ always() }}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
requests~=2.32.3
|
requests~=2.32.3
|
||||||
python-dotenv~=1.0.1
|
python-dotenv~=1.0.1
|
||||||
|
setuptools~=75.8.0
|
||||||
4
requirements_dev.txt
Normal file
4
requirements_dev.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
requests~=2.32.3
|
||||||
|
python-dotenv~=1.0.1
|
||||||
|
pytest~=8.3.5
|
||||||
|
pytest-cov
|
||||||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
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)
|
||||||
105
test/test_superfaktura_api.py
Normal file
105
test/test_superfaktura_api.py
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import patch, mock_open, MagicMock
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
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()):
|
||||||
|
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"}')
|
||||||
|
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
||||||
|
def test_download_not_writable_descriptor(api):
|
||||||
|
with patch("requests.get") as mock_get:
|
||||||
|
mock_get.return_value.status_code = 200
|
||||||
|
mock_get.return_value.content = b"test_content"
|
||||||
|
mock_descriptor = MagicMock()
|
||||||
|
mock_descriptor.writable.return_value = False
|
||||||
|
with pytest.raises(SuperFakturaAPIException, match=" is not writable"):
|
||||||
|
api.download("test_endpoint", mock_descriptor)
|
||||||
Reference in New Issue
Block a user