Skip to content

Commit dcf8dc1

Browse files
committed
fix: fix geogrpahy filter on family search
1 parent 5061ac3 commit dcf8dc1

File tree

4 files changed

+70
-34
lines changed

4 files changed

+70
-34
lines changed

app/repository/family.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from db_client.models.organisation.users import Organisation
2323
from sqlalchemy import Column, and_
2424
from sqlalchemy import delete as db_delete
25-
from sqlalchemy import desc, func, or_
25+
from sqlalchemy import desc, or_
2626
from sqlalchemy import update as db_update
2727
from sqlalchemy.exc import NoResultFound, OperationalError
2828
from sqlalchemy.orm import Query, Session
@@ -34,45 +34,39 @@
3434

3535
_LOGGER = logging.getLogger(__name__)
3636

37-
FamilyGeoMetaOrg = Tuple[Family, str, FamilyMetadata, Corpus, Organisation]
37+
FamilyGeoMetaOrg = Tuple[Family, Geography, FamilyMetadata, Corpus, Organisation]
3838

3939

4040
def _get_query(db: Session) -> Query:
4141
# NOTE: SqlAlchemy will make a complete hash of query generation
4242
# if columns are used in the query() call. Therefore, entire
4343
# objects are returned.
44-
geo_subquery = (
45-
db.query(
46-
func.min(Geography.value).label("value"),
47-
FamilyGeography.family_import_id,
48-
)
49-
.join(FamilyGeography, FamilyGeography.geography_id == Geography.id)
50-
.filter(FamilyGeography.family_import_id == Family.import_id)
51-
.group_by(Geography.value, FamilyGeography.family_import_id)
52-
).subquery("geo_subquery")
53-
5444
return (
55-
db.query(Family, geo_subquery.c.value, FamilyMetadata, Corpus, Organisation) # type: ignore
45+
db.query(Family, Geography, FamilyMetadata, Corpus, Organisation)
46+
.join(FamilyGeography, FamilyGeography.family_import_id == Family.import_id)
47+
.join(
48+
Geography,
49+
Geography.id == FamilyGeography.geography_id,
50+
)
5651
.join(FamilyMetadata, FamilyMetadata.family_import_id == Family.import_id)
5752
.join(FamilyCorpus, FamilyCorpus.family_import_id == Family.import_id)
5853
.join(Corpus, Corpus.import_id == FamilyCorpus.corpus_import_id)
5954
.join(Organisation, Corpus.organisation_id == Organisation.id)
60-
.filter(geo_subquery.c.family_import_id == Family.import_id) # type: ignore
6155
)
6256

6357

6458
def _family_to_dto(
6559
db: Session, fam_geo_meta_corp_org: FamilyGeoMetaOrg
6660
) -> FamilyReadDTO:
6761
fam, geo_value, meta, corpus, org = fam_geo_meta_corp_org
68-
6962
metadata = cast(dict, meta.value)
7063
org = cast(str, org.name)
64+
7165
return FamilyReadDTO(
7266
import_id=str(fam.import_id),
7367
title=str(fam.title),
7468
summary=str(fam.description),
75-
geography=geo_value,
69+
geography=str(geo_value.value),
7670
category=str(fam.family_category),
7771
status=str(fam.family_status),
7872
metadata=metadata,

tests/integration_tests/family/test_search.py

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@
77
from tests.integration_tests.setup_db import setup_db
88

99

10+
def test_search_geographies(
11+
client: TestClient, data_db: Session, superuser_header_token
12+
):
13+
setup_db(data_db)
14+
15+
# search for where we know there is 2 families with a geopraphy
16+
response = client.get(
17+
"/api/v1/families/?geography=afghanistan",
18+
headers=superuser_header_token,
19+
)
20+
assert response.status_code == status.HTTP_200_OK
21+
data = response.json()
22+
assert isinstance(data, list)
23+
24+
ids_found = set([f["import_id"] for f in data])
25+
assert len(ids_found) == 2
26+
27+
# search for where we know there is 1 families with a geopraphy
28+
response = client.get(
29+
"/api/v1/families/?geography=zimbabwe",
30+
headers=superuser_header_token,
31+
)
32+
assert response.status_code == status.HTTP_200_OK
33+
data = response.json()
34+
assert isinstance(data, list)
35+
36+
ids_found = set([f["import_id"] for f in data])
37+
assert len(ids_found) == 1
38+
39+
1040
def test_search_family_super(
1141
client: TestClient, data_db: Session, superuser_header_token
1242
):

tests/integration_tests/family/test_update.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def test_update_family_collections_to_one_that_does_not_exist(
232232
assert db_family.description == ""
233233

234234
expected_geo = (
235-
data_db.query(Geography).filter(Geography.display_value == "Other").one()
235+
data_db.query(Geography).filter(Geography.display_value == "Afghanistan").one()
236236
)
237237
assert expected_geo.id in [g.id for g in db_family.geographies]
238238
assert db_family.family_category == "UNFCCC"
@@ -279,7 +279,11 @@ def test_update_fails_family_when_user_org_different_to_family_org(
279279
assert db_family.description == "apple"
280280
assert db_family.family_category == "UNFCCC"
281281

282-
geo_id = data_db.query(Geography.id).filter(Geography.value == "Other").scalar()
282+
geo_id = (
283+
data_db.query(Geography.id)
284+
# TODO: PDCT-1406: Properly implement multi-geography support
285+
.filter(Geography.value == db_family.geographies[0].value).scalar()
286+
)
283287
assert geo_id in [g.id for g in db_family.geographies]
284288

285289

@@ -355,7 +359,11 @@ def test_update_family_when_collection_org_different_to_family_org(
355359
assert db_family.description == ""
356360
assert db_family.family_category == "UNFCCC"
357361

358-
geo_id = data_db.query(Geography.id).filter(Geography.value == "Other").scalar()
362+
geo_id = (
363+
data_db.query(Geography.id)
364+
# TODO: PDCT-1406: Properly implement multi-geography support
365+
.filter(Geography.value == db_family.geographies[0].value).scalar()
366+
)
359367
assert geo_id in [g.id for g in db_family.geographies]
360368

361369

@@ -375,28 +383,32 @@ def test_update_family_idempotent_when_ok(
375383
client: TestClient, data_db: Session, user_header_token
376384
):
377385
setup_db(data_db)
378-
family = EXPECTED_FAMILIES[1]
386+
expected_family = EXPECTED_FAMILIES[1]
379387
response = client.put(
380-
f"/api/v1/families/{family['import_id']}",
381-
json=family,
388+
f"/api/v1/families/{expected_family['import_id']}",
389+
json=expected_family,
382390
headers=user_header_token,
383391
)
384392
assert response.status_code == status.HTTP_200_OK
385393
data = response.json()
386-
assert data["title"] == EXPECTED_FAMILIES[1]["title"]
387-
assert data["summary"] == EXPECTED_FAMILIES[1]["summary"]
388-
assert data["geography"] == EXPECTED_FAMILIES[1]["geography"]
389-
assert data["category"] == EXPECTED_FAMILIES[1]["category"]
394+
assert data["title"] == expected_family["title"]
395+
assert data["summary"] == expected_family["summary"]
396+
assert data["geography"] == expected_family["geography"]
397+
assert data["category"] == expected_family["category"]
390398
db_family: Family = (
391399
data_db.query(Family)
392-
.filter(Family.import_id == EXPECTED_FAMILIES[1]["import_id"])
400+
.filter(Family.import_id == expected_family["import_id"])
393401
.one()
394402
)
395-
assert db_family.title == EXPECTED_FAMILIES[1]["title"]
396-
assert db_family.description == EXPECTED_FAMILIES[1]["summary"]
397-
assert db_family.family_category == EXPECTED_FAMILIES[1]["category"]
403+
assert db_family.title == expected_family["title"]
404+
assert db_family.description == expected_family["summary"]
405+
assert db_family.family_category == expected_family["category"]
398406

399-
geo_id = data_db.query(Geography.id).filter(Geography.value == "Other").scalar()
407+
geo_id = (
408+
data_db.query(Geography.id)
409+
.filter(Geography.value == expected_family["geography"])
410+
.scalar()
411+
)
400412
assert geo_id in [g.id for g in db_family.geographies]
401413

402414

tests/integration_tests/setup_db.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"import_id": "A.0.0.1",
3232
"title": "apple",
3333
"summary": "",
34-
"geography": "Other",
34+
"geography": "AFG",
3535
"category": "UNFCCC",
3636
"status": "Created",
3737
"metadata": {
@@ -57,7 +57,7 @@
5757
"import_id": "A.0.0.2",
5858
"title": "apple orange banana",
5959
"summary": "apple",
60-
"geography": "Other",
60+
"geography": "ZWE",
6161
"category": "UNFCCC",
6262
"status": "Created",
6363
"metadata": {
@@ -83,7 +83,7 @@
8383
"import_id": "A.0.0.3",
8484
"title": "title",
8585
"summary": "orange peas",
86-
"geography": "Other",
86+
"geography": "AFG",
8787
"category": "UNFCCC",
8888
"status": "Created",
8989
"metadata": {"author": "CPR", "author_type": "Party"},

0 commit comments

Comments
 (0)