Skip to content

Commit

Permalink
test: write tests for search formation + onisep api call
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Jan 1, 2024
1 parent 1310f1b commit 02d9a86
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 20 deletions.
15 changes: 5 additions & 10 deletions src/business_logic/formation/scrap/get_main_formation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
format_formations,
)
from src.business_logic.formation.scrap.types import FormationsWithTotal
from src.business_logic.formation.scrap.utils.get_onisep_data import get_onisep_data


def _get_raw_main_formations(limit: int = 10, offset: int = None) -> dict:
params = f"/search?&size={limit}"
if offset:
params += f"&from={offset}"
return get_onisep_data(params)
from src.business_logic.formation.scrap.utils.get_onisep_data import (
get_raw_data,
)


def get_main_formations(limit: int = 10, offset: int = None) -> FormationsWithTotal:
data = _get_raw_main_formations(limit, offset)
data = get_raw_data(limit, offset)

formated_formations = format_formations(data["results"])

Expand All @@ -24,7 +19,7 @@ def get_main_formations(limit: int = 10, offset: int = None) -> FormationsWithTo
def auth_get_main_formations(
user_id: int, limit: int = 10, offset: int = None
) -> FormationsWithTotal:
data = _get_raw_main_formations(limit, offset)
data = get_raw_data(limit, offset)

formated_formations = format_formation_with_is_favorite(user_id, data["results"])

Expand Down
15 changes: 5 additions & 10 deletions src/business_logic/formation/scrap/search_formation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
format_formations,
)
from src.business_logic.formation.scrap.types import FormationsWithTotal
from src.business_logic.formation.scrap.utils.get_onisep_data import get_onisep_data


def _get_raw_search_data(query: str, limit: int, offset: int = None) -> dict:
params = f"/search?q={query}&size={limit}"
if offset:
params += f"&from={offset}"
return get_onisep_data(params)
from src.business_logic.formation.scrap.utils.get_onisep_data import (
get_raw_data,
)


def search_formations(
query: str, limit: int, offset: int = None
) -> FormationsWithTotal:
data = _get_raw_search_data(query, limit, offset)
data = get_raw_data(query, limit, offset)

formated_formations = format_formations(data["results"])

Expand All @@ -26,7 +21,7 @@ def search_formations(
def auth_search_formations(
user_id: int, query: str, limit: int, offset: int = None
) -> FormationsWithTotal:
data = _get_raw_search_data(query, limit, offset)
data = get_raw_data(query, limit, offset)
formated_formations = format_formation_with_is_favorite(user_id, data["results"])

return FormationsWithTotal(data["total"], formated_formations)
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")
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)
17 changes: 17 additions & 0 deletions src/business_logic/formation/scrap/utils/get_onisep_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ def get_onisep_data(params: str) -> dict:
raise NoOnisepAPIException(
f"\n status: {response.status_code} \n message : Onisep API is down. \n dataset : {DATASET} \n headers : {HEADERS} "
)


def get_raw_data(
query: str = None,
limit: int = 10,
offset: int = None,
is_main_formations: bool = True,
) -> dict:
if is_main_formations:
params = f"/search?&size={limit}"
else:
params = f"/search?q={query}&size={limit}"

if offset:
params += f"&from={offset}"

return get_onisep_data(params)

0 comments on commit 02d9a86

Please sign in to comment.