Skip to content
Permalink
Browse files

Added unit test

  • Loading branch information
suricactus committed Dec 21, 2020
1 parent 68b65ca commit 44200e5be6567428d00d5521f09183f693601dda
Showing with 43 additions and 2 deletions.
  1. +43 −2 tests/src/python/test_qgsattributetablemodel.py
@@ -19,9 +19,11 @@
QgsGeometry,
QgsPointXY,
QgsVectorLayer,
QgsVectorLayerCache
QgsVectorLayerCache,
QgsConditionalStyle,
)

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QColor
from qgis.testing import (start_app,
unittest
)
@@ -118,6 +120,45 @@ def testEdit(self):
# check that index from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))

def testStyle(self):
style_threshold = 2
color = QColor(133, 133, 133)
style = QgsConditionalStyle()
style.setRule(f'"fldint" <= {style_threshold}')
style.setTextColor(color)
self.layer.conditionalStyles().setRowStyles([style])

for f in self.layer.getFeatures():
model_index = self.am.idToIndex(f.id())
text_color = self.am.data(model_index, Qt.TextColorRole)

if f['fldint'] <= style_threshold:
self.assertEqual(text_color, color)
else:
self.assertIsNone(text_color)

self.assertTrue(self.layer.startEditing())

feature1 = self.layer.getFeature(2)
feature1['fldint'] = style_threshold + 1
feature2 = self.layer.getFeature(8)
feature2['fldint'] = style_threshold

self.assertTrue(self.layer.updateFeature(feature1))
self.assertTrue(self.layer.updateFeature(feature2))
self.assertTrue(self.layer.commitChanges())

for f in self.layer.getFeatures():
model_index = self.am.idToIndex(f.id())
text_color = self.am.data(model_index, Qt.TextColorRole)

if f['fldint'] <= style_threshold:
self.assertEqual(color, text_color, f'Feature {f.id()} should have color')
else:
self.assertIsNone(text_color, f'Feature {f.id()} should have no color')

self.layer.conditionalStyles().setRowStyles([])


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

0 comments on commit 44200e5

Please sign in to comment.
You can’t perform that action at this time.