Skip to content

Commit

Permalink
Test type overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Nov 29, 2021
1 parent 8f6ea6e commit a94fc56
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions tests/src/python/test_qgsdelimitedtextprovider_types_override.py
Expand Up @@ -18,6 +18,7 @@

from qgis.core import (
QgsVectorLayer,
NULL,
)

from qgis.testing import start_app, unittest
Expand All @@ -38,7 +39,7 @@ def _run_test(self, csv_content, csvt_content='', uri_options=''):

try:
self.__text_index += 1
except:
except Exception:
self.__text_index = 1

basename = 'test_type_detection_{}'.format(self.__text_index)
Expand All @@ -62,24 +63,29 @@ def _run_test(self, csv_content, csvt_content='', uri_options=''):
def test_type_override(self):
"""Test type overrides"""

vl = self._run_test('\n'.join((
"integer,bool,long,real,text",
"1,true,9189304972279762602,1.234,text",
"2,false,,5.678,another text",
)))
self.assertTrue(vl.isValid())
fields = {f.name(): (f.type(), f.typeName()) for f in vl.fields()}
self.assertEqual(fields, {
'integer': (QVariant.Int, 'integer'),
'bool': (QVariant.Bool, 'bool'),
'long': (QVariant.LongLong, 'longlong'),
'real': (QVariant.Double, 'double'),
'text': (QVariant.String, 'text')})
if False:
vl = self._run_test('\n'.join((
"integer,bool,long,real,text",
"1,0,9189304972279762602,1.234,text",
"2,1,,5.678,another text",
)))
self.assertTrue(vl.isValid())
fields = {f.name(): (f.type(), f.typeName()) for f in vl.fields()}
self.assertEqual(fields, {
'integer': (QVariant.Int, 'integer'),
'bool': (QVariant.Bool, 'bool'),
'long': (QVariant.LongLong, 'longlong'),
'real': (QVariant.Double, 'double'),
'text': (QVariant.String, 'text')})

attrs = [f.attributes() for f in vl.getFeatures()]
self.assertEqual(attrs, [[1, False, '9189304972279762602', 1.234, 'text'],
[2, True, NULL, 5.678, 'another text']])

vl = self._run_test('\n'.join((
"integer,bool,long,real,text",
"1,true,9189304972279762602,1.234,text",
"2,false,,5.678,another text",
"1,0,9189304972279762602,1.234,text",
"2,1,,5.678,another text",
)), uri_options='field=bool:integer&field=integer:double&field=long:double&field=real:text')
self.assertTrue(vl.isValid())
fields = {f.name(): (f.type(), f.typeName()) for f in vl.fields()}
Expand All @@ -90,6 +96,10 @@ def test_type_override(self):
'real': (QVariant.String, 'text'),
'text': (QVariant.String, 'text')})

attrs = [f.attributes() for f in vl.getFeatures()]
self.assertEqual(attrs, [[1.0, 0, 9.189304972279763e+18, '1.234', 'text'],
[2.0, 1, NULL, '5.678', 'another text']])


if __name__ == '__main__':
unittest.main()

0 comments on commit a94fc56

Please sign in to comment.