From db854a3d9e173fa3a0e6269a26595951663a52f5 Mon Sep 17 00:00:00 2001 From: FrankApiyo Date: Wed, 8 Jan 2025 14:31:30 +0300 Subject: [PATCH] Add test cases for export_tools --- onadata/libs/tests/utils/test_export_tools.py | 46 +++++++++++++++++++ onadata/libs/utils/api_export_tools.py | 2 +- onadata/libs/utils/export_tools.py | 21 +++++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/onadata/libs/tests/utils/test_export_tools.py b/onadata/libs/tests/utils/test_export_tools.py index 26bf5169a6..2877600ad8 100644 --- a/onadata/libs/tests/utils/test_export_tools.py +++ b/onadata/libs/tests/utils/test_export_tools.py @@ -47,6 +47,7 @@ generate_geojson_export, generate_kml_export, generate_osm_export, + get_query_params_from_metadata, get_repeat_index_tags, kml_export_data, parse_request_export_options, @@ -277,6 +278,51 @@ def test_should_not_create_new_export_fn(self): self.assertFalse(will_create_new_export) + def test_get_query_params_from_metadata_fn(self): + self._publish_transportation_form_and_submit_instance() + metadata = MetaData.objects.create( + content_type=ContentType.objects.get_for_model(XForm), + data_type="media", + data_value=f"xform_geojson {self.xform.id} testgeojson", + extra_data={ + "data_title": "start", + "data_fields": "", + "data_geo_field": "qn09", + "data_simple_style": True, + }, + object_id=self.xform.id, + ) + self.assertEqual( + { + "title": "start", + "fields": "", + "geo_field": "qn09", + "simple_style": True, + }, + get_query_params_from_metadata(metadata), + ) + + metadata.delete() + metadata = MetaData.objects.create( + content_type=ContentType.objects.get_for_model(XForm), + data_type="media", + data_value=f"xform_geojson {self.xform.id} testgeojson", + extra_data={ + "data_title": "start", + "data_fields": "", + "data_geo_field": "qn09", + }, + object_id=self.xform.id, + ) + self.assertEqual( + { + "title": "start", + "fields": "", + "geo_field": "qn09", + }, + get_query_params_from_metadata(metadata), + ) + def test_should_not_create_new_export_when_old_exists(self): export_type = "geojson" self._publish_transportation_form_and_submit_instance() diff --git a/onadata/libs/utils/api_export_tools.py b/onadata/libs/utils/api_export_tools.py index 3fa4988007..a80daca5bf 100644 --- a/onadata/libs/utils/api_export_tools.py +++ b/onadata/libs/utils/api_export_tools.py @@ -152,7 +152,7 @@ def custom_response_handler( # noqa: C0901 ): export_type = Export.EXTERNAL_EXPORT - metadata_query_params = get_query_params_from_metadata(metadata) + metadata_query_params = get_query_params_from_metadata(metadata) or {} options = { **parse_request_export_options(request.query_params), diff --git a/onadata/libs/utils/export_tools.py b/onadata/libs/utils/export_tools.py index cda0b4d59a..0c931313f6 100644 --- a/onadata/libs/utils/export_tools.py +++ b/onadata/libs/utils/export_tools.py @@ -642,13 +642,24 @@ def generate_kml_export( def get_query_params_from_metadata(metadata): + """ + Build out query params to be used in GeoJsonSerializer + """ + if metadata is None: + return None + extra_data = metadata.extra_data - # build out query params to be used in GeoJsonSerializer + keys_mapping = { + "data_geo_field": "geo_field", + "data_simple_style": "simple_style", + "data_title": "title", + "data_fields": "fields", + } + return { - "geo_field": extra_data.get("data_geo_field"), - "simple_style": extra_data.get("data_simple_style"), - "title": extra_data.get("data_title"), - "fields": extra_data.get("data_fields"), + mapped_key: extra_data[original_key] + for original_key, mapped_key in keys_mapping.items() + if original_key in extra_data }