-
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.
test: write tests for search formation + onisep api call
- Loading branch information
1 parent
1310f1b
commit 02d9a86
Showing
5 changed files
with
144 additions
and
20 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
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
60 changes: 60 additions & 0 deletions
60
src/business_logic/formation/scrap/tests/unit/test_connect_to_onisep_api.py
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,60 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from src.business_logic.formation.exceptions import NoOnisepAPIException | ||
from src.business_logic.formation.scrap.utils.get_onisep_data import get_raw_data | ||
|
||
from src.constants.http_status_codes import ( | ||
HTTP_200_OK, | ||
HTTP_401_UNAUTHORIZED, | ||
HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mock_onisep_request(): | ||
with patch( | ||
"src.business_logic.formation.scrap.utils.get_onisep_data.requests.get" | ||
) as mock_requests_get: | ||
yield mock_requests_get | ||
|
||
|
||
def test_get_onisep_data_successful(mock_onisep_request): | ||
# Arrange | ||
mock_response = MagicMock() | ||
mock_response.status_code = HTTP_200_OK | ||
mock_response.json.return_value = {"total": "5173"} | ||
mock_onisep_request.return_value = mock_response | ||
# Act | ||
result = get_raw_data("SHR") | ||
# Assert | ||
assert result == {"total": "5173"} | ||
|
||
|
||
def test_get_onisep_data_retry_after_unauthorized(mock_onisep_request): | ||
# Arrange | ||
unauthorized_response = MagicMock() | ||
unauthorized_response.status_code = HTTP_401_UNAUTHORIZED | ||
authorized_response = MagicMock() | ||
authorized_response.status_code = HTTP_200_OK | ||
authorized_response.json.return_value = {"total": "5173"} | ||
|
||
# Set up the responses for the two requests | ||
mock_onisep_request.side_effect = [unauthorized_response, authorized_response] | ||
|
||
# Act | ||
result = get_raw_data("SHR") | ||
|
||
# Assert | ||
assert result == {"total": "5173"} | ||
|
||
|
||
def test_get_onisep_data_raises_exception(mock_onisep_request): | ||
# Arrange | ||
mock_response = MagicMock() | ||
mock_response.status_code = HTTP_500_INTERNAL_SERVER_ERROR | ||
mock_onisep_request.return_value = mock_response | ||
|
||
# Act and Assert | ||
with pytest.raises(NoOnisepAPIException): | ||
get_raw_data("SHR") |
57 changes: 57 additions & 0 deletions
57
src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py
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,57 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from src.business_logic.formation.scrap.search_formation import search_formations | ||
from src.business_logic.formation.scrap.types import ( | ||
FormationsWithTotal, | ||
) | ||
from src.business_logic.formation.scrap.utils.format_formations import format_formations | ||
|
||
|
||
MOKED_RESEARCH = { | ||
"total": 5754, | ||
"size": 1, | ||
"results": [ | ||
{ | ||
"code_nsf": "334", | ||
"sigle_type_formation": "", | ||
"libelle_type_formation": "baccalauréat technologique", | ||
"libelle_formation_principal": "bac techno STHR Sciences et technologies de l'hôtellerie et de la restauration", | ||
"sigle_formation": "STHR", | ||
"duree": "1 an", | ||
"niveau_de_sortie_indicatif": "Bac ou équivalent", | ||
"code_rncp": "", | ||
"niveau_de_certification": "4", | ||
"libelle_niveau_de_certification": "niveau 4 (bac ou équivalent)", | ||
"tutelle": "Ministère chargé de l'Éducation nationale et de la Jeunesse", | ||
"url_et_id_onisep": "http://www.onisep.fr/http/redirection/formation/slug/FOR.494", | ||
"domainesous-domaine": "hôtellerie-restauration, tourisme/hôtellerie | hôtellerie-restauration, tourisme/restauration", | ||
} | ||
], | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def mock_search_formations(): | ||
with patch( | ||
"src.business_logic.formation.scrap.search_formation.get_raw_data" | ||
) as mock_get_raw_data: | ||
yield mock_get_raw_data | ||
|
||
|
||
def test_search_formations_successful(mock_search_formations): | ||
# Arrange | ||
mock_search_formations.return_value = MOKED_RESEARCH | ||
|
||
# Act | ||
formations = search_formations("STHR", 1) | ||
|
||
# Assert | ||
moked_formation = MOKED_RESEARCH["results"] | ||
waited_formations = format_formations(moked_formation) | ||
waited_result = FormationsWithTotal( | ||
total=MOKED_RESEARCH["total"], formations=waited_formations | ||
) | ||
|
||
assert formations == waited_result | ||
mock_search_formations.assert_called_once_with("STHR", 1, None) |
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