Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b83fa0e
commit 646d8ef
Showing
2 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,315 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsOgcUtils. | ||
From build dir, run: ctest -R PyQgsOgcUtils -V | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'René-Luc Dhont' | ||
__date__ = '21/06/2019' | ||
__copyright__ = 'Copyright 2019, The QGIS Project' | ||
|
||
import qgis # NOQA switch sip api | ||
|
||
from qgis.PyQt.QtCore import QVariant | ||
from qgis.PyQt.QtXml import QDomDocument | ||
from qgis.core import QgsOgcUtils, QgsVectorLayer, QgsField | ||
|
||
from qgis.testing import start_app, unittest | ||
|
||
start_app() | ||
|
||
|
||
class TestQgsOgcUtils(unittest.TestCase): | ||
|
||
def test_expressionFromOgcFilterWithInt(self): | ||
""" | ||
Test expressionFromOgcFilter with Int type field | ||
""" | ||
vl = QgsVectorLayer('Point', 'vl', 'memory') | ||
vl.dataProvider().addAttributes([QgsField('id', QVariant.Int)]) | ||
vl.updateFields() | ||
|
||
# Literals are Integer 1 and 3 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1 AND id < 3') | ||
|
||
# Literals are Double 1.0 and 3.0 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.0</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.0</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1 AND id < 3') | ||
|
||
# Literals are Double 1.5 and 3.5 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.5</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.5</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 2 AND id < 4') | ||
|
||
def test_expressionFromOgcFilterWithLonglong(self): | ||
""" | ||
Test expressionFromOgcFilter with LongLong type field | ||
""" | ||
vl = QgsVectorLayer('Point', 'vl', 'memory') | ||
vl.dataProvider().addAttributes([QgsField('id', QVariant.LongLong)]) | ||
vl.updateFields() | ||
|
||
# Literals are Integer 1 and 3 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1 AND id < 3') | ||
# Literals are Double 1.0 and 3.0 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.0</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.0</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1 AND id < 3') | ||
|
||
# Literals are Double 1.5 and 3.5 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.5</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.5</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 2 AND id < 4') | ||
|
||
def test_expressionFromOgcFilterWithDouble(self): | ||
""" | ||
Test expressionFromOgcFilter with Double type field | ||
""" | ||
vl = QgsVectorLayer('Point', 'vl', 'memory') | ||
vl.dataProvider().addAttributes([QgsField('id', QVariant.Double)]) | ||
vl.updateFields() | ||
|
||
# Literals are Integer 1 and 3 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1 AND id < 3') | ||
|
||
# Literals are Double 1.0 and 3.0 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.0</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.0</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1 AND id < 3') | ||
|
||
# Literals are Double 1.5 and 3.5 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.5</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.5</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > 1.5 AND id < 3.5') | ||
|
||
def test_expressionFromOgcFilterWithString(self): | ||
""" | ||
Test expressionFromOgcFilter with String type field | ||
""" | ||
vl = QgsVectorLayer('Point', 'vl', 'memory') | ||
vl.dataProvider().addAttributes([QgsField('id', QVariant.String)]) | ||
vl.updateFields() | ||
|
||
# Literals are Integer 1 and 3 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > \'1\' AND id < \'3\'') | ||
|
||
# Literals are Double 1.0 and 3.0 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.0</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.0</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > \'1.0\' AND id < \'3.0\'') | ||
|
||
# Literals are Double 1.5 and 3.5 | ||
f = '''<?xml version="1.0" encoding="UTF-8"?> | ||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | ||
<ogc:And> | ||
<ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>1.5</ogc:Literal> | ||
</ogc:PropertyIsGreaterThan> | ||
<ogc:PropertyIsLessThan> | ||
<ogc:PropertyName>id</ogc:PropertyName> | ||
<ogc:Literal>3.5</ogc:Literal> | ||
</ogc:PropertyIsLessThan> | ||
</ogc:And> | ||
</ogc:Filter> | ||
''' | ||
d = QDomDocument('filter') | ||
d.setContent(f, True) | ||
|
||
e = QgsOgcUtils.expressionFromOgcFilter(d.documentElement(), vl) | ||
self.assertEqual(e.expression(), 'id > \'1.5\' AND id < \'3.5\'') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |