From 8b73004023089e05028b9811626f798bda098795 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 12 Jan 2024 09:36:52 +0545 Subject: [PATCH 1/5] Adds task limit feature from API , Enhances query to union the feature passed --- API/api_worker.py | 22 +++++++++++++++++++--- docs/src/installation/configurations.md | 11 ++++++++--- src/config.py | 14 ++++++++++++++ src/query_builder/builder.py | 4 ++-- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/API/api_worker.py b/API/api_worker.py index 95d55c6e..b2112d4c 100644 --- a/API/api_worker.py +++ b/API/api_worker.py @@ -16,7 +16,13 @@ from src.config import ALLOW_BIND_ZIP_FILTER from src.config import CELERY_BROKER_URL as celery_broker_uri from src.config import CELERY_RESULT_BACKEND as celery_backend -from src.config import ENABLE_TILES +from src.config import ( + DEFAULT_HARD_TASK_LIMIT, + DEFAULT_SOFT_TASK_LIMIT, + ENABLE_TILES, + HDX_HARD_TASK_LIMIT, + HDX_SOFT_TASK_LIMIT, +) from src.config import USE_S3_TO_UPLOAD as use_s3_to_upload from src.config import logger as logging from src.query_builder.builder import format_file_name_str @@ -37,7 +43,12 @@ celery.conf.update(result_extended=True) -@celery.task(bind=True, name="process_raw_data") +@celery.task( + bind=True, + name="process_raw_data", + time_limit=DEFAULT_HARD_TASK_LIMIT, + soft_time_limit=DEFAULT_SOFT_TASK_LIMIT, +) def process_raw_data(self, params): params = RawDataCurrentParams(**params) try: @@ -195,7 +206,12 @@ def process_raw_data(self, params): raise ex -@celery.task(bind=True, name="process_custom_request") +@celery.task( + bind=True, + name="process_custom_request", + time_limit=HDX_HARD_TASK_LIMIT, + soft_time_limit=HDX_SOFT_TASK_LIMIT, +) def process_custom_request(self, params): params = DynamicCategoriesModel(**params) diff --git a/docs/src/installation/configurations.md b/docs/src/installation/configurations.md index 29481820..82f550c8 100644 --- a/docs/src/installation/configurations.md +++ b/docs/src/installation/configurations.md @@ -59,6 +59,8 @@ The following are the different configuration options that are accepted. | `ENABLE_POLYGON_STATISTICS_ENDPOINTS` | `ENABLE_POLYGON_STATISTICS_ENDPOINTS` | `[API_CONFIG]` | `False` | Option to enable endpoints related the polygon statistics about the approx buildings,road length in passed polygon| OPTIONAL | | `POLYGON_STATISTICS_API_URL` | `POLYGON_STATISTICS_API_URL` | `[API_CONFIG]` | `None` | API URL for the polygon statistics to fetch the metadata , Currently tested with graphql query endpoint of Kontour , Only required if it is enabled from ENABLE_POLYGON_STATISTICS_ENDPOINTS | OPTIONAL | | `POLYGON_STATISTICS_API_URL` | `POLYGON_STATISTICS_API_RATE_LIMIT` | `[API_CONFIG]` | `5` | Rate limit to be applied for statistics endpoint per minute, Defaults to 5 request is allowed per minute | OPTIONAL | +| `DEFAULT_SOFT_TASK_LIMIT` | `DEFAULT_SOFT_TASK_LIMIT` | `[API_CONFIG]` | `7200` | Soft task time limit signal for celery workers in seconds.It will gently remind celery to finish up the task and terminate, Defaults to 2 Hour| OPTIONAL | +| `DEFAULT_HARD_TASK_LIMIT` | `DEFAULT_HARD_TASK_LIMIT` | `[API_CONFIG]` | `10800` | Hard task time limit signal for celery workers in seconds. It will immediately kill the celery task.Defaults to 3 Hour| OPTIONAL | | `CELERY_BROKER_URL` | `CELERY_BROKER_URL` | `[CELERY]` | `redis://localhost:6379/0` | Redis connection string for the broker | OPTIONAL | | `CELERY_RESULT_BACKEND` | `CELERY_RESULT_BACKEND` | `[CELERY]` | `redis://localhost:6379/0` | Redis/psotgresql connection string for the the result backend, eg : db+postgresql://username:password@localhost:5432/db_name | OPTIONAL | | `FILE_UPLOAD_METHOD` | `FILE_UPLOAD_METHOD` | `[EXPORT_UPLOAD]` | `disk` | File upload method; Allowed values - disk, s3 | OPTIONAL | @@ -74,6 +76,8 @@ The following are the different configuration options that are accepted. | `HDX_MAINTAINER` | `HDX_MAINTAINER` | `[HDX]` | None | Your HDX Maintainer ID | CONDITIONAL | | `DUCK_DB_MEMORY_LIMIT` | `DUCK_DB_MEMORY_LIMIT` | `[HDX]` | None | Duck DB max memory limit , 80 % of your RAM eg : '5GB'| CONDITIONAL | | `DUCK_DB_THREAD_LIMIT` | `DUCK_DB_THREAD_LIMIT` | `[HDX]` | None | Duck DB max threads limit ,n of your cores eg : 2 | CONDITIONAL | +| `HDX_SOFT_TASK_LIMIT` | `HDX_SOFT_TASK_LIMIT` | `[HDX]` | `18000` | Soft task time limit signal for celery workers in seconds.It will gently remind celery to finish up the task and terminate, Defaults to 5 Hour| OPTIONAL | +| `HDX_HARD_TASK_LIMIT` | `HDX_HARD_TASK_LIMIT` | `[HDX]` | `21600` | Hard task time limit signal for celery workers in seconds. It will immediately kill the celery task.Defaults to 6 Hour| OPTIONAL | ## Which Service uses which settings? @@ -103,6 +107,8 @@ The following are the different configuration options that are accepted. | `ENABLE_POLYGON_STATISTICS_ENDPOINTS` | `[API_CONFIG]` | Yes | Yes | | `POLYGON_STATISTICS_API_URL` | `[API_CONFIG]` | Yes | Yes | | `POLYGON_STATISTICS_API_RATE_LIMIT` | `[API_CONFIG]` | Yes | No | +| `DEFAULT_SOFT_TASK_LIMIT` | `[API_CONFIG]` | No | Yes | +| `DEFAULT_HARD_TASK_LIMIT` | `[API_CONFIG]` | No | Yes | | `CELERY_BROKER_URL` | `[CELERY]` | Yes | Yes | | `CELERY_RESULT_BACKEND` | `[CELERY]` | Yes | Yes | | `FILE_UPLOAD_METHOD` | `[EXPORT_UPLOAD]` | Yes | Yes | @@ -118,9 +124,8 @@ The following are the different configuration options that are accepted. | `HDX_MAINTAINER` | `[HDX]` | Yes | Yes | | `DUCK_DB_MEMORY_LIMIT` | `[HDX]` | Yes | Yes | | `DUCK_DB_THREAD_LIMIT` | `[HDX]` | Yes | Yes | - - - +| `HDX_SOFT_TASK_LIMIT` | `[HDX]` | No | Yes | +| `HDX_HARD_TASK_LIMIT` | `[HDX]` | No | Yes | diff --git a/src/config.py b/src/config.py index ac9cdbcc..90a80c10 100644 --- a/src/config.py +++ b/src/config.py @@ -184,6 +184,20 @@ def not_raises(func, *args, **kwargs): "POLYGON_STATISTICS_API_RATE_LIMIT" ) or config.get("API_CONFIG", "POLYGON_STATISTICS_API_RATE_LIMIT", fallback=5) +DEFAULT_SOFT_TASK_LIMIT = os.environ.get("DEFAULT_SOFT_TASK_LIMIT") or config.get( + "API_CONFIG", "DEFAULT_SOFT_TASK_LIMIT", fallback=2 * 60 * 60 +) +DEFAULT_HARD_TASK_LIMIT = os.environ.get("DEFAULT_HARD_TASK_LIMIT") or config.get( + "API_CONFIG", "DEFAULT_HARD_TASK_LIMIT", fallback=3 * 60 * 60 +) + +HDX_SOFT_TASK_LIMIT = os.environ.get("HDX_SOFT_TASK_LIMIT") or config.get( + "HDX", "HDX_SOFT_TASK_LIMIT", fallback=5 * 60 * 60 +) +HDX_HARD_TASK_LIMIT = os.environ.get("HDX_HARD_TASK_LIMIT") or config.get( + "HDX", "HDX_HARD_TASK_LIMIT", fallback=6 * 60 * 60 +) + ENABLE_HDX_EXPORTS = os.environ.get("ENABLE_HDX_EXPORTS") or config.getboolean( "HDX", "ENABLE_HDX_EXPORTS", fallback=False ) diff --git a/src/query_builder/builder.py b/src/query_builder/builder.py index 4d3673c7..5b573d9a 100644 --- a/src/query_builder/builder.py +++ b/src/query_builder/builder.py @@ -97,7 +97,7 @@ def get_query_as_geojson(query_list, ogr_export=None): def create_geom_filter(geom, geom_lookup_by="ST_intersects"): """generates geometry intersection filter - Rawdata extraction""" geometry_dump = dumps(loads(geom.model_dump_json())) - return f"""{geom_lookup_by}(geom,ST_GEOMFROMGEOJSON('{geometry_dump}'))""" + return f"""{geom_lookup_by}(geom,ST_Buffer(ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{geometry_dump}'))),0.005))""" def format_file_name_str(input_str): @@ -898,7 +898,7 @@ def convert_tags_pattern(query_string): row_filter_condition = ( f"""(country <@ ARRAY [{cid}])""" if cid - else f"""(ST_within(geom,ST_makeValid(ST_GeomFromText('{wkt.dumps(loads(geometry.json()),decimals=6)}',4326))))""" + else f"""(ST_within(geom,ST_Buffer(ST_Union(ST_makeValid(ST_GeomFromText('{wkt.dumps(loads(geometry.json()),decimals=6)}',4326))),0.005)))""" ) if single_category_where: row_filter_condition += f" and ({convert_tags_pattern(single_category_where)})" From 65e5a6faa95ed1c3216a866be791a16bfaa8d728 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 12 Jan 2024 09:53:13 +0545 Subject: [PATCH 2/5] Fix Query filter condition --- API/tasks.py | 1 + src/query_builder/builder.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/API/tasks.py b/API/tasks.py index ff1b4ab2..0768d697 100644 --- a/API/tasks.py +++ b/API/tasks.py @@ -62,6 +62,7 @@ def get_task_status( """ task_result = AsyncResult(task_id, app=celery) + task_response_result = None if task_result.status == "SUCCESS": task_response_result = task_result.result diff --git a/src/query_builder/builder.py b/src/query_builder/builder.py index 5b573d9a..6f4a8a5c 100644 --- a/src/query_builder/builder.py +++ b/src/query_builder/builder.py @@ -97,7 +97,7 @@ def get_query_as_geojson(query_list, ogr_export=None): def create_geom_filter(geom, geom_lookup_by="ST_intersects"): """generates geometry intersection filter - Rawdata extraction""" geometry_dump = dumps(loads(geom.model_dump_json())) - return f"""{geom_lookup_by}(geom,ST_Buffer(ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{geometry_dump}'))),0.005))""" + return f"""{geom_lookup_by}(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{geometry_dump}')))),0.005))""" def format_file_name_str(input_str): @@ -898,7 +898,7 @@ def convert_tags_pattern(query_string): row_filter_condition = ( f"""(country <@ ARRAY [{cid}])""" if cid - else f"""(ST_within(geom,ST_Buffer(ST_Union(ST_makeValid(ST_GeomFromText('{wkt.dumps(loads(geometry.json()),decimals=6)}',4326))),0.005)))""" + else f"""(ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GeomFromText('{wkt.dumps(loads(geometry.json()),decimals=6)}',4326)))),0.005)))""" ) if single_category_where: row_filter_condition += f" and ({convert_tags_pattern(single_category_where)})" From 7c81853ccbe3a15459b497d0c9e7c038ef6dcde7 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 12 Jan 2024 10:10:20 +0545 Subject: [PATCH 3/5] Fix Unit tests --- tests/test_app.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index bfacd3cc..021975a6 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -50,22 +50,22 @@ def test_rawdata_current_snapshot_geometry_query(): from nodes where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')) and (tags ->> 'amenity' IN ( 'shop' , 'toilet' ))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005)) and (tags ->> 'amenity' IN ( 'shop' , 'toilet' ))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_line where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_poly where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from relations where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t3""" + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t3""" query_result = raw_currentdata_extraction_query( validated_params, @@ -96,22 +96,22 @@ def test_rawdata_current_snapshot_normal_query(): from nodes where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_line where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_poly where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from relations where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t3""" + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t3""" query_result = raw_currentdata_extraction_query( validated_params, ) @@ -140,22 +140,22 @@ def test_rawdata_current_snapshot_normal_query_ST_within(): from nodes where - ST_within(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_line where - ST_within(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_poly where - ST_within(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from relations where - ST_within(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t3""" + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t3""" query_result = raw_currentdata_extraction_query( validated_params, ) @@ -190,22 +190,22 @@ def test_attribute_filter_rawdata(): from ways_line where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')) and (tags ->> 'building' = 'yes')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')))),0.005)) and (tags ->> 'building' = 'yes')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id , tableoid::regclass AS osm_type , tags ->> 'name' as name , geom from relations where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='MULTILINESTRING')) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')))),0.005)) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='MULTILINESTRING')) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_poly where - (grid = 1187 OR grid = 1188) and (ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}'))) and (tags ->> 'building' = 'yes')) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select + (grid = 1187 OR grid = 1188) and (ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')))),0.005))) and (tags ->> 'building' = 'yes')) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from relations where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t3""" + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[83.502574, 27.569073], [83.502574, 28.332758], [85.556417, 28.332758], [85.556417, 27.569073], [83.502574, 27.569073]]]}')))),0.005)) and (tags ->> 'building' = 'yes') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t3""" query_result = raw_currentdata_extraction_query( validated_params, g_id=[[1187], [1188]], @@ -262,12 +262,12 @@ def test_and_filters(): from ways_poly where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id , tableoid::regclass AS osm_type , tags ->> 'building' as building , tags ->> 'destroyed:building' as destroyed_building , tags ->> 'damage:date' as damage_date , tags ->> 'name' as name , tags ->> 'source' as source , geom from relations where - ST_intersects(geom,ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t1""" + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t1""" query_result = raw_currentdata_extraction_query( validated_params, ) From d2aa3371087afb555b456410f206cc4584751a52 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 12 Jan 2024 10:16:14 +0545 Subject: [PATCH 4/5] Fix remaining unit tests --- tests/test_API.py | 2632 ++++++++++++++++++++++----------------------- tests/test_app.py | 14 +- 2 files changed, 1323 insertions(+), 1323 deletions(-) diff --git a/tests/test_API.py b/tests/test_API.py index a4e43863..834ff4fd 100644 --- a/tests/test_API.py +++ b/tests/test_API.py @@ -1,1319 +1,1319 @@ -import os -import time +# import os +# import time -from fastapi.testclient import TestClient +# from fastapi.testclient import TestClient -from API.main import app +# from API.main import app -client = TestClient(app) - -access_token = os.environ.get("ACCESS_TOKEN") - -## Status - - -def test_status(): - response = client.get("/v1/status/") - assert response.status_code == 200 - - -## Login -def test_login_url(): - response = client.get("/v1/auth/login/") - assert response.status_code == 200 - - -def test_login_auth_me(): - headers = {"access-token": access_token} - response = client.get("/v1/auth/me/", headers=headers) - assert response.status_code == 200 - - -## Countries - - -def test_countries_endpoint(): - response = client.get("/v1/countries/?q=nepal") - assert response.status_code == 200 - - -## test osm_id - - -def test_osm_id_endpoint(): - response = client.get("/v1/osm_id/?osm_id=421498318") - assert response.status_code == 200 - - -## Snapshot -def test_snapshot(): - response = client.post( - "/v1/snapshot/", - json={ - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - } - }, - ) - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_featurecollection(): - response = client.post( - "/v1/snapshot/", - json={ - "geometry": { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": {}, - "geometry": { - "coordinates": [ - [ - [83.97346137271688, 28.217525272345284], - [83.97346137271688, 28.192595937414737], - [84.01473909818759, 28.192595937414737], - [84.01473909818759, 28.217525272345284], - [83.97346137271688, 28.217525272345284], - ] - ], - "type": "Polygon", - }, - } - ], - } - }, - ) - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_feature(): - response = client.post( - "/v1/snapshot/", - json={ - "geometry": { - "type": "Feature", - "properties": {}, - "geometry": { - "coordinates": [ - [ - [83.97346137271688, 28.217525272345284], - [83.97346137271688, 28.192595937414737], - [84.01473909818759, 28.192595937414737], - [84.01473909818759, 28.217525272345284], - [83.97346137271688, 28.217525272345284], - ] - ], - "type": "Polygon", - }, - } - }, - ) - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_centroid(): - response = client.post( - "/v1/snapshot/", - json={ - "centroid": True, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, - }, - ) - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_filters(): - response = client.post( - "/v1/snapshot/", - json={ - "fileName": "Example export with all features", - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.585701, 28.046607], - [83.585701, 28.382561], - [84.391823, 28.382561], - [84.391823, 28.046607], - [83.585701, 28.046607], - ] - ], - }, - "outputType": "geojson", - "geometryType": ["point", "line", "polygon"], - "filters": { - "tags": { - "point": { - "join_or": { - "amenity": [ - "bank", - "ferry_terminal", - "bus_station", - "fuel", - "kindergarten", - "school", - "college", - "university", - "place_of_worship", - "marketplace", - "clinic", - "hospital", - "police", - "fire_station", - ], - "building": [ - "bank", - "aerodrome", - "ferry_terminal", - "train_station", - "bus_station", - "pumping_station", - "power_substation", - "kindergarten", - "school", - "college", - "university", - "mosque ", - " church ", - " temple", - "supermarket", - "marketplace", - "clinic", - "hospital", - "police", - "fire_station", - "stadium ", - " sports_centre", - "governor_office ", - " townhall ", - " subdistrict_office ", - " village_office ", - " community_group_office", - "government_office", - ], - "man_made": ["tower", "water_tower", "pumping_station"], - "tower:type": ["communication"], - "aeroway": ["aerodrome"], - "railway": ["station"], - "emergency": ["fire_hydrant"], - "landuse": ["reservoir", "recreation_gound"], - "waterway": ["floodgate"], - "natural": ["spring"], - "power": ["tower", "substation"], - "shop": ["supermarket"], - "leisure": [ - "stadium ", - " sports_centre ", - " pitch ", - " swimming_pool", - "park", - ], - "office": ["government"], - } - }, - "line": { - "join_or": { - "highway": [ - "motorway ", - " trunk ", - " primary ", - " secondary ", - " tertiary ", - " service ", - " residential ", - " pedestrian ", - " path ", - " living_street ", - " track", - ], - "railway": ["rail"], - "man_made": ["embankment"], - "waterway": [], - } - }, - "polygon": { - "join_or": { - "amenity": [ - "bank", - "ferry_terminal", - "bus_station", - "fuel", - "kindergarten", - "school", - "college", - "university", - "place_of_worship", - "marketplace", - "clinic", - "hospital", - "police", - "fire_station", - ], - "building": [ - "bank", - "aerodrome", - "ferry_terminal", - "train_station", - "bus_station", - "pumping_station", - "power_substation", - "power_plant", - "kindergarten", - "school", - "college", - "university", - "mosque ", - " church ", - " temple", - "supermarket", - "marketplace", - "clinic", - "hospital", - "police", - "fire_station", - "stadium ", - " sports_centre", - "governor_office ", - " townhall ", - " subdistrict_office ", - " village_office ", - " community_group_office", - "government_office", - ], - "man_made": ["tower", "water_tower", "pumping_station"], - "tower:type": ["communication"], - "aeroway": ["aerodrome"], - "railway": ["station"], - "landuse": ["reservoir", "recreation_gound"], - "waterway": [], - "natural": ["spring"], - "power": ["substation", "plant"], - "shop": ["supermarket"], - "leisure": [ - "stadium ", - " sports_centre ", - " pitch ", - " swimming_pool", - "park", - ], - "office": ["government"], - "type": ["boundary"], - "boundary": ["administrative"], - }, - }, - }, - "attributes": { - "point": [ - "building", - "ground_floor:height", - "capacity:persons", - "building:structure", - "building:condition", - "name", - "admin_level", - "building:material", - "office", - "building:roof", - "backup_generator", - "access:roof", - "building:levels", - "building:floor", - "addr:full", - "addr:city", - "source", - ], - "line": ["width", "source", "waterway", "name"], - "polygon": [ - "landslide_prone", - "name", - "admin_level", - "type", - "is_in:town", - "flood_prone", - "is_in:province", - "is_in:city", - "is_in:municipality", - "is_in:RW", - "is_in:village", - "source", - "boundary", - ], - }, - }, - }, - ) - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_and_filter(): - response = client.post( - "/v1/snapshot/", - json={ - "fileName": "Destroyed_Buildings_Turkey", - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [36.70588085657477, 37.1979648807274], - [36.70588085657477, 37.1651408422983], - [36.759267544807194, 37.1651408422983], - [36.759267544807194, 37.1979648807274], - [36.70588085657477, 37.1979648807274], - ] - ], - }, - "outputType": "geojson", - "geometryType": ["polygon"], - "filters": { - "tags": { - "point": {}, - "line": {}, - "polygon": { - "join_or": {}, - "join_and": { - "destroyed:building": ["yes"], - "damage:date": ["2023-02-06"], - }, - }, - }, - "attributes": { - "point": [], - "line": [], - "polygon": [ - "building", - "destroyed:building", - "damage:date", - "name", - "source", - ], - }, - }, - }, - ) - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_authentication_uuid(): - headers = {"access-token": access_token} - payload = { - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, - "uuid": False, - } - - response = client.post("/v1/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_snapshot_bind_zip(): - headers = {"access-token": access_token} - payload = { - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, - "bindZip": False, - } - - response = client.post("/v1/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -## Snapshot Plain - - -def test_snapshot_plain(): - response = client.post( - "/v1/snapshot/plain/", - json={ - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - } - }, - ) - assert response.status_code == 200 - - -## Stats - - -def test_stats_endpoint_custom_polygon(): - headers = {"access-token": access_token} - payload = { - "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/stats/polygon/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - assert ( - res["meta"]["indicators"] - == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" - ) - - -def test_stats_endpoint_iso3(): - headers = {"access-token": access_token} - payload = {"iso3": "npl"} - - response = client.post("/v1/stats/polygon/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - assert ( - res["meta"]["indicators"] - == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" - ) - - -## HDX - - -def test_hdx_submit_normal_iso3(): - headers = {"access-token": access_token} - payload = { - "iso3": "NPL", - "hdx_upload": False, - "categories": [ - { - "Roads": { - "hdx": { - "tags": ["roads", "transportation", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines"], - "select": ["name", "highway"], - "where": "tags['highway'] IS NOT NULL", - "formats": ["geojson"], - } - } - ], - } - - response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_hdx_submit_normal_iso3_multiple_format(): - headers = {"access-token": access_token} - payload = { - "iso3": "NPL", - "hdx_upload": False, - "categories": [ - { - "Roads": { - "hdx": { - "tags": ["roads", "transportation", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines"], - "select": ["name", "highway"], - "where": "tags['highway'] IS NOT NULL", - "formats": ["geojson", "gpkg", "kml", "shp"], - } - } - ], - } - - response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_hdx_submit_normal_custom_polygon(): - headers = {"access-token": access_token} - payload = { - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, - "hdx_upload": False, - "dataset": { - "subnational": True, - "dataset_title": "Pokhara", - "dataset_prefix": "hotosm_pkr", - "dataset_locations": ["npl"], - }, - "categories": [ - { - "Roads": { - "hdx": { - "tags": ["roads", "transportation", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines"], - "select": ["name", "highway"], - "where": "tags['highway'] IS NOT NULL", - "formats": ["geojson"], - } - } - ], - } - - response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_custom_submit_normal_custom_polygon_TM_project(): - headers = {"access-token": access_token} - payload = { - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, - "queue": "raw_default", - "dataset": { - "dataset_prefix": "hotosm_project_1", - "dataset_folder": "TM", - "dataset_title": "Tasking Manger Project 1", - }, - "categories": [ - { - "Buildings": { - "types": ["polygons"], - "select": [ - "name", - "building", - "building:levels", - "building:materials", - "addr:full", - "addr:housenumber", - "addr:street", - "addr:city", - "office", - "source", - ], - "where": "tags['building'] IS NOT NULL", - "formats": ["geojson", "shp", "kml"], - }, - "Roads": { - "types": ["lines"], - "select": [ - "name", - "highway", - "surface", - "smoothness", - "width", - "lanes", - "oneway", - "bridge", - "layer", - "source", - ], - "where": "tags['highway'] IS NOT NULL", - "formats": ["geojson", "shp", "kml"], - }, - "Waterways": { - "types": ["lines", "polygons"], - "select": [ - "name", - "waterway", - "covered", - "width", - "depth", - "layer", - "blockage", - "tunnel", - "natural", - "water", - "source", - ], - "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", - "formats": ["geojson", "shp", "kml"], - }, - "Landuse": { - "types": ["points", "polygons"], - "select": ["name", "amenity", "landuse", "leisure"], - "where": "tags['landuse'] IS NOT NULL", - "formats": ["geojson", "shp", "kml"], - }, - } - ], - } - - response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_hdx_submit_normal_custom_polygon_upload(): - headers = {"access-token": access_token} - payload = { - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, - "hdx_upload": True, - "dataset": { - "subnational": True, - "dataset_title": "Pokhara", - "dataset_folder": "Test", - "dataset_prefix": "hotosm_pkr", - "dataset_locations": ["npl"], - }, - "categories": [ - { - "Roads": { - "hdx": { - "tags": ["roads", "transportation", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines"], - "select": ["name", "highway"], - "where": "tags['highway'] IS NOT NULL", - "formats": ["geojson"], - } - } - ], - } - - response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - max_attempts = 6 - interval_seconds = 10 - for attempt in range(1, max_attempts + 1): - time.sleep(interval_seconds) # wait for worker to complete task - - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - - if check_status == "SUCCESS": - break # exit the loop if the status is SUCCESS - - if attempt == max_attempts: - # If max_attempts reached and status is not SUCCESS, raise an AssertionError - assert ( - False - ), f"Task did not complete successfully after {max_attempts} attempts" - - -def test_full_hdx_set_iso(): - headers = {"access-token": access_token} - payload = { - "iso3": "NPL", - "hdx_upload": False, - "categories": [ - { - "Buildings": { - "hdx": { - "tags": [ - "facilities-infrastructure", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["polygons"], - "select": [ - "name", - "building", - "building:levels", - "building:materials", - "addr:full", - "addr:housenumber", - "addr:street", - "addr:city", - "office", - "source", - ], - "where": "tags['building'] IS NOT NULL", - "formats": ["geojson"], - } - }, - { - "Roads": { - "hdx": { - "tags": ["transportation", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines"], - "select": [ - "name", - "highway", - "surface", - "smoothness", - "width", - "lanes", - "oneway", - "bridge", - "layer", - "source", - ], - "where": "tags['highway'] IS NOT NULL", - "formats": ["geojson"], - } - }, - { - "Waterways": { - "hdx": { - "tags": ["hydrology", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines", "polygons"], - "select": [ - "name", - "waterway", - "covered", - "width", - "depth", - "layer", - "blockage", - "tunnel", - "natural", - "water", - "source", - ], - "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", - "formats": ["geojson"], - } - }, - { - "Points of Interest": { - "hdx": { - "tags": [ - "facilities-infrastructure", - "points of interest-poi", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points", "polygons"], - "select": [ - "name", - "amenity", - "man_made", - "shop", - "tourism", - "opening_hours", - "beds", - "rooms", - "addr:full", - "addr:housenumber", - "addr:street", - "addr:city", - "source", - ], - "where": "tags['amenity'] IS NOT NULL OR tags['man_made'] IS NOT NULL OR tags['shop'] IS NOT NULL OR tags['tourism'] IS NOT NULL", - "formats": ["geojson"], - } - }, - { - "Airports": { - "hdx": { - "tags": [ - "aviation", - "facilities-infrastructure", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points", "lines", "polygons"], - "select": [ - "name", - "aeroway", - "building", - "emergency", - "emergency:helipad", - "operator:type", - "capacity:persons", - "addr:full", - "addr:city", - "source", - ], - "where": "tags['aeroway'] IS NOT NULL OR tags['building'] = 'aerodrome' OR tags['emergency:helipad'] IS NOT NULL OR tags['emergency'] = 'landing_site'", - "formats": ["geojson"], - } - }, - { - "Sea Ports": { - "hdx": { - "tags": [ - "facilities-infrastructure", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points", "lines", "polygons"], - "select": [ - "name", - "amenity", - "building", - "port", - "operator:type", - "addr:full", - "addr:city", - "source", - ], - "where": "tags['amenity'] = 'ferry_terminal' OR tags['building'] = 'ferry_terminal' OR tags['port'] IS NOT NULL", - "formats": ["geojson"], - } - }, - { - "Education Facilities": { - "hdx": { - "tags": [ - "education facilities-schools", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points", "polygons"], - "select": [ - "name", - "amenity", - "building", - "operator:type", - "capacity:persons", - "addr:full", - "addr:city", - "source", - ], - "where": "tags['amenity'] IN ('kindergarten', 'school', 'college', 'university') OR building IN ('kindergarten', 'school', 'college', 'university')", - "formats": ["geojson"], - } - }, - { - "Health Facilities": { - "hdx": { - "tags": ["geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points", "polygons"], - "select": [ - "name", - "amenity", - "building", - "healthcare", - "healthcare:speciality", - "operator:type", - "capacity:persons", - "addr:full", - "addr:city", - "source", - ], - "where": "tags['healthcare'] IS NOT NULL OR tags['amenity'] IN ('doctors', 'dentist', 'clinic', 'hospital', 'pharmacy')", - "formats": ["geojson"], - } - }, - { - "Populated Places": { - "hdx": { - "tags": [ - "populated places-settlements", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points"], - "select": [ - "name", - "place", - "population", - "is_in", - "source", - ], - "where": "tags['place'] IN ('isolated_dwelling', 'town', 'village', 'hamlet', 'city')", - "formats": ["geojson"], - } - }, - { - "Financial Services": { - "hdx": { - "tags": ["economics", "geodata"], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["points", "polygons"], - "select": [ - "name", - "amenity", - "operator", - "network", - "addr:full", - "addr:city", - "source", - ], - "where": "tags['amenity'] IN ('mobile_money_agent','bureau_de_change','bank','microfinance','atm','sacco','money_transfer','post_office')", - "formats": ["geojson"], - } - }, - { - "Railways": { - "hdx": { - "tags": [ - "facilities-infrastructure", - "railways", - "transportation", - "geodata", - ], - "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", - }, - "types": ["lines"], - "select": [ - "name", - "railway", - "ele", - "operator:type", - "layer", - "addr:full", - "addr:city", - "source", - ], - "where": "tags['railway'] IN ('rail','station')", - "formats": ["geojson"], - } - }, - ], - } - - response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - - assert response.status_code == 200 - res = response.json() - track_link = res["track_link"] - time.sleep(60) # wait for worker to complete task - response = client.get(f"/v1{track_link}") - assert response.status_code == 200 - res = response.json() - check_status = res["status"] - assert check_status == "SUCCESS" - - -## Tasks connection - - -def test_worker_connection(): - response = client.get("/v1/tasks/ping/") - assert response.status_code == 200 +# client = TestClient(app) + +# access_token = os.environ.get("ACCESS_TOKEN") + +# ## Status + + +# def test_status(): +# response = client.get("/v1/status/") +# assert response.status_code == 200 + + +# ## Login +# def test_login_url(): +# response = client.get("/v1/auth/login/") +# assert response.status_code == 200 + + +# def test_login_auth_me(): +# headers = {"access-token": access_token} +# response = client.get("/v1/auth/me/", headers=headers) +# assert response.status_code == 200 + + +# ## Countries + + +# def test_countries_endpoint(): +# response = client.get("/v1/countries/?q=nepal") +# assert response.status_code == 200 + + +# ## test osm_id + + +# def test_osm_id_endpoint(): +# response = client.get("/v1/osm_id/?osm_id=421498318") +# assert response.status_code == 200 + + +# ## Snapshot +# def test_snapshot(): +# response = client.post( +# "/v1/snapshot/", +# json={ +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# } +# }, +# ) +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_featurecollection(): +# response = client.post( +# "/v1/snapshot/", +# json={ +# "geometry": { +# "type": "FeatureCollection", +# "features": [ +# { +# "type": "Feature", +# "properties": {}, +# "geometry": { +# "coordinates": [ +# [ +# [83.97346137271688, 28.217525272345284], +# [83.97346137271688, 28.192595937414737], +# [84.01473909818759, 28.192595937414737], +# [84.01473909818759, 28.217525272345284], +# [83.97346137271688, 28.217525272345284], +# ] +# ], +# "type": "Polygon", +# }, +# } +# ], +# } +# }, +# ) +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_feature(): +# response = client.post( +# "/v1/snapshot/", +# json={ +# "geometry": { +# "type": "Feature", +# "properties": {}, +# "geometry": { +# "coordinates": [ +# [ +# [83.97346137271688, 28.217525272345284], +# [83.97346137271688, 28.192595937414737], +# [84.01473909818759, 28.192595937414737], +# [84.01473909818759, 28.217525272345284], +# [83.97346137271688, 28.217525272345284], +# ] +# ], +# "type": "Polygon", +# }, +# } +# }, +# ) +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_centroid(): +# response = client.post( +# "/v1/snapshot/", +# json={ +# "centroid": True, +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# }, +# }, +# ) +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_filters(): +# response = client.post( +# "/v1/snapshot/", +# json={ +# "fileName": "Example export with all features", +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.585701, 28.046607], +# [83.585701, 28.382561], +# [84.391823, 28.382561], +# [84.391823, 28.046607], +# [83.585701, 28.046607], +# ] +# ], +# }, +# "outputType": "geojson", +# "geometryType": ["point", "line", "polygon"], +# "filters": { +# "tags": { +# "point": { +# "join_or": { +# "amenity": [ +# "bank", +# "ferry_terminal", +# "bus_station", +# "fuel", +# "kindergarten", +# "school", +# "college", +# "university", +# "place_of_worship", +# "marketplace", +# "clinic", +# "hospital", +# "police", +# "fire_station", +# ], +# "building": [ +# "bank", +# "aerodrome", +# "ferry_terminal", +# "train_station", +# "bus_station", +# "pumping_station", +# "power_substation", +# "kindergarten", +# "school", +# "college", +# "university", +# "mosque ", +# " church ", +# " temple", +# "supermarket", +# "marketplace", +# "clinic", +# "hospital", +# "police", +# "fire_station", +# "stadium ", +# " sports_centre", +# "governor_office ", +# " townhall ", +# " subdistrict_office ", +# " village_office ", +# " community_group_office", +# "government_office", +# ], +# "man_made": ["tower", "water_tower", "pumping_station"], +# "tower:type": ["communication"], +# "aeroway": ["aerodrome"], +# "railway": ["station"], +# "emergency": ["fire_hydrant"], +# "landuse": ["reservoir", "recreation_gound"], +# "waterway": ["floodgate"], +# "natural": ["spring"], +# "power": ["tower", "substation"], +# "shop": ["supermarket"], +# "leisure": [ +# "stadium ", +# " sports_centre ", +# " pitch ", +# " swimming_pool", +# "park", +# ], +# "office": ["government"], +# } +# }, +# "line": { +# "join_or": { +# "highway": [ +# "motorway ", +# " trunk ", +# " primary ", +# " secondary ", +# " tertiary ", +# " service ", +# " residential ", +# " pedestrian ", +# " path ", +# " living_street ", +# " track", +# ], +# "railway": ["rail"], +# "man_made": ["embankment"], +# "waterway": [], +# } +# }, +# "polygon": { +# "join_or": { +# "amenity": [ +# "bank", +# "ferry_terminal", +# "bus_station", +# "fuel", +# "kindergarten", +# "school", +# "college", +# "university", +# "place_of_worship", +# "marketplace", +# "clinic", +# "hospital", +# "police", +# "fire_station", +# ], +# "building": [ +# "bank", +# "aerodrome", +# "ferry_terminal", +# "train_station", +# "bus_station", +# "pumping_station", +# "power_substation", +# "power_plant", +# "kindergarten", +# "school", +# "college", +# "university", +# "mosque ", +# " church ", +# " temple", +# "supermarket", +# "marketplace", +# "clinic", +# "hospital", +# "police", +# "fire_station", +# "stadium ", +# " sports_centre", +# "governor_office ", +# " townhall ", +# " subdistrict_office ", +# " village_office ", +# " community_group_office", +# "government_office", +# ], +# "man_made": ["tower", "water_tower", "pumping_station"], +# "tower:type": ["communication"], +# "aeroway": ["aerodrome"], +# "railway": ["station"], +# "landuse": ["reservoir", "recreation_gound"], +# "waterway": [], +# "natural": ["spring"], +# "power": ["substation", "plant"], +# "shop": ["supermarket"], +# "leisure": [ +# "stadium ", +# " sports_centre ", +# " pitch ", +# " swimming_pool", +# "park", +# ], +# "office": ["government"], +# "type": ["boundary"], +# "boundary": ["administrative"], +# }, +# }, +# }, +# "attributes": { +# "point": [ +# "building", +# "ground_floor:height", +# "capacity:persons", +# "building:structure", +# "building:condition", +# "name", +# "admin_level", +# "building:material", +# "office", +# "building:roof", +# "backup_generator", +# "access:roof", +# "building:levels", +# "building:floor", +# "addr:full", +# "addr:city", +# "source", +# ], +# "line": ["width", "source", "waterway", "name"], +# "polygon": [ +# "landslide_prone", +# "name", +# "admin_level", +# "type", +# "is_in:town", +# "flood_prone", +# "is_in:province", +# "is_in:city", +# "is_in:municipality", +# "is_in:RW", +# "is_in:village", +# "source", +# "boundary", +# ], +# }, +# }, +# }, +# ) +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_and_filter(): +# response = client.post( +# "/v1/snapshot/", +# json={ +# "fileName": "Destroyed_Buildings_Turkey", +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [36.70588085657477, 37.1979648807274], +# [36.70588085657477, 37.1651408422983], +# [36.759267544807194, 37.1651408422983], +# [36.759267544807194, 37.1979648807274], +# [36.70588085657477, 37.1979648807274], +# ] +# ], +# }, +# "outputType": "geojson", +# "geometryType": ["polygon"], +# "filters": { +# "tags": { +# "point": {}, +# "line": {}, +# "polygon": { +# "join_or": {}, +# "join_and": { +# "destroyed:building": ["yes"], +# "damage:date": ["2023-02-06"], +# }, +# }, +# }, +# "attributes": { +# "point": [], +# "line": [], +# "polygon": [ +# "building", +# "destroyed:building", +# "damage:date", +# "name", +# "source", +# ], +# }, +# }, +# }, +# ) +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_authentication_uuid(): +# headers = {"access-token": access_token} +# payload = { +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# }, +# "uuid": False, +# } + +# response = client.post("/v1/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_snapshot_bind_zip(): +# headers = {"access-token": access_token} +# payload = { +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# }, +# "bindZip": False, +# } + +# response = client.post("/v1/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# ## Snapshot Plain + + +# def test_snapshot_plain(): +# response = client.post( +# "/v1/snapshot/plain/", +# json={ +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# } +# }, +# ) +# assert response.status_code == 200 + + +# ## Stats + + +# def test_stats_endpoint_custom_polygon(): +# headers = {"access-token": access_token} +# payload = { +# "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/stats/polygon/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# assert ( +# res["meta"]["indicators"] +# == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" +# ) + + +# def test_stats_endpoint_iso3(): +# headers = {"access-token": access_token} +# payload = {"iso3": "npl"} + +# response = client.post("/v1/stats/polygon/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# assert ( +# res["meta"]["indicators"] +# == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" +# ) + + +# ## HDX + + +# def test_hdx_submit_normal_iso3(): +# headers = {"access-token": access_token} +# payload = { +# "iso3": "NPL", +# "hdx_upload": False, +# "categories": [ +# { +# "Roads": { +# "hdx": { +# "tags": ["roads", "transportation", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines"], +# "select": ["name", "highway"], +# "where": "tags['highway'] IS NOT NULL", +# "formats": ["geojson"], +# } +# } +# ], +# } + +# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_hdx_submit_normal_iso3_multiple_format(): +# headers = {"access-token": access_token} +# payload = { +# "iso3": "NPL", +# "hdx_upload": False, +# "categories": [ +# { +# "Roads": { +# "hdx": { +# "tags": ["roads", "transportation", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines"], +# "select": ["name", "highway"], +# "where": "tags['highway'] IS NOT NULL", +# "formats": ["geojson", "gpkg", "kml", "shp"], +# } +# } +# ], +# } + +# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_hdx_submit_normal_custom_polygon(): +# headers = {"access-token": access_token} +# payload = { +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# }, +# "hdx_upload": False, +# "dataset": { +# "subnational": True, +# "dataset_title": "Pokhara", +# "dataset_prefix": "hotosm_pkr", +# "dataset_locations": ["npl"], +# }, +# "categories": [ +# { +# "Roads": { +# "hdx": { +# "tags": ["roads", "transportation", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines"], +# "select": ["name", "highway"], +# "where": "tags['highway'] IS NOT NULL", +# "formats": ["geojson"], +# } +# } +# ], +# } + +# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_custom_submit_normal_custom_polygon_TM_project(): +# headers = {"access-token": access_token} +# payload = { +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# }, +# "queue": "raw_default", +# "dataset": { +# "dataset_prefix": "hotosm_project_1", +# "dataset_folder": "TM", +# "dataset_title": "Tasking Manger Project 1", +# }, +# "categories": [ +# { +# "Buildings": { +# "types": ["polygons"], +# "select": [ +# "name", +# "building", +# "building:levels", +# "building:materials", +# "addr:full", +# "addr:housenumber", +# "addr:street", +# "addr:city", +# "office", +# "source", +# ], +# "where": "tags['building'] IS NOT NULL", +# "formats": ["geojson", "shp", "kml"], +# }, +# "Roads": { +# "types": ["lines"], +# "select": [ +# "name", +# "highway", +# "surface", +# "smoothness", +# "width", +# "lanes", +# "oneway", +# "bridge", +# "layer", +# "source", +# ], +# "where": "tags['highway'] IS NOT NULL", +# "formats": ["geojson", "shp", "kml"], +# }, +# "Waterways": { +# "types": ["lines", "polygons"], +# "select": [ +# "name", +# "waterway", +# "covered", +# "width", +# "depth", +# "layer", +# "blockage", +# "tunnel", +# "natural", +# "water", +# "source", +# ], +# "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", +# "formats": ["geojson", "shp", "kml"], +# }, +# "Landuse": { +# "types": ["points", "polygons"], +# "select": ["name", "amenity", "landuse", "leisure"], +# "where": "tags['landuse'] IS NOT NULL", +# "formats": ["geojson", "shp", "kml"], +# }, +# } +# ], +# } + +# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_hdx_submit_normal_custom_polygon_upload(): +# headers = {"access-token": access_token} +# payload = { +# "geometry": { +# "type": "Polygon", +# "coordinates": [ +# [ +# [83.96919250488281, 28.194446860487773], +# [83.99751663208006, 28.194446860487773], +# [83.99751663208006, 28.214869548073377], +# [83.96919250488281, 28.214869548073377], +# [83.96919250488281, 28.194446860487773], +# ] +# ], +# }, +# "hdx_upload": True, +# "dataset": { +# "subnational": True, +# "dataset_title": "Pokhara", +# "dataset_folder": "Test", +# "dataset_prefix": "hotosm_pkr", +# "dataset_locations": ["npl"], +# }, +# "categories": [ +# { +# "Roads": { +# "hdx": { +# "tags": ["roads", "transportation", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines"], +# "select": ["name", "highway"], +# "where": "tags['highway'] IS NOT NULL", +# "formats": ["geojson"], +# } +# } +# ], +# } + +# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# max_attempts = 6 +# interval_seconds = 10 +# for attempt in range(1, max_attempts + 1): +# time.sleep(interval_seconds) # wait for worker to complete task + +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] + +# if check_status == "SUCCESS": +# break # exit the loop if the status is SUCCESS + +# if attempt == max_attempts: +# # If max_attempts reached and status is not SUCCESS, raise an AssertionError +# assert ( +# False +# ), f"Task did not complete successfully after {max_attempts} attempts" + + +# def test_full_hdx_set_iso(): +# headers = {"access-token": access_token} +# payload = { +# "iso3": "NPL", +# "hdx_upload": False, +# "categories": [ +# { +# "Buildings": { +# "hdx": { +# "tags": [ +# "facilities-infrastructure", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["polygons"], +# "select": [ +# "name", +# "building", +# "building:levels", +# "building:materials", +# "addr:full", +# "addr:housenumber", +# "addr:street", +# "addr:city", +# "office", +# "source", +# ], +# "where": "tags['building'] IS NOT NULL", +# "formats": ["geojson"], +# } +# }, +# { +# "Roads": { +# "hdx": { +# "tags": ["transportation", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines"], +# "select": [ +# "name", +# "highway", +# "surface", +# "smoothness", +# "width", +# "lanes", +# "oneway", +# "bridge", +# "layer", +# "source", +# ], +# "where": "tags['highway'] IS NOT NULL", +# "formats": ["geojson"], +# } +# }, +# { +# "Waterways": { +# "hdx": { +# "tags": ["hydrology", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines", "polygons"], +# "select": [ +# "name", +# "waterway", +# "covered", +# "width", +# "depth", +# "layer", +# "blockage", +# "tunnel", +# "natural", +# "water", +# "source", +# ], +# "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", +# "formats": ["geojson"], +# } +# }, +# { +# "Points of Interest": { +# "hdx": { +# "tags": [ +# "facilities-infrastructure", +# "points of interest-poi", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points", "polygons"], +# "select": [ +# "name", +# "amenity", +# "man_made", +# "shop", +# "tourism", +# "opening_hours", +# "beds", +# "rooms", +# "addr:full", +# "addr:housenumber", +# "addr:street", +# "addr:city", +# "source", +# ], +# "where": "tags['amenity'] IS NOT NULL OR tags['man_made'] IS NOT NULL OR tags['shop'] IS NOT NULL OR tags['tourism'] IS NOT NULL", +# "formats": ["geojson"], +# } +# }, +# { +# "Airports": { +# "hdx": { +# "tags": [ +# "aviation", +# "facilities-infrastructure", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points", "lines", "polygons"], +# "select": [ +# "name", +# "aeroway", +# "building", +# "emergency", +# "emergency:helipad", +# "operator:type", +# "capacity:persons", +# "addr:full", +# "addr:city", +# "source", +# ], +# "where": "tags['aeroway'] IS NOT NULL OR tags['building'] = 'aerodrome' OR tags['emergency:helipad'] IS NOT NULL OR tags['emergency'] = 'landing_site'", +# "formats": ["geojson"], +# } +# }, +# { +# "Sea Ports": { +# "hdx": { +# "tags": [ +# "facilities-infrastructure", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points", "lines", "polygons"], +# "select": [ +# "name", +# "amenity", +# "building", +# "port", +# "operator:type", +# "addr:full", +# "addr:city", +# "source", +# ], +# "where": "tags['amenity'] = 'ferry_terminal' OR tags['building'] = 'ferry_terminal' OR tags['port'] IS NOT NULL", +# "formats": ["geojson"], +# } +# }, +# { +# "Education Facilities": { +# "hdx": { +# "tags": [ +# "education facilities-schools", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points", "polygons"], +# "select": [ +# "name", +# "amenity", +# "building", +# "operator:type", +# "capacity:persons", +# "addr:full", +# "addr:city", +# "source", +# ], +# "where": "tags['amenity'] IN ('kindergarten', 'school', 'college', 'university') OR building IN ('kindergarten', 'school', 'college', 'university')", +# "formats": ["geojson"], +# } +# }, +# { +# "Health Facilities": { +# "hdx": { +# "tags": ["geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points", "polygons"], +# "select": [ +# "name", +# "amenity", +# "building", +# "healthcare", +# "healthcare:speciality", +# "operator:type", +# "capacity:persons", +# "addr:full", +# "addr:city", +# "source", +# ], +# "where": "tags['healthcare'] IS NOT NULL OR tags['amenity'] IN ('doctors', 'dentist', 'clinic', 'hospital', 'pharmacy')", +# "formats": ["geojson"], +# } +# }, +# { +# "Populated Places": { +# "hdx": { +# "tags": [ +# "populated places-settlements", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points"], +# "select": [ +# "name", +# "place", +# "population", +# "is_in", +# "source", +# ], +# "where": "tags['place'] IN ('isolated_dwelling', 'town', 'village', 'hamlet', 'city')", +# "formats": ["geojson"], +# } +# }, +# { +# "Financial Services": { +# "hdx": { +# "tags": ["economics", "geodata"], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["points", "polygons"], +# "select": [ +# "name", +# "amenity", +# "operator", +# "network", +# "addr:full", +# "addr:city", +# "source", +# ], +# "where": "tags['amenity'] IN ('mobile_money_agent','bureau_de_change','bank','microfinance','atm','sacco','money_transfer','post_office')", +# "formats": ["geojson"], +# } +# }, +# { +# "Railways": { +# "hdx": { +# "tags": [ +# "facilities-infrastructure", +# "railways", +# "transportation", +# "geodata", +# ], +# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", +# }, +# "types": ["lines"], +# "select": [ +# "name", +# "railway", +# "ele", +# "operator:type", +# "layer", +# "addr:full", +# "addr:city", +# "source", +# ], +# "where": "tags['railway'] IN ('rail','station')", +# "formats": ["geojson"], +# } +# }, +# ], +# } + +# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + +# assert response.status_code == 200 +# res = response.json() +# track_link = res["track_link"] +# time.sleep(60) # wait for worker to complete task +# response = client.get(f"/v1{track_link}") +# assert response.status_code == 200 +# res = response.json() +# check_status = res["status"] +# assert check_status == "SUCCESS" + + +# ## Tasks connection + + +# def test_worker_connection(): +# response = client.get("/v1/tasks/ping/") +# assert response.status_code == 200 diff --git a/tests/test_app.py b/tests/test_app.py index 021975a6..5a058e49 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -50,7 +50,7 @@ def test_rawdata_current_snapshot_geometry_query(): from nodes where - ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005)) and (tags ->> 'amenity' IN ( 'shop' , 'toilet' ))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005)) and (tags ->> 'amenity' IN ( 'shop' , 'toilet' ))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_line @@ -140,22 +140,22 @@ def test_rawdata_current_snapshot_normal_query_ST_within(): from nodes where - ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_line where - ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t1 UNION ALL select ST_AsGeoJSON(t2.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from ways_poly where - ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t2 UNION ALL select ST_AsGeoJSON(t3.*) from (select osm_id, tableoid::regclass AS osm_type, version,tags,changeset,timestamp,geom from relations where - ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}'))) t3""" + ST_within(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[84.92431640625, 27.766190642387496], [85.31982421875, 27.766190642387496], [85.31982421875, 28.02592458049937], [84.92431640625, 28.02592458049937], [84.92431640625, 27.766190642387496]]]}')))),0.005))) t3""" query_result = raw_currentdata_extraction_query( validated_params, ) @@ -262,12 +262,12 @@ def test_and_filters(): from ways_poly where - ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')))),0.005)) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06')) t0 UNION ALL select ST_AsGeoJSON(t1.*) from (select osm_id , tableoid::regclass AS osm_type , tags ->> 'building' as building , tags ->> 'destroyed:building' as destroyed_building , tags ->> 'damage:date' as damage_date , tags ->> 'name' as name , tags ->> 'source' as source , geom from relations where - ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t1""" + ST_intersects(geom,ST_Buffer((select ST_Union(ST_makeValid(ST_GEOMFROMGEOJSON('{"type": "Polygon", "coordinates": [[[36.70588085657477, 37.1979648807274], [36.70588085657477, 37.1651408422983], [36.759267544807194, 37.1651408422983], [36.759267544807194, 37.1979648807274], [36.70588085657477, 37.1979648807274]]]}')))),0.005)) and (tags ->> 'destroyed:building' = 'yes' AND tags ->> 'damage:date' = '2023-02-06') and (geometrytype(geom)='POLYGON' or geometrytype(geom)='MULTIPOLYGON')) t1""" query_result = raw_currentdata_extraction_query( validated_params, ) From e9d3d42e9d62fc704607ca2382930611f3262ab4 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 12 Jan 2024 10:16:31 +0545 Subject: [PATCH 5/5] Typo revert for test_api comment --- tests/test_API.py | 2632 ++++++++++++++++++++++----------------------- 1 file changed, 1316 insertions(+), 1316 deletions(-) diff --git a/tests/test_API.py b/tests/test_API.py index 834ff4fd..a4e43863 100644 --- a/tests/test_API.py +++ b/tests/test_API.py @@ -1,1319 +1,1319 @@ -# import os -# import time +import os +import time -# from fastapi.testclient import TestClient +from fastapi.testclient import TestClient -# from API.main import app +from API.main import app -# client = TestClient(app) - -# access_token = os.environ.get("ACCESS_TOKEN") - -# ## Status - - -# def test_status(): -# response = client.get("/v1/status/") -# assert response.status_code == 200 - - -# ## Login -# def test_login_url(): -# response = client.get("/v1/auth/login/") -# assert response.status_code == 200 - - -# def test_login_auth_me(): -# headers = {"access-token": access_token} -# response = client.get("/v1/auth/me/", headers=headers) -# assert response.status_code == 200 - - -# ## Countries - - -# def test_countries_endpoint(): -# response = client.get("/v1/countries/?q=nepal") -# assert response.status_code == 200 - - -# ## test osm_id - - -# def test_osm_id_endpoint(): -# response = client.get("/v1/osm_id/?osm_id=421498318") -# assert response.status_code == 200 - - -# ## Snapshot -# def test_snapshot(): -# response = client.post( -# "/v1/snapshot/", -# json={ -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# } -# }, -# ) -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_featurecollection(): -# response = client.post( -# "/v1/snapshot/", -# json={ -# "geometry": { -# "type": "FeatureCollection", -# "features": [ -# { -# "type": "Feature", -# "properties": {}, -# "geometry": { -# "coordinates": [ -# [ -# [83.97346137271688, 28.217525272345284], -# [83.97346137271688, 28.192595937414737], -# [84.01473909818759, 28.192595937414737], -# [84.01473909818759, 28.217525272345284], -# [83.97346137271688, 28.217525272345284], -# ] -# ], -# "type": "Polygon", -# }, -# } -# ], -# } -# }, -# ) -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_feature(): -# response = client.post( -# "/v1/snapshot/", -# json={ -# "geometry": { -# "type": "Feature", -# "properties": {}, -# "geometry": { -# "coordinates": [ -# [ -# [83.97346137271688, 28.217525272345284], -# [83.97346137271688, 28.192595937414737], -# [84.01473909818759, 28.192595937414737], -# [84.01473909818759, 28.217525272345284], -# [83.97346137271688, 28.217525272345284], -# ] -# ], -# "type": "Polygon", -# }, -# } -# }, -# ) -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_centroid(): -# response = client.post( -# "/v1/snapshot/", -# json={ -# "centroid": True, -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# }, -# }, -# ) -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_filters(): -# response = client.post( -# "/v1/snapshot/", -# json={ -# "fileName": "Example export with all features", -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.585701, 28.046607], -# [83.585701, 28.382561], -# [84.391823, 28.382561], -# [84.391823, 28.046607], -# [83.585701, 28.046607], -# ] -# ], -# }, -# "outputType": "geojson", -# "geometryType": ["point", "line", "polygon"], -# "filters": { -# "tags": { -# "point": { -# "join_or": { -# "amenity": [ -# "bank", -# "ferry_terminal", -# "bus_station", -# "fuel", -# "kindergarten", -# "school", -# "college", -# "university", -# "place_of_worship", -# "marketplace", -# "clinic", -# "hospital", -# "police", -# "fire_station", -# ], -# "building": [ -# "bank", -# "aerodrome", -# "ferry_terminal", -# "train_station", -# "bus_station", -# "pumping_station", -# "power_substation", -# "kindergarten", -# "school", -# "college", -# "university", -# "mosque ", -# " church ", -# " temple", -# "supermarket", -# "marketplace", -# "clinic", -# "hospital", -# "police", -# "fire_station", -# "stadium ", -# " sports_centre", -# "governor_office ", -# " townhall ", -# " subdistrict_office ", -# " village_office ", -# " community_group_office", -# "government_office", -# ], -# "man_made": ["tower", "water_tower", "pumping_station"], -# "tower:type": ["communication"], -# "aeroway": ["aerodrome"], -# "railway": ["station"], -# "emergency": ["fire_hydrant"], -# "landuse": ["reservoir", "recreation_gound"], -# "waterway": ["floodgate"], -# "natural": ["spring"], -# "power": ["tower", "substation"], -# "shop": ["supermarket"], -# "leisure": [ -# "stadium ", -# " sports_centre ", -# " pitch ", -# " swimming_pool", -# "park", -# ], -# "office": ["government"], -# } -# }, -# "line": { -# "join_or": { -# "highway": [ -# "motorway ", -# " trunk ", -# " primary ", -# " secondary ", -# " tertiary ", -# " service ", -# " residential ", -# " pedestrian ", -# " path ", -# " living_street ", -# " track", -# ], -# "railway": ["rail"], -# "man_made": ["embankment"], -# "waterway": [], -# } -# }, -# "polygon": { -# "join_or": { -# "amenity": [ -# "bank", -# "ferry_terminal", -# "bus_station", -# "fuel", -# "kindergarten", -# "school", -# "college", -# "university", -# "place_of_worship", -# "marketplace", -# "clinic", -# "hospital", -# "police", -# "fire_station", -# ], -# "building": [ -# "bank", -# "aerodrome", -# "ferry_terminal", -# "train_station", -# "bus_station", -# "pumping_station", -# "power_substation", -# "power_plant", -# "kindergarten", -# "school", -# "college", -# "university", -# "mosque ", -# " church ", -# " temple", -# "supermarket", -# "marketplace", -# "clinic", -# "hospital", -# "police", -# "fire_station", -# "stadium ", -# " sports_centre", -# "governor_office ", -# " townhall ", -# " subdistrict_office ", -# " village_office ", -# " community_group_office", -# "government_office", -# ], -# "man_made": ["tower", "water_tower", "pumping_station"], -# "tower:type": ["communication"], -# "aeroway": ["aerodrome"], -# "railway": ["station"], -# "landuse": ["reservoir", "recreation_gound"], -# "waterway": [], -# "natural": ["spring"], -# "power": ["substation", "plant"], -# "shop": ["supermarket"], -# "leisure": [ -# "stadium ", -# " sports_centre ", -# " pitch ", -# " swimming_pool", -# "park", -# ], -# "office": ["government"], -# "type": ["boundary"], -# "boundary": ["administrative"], -# }, -# }, -# }, -# "attributes": { -# "point": [ -# "building", -# "ground_floor:height", -# "capacity:persons", -# "building:structure", -# "building:condition", -# "name", -# "admin_level", -# "building:material", -# "office", -# "building:roof", -# "backup_generator", -# "access:roof", -# "building:levels", -# "building:floor", -# "addr:full", -# "addr:city", -# "source", -# ], -# "line": ["width", "source", "waterway", "name"], -# "polygon": [ -# "landslide_prone", -# "name", -# "admin_level", -# "type", -# "is_in:town", -# "flood_prone", -# "is_in:province", -# "is_in:city", -# "is_in:municipality", -# "is_in:RW", -# "is_in:village", -# "source", -# "boundary", -# ], -# }, -# }, -# }, -# ) -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_and_filter(): -# response = client.post( -# "/v1/snapshot/", -# json={ -# "fileName": "Destroyed_Buildings_Turkey", -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [36.70588085657477, 37.1979648807274], -# [36.70588085657477, 37.1651408422983], -# [36.759267544807194, 37.1651408422983], -# [36.759267544807194, 37.1979648807274], -# [36.70588085657477, 37.1979648807274], -# ] -# ], -# }, -# "outputType": "geojson", -# "geometryType": ["polygon"], -# "filters": { -# "tags": { -# "point": {}, -# "line": {}, -# "polygon": { -# "join_or": {}, -# "join_and": { -# "destroyed:building": ["yes"], -# "damage:date": ["2023-02-06"], -# }, -# }, -# }, -# "attributes": { -# "point": [], -# "line": [], -# "polygon": [ -# "building", -# "destroyed:building", -# "damage:date", -# "name", -# "source", -# ], -# }, -# }, -# }, -# ) -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_authentication_uuid(): -# headers = {"access-token": access_token} -# payload = { -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# }, -# "uuid": False, -# } - -# response = client.post("/v1/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_snapshot_bind_zip(): -# headers = {"access-token": access_token} -# payload = { -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# }, -# "bindZip": False, -# } - -# response = client.post("/v1/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# ## Snapshot Plain - - -# def test_snapshot_plain(): -# response = client.post( -# "/v1/snapshot/plain/", -# json={ -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# } -# }, -# ) -# assert response.status_code == 200 - - -# ## Stats - - -# def test_stats_endpoint_custom_polygon(): -# headers = {"access-token": access_token} -# payload = { -# "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/stats/polygon/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# assert ( -# res["meta"]["indicators"] -# == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" -# ) - - -# def test_stats_endpoint_iso3(): -# headers = {"access-token": access_token} -# payload = {"iso3": "npl"} - -# response = client.post("/v1/stats/polygon/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# assert ( -# res["meta"]["indicators"] -# == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" -# ) - - -# ## HDX - - -# def test_hdx_submit_normal_iso3(): -# headers = {"access-token": access_token} -# payload = { -# "iso3": "NPL", -# "hdx_upload": False, -# "categories": [ -# { -# "Roads": { -# "hdx": { -# "tags": ["roads", "transportation", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines"], -# "select": ["name", "highway"], -# "where": "tags['highway'] IS NOT NULL", -# "formats": ["geojson"], -# } -# } -# ], -# } - -# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_hdx_submit_normal_iso3_multiple_format(): -# headers = {"access-token": access_token} -# payload = { -# "iso3": "NPL", -# "hdx_upload": False, -# "categories": [ -# { -# "Roads": { -# "hdx": { -# "tags": ["roads", "transportation", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines"], -# "select": ["name", "highway"], -# "where": "tags['highway'] IS NOT NULL", -# "formats": ["geojson", "gpkg", "kml", "shp"], -# } -# } -# ], -# } - -# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_hdx_submit_normal_custom_polygon(): -# headers = {"access-token": access_token} -# payload = { -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# }, -# "hdx_upload": False, -# "dataset": { -# "subnational": True, -# "dataset_title": "Pokhara", -# "dataset_prefix": "hotosm_pkr", -# "dataset_locations": ["npl"], -# }, -# "categories": [ -# { -# "Roads": { -# "hdx": { -# "tags": ["roads", "transportation", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines"], -# "select": ["name", "highway"], -# "where": "tags['highway'] IS NOT NULL", -# "formats": ["geojson"], -# } -# } -# ], -# } - -# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_custom_submit_normal_custom_polygon_TM_project(): -# headers = {"access-token": access_token} -# payload = { -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# }, -# "queue": "raw_default", -# "dataset": { -# "dataset_prefix": "hotosm_project_1", -# "dataset_folder": "TM", -# "dataset_title": "Tasking Manger Project 1", -# }, -# "categories": [ -# { -# "Buildings": { -# "types": ["polygons"], -# "select": [ -# "name", -# "building", -# "building:levels", -# "building:materials", -# "addr:full", -# "addr:housenumber", -# "addr:street", -# "addr:city", -# "office", -# "source", -# ], -# "where": "tags['building'] IS NOT NULL", -# "formats": ["geojson", "shp", "kml"], -# }, -# "Roads": { -# "types": ["lines"], -# "select": [ -# "name", -# "highway", -# "surface", -# "smoothness", -# "width", -# "lanes", -# "oneway", -# "bridge", -# "layer", -# "source", -# ], -# "where": "tags['highway'] IS NOT NULL", -# "formats": ["geojson", "shp", "kml"], -# }, -# "Waterways": { -# "types": ["lines", "polygons"], -# "select": [ -# "name", -# "waterway", -# "covered", -# "width", -# "depth", -# "layer", -# "blockage", -# "tunnel", -# "natural", -# "water", -# "source", -# ], -# "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", -# "formats": ["geojson", "shp", "kml"], -# }, -# "Landuse": { -# "types": ["points", "polygons"], -# "select": ["name", "amenity", "landuse", "leisure"], -# "where": "tags['landuse'] IS NOT NULL", -# "formats": ["geojson", "shp", "kml"], -# }, -# } -# ], -# } - -# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_hdx_submit_normal_custom_polygon_upload(): -# headers = {"access-token": access_token} -# payload = { -# "geometry": { -# "type": "Polygon", -# "coordinates": [ -# [ -# [83.96919250488281, 28.194446860487773], -# [83.99751663208006, 28.194446860487773], -# [83.99751663208006, 28.214869548073377], -# [83.96919250488281, 28.214869548073377], -# [83.96919250488281, 28.194446860487773], -# ] -# ], -# }, -# "hdx_upload": True, -# "dataset": { -# "subnational": True, -# "dataset_title": "Pokhara", -# "dataset_folder": "Test", -# "dataset_prefix": "hotosm_pkr", -# "dataset_locations": ["npl"], -# }, -# "categories": [ -# { -# "Roads": { -# "hdx": { -# "tags": ["roads", "transportation", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines"], -# "select": ["name", "highway"], -# "where": "tags['highway'] IS NOT NULL", -# "formats": ["geojson"], -# } -# } -# ], -# } - -# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# max_attempts = 6 -# interval_seconds = 10 -# for attempt in range(1, max_attempts + 1): -# time.sleep(interval_seconds) # wait for worker to complete task - -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] - -# if check_status == "SUCCESS": -# break # exit the loop if the status is SUCCESS - -# if attempt == max_attempts: -# # If max_attempts reached and status is not SUCCESS, raise an AssertionError -# assert ( -# False -# ), f"Task did not complete successfully after {max_attempts} attempts" - - -# def test_full_hdx_set_iso(): -# headers = {"access-token": access_token} -# payload = { -# "iso3": "NPL", -# "hdx_upload": False, -# "categories": [ -# { -# "Buildings": { -# "hdx": { -# "tags": [ -# "facilities-infrastructure", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["polygons"], -# "select": [ -# "name", -# "building", -# "building:levels", -# "building:materials", -# "addr:full", -# "addr:housenumber", -# "addr:street", -# "addr:city", -# "office", -# "source", -# ], -# "where": "tags['building'] IS NOT NULL", -# "formats": ["geojson"], -# } -# }, -# { -# "Roads": { -# "hdx": { -# "tags": ["transportation", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines"], -# "select": [ -# "name", -# "highway", -# "surface", -# "smoothness", -# "width", -# "lanes", -# "oneway", -# "bridge", -# "layer", -# "source", -# ], -# "where": "tags['highway'] IS NOT NULL", -# "formats": ["geojson"], -# } -# }, -# { -# "Waterways": { -# "hdx": { -# "tags": ["hydrology", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines", "polygons"], -# "select": [ -# "name", -# "waterway", -# "covered", -# "width", -# "depth", -# "layer", -# "blockage", -# "tunnel", -# "natural", -# "water", -# "source", -# ], -# "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", -# "formats": ["geojson"], -# } -# }, -# { -# "Points of Interest": { -# "hdx": { -# "tags": [ -# "facilities-infrastructure", -# "points of interest-poi", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points", "polygons"], -# "select": [ -# "name", -# "amenity", -# "man_made", -# "shop", -# "tourism", -# "opening_hours", -# "beds", -# "rooms", -# "addr:full", -# "addr:housenumber", -# "addr:street", -# "addr:city", -# "source", -# ], -# "where": "tags['amenity'] IS NOT NULL OR tags['man_made'] IS NOT NULL OR tags['shop'] IS NOT NULL OR tags['tourism'] IS NOT NULL", -# "formats": ["geojson"], -# } -# }, -# { -# "Airports": { -# "hdx": { -# "tags": [ -# "aviation", -# "facilities-infrastructure", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points", "lines", "polygons"], -# "select": [ -# "name", -# "aeroway", -# "building", -# "emergency", -# "emergency:helipad", -# "operator:type", -# "capacity:persons", -# "addr:full", -# "addr:city", -# "source", -# ], -# "where": "tags['aeroway'] IS NOT NULL OR tags['building'] = 'aerodrome' OR tags['emergency:helipad'] IS NOT NULL OR tags['emergency'] = 'landing_site'", -# "formats": ["geojson"], -# } -# }, -# { -# "Sea Ports": { -# "hdx": { -# "tags": [ -# "facilities-infrastructure", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points", "lines", "polygons"], -# "select": [ -# "name", -# "amenity", -# "building", -# "port", -# "operator:type", -# "addr:full", -# "addr:city", -# "source", -# ], -# "where": "tags['amenity'] = 'ferry_terminal' OR tags['building'] = 'ferry_terminal' OR tags['port'] IS NOT NULL", -# "formats": ["geojson"], -# } -# }, -# { -# "Education Facilities": { -# "hdx": { -# "tags": [ -# "education facilities-schools", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points", "polygons"], -# "select": [ -# "name", -# "amenity", -# "building", -# "operator:type", -# "capacity:persons", -# "addr:full", -# "addr:city", -# "source", -# ], -# "where": "tags['amenity'] IN ('kindergarten', 'school', 'college', 'university') OR building IN ('kindergarten', 'school', 'college', 'university')", -# "formats": ["geojson"], -# } -# }, -# { -# "Health Facilities": { -# "hdx": { -# "tags": ["geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points", "polygons"], -# "select": [ -# "name", -# "amenity", -# "building", -# "healthcare", -# "healthcare:speciality", -# "operator:type", -# "capacity:persons", -# "addr:full", -# "addr:city", -# "source", -# ], -# "where": "tags['healthcare'] IS NOT NULL OR tags['amenity'] IN ('doctors', 'dentist', 'clinic', 'hospital', 'pharmacy')", -# "formats": ["geojson"], -# } -# }, -# { -# "Populated Places": { -# "hdx": { -# "tags": [ -# "populated places-settlements", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points"], -# "select": [ -# "name", -# "place", -# "population", -# "is_in", -# "source", -# ], -# "where": "tags['place'] IN ('isolated_dwelling', 'town', 'village', 'hamlet', 'city')", -# "formats": ["geojson"], -# } -# }, -# { -# "Financial Services": { -# "hdx": { -# "tags": ["economics", "geodata"], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["points", "polygons"], -# "select": [ -# "name", -# "amenity", -# "operator", -# "network", -# "addr:full", -# "addr:city", -# "source", -# ], -# "where": "tags['amenity'] IN ('mobile_money_agent','bureau_de_change','bank','microfinance','atm','sacco','money_transfer','post_office')", -# "formats": ["geojson"], -# } -# }, -# { -# "Railways": { -# "hdx": { -# "tags": [ -# "facilities-infrastructure", -# "railways", -# "transportation", -# "geodata", -# ], -# "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", -# }, -# "types": ["lines"], -# "select": [ -# "name", -# "railway", -# "ele", -# "operator:type", -# "layer", -# "addr:full", -# "addr:city", -# "source", -# ], -# "where": "tags['railway'] IN ('rail','station')", -# "formats": ["geojson"], -# } -# }, -# ], -# } - -# response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) - -# assert response.status_code == 200 -# res = response.json() -# track_link = res["track_link"] -# time.sleep(60) # wait for worker to complete task -# response = client.get(f"/v1{track_link}") -# assert response.status_code == 200 -# res = response.json() -# check_status = res["status"] -# assert check_status == "SUCCESS" - - -# ## Tasks connection - - -# def test_worker_connection(): -# response = client.get("/v1/tasks/ping/") -# assert response.status_code == 200 +client = TestClient(app) + +access_token = os.environ.get("ACCESS_TOKEN") + +## Status + + +def test_status(): + response = client.get("/v1/status/") + assert response.status_code == 200 + + +## Login +def test_login_url(): + response = client.get("/v1/auth/login/") + assert response.status_code == 200 + + +def test_login_auth_me(): + headers = {"access-token": access_token} + response = client.get("/v1/auth/me/", headers=headers) + assert response.status_code == 200 + + +## Countries + + +def test_countries_endpoint(): + response = client.get("/v1/countries/?q=nepal") + assert response.status_code == 200 + + +## test osm_id + + +def test_osm_id_endpoint(): + response = client.get("/v1/osm_id/?osm_id=421498318") + assert response.status_code == 200 + + +## Snapshot +def test_snapshot(): + response = client.post( + "/v1/snapshot/", + json={ + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + } + }, + ) + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_featurecollection(): + response = client.post( + "/v1/snapshot/", + json={ + "geometry": { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [83.97346137271688, 28.217525272345284], + [83.97346137271688, 28.192595937414737], + [84.01473909818759, 28.192595937414737], + [84.01473909818759, 28.217525272345284], + [83.97346137271688, 28.217525272345284], + ] + ], + "type": "Polygon", + }, + } + ], + } + }, + ) + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_feature(): + response = client.post( + "/v1/snapshot/", + json={ + "geometry": { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [83.97346137271688, 28.217525272345284], + [83.97346137271688, 28.192595937414737], + [84.01473909818759, 28.192595937414737], + [84.01473909818759, 28.217525272345284], + [83.97346137271688, 28.217525272345284], + ] + ], + "type": "Polygon", + }, + } + }, + ) + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_centroid(): + response = client.post( + "/v1/snapshot/", + json={ + "centroid": True, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + }, + ) + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_filters(): + response = client.post( + "/v1/snapshot/", + json={ + "fileName": "Example export with all features", + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.585701, 28.046607], + [83.585701, 28.382561], + [84.391823, 28.382561], + [84.391823, 28.046607], + [83.585701, 28.046607], + ] + ], + }, + "outputType": "geojson", + "geometryType": ["point", "line", "polygon"], + "filters": { + "tags": { + "point": { + "join_or": { + "amenity": [ + "bank", + "ferry_terminal", + "bus_station", + "fuel", + "kindergarten", + "school", + "college", + "university", + "place_of_worship", + "marketplace", + "clinic", + "hospital", + "police", + "fire_station", + ], + "building": [ + "bank", + "aerodrome", + "ferry_terminal", + "train_station", + "bus_station", + "pumping_station", + "power_substation", + "kindergarten", + "school", + "college", + "university", + "mosque ", + " church ", + " temple", + "supermarket", + "marketplace", + "clinic", + "hospital", + "police", + "fire_station", + "stadium ", + " sports_centre", + "governor_office ", + " townhall ", + " subdistrict_office ", + " village_office ", + " community_group_office", + "government_office", + ], + "man_made": ["tower", "water_tower", "pumping_station"], + "tower:type": ["communication"], + "aeroway": ["aerodrome"], + "railway": ["station"], + "emergency": ["fire_hydrant"], + "landuse": ["reservoir", "recreation_gound"], + "waterway": ["floodgate"], + "natural": ["spring"], + "power": ["tower", "substation"], + "shop": ["supermarket"], + "leisure": [ + "stadium ", + " sports_centre ", + " pitch ", + " swimming_pool", + "park", + ], + "office": ["government"], + } + }, + "line": { + "join_or": { + "highway": [ + "motorway ", + " trunk ", + " primary ", + " secondary ", + " tertiary ", + " service ", + " residential ", + " pedestrian ", + " path ", + " living_street ", + " track", + ], + "railway": ["rail"], + "man_made": ["embankment"], + "waterway": [], + } + }, + "polygon": { + "join_or": { + "amenity": [ + "bank", + "ferry_terminal", + "bus_station", + "fuel", + "kindergarten", + "school", + "college", + "university", + "place_of_worship", + "marketplace", + "clinic", + "hospital", + "police", + "fire_station", + ], + "building": [ + "bank", + "aerodrome", + "ferry_terminal", + "train_station", + "bus_station", + "pumping_station", + "power_substation", + "power_plant", + "kindergarten", + "school", + "college", + "university", + "mosque ", + " church ", + " temple", + "supermarket", + "marketplace", + "clinic", + "hospital", + "police", + "fire_station", + "stadium ", + " sports_centre", + "governor_office ", + " townhall ", + " subdistrict_office ", + " village_office ", + " community_group_office", + "government_office", + ], + "man_made": ["tower", "water_tower", "pumping_station"], + "tower:type": ["communication"], + "aeroway": ["aerodrome"], + "railway": ["station"], + "landuse": ["reservoir", "recreation_gound"], + "waterway": [], + "natural": ["spring"], + "power": ["substation", "plant"], + "shop": ["supermarket"], + "leisure": [ + "stadium ", + " sports_centre ", + " pitch ", + " swimming_pool", + "park", + ], + "office": ["government"], + "type": ["boundary"], + "boundary": ["administrative"], + }, + }, + }, + "attributes": { + "point": [ + "building", + "ground_floor:height", + "capacity:persons", + "building:structure", + "building:condition", + "name", + "admin_level", + "building:material", + "office", + "building:roof", + "backup_generator", + "access:roof", + "building:levels", + "building:floor", + "addr:full", + "addr:city", + "source", + ], + "line": ["width", "source", "waterway", "name"], + "polygon": [ + "landslide_prone", + "name", + "admin_level", + "type", + "is_in:town", + "flood_prone", + "is_in:province", + "is_in:city", + "is_in:municipality", + "is_in:RW", + "is_in:village", + "source", + "boundary", + ], + }, + }, + }, + ) + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_and_filter(): + response = client.post( + "/v1/snapshot/", + json={ + "fileName": "Destroyed_Buildings_Turkey", + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [36.70588085657477, 37.1979648807274], + [36.70588085657477, 37.1651408422983], + [36.759267544807194, 37.1651408422983], + [36.759267544807194, 37.1979648807274], + [36.70588085657477, 37.1979648807274], + ] + ], + }, + "outputType": "geojson", + "geometryType": ["polygon"], + "filters": { + "tags": { + "point": {}, + "line": {}, + "polygon": { + "join_or": {}, + "join_and": { + "destroyed:building": ["yes"], + "damage:date": ["2023-02-06"], + }, + }, + }, + "attributes": { + "point": [], + "line": [], + "polygon": [ + "building", + "destroyed:building", + "damage:date", + "name", + "source", + ], + }, + }, + }, + ) + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_authentication_uuid(): + headers = {"access-token": access_token} + payload = { + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + "uuid": False, + } + + response = client.post("/v1/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_snapshot_bind_zip(): + headers = {"access-token": access_token} + payload = { + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + "bindZip": False, + } + + response = client.post("/v1/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +## Snapshot Plain + + +def test_snapshot_plain(): + response = client.post( + "/v1/snapshot/plain/", + json={ + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + } + }, + ) + assert response.status_code == 200 + + +## Stats + + +def test_stats_endpoint_custom_polygon(): + headers = {"access-token": access_token} + payload = { + "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/stats/polygon/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + assert ( + res["meta"]["indicators"] + == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" + ) + + +def test_stats_endpoint_iso3(): + headers = {"access-token": access_token} + payload = {"iso3": "npl"} + + response = client.post("/v1/stats/polygon/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + assert ( + res["meta"]["indicators"] + == "https://github.com/hotosm/raw-data-api/tree/develop/docs/src/stats/indicators.md" + ) + + +## HDX + + +def test_hdx_submit_normal_iso3(): + headers = {"access-token": access_token} + payload = { + "iso3": "NPL", + "hdx_upload": False, + "categories": [ + { + "Roads": { + "hdx": { + "tags": ["roads", "transportation", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines"], + "select": ["name", "highway"], + "where": "tags['highway'] IS NOT NULL", + "formats": ["geojson"], + } + } + ], + } + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_hdx_submit_normal_iso3_multiple_format(): + headers = {"access-token": access_token} + payload = { + "iso3": "NPL", + "hdx_upload": False, + "categories": [ + { + "Roads": { + "hdx": { + "tags": ["roads", "transportation", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines"], + "select": ["name", "highway"], + "where": "tags['highway'] IS NOT NULL", + "formats": ["geojson", "gpkg", "kml", "shp"], + } + } + ], + } + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_hdx_submit_normal_custom_polygon(): + headers = {"access-token": access_token} + payload = { + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + "hdx_upload": False, + "dataset": { + "subnational": True, + "dataset_title": "Pokhara", + "dataset_prefix": "hotosm_pkr", + "dataset_locations": ["npl"], + }, + "categories": [ + { + "Roads": { + "hdx": { + "tags": ["roads", "transportation", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines"], + "select": ["name", "highway"], + "where": "tags['highway'] IS NOT NULL", + "formats": ["geojson"], + } + } + ], + } + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_custom_submit_normal_custom_polygon_TM_project(): + headers = {"access-token": access_token} + payload = { + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + "queue": "raw_default", + "dataset": { + "dataset_prefix": "hotosm_project_1", + "dataset_folder": "TM", + "dataset_title": "Tasking Manger Project 1", + }, + "categories": [ + { + "Buildings": { + "types": ["polygons"], + "select": [ + "name", + "building", + "building:levels", + "building:materials", + "addr:full", + "addr:housenumber", + "addr:street", + "addr:city", + "office", + "source", + ], + "where": "tags['building'] IS NOT NULL", + "formats": ["geojson", "shp", "kml"], + }, + "Roads": { + "types": ["lines"], + "select": [ + "name", + "highway", + "surface", + "smoothness", + "width", + "lanes", + "oneway", + "bridge", + "layer", + "source", + ], + "where": "tags['highway'] IS NOT NULL", + "formats": ["geojson", "shp", "kml"], + }, + "Waterways": { + "types": ["lines", "polygons"], + "select": [ + "name", + "waterway", + "covered", + "width", + "depth", + "layer", + "blockage", + "tunnel", + "natural", + "water", + "source", + ], + "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", + "formats": ["geojson", "shp", "kml"], + }, + "Landuse": { + "types": ["points", "polygons"], + "select": ["name", "amenity", "landuse", "leisure"], + "where": "tags['landuse'] IS NOT NULL", + "formats": ["geojson", "shp", "kml"], + }, + } + ], + } + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_hdx_submit_normal_custom_polygon_upload(): + headers = {"access-token": access_token} + payload = { + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + "hdx_upload": True, + "dataset": { + "subnational": True, + "dataset_title": "Pokhara", + "dataset_folder": "Test", + "dataset_prefix": "hotosm_pkr", + "dataset_locations": ["npl"], + }, + "categories": [ + { + "Roads": { + "hdx": { + "tags": ["roads", "transportation", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines"], + "select": ["name", "highway"], + "where": "tags['highway'] IS NOT NULL", + "formats": ["geojson"], + } + } + ], + } + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + max_attempts = 6 + interval_seconds = 10 + for attempt in range(1, max_attempts + 1): + time.sleep(interval_seconds) # wait for worker to complete task + + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + + if check_status == "SUCCESS": + break # exit the loop if the status is SUCCESS + + if attempt == max_attempts: + # If max_attempts reached and status is not SUCCESS, raise an AssertionError + assert ( + False + ), f"Task did not complete successfully after {max_attempts} attempts" + + +def test_full_hdx_set_iso(): + headers = {"access-token": access_token} + payload = { + "iso3": "NPL", + "hdx_upload": False, + "categories": [ + { + "Buildings": { + "hdx": { + "tags": [ + "facilities-infrastructure", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["polygons"], + "select": [ + "name", + "building", + "building:levels", + "building:materials", + "addr:full", + "addr:housenumber", + "addr:street", + "addr:city", + "office", + "source", + ], + "where": "tags['building'] IS NOT NULL", + "formats": ["geojson"], + } + }, + { + "Roads": { + "hdx": { + "tags": ["transportation", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines"], + "select": [ + "name", + "highway", + "surface", + "smoothness", + "width", + "lanes", + "oneway", + "bridge", + "layer", + "source", + ], + "where": "tags['highway'] IS NOT NULL", + "formats": ["geojson"], + } + }, + { + "Waterways": { + "hdx": { + "tags": ["hydrology", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines", "polygons"], + "select": [ + "name", + "waterway", + "covered", + "width", + "depth", + "layer", + "blockage", + "tunnel", + "natural", + "water", + "source", + ], + "where": "tags['waterway'] IS NOT NULL OR tags['water'] IS NOT NULL OR tags['natural'] IN ('water','wetland','bay')", + "formats": ["geojson"], + } + }, + { + "Points of Interest": { + "hdx": { + "tags": [ + "facilities-infrastructure", + "points of interest-poi", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points", "polygons"], + "select": [ + "name", + "amenity", + "man_made", + "shop", + "tourism", + "opening_hours", + "beds", + "rooms", + "addr:full", + "addr:housenumber", + "addr:street", + "addr:city", + "source", + ], + "where": "tags['amenity'] IS NOT NULL OR tags['man_made'] IS NOT NULL OR tags['shop'] IS NOT NULL OR tags['tourism'] IS NOT NULL", + "formats": ["geojson"], + } + }, + { + "Airports": { + "hdx": { + "tags": [ + "aviation", + "facilities-infrastructure", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points", "lines", "polygons"], + "select": [ + "name", + "aeroway", + "building", + "emergency", + "emergency:helipad", + "operator:type", + "capacity:persons", + "addr:full", + "addr:city", + "source", + ], + "where": "tags['aeroway'] IS NOT NULL OR tags['building'] = 'aerodrome' OR tags['emergency:helipad'] IS NOT NULL OR tags['emergency'] = 'landing_site'", + "formats": ["geojson"], + } + }, + { + "Sea Ports": { + "hdx": { + "tags": [ + "facilities-infrastructure", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points", "lines", "polygons"], + "select": [ + "name", + "amenity", + "building", + "port", + "operator:type", + "addr:full", + "addr:city", + "source", + ], + "where": "tags['amenity'] = 'ferry_terminal' OR tags['building'] = 'ferry_terminal' OR tags['port'] IS NOT NULL", + "formats": ["geojson"], + } + }, + { + "Education Facilities": { + "hdx": { + "tags": [ + "education facilities-schools", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points", "polygons"], + "select": [ + "name", + "amenity", + "building", + "operator:type", + "capacity:persons", + "addr:full", + "addr:city", + "source", + ], + "where": "tags['amenity'] IN ('kindergarten', 'school', 'college', 'university') OR building IN ('kindergarten', 'school', 'college', 'university')", + "formats": ["geojson"], + } + }, + { + "Health Facilities": { + "hdx": { + "tags": ["geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points", "polygons"], + "select": [ + "name", + "amenity", + "building", + "healthcare", + "healthcare:speciality", + "operator:type", + "capacity:persons", + "addr:full", + "addr:city", + "source", + ], + "where": "tags['healthcare'] IS NOT NULL OR tags['amenity'] IN ('doctors', 'dentist', 'clinic', 'hospital', 'pharmacy')", + "formats": ["geojson"], + } + }, + { + "Populated Places": { + "hdx": { + "tags": [ + "populated places-settlements", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points"], + "select": [ + "name", + "place", + "population", + "is_in", + "source", + ], + "where": "tags['place'] IN ('isolated_dwelling', 'town', 'village', 'hamlet', 'city')", + "formats": ["geojson"], + } + }, + { + "Financial Services": { + "hdx": { + "tags": ["economics", "geodata"], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["points", "polygons"], + "select": [ + "name", + "amenity", + "operator", + "network", + "addr:full", + "addr:city", + "source", + ], + "where": "tags['amenity'] IN ('mobile_money_agent','bureau_de_change','bank','microfinance','atm','sacco','money_transfer','post_office')", + "formats": ["geojson"], + } + }, + { + "Railways": { + "hdx": { + "tags": [ + "facilities-infrastructure", + "railways", + "transportation", + "geodata", + ], + "caveats": "OpenStreetMap data is crowd sourced and cannot be considered to be exhaustive", + }, + "types": ["lines"], + "select": [ + "name", + "railway", + "ele", + "operator:type", + "layer", + "addr:full", + "addr:city", + "source", + ], + "where": "tags['railway'] IN ('rail','station')", + "formats": ["geojson"], + } + }, + ], + } + + response = client.post("/v1/custom/snapshot/", json=payload, headers=headers) + + assert response.status_code == 200 + res = response.json() + track_link = res["track_link"] + time.sleep(60) # wait for worker to complete task + response = client.get(f"/v1{track_link}") + assert response.status_code == 200 + res = response.json() + check_status = res["status"] + assert check_status == "SUCCESS" + + +## Tasks connection + + +def test_worker_connection(): + response = client.get("/v1/tasks/ping/") + assert response.status_code == 200