From 572abd8db90da523d19b6d093f9f9ab39f36133a Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 13 May 2018 13:30:44 -0400 Subject: [PATCH] Added unit tests for grass engine --- app/boot.py | 2 +- app/plugins/grass_engine.py | 3 +- app/tests/grass_scripts/simple_test.grass | 6 ++++ app/tests/test_plugins.py | 43 +++++++++++++++++++++-- webodm/settings.py | 5 ++- 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 app/tests/grass_scripts/simple_test.grass diff --git a/app/boot.py b/app/boot.py index ef85792b..e6fc21d5 100644 --- a/app/boot.py +++ b/app/boot.py @@ -37,7 +37,7 @@ def boot(): # Make sure our app/media/tmp folder exists if not os.path.exists(settings.MEDIA_TMP): - os.mkdir(settings.MEDIA_TMP) + os.makedirs(settings.MEDIA_TMP) # Check default group try: diff --git a/app/plugins/grass_engine.py b/app/plugins/grass_engine.py index 5d400945..021824ee 100644 --- a/app/plugins/grass_engine.py +++ b/app/plugins/grass_engine.py @@ -60,7 +60,7 @@ class GrassContext: """ :param location: either a "epsg:XXXXX" string or a path to a geospatial file defining the location """ - if not location.startswith('epsg:'): + if not location.lower().startswith('epsg:'): location = os.path.abspath(location) self.location = location @@ -107,6 +107,7 @@ class GrassContext: } def __del__(self): + pass # Cleanup if os.path.exists(self.get_cwd()): shutil.rmtree(self.get_cwd()) diff --git a/app/tests/grass_scripts/simple_test.grass b/app/tests/grass_scripts/simple_test.grass new file mode 100644 index 00000000..732810c2 --- /dev/null +++ b/app/tests/grass_scripts/simple_test.grass @@ -0,0 +1,6 @@ +# test: Geospatial test file +# ------ +# output: greets the user and prints the information of a spatial file + +v.in.ogr input=${test} layer=test output=test --overwrite +v.info map=test diff --git a/app/tests/test_plugins.py b/app/tests/test_plugins.py index 004d6e9c..16aefae9 100644 --- a/app/tests/test_plugins.py +++ b/app/tests/test_plugins.py @@ -5,6 +5,9 @@ from rest_framework import status from app.plugins import get_plugin_by_name from .classes import BootTestCase +from app.plugins.grass_engine import grass, GrassEngineException + +from worker.tasks import execute_grass_script class TestPlugins(BootTestCase): def setUp(self): @@ -45,5 +48,41 @@ class TestPlugins(BootTestCase): test_plugin = get_plugin_by_name("test") self.assertTrue(os.path.exists(test_plugin.get_path("public/node_modules"))) - # TODO: - # test GRASS engine + def test_grass_engine(self): + cwd = os.path.dirname(os.path.realpath(__file__)) + grass_scripts_dir = os.path.join(cwd, "grass_scripts") + + ctx = grass.create_context() + ctx.add_file('test.geojson', """{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 13.770675659179686, + 45.655328041141374 + ] + } + } + ] +}""") + ctx.set_location("EPSG:4326") + + output = execute_grass_script.delay( + os.path.join(grass_scripts_dir, "simple_test.grass"), + ctx.serialize() + ).get() + self.assertTrue("Number of points: 1" in output) + + error = execute_grass_script.delay( + os.path.join(grass_scripts_dir, "nonexistant_script.grass"), + ctx.serialize() + ).get() + self.assertIsInstance(error, dict) + self.assertIsInstance(error['error'], str) + + with self.assertRaises(GrassEngineException): + ctx.execute(os.path.join(grass_scripts_dir, "nonexistant_script.grass")) diff --git a/webodm/settings.py b/webodm/settings.py index a4f02666..ffdc7ffe 100644 --- a/webodm/settings.py +++ b/webodm/settings.py @@ -250,6 +250,8 @@ CORS_ALLOW_CREDENTIALS = True # File uploads MEDIA_ROOT = os.path.join(BASE_DIR, 'app', 'media') +if TESTING: + MEDIA_ROOT = os.path.join(BASE_DIR, 'app', 'media_test') MEDIA_TMP = os.path.join(MEDIA_ROOT, 'tmp') # Store flash messages in cookies @@ -330,9 +332,6 @@ CELERY_WORKER_HIJACK_ROOT_LOGGER = False if TESTING: CELERY_TASK_ALWAYS_EAGER = True -if TESTING: - MEDIA_ROOT = os.path.join(BASE_DIR, 'app', 'media_test') - try: from .local_settings import * except ImportError: