From 02172220d04329df58531c486201a1767e7a4b2f Mon Sep 17 00:00:00 2001 From: wlorenzetti Date: Thu, 12 Oct 2023 09:35:42 +0200 Subject: [PATCH] :white_check_mark: Add test fro vectro api URl parameter ftod. --- .../data/geodata/qgis_widget_test_data.gpkg | Bin 122880 -> 122880 bytes g3w-admin/qdjango/tests/test_api.py | 113 +++++++++++++++++- g3w-admin/qdjango/vector.py | 3 + 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/g3w-admin/qdjango/tests/data/geodata/qgis_widget_test_data.gpkg b/g3w-admin/qdjango/tests/data/geodata/qgis_widget_test_data.gpkg index 69b5fbbd8c9358c12465cc6aa480c2ab3d8c0f20..2cead37f728f0dfb32a92b601fbba152b1a8f53a 100644 GIT binary patch delta 22 ecmZoTz}|3xeS$ROtBEqsjISCKwk9wxxDNnmM+tTS delta 22 ecmZoTz}|3xeS$ROlZi6Uj87U9wk9wxxDNnl$q8xz diff --git a/g3w-admin/qdjango/tests/test_api.py b/g3w-admin/qdjango/tests/test_api.py index 0439a2795..976a3fd5f 100644 --- a/g3w-admin/qdjango/tests/test_api.py +++ b/g3w-admin/qdjango/tests/test_api.py @@ -661,7 +661,7 @@ def test_server_filters_value_relation_api(self): qgs_request = QgsFeatureRequest() qgs_request.setFilterExpression("\"type\" IN ('B','B1')") total_count = len([f for f in qgis_layer.getFeatures(qgs_request)]) - + resp = json.loads( self._testApiCall( "core-vector-api", @@ -674,9 +674,118 @@ def test_server_filters_value_relation_api(self): {"field": "type|ilike|B", "formatter": "1"}, ).content ) - + self.assertEqual(resp["vector"]["count"], total_count) + def test_vector_api_url_param_ftod(self): + """Test for URL Parameter for vecto api REST ftod (fields to download)""" + + # Set download propteries + pois = Layer.objects.get( + project_id=self.project328_value_relation.instance.pk, + qgs_layer_id='poi_2c470d17_a234_464c_83f8_416bcdedda17') + + pois.download = True + pois.download_csv = True + pois.download_xls = True + pois.download_gpx = True + pois.download_gpkg = True + + pois.save() + + response = self._testApiCall('core-vector-api', + [ + 'csv', + 'qdjango', + self.project328_value_relation.instance.pk, + 'poi_2c470d17_a234_464c_83f8_416bcdedda17' + ], + { + 'ftod': '' + }) + self.assertEqual(response.status_code, 200) + temp = QTemporaryDir() + fname = temp.path() + '/temp_ftod.csv' + with open(fname, 'wb') as f: + f.write(response.content) + + vl = QgsVectorLayer(fname) + self.assertTrue(vl.isValid()) + + fields = [f for f in vl.fields()] + + self.assertEqual(len(fields), 4) + self.assertEqual(fields[0].name(), 'pkuid') + + response = self._testApiCall('core-vector-api', + [ + 'csv', + 'qdjango', + self.project328_value_relation.instance.pk, + 'poi_2c470d17_a234_464c_83f8_416bcdedda17' + ], + { + 'ftod': 'rif,type' + }) + self.assertEqual(response.status_code, 200) + temp = QTemporaryDir() + fname = temp.path() + '/temp_ftod.csv' + with open(fname, 'wb') as f: + f.write(response.content) + + vl = QgsVectorLayer(fname) + self.assertTrue(vl.isValid()) + + fields = [f for f in vl.fields()] + + self.assertEqual(len(fields), 2) + self.assertEqual(fields[0].name(), 'rif') + self.assertEqual(fields[1].name(), 'type') + + response = self._testApiCall('core-vector-api', + [ + 'csv', + 'qdjango', + self.project328_value_relation.instance.pk, + 'poi_2c470d17_a234_464c_83f8_416bcdedda17' + ], + { + 'ftod': 'goofy' + }) + self.assertEqual(response.status_code, 200) + temp = QTemporaryDir() + fname = temp.path() + '/temp_ftod.csv' + with open(fname, 'wb') as f: + f.write(response.content) + + vl = QgsVectorLayer(fname) + self.assertFalse(vl.isValid()) + + response = self._testApiCall('core-vector-api', + [ + 'csv', + 'qdjango', + self.project328_value_relation.instance.pk, + 'poi_2c470d17_a234_464c_83f8_416bcdedda17' + ], + { + 'ftod': 'rif,goofy' + }) + self.assertEqual(response.status_code, 200) + temp = QTemporaryDir() + fname = temp.path() + '/temp_ftod.csv' + with open(fname, 'wb') as f: + f.write(response.content) + + vl = QgsVectorLayer(fname) + self.assertTrue(vl.isValid()) + + fields = [f for f in vl.fields()] + + self.assertEqual(len(fields), 1) + self.assertEqual(fields[0].name(), 'rif') + + class TestQdjangoLayersAPI(QdjangoTestBase): """ Test qdjango layer API """ diff --git a/g3w-admin/qdjango/vector.py b/g3w-admin/qdjango/vector.py index eee63a1b0..2f2357de8 100644 --- a/g3w-admin/qdjango/vector.py +++ b/g3w-admin/qdjango/vector.py @@ -611,6 +611,9 @@ def _set_download_attributes(self, save_options): except: pass + if len(save_options.attributes) == 0: + save_options.attributes = [-1] + def response_shp_mode(self, request): """ Download Shapefile of data