-
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 * test: write tests for a auth user * feat: add types * test: Write first service test * Fix: CI errors
- Loading branch information
1 parent
1310f1b
commit f8945ae
Showing
9 changed files
with
263 additions
and
29 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/blueprints/tests/services/test_search_formations_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,42 @@ | ||
from unittest.mock import patch | ||
import pytest | ||
from src.business_logic.formation.scrap.tests.unit.test_search_onisep_formations import ( | ||
MOKED_RESEARCH, | ||
) | ||
from src.business_logic.formation.scrap.types import FormationIsFavorite | ||
from src.models.formation import Formation | ||
|
||
|
||
@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_no_authenticated_search_formation_api_should_return_formations( | ||
client, mock_search_formations | ||
): | ||
# Arrange | ||
mock_search_formations.return_value = MOKED_RESEARCH | ||
|
||
# When | ||
response = client.post( | ||
"/api/v1/formations/search", json={"query": "STH", "limit": 1} | ||
) | ||
|
||
result = response.json | ||
print(result) | ||
|
||
# Then | ||
formation_favorite = result["formations"][0] | ||
formation_favorite_instance = FormationIsFavorite(**formation_favorite) | ||
|
||
formation = formation_favorite["formation"] | ||
formation_instance = Formation(**formation) | ||
|
||
assert result["total"] == 5754 | ||
assert isinstance(formation_favorite_instance, FormationIsFavorite) | ||
assert formation_favorite["is_favorite"] is False | ||
assert isinstance(formation_instance, Formation) |
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") |
100 changes: 100 additions & 0 deletions
100
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,100 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from src.business_logic.formation.scrap.search_formation import ( | ||
auth_search_formations, | ||
search_formations, | ||
) | ||
from src.business_logic.formation.scrap.types import ( | ||
FormationsWithTotal, | ||
) | ||
from src.business_logic.formation.scrap.utils.format_formations import ( | ||
format_formation_with_is_favorite, | ||
format_formations, | ||
) | ||
from src.models.user import User | ||
from src.models.user_favori import UserFavori | ||
from src.tests.factories.factories import ( | ||
UserFactory, | ||
UserFavorisFactory, | ||
) | ||
|
||
|
||
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_should_return_formations_without_favorite( | ||
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) | ||
|
||
|
||
def test_authenticated_search_formations_should_return_formations_with_favorite( | ||
mock_search_formations, db_session | ||
): | ||
# Arrange | ||
mock_search_formations.return_value = MOKED_RESEARCH | ||
|
||
# Given | ||
user: User = UserFactory() | ||
db_session.add(user) | ||
db_session.flush() | ||
|
||
user_favori: UserFavori = UserFavorisFactory(user_id=user.id) | ||
db_session.add(user_favori) | ||
db_session.commit() | ||
|
||
# Act | ||
formations = auth_search_formations(user.id, "STHR", 1) | ||
|
||
# Assert | ||
moked_formation = MOKED_RESEARCH["results"] | ||
waited_formations = format_formation_with_is_favorite(user.id, 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
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