Skip to content

Commit

Permalink
Add tests for geojson exports
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Jun 27, 2022
1 parent da4f8b1 commit 6c8913f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
17 changes: 15 additions & 2 deletions onadata/apps/api/tests/viewsets/test_abstract_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,21 @@ def _post_metadata(self, data, test=True):

return response

def _add_form_metadata(self, xform, data_type, data_value, path=None, test=True):
data = {"data_type": data_type, "data_value": data_value, "xform": xform.id}
def _add_form_metadata(
self,
xform,
data_type,
data_value,
extra_data=None,
path=None,
test=True
):
data = {
"data_type": data_type,
"data_value": data_value,
"xform": xform.id,
"extra_data": extra_data
}

if path and data_value:
with open(path, "rb") as media_file:
Expand Down
8 changes: 7 additions & 1 deletion onadata/apps/api/tests/viewsets/test_metadata_viewset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json
from builtins import open

from django.conf import settings
Expand Down Expand Up @@ -274,7 +275,12 @@ def test_add_media_xform_link(self):
def test_add_media_geojson_link(self):
data_type = 'media'
data_value = 'geojson {} transportation'.format(self.xform.pk)
self._add_form_metadata(self.xform, data_type, data_value, True)
extra_data = {
"data_title": "test",
"data_simple_style": True,
"data_geo_field": "test"
}
self._add_form_metadata(self.xform, data_type, data_value, extra_data=json.dumps(extra_data))
self.assertIsNotNone(self.metadata_data['media_url'])
request = self.factory.get('/', **self.extra)
ext = self.data_value[self.data_value.rindex('.') + 1:]
Expand Down
47 changes: 46 additions & 1 deletion onadata/libs/tests/utils/test_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
import tempfile
import zipfile
import json
from builtins import open
from datetime import date, datetime, timedelta

Expand All @@ -22,6 +23,8 @@
from savReaderWriter import SavWriter

from onadata.apps.api import tests as api_tests
from onadata.apps.api.tests.viewsets.test_abstract_viewset import (
TestAbstractViewSet)
from onadata.apps.logger.models import Attachment, Instance, XForm
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.viewer.models.export import Export
Expand All @@ -36,6 +39,7 @@
generate_export,
generate_kml_export,
generate_osm_export,
generate_geojson_export,
get_repeat_index_tags,
kml_export_data,
parse_request_export_options,
Expand All @@ -48,7 +52,7 @@ def _logger_fixture_path(*args):
return os.path.join(settings.PROJECT_ROOT, "libs", "tests", "fixtures", *args)


class TestExportTools(TestBase):
class TestExportTools(TestBase, TestAbstractViewSet):
"""
Test export_tools functions.
"""
Expand Down Expand Up @@ -532,6 +536,47 @@ def test_kml_exports(self):
self.assertIsNotNone(export)
self.assertTrue(export.is_successful)

def test_geojson_exports(self):
"""
Test generate_geojson_export()
"""
export_type = "geojson"
options = {
"extension": "geojson",
}
self._publish_transportation_form_and_submit_instance()
# set metadata to xform
data_type = "media"
data_value = 'geojson {} {}'.format(self.xform.pk, self.xform.id_string)
extra_data = {
"data_title": "test",
"data_geo_field": "test",
"data_simple_style": True
}
self.extra = {"HTTP_AUTHORIZATION": f"Token {self.user.auth_token}"}
response = self._add_form_metadata(self.xform, data_type, data_value, extra_data=json.dumps(extra_data))
self.assertEqual(response.status_code, 201)

username = self.xform.user.username
id_string = self.xform.id_string
# get metadata instance and pass to geojson export util function
self.assertEqual(self.xform.metadata_set.count(), 1)
metadata = self.xform.metadata_set.get(id=1)
export = generate_geojson_export(export_type, username, id_string, metadata, options=options)
self.assertIsNotNone(export)
self.assertTrue(export.is_successful)

export_id = export.id

export.delete()

export = generate_geojson_export(
export_type, username, id_string, metadata, export_id=export_id, options=options
)

self.assertIsNotNone(export)
self.assertTrue(export.is_successful)

def test_str_to_bool(self):
self.assertTrue(str_to_bool(True))
self.assertTrue(str_to_bool("True"))
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/utils/export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def generate_geojson_export(
export_type,
username,
id_string,
metadata,
metadata=None,
export_id=None,
options=None,
xform=None
Expand Down

0 comments on commit 6c8913f

Please sign in to comment.