-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #474 from koukihai/koukihai/mock-requests
tests: Mock requests et DB
- Loading branch information
Showing
6 changed files
with
184 additions
and
103 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,41 @@ | ||
name: Run Pytests | ||
|
||
on: | ||
# Run workflow automatically whenever the workflow, app or tests are updated | ||
push: | ||
paths: | ||
- .github/workflows/pytest.yaml # Assuming this file is stored in .github/workflows/pytest.yaml | ||
- src/** | ||
- tests/** | ||
|
||
jobs: | ||
pytest: | ||
name: Run pytests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: generate FR locale | ||
run: sudo locale-gen fr_FR.UTF-8 | ||
- uses: actions/checkout@v4 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.12' | ||
cache: 'pip' | ||
- name: Lint with Ruff # Running an optional linter step | ||
run: | | ||
pip install ruff | ||
ruff --output-format=github src/ | ||
continue-on-error: true | ||
- name: Install application dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r src/requirements.txt | ||
- name: Test with pytest | ||
run: | | ||
pip install pytest pytest-cov pytest-mock requests-mock | ||
pytest --cov=src/ --cov-report=xml | ||
- name: Upload coverage reports to Codecov # Optional: Run codecov. Requires: secrets.CODECOV_TOKEN | ||
uses: codecov/codecov-action@v3 | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
continue-on-error: true |
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
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
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,86 @@ | ||
import pytest | ||
|
||
from db_schema import UsagePoints | ||
from test_jobs import job | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"status_response, status_code", | ||
[ | ||
({"incomplete": "response"}, 200), | ||
({"detail": "truthy response"}, 300), | ||
({"detail": "falsy response"}, 500), | ||
({ | ||
"consent_expiration_date": "2099-01-01T00:00:00", | ||
"call_number": 42, | ||
"quota_limit": 42, | ||
"quota_reached": 42, | ||
"quota_reset_at": "2099-01-01T00:00:00.000000", | ||
"ban": False | ||
}, 200)], | ||
) | ||
def test_get_account_status(mocker, job, caplog, status_response, status_code, requests_mock): | ||
from config import URL | ||
|
||
m_set_error_log = mocker.patch("models.database.Database.set_error_log") | ||
m_usage_point_update = mocker.patch('models.database.Database.usage_point_update') | ||
mocker.patch("models.jobs.Job.header_generate") | ||
requests_mocks = list() | ||
|
||
if job.usage_point_id: | ||
rm = requests_mock.get(f"{URL}/valid_access/{job.usage_point_id}/cache", json=status_response, | ||
status_code=status_code) | ||
requests_mocks.append(rm) | ||
expected_count = 1 | ||
# FIXME: If job has usage_point_id, get_account_status() expects | ||
# job.usage_point_config.usage_point_id to be populated from a side effect | ||
job.usage_point_config = UsagePoints(usage_point_id=job.usage_point_id) | ||
enabled_usage_points = [job.usage_point_config] | ||
else: | ||
enabled_usage_points = [up for up in job.usage_points if up.enable] | ||
for u in enabled_usage_points: | ||
rm = requests_mock.get(f"{URL}/valid_access/{u.usage_point_id}/cache", json=status_response, | ||
status_code=status_code) | ||
requests_mocks.append(rm) | ||
expected_count = len(enabled_usage_points) | ||
|
||
res = job.get_account_status() | ||
|
||
assert "INFO root:dependencies.py:88 [PDL1] RÉCUPÉRATION DES INFORMATIONS DU COMPTE :" in caplog.text | ||
is_complete = {"consent_expiration_date", "call_number", "quota_limit", "quota_reached", "quota_reset_at", | ||
"ban"}.issubset(set(status_response.keys())) | ||
is_truthy_response = 200 <= status_code < 400 | ||
|
||
if is_truthy_response: | ||
if status_code != 200: | ||
# If the status code is truthy, but not 200, the contents of response['detail'] are logged | ||
assert f'ERROR root:query_status.py:75 {status_response["detail"]}\n' | ||
|
||
if not is_complete: | ||
# If some fields are missing from a truthy response, an exception is thrown and an error message is displayed | ||
assert "ERROR root:jobs.py:196 Erreur lors de la récupération des informations du compte" in caplog.text | ||
|
||
# db.usage_point_update is not called | ||
assert 0 == m_usage_point_update.call_count | ||
# FIXME: db.set_error_log is not called, because returned errors are missing status_code and description.detail | ||
assert 0 == m_set_error_log.call_count | ||
|
||
if is_complete and status_code == 200: | ||
# Successful case: db is updated & set_error_log is called with None | ||
assert 1 == m_usage_point_update.call_count | ||
assert expected_count == m_set_error_log.call_count | ||
for u in enabled_usage_points: | ||
m_set_error_log.assert_called_once_with(u.usage_point_id, None) | ||
|
||
if not is_truthy_response: | ||
# FIXME: If response(500), no error is displayed | ||
assert "ERROR root:jobs.py:196 Erreur lors de la récupération des informations du compte" not in caplog.text | ||
# db.set_error_log is called | ||
assert expected_count == m_set_error_log.call_count | ||
for u in enabled_usage_points: | ||
m_set_error_log.assert_called_once_with(u.usage_point_id, f"{status_code} - {status_response['detail']}") | ||
|
||
# Ensuring {URL}/valid_access/{usage_point_id} is called exactly as many times as enabled usage_points | ||
# and only once per enabled usage_point | ||
for rm in requests_mocks: | ||
assert len(rm.request_history) == 1 |
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,25 @@ | ||
import pytest | ||
from test_jobs import job | ||
|
||
|
||
@pytest.mark.parametrize("response, status_code", [ | ||
(None, 200), | ||
(None, 500), | ||
({"mock": "response"}, 200) | ||
]) | ||
def test_get_gateway_status(job, caplog, requests_mock, response, status_code): | ||
from config import URL | ||
requests_mock.get(f"{URL}/ping", json=response, status_code=status_code) | ||
|
||
job.get_gateway_status() | ||
|
||
assert 'INFO root:dependencies.py:88 RÉCUPÉRATION DU STATUT DE LA PASSERELLE :\n' in caplog.text | ||
if status_code != 200: | ||
# FIXME: No error is displayed | ||
assert 'ERROR root:jobs.py:170 Erreur lors de la récupération du statut de la passerelle :\n' not in caplog.text | ||
|
||
if status_code == 200: | ||
if response: | ||
assert 'ERROR root:jobs.py:170 Erreur lors de la récupération du statut de la passerelle :\n' not in caplog.text | ||
else: | ||
assert 'ERROR root:jobs.py:170 Erreur lors de la récupération du statut de la passerelle :\n' in caplog.text |
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