Skip to content

Commit

Permalink
Merge pull request #4393 from unicef/geo-api-search-and-filter
Browse files Browse the repository at this point in the history
[219912] Add all fields to country endpoint, modify tests
  • Loading branch information
domdinicola authored Nov 4, 2024
2 parents 0bc1efe + ef77baa commit cac3165
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
12 changes: 11 additions & 1 deletion src/hct_mis_api/api/endpoints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ class RejectPolicy(models.TextChoices):
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = ("name", "short_name", "iso_code2", "iso_code3", "iso_num")
fields = (
"id",
"name",
"short_name",
"iso_code2",
"iso_code3",
"iso_num",
"valid_from",
"valid_until",
"updated_at",
)
47 changes: 18 additions & 29 deletions tests/unit/api/test_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from hct_mis_api.api.models import Grant
from hct_mis_api.apps.geo.fixtures import AreaFactory, AreaTypeFactory, CountryFactory
from hct_mis_api.apps.geo.models import Area, AreaType
from hct_mis_api.apps.geo.models import Area, AreaType, Country
from hct_mis_api.apps.program.models import Program
from tests.unit.api.base import HOPEApiTestCase, token_grant_permission

Expand Down Expand Up @@ -53,27 +53,28 @@ def setUpTestData(cls) -> None:
cls.country_afghanistan.save(update_fields=["valid_from", "valid_until"])
cls.url = reverse("api:country-list")

def get_response(self, country: Country) -> dict:
return {
"id": str(country.id),
"name": country.name,
"short_name": country.short_name,
"iso_code2": country.iso_code2,
"iso_code3": country.iso_code3,
"iso_num": country.iso_num,
"updated_at": country.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"valid_from": country.valid_from.strftime("%Y-%m-%dT%H:%M:%SZ"),
"valid_until": country.valid_until.strftime("%Y-%m-%dT%H:%M:%SZ"),
}

def test_get_countries(self) -> None:
with token_grant_permission(self.token, Grant.API_READ_ONLY):
response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.json()["results"],
[
{
"name": self.country_afghanistan.name,
"short_name": self.country_afghanistan.short_name,
"iso_code2": self.country_afghanistan.iso_code2,
"iso_code3": self.country_afghanistan.iso_code3,
"iso_num": self.country_afghanistan.iso_num,
},
{
"name": self.country_poland.name,
"short_name": self.country_poland.short_name,
"iso_code2": self.country_poland.iso_code2,
"iso_code3": self.country_poland.iso_code3,
"iso_num": self.country_poland.iso_num,
},
self.get_response(self.country_afghanistan),
self.get_response(self.country_poland),
],
)
self.assertIn("count", response.json())
Expand All @@ -93,13 +94,7 @@ def test_get_countries_filter_valid_from_until(self) -> None:
self.assertEqual(len(response.json()["results"]), len(expected_result), filter_data)
for result in expected_result:
self.assertIn(
{
"name": result.name,
"short_name": result.short_name,
"iso_code2": result.iso_code2,
"iso_code3": result.iso_code3,
"iso_num": result.iso_num,
},
self.get_response(result),
response.json()["results"],
filter_data,
)
Expand All @@ -117,13 +112,7 @@ def test_get_countries_search(self) -> None:
self.assertEqual(response.status_code, status.HTTP_200_OK, filter_data)
self.assertEqual(len(response.json()["results"]), 1, filter_data)
self.assertIn(
{
"name": expected_result.name,
"short_name": expected_result.short_name,
"iso_code2": expected_result.iso_code2,
"iso_code3": expected_result.iso_code3,
"iso_num": expected_result.iso_num,
},
self.get_response(expected_result),
response.json()["results"],
filter_data,
)
Expand Down

0 comments on commit cac3165

Please sign in to comment.