Skip to content

Commit

Permalink
✅ Add test fro vectro api URl parameter ftod.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Oct 12, 2023
1 parent cef719e commit 0217222
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
Binary file modified g3w-admin/qdjango/tests/data/geodata/qgis_widget_test_data.gpkg
Binary file not shown.
113 changes: 111 additions & 2 deletions g3w-admin/qdjango/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 """

Expand Down
3 changes: 3 additions & 0 deletions g3w-admin/qdjango/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0217222

Please sign in to comment.