Skip to content

Commit

Permalink
Add test cases for export_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Jan 8, 2025
1 parent b31c692 commit db854a3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
46 changes: 46 additions & 0 deletions onadata/libs/tests/utils/test_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/utils/api_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 16 additions & 5 deletions onadata/libs/utils/export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down

0 comments on commit db854a3

Please sign in to comment.