From b724e5ea498100022e416d5b2df69993b3fa2019 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Sun, 21 Jul 2024 09:03:33 +0545 Subject: [PATCH] test(custom-yaml): adds example request body and relevant test case for fmtm project extracts --- API/custom_exports.py | 22 ++++++++++++++++++++- src/validation/models.py | 2 +- tests/test_API.py | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/API/custom_exports.py b/API/custom_exports.py index 96d0cd96..089317a6 100644 --- a/API/custom_exports.py +++ b/API/custom_exports.py @@ -852,4 +852,24 @@ async def process_custom_requests_yaml( validated_data = DynamicCategoriesModel.model_validate(data) except ValidationError as e: raise HTTPException(status_code=422, detail=e.errors(include_url=False)) - return validated_data + + queue_name = validated_data.queue + if validated_data.queue != DEFAULT_QUEUE_NAME and user.role != UserRole.ADMIN.value: + raise HTTPException( + status_code=403, + detail=[{"msg": "Insufficient Permission to choose queue"}], + ) + validated_data.categories = [ + category for category in validated_data.categories if category + ] + if len(validated_data.categories) == 0: + raise HTTPException( + status_code=400, detail=[{"msg": "Categories can't be empty"}] + ) + task = process_custom_request.apply_async( + args=(validated_data.model_dump(),), + queue=queue_name, + track_started=True, + kwargs={"user": user.model_dump()}, + ) + return JSONResponse({"task_id": task.id, "track_link": f"/tasks/status/{task.id}/"}) diff --git a/src/validation/models.py b/src/validation/models.py index e0e8fa73..0af1a0c6 100644 --- a/src/validation/models.py +++ b/src/validation/models.py @@ -590,7 +590,7 @@ class CategoriesBase(BaseModel): }, "types": ["lines", "polygons"], "select": ["name", "highway"], - "where": "highway IS NOT NULL", + "where": "tags['highway'] IS NOT NULL", "formats": ["geojson"], } } diff --git a/tests/test_API.py b/tests/test_API.py index 09db05d9..d2b90db8 100644 --- a/tests/test_API.py +++ b/tests/test_API.py @@ -1320,6 +1320,48 @@ def test_full_hdx_set_iso(): wait_for_task_completion(track_link) +### Custom Snapshot Using YAML + + +def test_custom_yaml_normal_fmtm_request(): + headers = {"access-token": access_token} + payload = """ + dataset: + dataset_folder: FMTM + dataset_prefix: hotosm_fmtm_project_1 + dataset_title: Field Mapping Tasking Manger Project 1 + categories: + - Buildings: + formats: + - geojson + - fgb + select: + - name + - name:en + types: + - polygons + where: tags['building'] IS NOT NULL + geometry: { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }""" + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + wait_for_task_completion(track_link) + + # ## Tasks connection