-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/unit-tests' into develop
- Loading branch information
Showing
7 changed files
with
381 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.