-
Notifications
You must be signed in to change notification settings - Fork 5
🔍 Ajout du filtre objet dans l'API Rest #1556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3c50f07
force p2p
kolok c3c2e5e
Add tests for REST API
fabienheureux c8dd8b5
Add file
fabienheureux 8bf15fc
Update tests
fabienheureux 023ed52
Merge branch 'add-tests-for-rest-api' into add-object-filter-rest-api
fabienheureux 74907f4
Add filter
fabienheureux 06b31d2
Add fixtures to tests
fabienheureux 5cdef65
Add inactif acteur test
fabienheureux 90414ea
add mock
fabienheureux 7022a8b
add mock
fabienheureux 6c7d592
finalize tests
fabienheureux 86f1583
Merge branch 'main' into add-object-filter-rest-api
fabienheureux cf8a86e
Merge branch 'main' into add-object-filter-rest-api
fabienheureux 193f34a
Update qfdmo/api.py
fabienheureux b45c90f
Merge branch 'main' into add-object-filter-rest-api
fabienheureux 5af6442
Merge branch 'main' into add-object-filter-rest-api
fabienheureux 55e3e41
Merge branch 'main' into add-object-filter-rest-api
fabienheureux ec31d03
Merge branch 'main' into add-object-filter-rest-api
fabienheureux d6871b3
Update pyproject.toml
fabienheureux d8c19b2
Merge branch 'main' into add-object-filter-rest-api
fabienheureux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,185 @@ | ||
from urllib.parse import urlencode | ||
|
||
import pytest | ||
from django.contrib.gis.geos import Point | ||
from django.core.management import call_command | ||
|
||
from qfdmo.models.acteur import ActeurService, ActeurStatus, DisplayedActeur | ||
from qfdmo.models.action import GroupeAction | ||
from qfdmo.models.categorie_objet import SousCategorieObjet | ||
from unit_tests.qfdmo.acteur_factory import ( | ||
DisplayedActeurFactory, | ||
DisplayedPropositionServiceFactory, | ||
) | ||
from unit_tests.qfdmo.sscatobj_factory import SousCategorieObjetFactory | ||
|
||
BASE_URL = "http://localhost:8000/api/qfdmo" | ||
|
||
|
||
# Fixtures | ||
# -------- | ||
@pytest.fixture | ||
def sous_categorie(): | ||
sous_categorie = SousCategorieObjetFactory() | ||
return sous_categorie | ||
|
||
|
||
@pytest.fixture | ||
def displayed_acteur(): | ||
DisplayedActeurFactory( | ||
pk="UN-ACTEUR", location=Point(2.3, 48.86), statut=ActeurStatus.ACTIF | ||
) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def django_db_setup(django_db_setup, django_db_blocker): | ||
with django_db_blocker.unblock(): | ||
call_command( | ||
"loaddata", | ||
"categories", | ||
"actions", | ||
"acteur_services", | ||
"acteur_types", | ||
) | ||
|
||
|
||
# Tests | ||
# ----- | ||
@pytest.mark.django_db | ||
def test_get_actions(client): | ||
"""Test the /actions endpoint""" | ||
response = client.get(f"{BASE_URL}/actions") | ||
assert response.status_code == 200 | ||
assert isinstance(response.json(), list) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_groupe_actions(client): | ||
"""Test the /actions/groupes endpoint""" | ||
response = client.get(f"{BASE_URL}/actions/groupes") | ||
assert response.status_code == 200 | ||
assert isinstance(response.json(), list) | ||
assert len(response.json()) == GroupeAction.objects.filter(afficher=True).count() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_acteurs(client, displayed_acteur): | ||
"""Test the /acteurs endpoint with filters""" | ||
params = { | ||
"latitude": 48.86, | ||
"longitude": 2.3, | ||
"rayon": 5, | ||
} | ||
response = client.get(f"{BASE_URL}/acteurs?{urlencode(params)}") | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert isinstance(data["items"], list) | ||
assert data["count"] > 0 | ||
|
||
returned_acteur = data["items"][0] | ||
assert "nom" in returned_acteur | ||
assert "adresse" in returned_acteur | ||
assert returned_acteur["identifiant_unique"] == "UN-ACTEUR" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_acteurs_sous_categorie_filter(client, displayed_acteur): | ||
"""Test the /acteurs endpoint with sous categorie filter""" | ||
acteur = DisplayedActeurFactory( | ||
pk="UN-AUTRE-ACTEUR", location=Point(2.3, 48.86), statut=ActeurStatus.ACTIF | ||
) | ||
sous_categorie = SousCategorieObjetFactory(id=666) | ||
proposition_service = DisplayedPropositionServiceFactory(acteur=acteur) | ||
proposition_service.sous_categories.add(sous_categorie) | ||
|
||
params = {"latitude": 48.86, "longitude": 2.3, "rayon": 5, "sous_categories": 666} | ||
response = client.get(f"{BASE_URL}/acteurs?{urlencode(params)}") | ||
data = response.json() | ||
|
||
assert SousCategorieObjet.objects.filter(id=666).exists() | ||
assert DisplayedActeur.objects.filter(statut=ActeurStatus.ACTIF).count() == 2 | ||
assert ( | ||
DisplayedActeur.objects.filter( | ||
statut=ActeurStatus.ACTIF, | ||
proposition_services__sous_categories__id__in=[666], | ||
).count() | ||
== 1 | ||
) | ||
assert data["count"] == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_acteurs_types(client): | ||
"""Test the /acteurs/types endpoint""" | ||
response = client.get(f"{BASE_URL}/acteurs/types") | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert isinstance(data, list) | ||
assert len(data) > 0 | ||
assert "code" in data[0] | ||
assert "id" in data[0] | ||
assert "libelle" in data[0] | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_acteurs_services(client): | ||
"""Test the /acteurs/services endpoint""" | ||
response = client.get(f"{BASE_URL}/acteurs/services") | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert isinstance(data, list) | ||
assert len(data) == ActeurService.objects.all().count() | ||
assert "code" in data[0] | ||
assert "id" in data[0] | ||
assert "libelle" in data[0] | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_acteur_by_identifiant(client): | ||
"""Test the /acteur endpoint""" | ||
identifiant_unique = "ACT12345" | ||
DisplayedActeurFactory(pk=identifiant_unique, statut=ActeurStatus.ACTIF) | ||
response = client.get( | ||
f"{BASE_URL}/acteur", query_params={"identifiant_unique": identifiant_unique} | ||
) | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert "nom" in data | ||
assert "nom_commercial" in data | ||
assert "siret" in data | ||
assert "adresse" in data | ||
assert data.get("identifiant_unique") == identifiant_unique | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_acteur_inacteur_returns_404(client): | ||
identifiant_unique = "INACTIF_ACT12345" | ||
DisplayedActeurFactory(pk=identifiant_unique, statut=ActeurStatus.INACTIF) | ||
response = client.get( | ||
f"{BASE_URL}/acteur", query_params={"identifiant_unique": identifiant_unique} | ||
) | ||
assert response.status_code == 404 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_autocomplete_epci(client, mocker): | ||
"""Test the /autocomplete/configurateur endpoint""" | ||
# Mock setup | ||
mock_formatted_epcis_list = mocker.patch("qfdmo.geo_api.formatted_epcis_list") | ||
mock_formatted_epcis_list.return_value = [ | ||
"200043123 - CC Auray Quiberon Terre Atlantique", | ||
"200043156 - CC du Pays Rethélois", | ||
] | ||
|
||
# Simulate a successful response | ||
response = client.get( | ||
f"{BASE_URL}/autocomplete/configurateur", query_params={"query": "Quiberon"} | ||
) | ||
|
||
# Assertions | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert isinstance(data, list) | ||
assert len(data) == 2 | ||
if data: | ||
assert "Quiberon" in data[0] |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.