Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "contains" method to QgsObjectCustomProperties, improve dox, add …
…tests
- Loading branch information
1 parent
33fc5d4
commit eee4327
Showing
5 changed files
with
141 additions
and
16 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
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
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,84 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsObjectCustomProperties | ||
.. 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__ = 'Nyall Dawson' | ||
__date__ = '02/06/2020' | ||
__copyright__ = 'Copyright 2020, The QGIS Project' | ||
|
||
import qgis # NOQA | ||
|
||
from qgis.core import QgsObjectCustomProperties | ||
|
||
from qgis.testing import start_app, unittest | ||
from qgis.PyQt.QtXml import QDomDocument | ||
|
||
start_app() | ||
|
||
|
||
class TestQgsObjectCustomProperties(unittest.TestCase): | ||
|
||
def testSimple(self): | ||
""" test storing/retrieving properties """ | ||
props = QgsObjectCustomProperties() | ||
self.assertFalse(props.keys()) | ||
self.assertFalse(props.contains('a')) | ||
self.assertFalse(props.value('a')) | ||
self.assertEqual(props.value('a', defaultValue=6), 6) | ||
|
||
# remove non-present key, no crash | ||
props.remove('a') | ||
|
||
props.setValue('a', 7) | ||
self.assertEqual(props.keys(), ['a']) | ||
self.assertTrue(props.contains('a')) | ||
self.assertFalse(props.contains('b')) | ||
self.assertEqual(props.value('a', defaultValue=6), 7) | ||
self.assertEqual(props.value('b', defaultValue='yy'), 'yy') | ||
props.setValue('b', 'xx') | ||
self.assertCountEqual(props.keys(), ['a', 'b']) | ||
self.assertTrue(props.contains('a')) | ||
self.assertTrue(props.contains('b')) | ||
self.assertEqual(props.value('a', defaultValue=6), 7) | ||
self.assertEqual(props.value('b', defaultValue='yy'), 'xx') | ||
|
||
props.remove('a') | ||
self.assertCountEqual(props.keys(), ['b']) | ||
self.assertFalse(props.contains('a')) | ||
self.assertTrue(props.contains('b')) | ||
self.assertEqual(props.value('a', defaultValue=6), 6) | ||
self.assertEqual(props.value('b', defaultValue='yy'), 'xx') | ||
|
||
props.remove('b') | ||
self.assertFalse(props.keys()) | ||
self.assertFalse(props.contains('a')) | ||
self.assertFalse(props.contains('b')) | ||
self.assertEqual(props.value('a', defaultValue=6), 6) | ||
self.assertEqual(props.value('b', defaultValue='yy'), 'yy') | ||
|
||
def testSaveRestore(self): | ||
doc = QDomDocument() | ||
elem = doc.createElement('test') | ||
|
||
props = QgsObjectCustomProperties() | ||
props.setValue('a', '7') | ||
props.setValue('b', 'xx') | ||
|
||
props.writeXml(elem, doc) | ||
|
||
props2 = QgsObjectCustomProperties() | ||
props2.readXml(elem) | ||
|
||
self.assertCountEqual(props2.keys(), ['a', 'b']) | ||
self.assertTrue(props2.contains('a')) | ||
self.assertTrue(props2.contains('b')) | ||
self.assertEqual(props2.value('a', defaultValue=6), '7') | ||
self.assertEqual(props2.value('b', defaultValue='yy'), 'xx') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |