def test_process_map_calls_map_transformer_correctly_with_loss_to_db(self): """ The map transformer is invoked correctly for a loss map that is to be written to the database. Specifically: the '--shapefile' command line argument is not supplied. """ config = {'jobid': self.job.id} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [_, loss_map] = maps basename = os.path.basename(loss_map.path) self.assertEqual("loss_map", loss_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = (0, "RESULT: (%s, 19.19, 21.21)" % loss_map.id, "") process_map(loss_map, config) args, kwargs = mock_func.call_args expected = ([ '%s/bin/map_transformer.py' % settings.OQ_APIAPP_DIR, '-k', str(loss_map.id), '-p', '%s/computed_output/%s' % (self.job.path, basename), '-t', 'loss' ], ) self.assertEqual(expected, args) self.assertEqual({'ignore_exit_code': True}, kwargs)
def test_process_map_with_shapefile_and_loss_to_db(self): """The db record for the loss map is updated.""" config = {'jobid': self.job.id} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [_, loss_map] = maps self.assertEqual("loss_map", loss_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = (0, "RESULT: (%s, 19.19, 21.21)" % loss_map.id, "") process_map(loss_map, config) self.assertTrue(loss_map.shapefile_path is None) self.assertEqual(19.19, loss_map.min_value) self.assertEqual(21.21, loss_map.max_value)
def test_process_map_with_shapefile_and_loss_to_db(self): """The db record for the loss map is updated.""" config = {'jobid': self.job.id} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [_, loss_map] = maps self.assertEqual("loss_map", loss_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: (%s, 19.19, 21.21)" % loss_map.id, "") process_map(loss_map, config) self.assertTrue(loss_map.shapefile_path is None) self.assertEqual(19.19, loss_map.min_value) self.assertEqual(21.21, loss_map.max_value)
def test_process_map_with_shapefile_and_hazard_to_db(self): """The db record for the hazard map is updated.""" config = {'jobid': self.job.id} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [hazard_map, _] = maps self.assertEqual("hazard_map", hazard_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = (0, "RESULT: (%s, 17.17, 18.18)" % hazard_map.id, "") process_map(hazard_map, config) self.assertTrue(hazard_map.shapefile_path is None) self.assertEqual(17.17, hazard_map.min_value) self.assertEqual(18.18, hazard_map.max_value)
def test_process_map_with_shapefile_and_hazard_to_db(self): """The db record for the hazard map is updated.""" config = {'jobid': self.job.id} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [hazard_map, _] = maps self.assertEqual("hazard_map", hazard_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: (%s, 17.17, 18.18)" % hazard_map.id, "") process_map(hazard_map, config) self.assertTrue(hazard_map.shapefile_path is None) self.assertEqual(17.17, hazard_map.min_value) self.assertEqual(18.18, hazard_map.max_value)
def test_find_maps(self): """ All maps are found. """ expected = [ '%s/hazardmap-0.01-mean.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.50.xml' % self.output_path, '%s/hazardmap-0.1-mean.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.50.xml' % self.output_path, '%s/loss-map-0fcfdbc7.xml' % self.output_path] found = find_maps(self.job) self.assertEqual(expected, list(sorted([output.path for output in found])))
def test_process_map_with_map_transformer_error(self): """ If the shapefile generation fails for a map the db record is *not* updated. """ config = {'jobid': self.job.id, 'shapefile': True} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [hazard_map, _] = maps self.assertEqual("hazard_map", hazard_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = (1, "", "failed to generate shapefile") process_map(hazard_map, config) self.assertTrue(hazard_map.shapefile_path is None) self.assertTrue(hazard_map.min_value is None) self.assertTrue(hazard_map.max_value is None)
def test_find_maps_and_sizes(self): """ All maps are found, the sizes captured in the db records are correct. """ expected = [ '%s/hazardmap-0.01-mean.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.50.xml' % self.output_path, '%s/hazardmap-0.1-mean.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.50.xml' % self.output_path, '%s/loss-map-0fcfdbc7.xml' % self.output_path] sizes = dict([(f, os.path.getsize(f)) for f in expected]) found = find_maps(self.job) for output in found: self.assertEqual(sizes[output.path], output.size)
def test_find_maps_and_job_reference(self): """ All maps are found, the db records refer to the correct job. """ expected = [('hazardmap-0.01-mean.xml', self.job), ('hazardmap-0.01-quantile-0.25.xml', self.job), ('hazardmap-0.01-quantile-0.50.xml', self.job), ('hazardmap-0.1-mean.xml', self.job), ('hazardmap-0.1-quantile-0.25.xml', self.job), ('hazardmap-0.1-quantile-0.50.xml', self.job), ('loss-map-0fcfdbc7.xml', self.job)] found = find_maps(self.job) self.assertEqual( expected, list(sorted([(os.path.basename(o.path), o.oq_job) for o in found])))
def test_find_maps(self): """ All maps are found. """ expected = [ '%s/hazardmap-0.01-mean.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.50.xml' % self.output_path, '%s/hazardmap-0.1-mean.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.50.xml' % self.output_path, '%s/loss-map-0fcfdbc7.xml' % self.output_path ] found = find_maps(self.job) self.assertEqual(expected, list(sorted([output.path for output in found])))
def test_find_maps_and_types(self): """ All maps are found, the types are correct. """ expected = [('hazardmap-0.01-mean.xml', "hazard_map"), ('hazardmap-0.01-quantile-0.25.xml', "hazard_map"), ('hazardmap-0.01-quantile-0.50.xml', "hazard_map"), ('hazardmap-0.1-mean.xml', "hazard_map"), ('hazardmap-0.1-quantile-0.25.xml', "hazard_map"), ('hazardmap-0.1-quantile-0.50.xml', "hazard_map"), ('loss-map-0fcfdbc7.xml', "loss_map")] found = find_maps(self.job) self.assertEqual( expected, list( sorted([(os.path.basename(o.path), o.output_type) for o in found])))
def test_find_maps_and_job_reference(self): """ All maps are found, the db records refer to the correct job. """ expected = [ ('hazardmap-0.01-mean.xml', self.job), ('hazardmap-0.01-quantile-0.25.xml', self.job), ('hazardmap-0.01-quantile-0.50.xml', self.job), ('hazardmap-0.1-mean.xml', self.job), ('hazardmap-0.1-quantile-0.25.xml', self.job), ('hazardmap-0.1-quantile-0.50.xml', self.job), ('loss-map-0fcfdbc7.xml', self.job)] found = find_maps(self.job) self.assertEqual( expected, list(sorted([(os.path.basename(o.path), o.oq_job) for o in found])))
def test_find_maps_and_types(self): """ All maps are found, the types are correct. """ expected = [ ('hazardmap-0.01-mean.xml', "hazard_map"), ('hazardmap-0.01-quantile-0.25.xml', "hazard_map"), ('hazardmap-0.01-quantile-0.50.xml', "hazard_map"), ('hazardmap-0.1-mean.xml', "hazard_map"), ('hazardmap-0.1-quantile-0.25.xml', "hazard_map"), ('hazardmap-0.1-quantile-0.50.xml', "hazard_map"), ('loss-map-0fcfdbc7.xml', "loss_map")] found = find_maps(self.job) self.assertEqual( expected, list(sorted([(os.path.basename(o.path), o.output_type) for o in found])))
def test_process_map_with_map_transformer_error(self): """ If the shapefile generation fails for a map the db record is *not* updated. """ config = {'jobid': self.job.id, 'shapefile': True} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [hazard_map, _] = maps self.assertEqual("hazard_map", hazard_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 1, "", "failed to generate shapefile") process_map(hazard_map, config) self.assertTrue(hazard_map.shapefile_path is None) self.assertTrue(hazard_map.min_value is None) self.assertTrue(hazard_map.max_value is None)
def test_find_maps_and_sizes(self): """ All maps are found, the sizes captured in the db records are correct. """ expected = [ '%s/hazardmap-0.01-mean.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.01-quantile-0.50.xml' % self.output_path, '%s/hazardmap-0.1-mean.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.25.xml' % self.output_path, '%s/hazardmap-0.1-quantile-0.50.xml' % self.output_path, '%s/loss-map-0fcfdbc7.xml' % self.output_path ] sizes = dict([(f, os.path.getsize(f)) for f in expected]) found = find_maps(self.job) for output in found: self.assertEqual(sizes[output.path], output.size)
def test_process_map_calls_map_transformer_correctly_with_loss(self): """ The map transformer is invoked correctly for a loss map that is to be written to a shapefile. """ config = {'jobid': self.job.id, 'shapefile': True} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [_, loss_map] = maps basename = os.path.basename(loss_map.path) self.assertEqual("loss_map", loss_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: ('/d/e/f', 61.4039544548262, 926.3032629745)", "") process_map(loss_map, config) args, kwargs = mock_func.call_args expected = ( ['%s/bin/map_transformer.py' % settings.OQ_APIAPP_DIR, '-k', str(loss_map.id), '-p', '%s/computed_output/%s' % (self.job.path, basename), '--shapefile', '-t', 'loss'],) self.assertEqual(expected, args) self.assertEqual({'ignore_exit_code': True}, kwargs)
def test_process_map_calls_map_transformer_correctly_with_hazard(self): """ The map transformer is invoked correctly for a hazard map that is to be written to a shapefile. """ config = {'jobid': self.job.id, 'shapefile': True} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [hazard_map, _] = maps basename = os.path.basename(hazard_map.path) self.assertEqual("hazard_map", hazard_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: ('/a/b/c', 16.04934554846202, 629.323267954)", "") process_map(hazard_map, config) args, kwargs = mock_func.call_args expected = ( ['%s/bin/map_transformer.py' % settings.OQ_APIAPP_DIR, '-k', str(hazard_map.id), '-p', '%s/computed_output/%s' % (self.job.path, basename), '--shapefile', '-t', 'hazard'],) self.assertEqual(expected, args) self.assertEqual({'ignore_exit_code': True}, kwargs)
def test_process_map_calls_map_transformer_correctly_with_loss_to_db(self): """ The map transformer is invoked correctly for a loss map that is to be written to the database. Specifically: the '--shapefile' command line argument is not supplied. """ config = {'jobid': self.job.id} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [_, loss_map] = maps basename = os.path.basename(loss_map.path) self.assertEqual("loss_map", loss_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: (%s, 19.19, 21.21)" % loss_map.id, "") process_map(loss_map, config) args, kwargs = mock_func.call_args expected = ( ['%s/bin/map_transformer.py' % settings.OQ_APIAPP_DIR, '-k', str(loss_map.id), '-p', '%s/computed_output/%s' % (self.job.path, basename), '-t', 'loss'],) self.assertEqual(expected, args) self.assertEqual({'ignore_exit_code': True}, kwargs)
def test_process_map_calls_map_transformer_correctly_with_hazard(self): """ The map transformer is invoked correctly for a hazard map that is to be written to a shapefile. """ config = {'jobid': self.job.id, 'shapefile': True} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [hazard_map, _] = maps basename = os.path.basename(hazard_map.path) self.assertEqual("hazard_map", hazard_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: ('/a/b/c', 16.04934554846202, 629.323267954)", "") process_map(hazard_map, config) args, kwargs = mock_func.call_args expected = ([ '%s/bin/map_transformer.py' % settings.OQ_APIAPP_DIR, '-k', str(hazard_map.id), '-p', '%s/computed_output/%s' % (self.job.path, basename), '--shapefile', '-t', 'hazard' ], ) self.assertEqual(expected, args) self.assertEqual({'ignore_exit_code': True}, kwargs)
def test_process_map_calls_map_transformer_correctly_with_loss(self): """ The map transformer is invoked correctly for a loss map that is to be written to a shapefile. """ config = {'jobid': self.job.id, 'shapefile': True} maps = find_maps(self.job) self.assertEqual(2, len(maps)) [_, loss_map] = maps basename = os.path.basename(loss_map.path) self.assertEqual("loss_map", loss_map.output_type) with mock.patch('geonode.mtapi.view_utils.run_cmd') as mock_func: mock_func.return_value = ( 0, "RESULT: ('/d/e/f', 61.4039544548262, 926.3032629745)", "") process_map(loss_map, config) args, kwargs = mock_func.call_args expected = ([ '%s/bin/map_transformer.py' % settings.OQ_APIAPP_DIR, '-k', str(loss_map.id), '-p', '%s/computed_output/%s' % (self.job.path, basename), '--shapefile', '-t', 'loss' ], ) self.assertEqual(expected, args) self.assertEqual({'ignore_exit_code': True}, kwargs)