|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsFieldValidator. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Alessandro Pasotti' |
| 10 | +__date__ = '31/01/2018' |
| 11 | +__copyright__ = 'Copyright 2018, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import qgis # NOQA |
| 16 | + |
| 17 | +from qgis.PyQt.QtCore import QVariant |
| 18 | +from qgis.PyQt.QtGui import QValidator |
| 19 | +from qgis.core import QgsVectorLayer |
| 20 | +from qgis.gui import QgsFieldValidator |
| 21 | +from qgis.testing import start_app, unittest |
| 22 | +from utilities import unitTestDataPath |
| 23 | + |
| 24 | +TEST_DATA_DIR = unitTestDataPath() |
| 25 | + |
| 26 | + |
| 27 | +start_app() |
| 28 | + |
| 29 | + |
| 30 | +class TestQgsFieldValidator(unittest.TestCase): |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + """Run before each test.""" |
| 34 | + testPath = TEST_DATA_DIR + '/' + 'bug_17878.gpkg|layername=bug_17878' |
| 35 | + self.vl = QgsVectorLayer(testPath, "test_data", "ogr") |
| 36 | + assert self.vl.isValid() |
| 37 | + |
| 38 | + def tearDown(self): |
| 39 | + """Run after each test.""" |
| 40 | + pass |
| 41 | + |
| 42 | + def test_validator(self): |
| 43 | + # Test the double |
| 44 | + """ |
| 45 | + Expected results from validate |
| 46 | + QValidator::Invalid 0 The string is clearly invalid. |
| 47 | + QValidator::Intermediate 1 The string is a plausible intermediate value. |
| 48 | + QValidator::Acceptable 2 The string is acceptable as a final result; i.e. it is valid. |
| 49 | + """ |
| 50 | + |
| 51 | + double_field = self.vl.fields()[self.vl.fields().indexFromName('double_field')] |
| 52 | + self.assertEqual(double_field.precision(), 0) # this is what the provider reports :( |
| 53 | + self.assertEqual(double_field.length(), 0) # not set |
| 54 | + self.assertEqual(double_field.type(), QVariant.Double) |
| 55 | + |
| 56 | + validator = QgsFieldValidator(None, double_field, '0.0', '') |
| 57 | + |
| 58 | + def _test(value, expected): |
| 59 | + ret = validator.validate(value, 0) |
| 60 | + self.assertEqual(ret[0], expected, value) |
| 61 | + if value: |
| 62 | + self.assertEqual(validator.validate('-' + value, 0)[0], expected, '-' + value) |
| 63 | + # Check the decimal comma separator has been properly transformed |
| 64 | + if expected != QValidator.Invalid: |
| 65 | + self.assertEqual(ret[1], value.replace(',', '.')) |
| 66 | + |
| 67 | + # Valid |
| 68 | + _test('0.1234', QValidator.Acceptable) |
| 69 | + _test('0,1234', QValidator.Acceptable) |
| 70 | + _test('12345.1234e+123', QValidator.Acceptable) |
| 71 | + _test('12345.1234e-123', QValidator.Acceptable) |
| 72 | + _test('12345,1234e+123', QValidator.Acceptable) |
| 73 | + _test('12345,1234e-123', QValidator.Acceptable) |
| 74 | + _test('', QValidator.Acceptable) |
| 75 | + |
| 76 | + # Out of range |
| 77 | + _test('12345.1234e+823', QValidator.Intermediate) |
| 78 | + _test('12345.1234e-823', QValidator.Intermediate) |
| 79 | + _test('12345,1234e+823', QValidator.Intermediate) |
| 80 | + _test('12345,1234e-823', QValidator.Intermediate) |
| 81 | + |
| 82 | + # Invalid |
| 83 | + _test('12345-1234', QValidator.Invalid) |
| 84 | + _test('onetwothree', QValidator.Invalid) |
| 85 | + |
| 86 | + int_field = self.vl.fields()[self.vl.fields().indexFromName('int_field')] |
| 87 | + self.assertEqual(int_field.precision(), 0) # this is what the provider reports :( |
| 88 | + self.assertEqual(int_field.length(), 0) # not set |
| 89 | + self.assertEqual(int_field.type(), QVariant.Int) |
| 90 | + |
| 91 | + validator = QgsFieldValidator(None, int_field, '0', '') |
| 92 | + |
| 93 | + # Valid |
| 94 | + _test('0', QValidator.Acceptable) |
| 95 | + _test('1234', QValidator.Acceptable) |
| 96 | + _test('', QValidator.Acceptable) |
| 97 | + |
| 98 | + # Invalid |
| 99 | + _test('12345-1234', QValidator.Invalid) |
| 100 | + _test('12345.1234', QValidator.Invalid) |
| 101 | + _test('onetwothree', QValidator.Invalid) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + unittest.main() |
0 commit comments