Skip to content

Commit

Permalink
Merge branch 'feature/unit-tests' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
M4RC0Sx committed Aug 17, 2024
2 parents 351d1c4 + e538455 commit e68e6d8
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 4 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: test
on:
push:
branches:
- develop
pull_request:
types:
- opened
- synchronize

jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
#----------------------------------------------
# Check-out repo
#----------------------------------------------
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

#----------------------------------------------
# Install Python and Poetry
# It uses .python-version file
#----------------------------------------------
- name: Set up Python
uses: actions/setup-python@v5
id: setup-python
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.3
virtualenvs-create: true
virtualenvs-in-project: false
virtualenvs-path: ~/venvs
installer-parallel: true

#----------------------------------------------
# Cache venv
#----------------------------------------------
- name: Cache venv
uses: actions/cache@v4
id: cached-poetry-dependencies
with:
path: ~/venvs
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

#----------------------------------------------
# Install dependencies if cache miss
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

#----------------------------------------------
# Run tests
#----------------------------------------------
- name: Run tests
run: poetry run pytest

2 changes: 1 addition & 1 deletion esiosapy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def raw_request(self, url: str, headers: Dict[str, str] = {}) -> requests.Respon
if urlparse(url).netloc == "":
url = urljoin(self.base_url, url)

return requests.get(url, headers={"Accept": "application/json"})
return requests.get(url, headers=headers)
105 changes: 103 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ ruff = "^0.1.12"
mypy = "^1.8.0"
types-requests = "^2.31.0.20240106"


[tool.poetry.group.test.dependencies]
pytest = "^8.3.2"
pytest-mock = "^3.14.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand All @@ -42,4 +47,7 @@ disallow_untyped_defs = true
[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_required_dynamic_aliases = true

[tool.pytest.ini_options]
testpaths = ["tests"]
Empty file added tests/__init__.py
Empty file.
96 changes: 96 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pytest
from pytest_mock import MockerFixture
from typing import Dict
from urllib.parse import urljoin
import requests
from esiosapy.client import ESIOSAPYClient
from esiosapy.managers.archive_manager import ArchiveManager
from esiosapy.managers.indicator_manager import IndicatorManager
from esiosapy.managers.offer_indicator_manager import OfferIndicatorManager
from esiosapy.utils.request_helper import RequestHelper


class TestESIOSAPYClient:
@pytest.fixture
def esios_client(self, mocker: MockerFixture) -> ESIOSAPYClient:
mock_request_helper = mocker.patch(
"esiosapy.client.RequestHelper", autospec=True
)
mock_archive_manager = mocker.patch(
"esiosapy.client.ArchiveManager", autospec=True
)
mock_indicator_manager = mocker.patch(
"esiosapy.client.IndicatorManager", autospec=True
)
mock_offer_indicator_manager = mocker.patch(
"esiosapy.client.OfferIndicatorManager", autospec=True
)

mock_add_default_headers = mock_request_helper.return_value.add_default_headers

def add_default_headers_side_effect(headers: Dict[str, str]) -> Dict[str, str]:
default_headers = {
"Accept": "application/json; application/vnd.esios-api-v1+json",
"Content-Type": "application/json",
"x-api-key": "test-token",
}
default_headers.update(headers)
return default_headers

mock_add_default_headers.side_effect = add_default_headers_side_effect

return ESIOSAPYClient(token="test-token", base_url="https://api.example.com")

def test_initialization(
self, esios_client: ESIOSAPYClient, mocker: MockerFixture
) -> None:
assert esios_client.token == "test-token"
assert esios_client.base_url == "https://api.example.com"
assert isinstance(esios_client.request_helper, RequestHelper)
assert isinstance(esios_client.archives, ArchiveManager)
assert isinstance(esios_client.indicators, IndicatorManager)
assert isinstance(esios_client.offer_indicators, OfferIndicatorManager)

def test_raw_request_with_absolute_url(
self, esios_client: ESIOSAPYClient, mocker: MockerFixture
) -> None:
mock_get = mocker.patch("requests.get", autospec=True)
mock_response = mocker.Mock()
mock_get.return_value = mock_response

url: str = "https://api.example.com/data"
headers: Dict[str, str] = {"Custom-Header": "value"}

response: requests.Response = esios_client.raw_request(url, headers)

expected_headers: Dict[str, str] = {
"Accept": "application/json; application/vnd.esios-api-v1+json",
"Content-Type": "application/json",
"x-api-key": "test-token",
"Custom-Header": "value",
}

mock_get.assert_called_once_with(url, headers=expected_headers)
assert response == mock_response

def test_raw_request_with_relative_url(
self, esios_client: ESIOSAPYClient, mocker: MockerFixture
) -> None:
mock_get = mocker.patch("requests.get", autospec=True)
mock_response = mocker.Mock()
mock_get.return_value = mock_response

url: str = "/data"
headers: Dict[str, str] = {}

expected_url: str = urljoin(esios_client.base_url, url)
expected_headers: Dict[str, str] = {
"Accept": "application/json; application/vnd.esios-api-v1+json",
"Content-Type": "application/json",
"x-api-key": "test-token",
}

response: requests.Response = esios_client.raw_request(url, headers)

mock_get.assert_called_once_with(expected_url, headers=expected_headers)
assert response == mock_response
Loading

0 comments on commit e68e6d8

Please sign in to comment.