16
16
import tempfile
17
17
import shutil
18
18
19
- from qgis .PyQt .QtCore import QVariant , Qt
19
+ from qgis .PyQt .QtCore import QDate , QDateTime , QVariant , Qt
20
20
from qgis .PyQt .QtGui import QPainter , QColor
21
21
from qgis .PyQt .QtXml import QDomDocument
22
22
@@ -137,20 +137,20 @@ def createLayerWithFivePoints():
137
137
138
138
def createJoinLayer ():
139
139
joinLayer = QgsVectorLayer (
140
- "Point?field=x:string&field=y:integer&field=z:integer" ,
140
+ "Point?field=x:string&field=y:integer&field=z:integer&field=date:datetime " ,
141
141
"joinlayer" , "memory" )
142
142
pr = joinLayer .dataProvider ()
143
143
f1 = QgsFeature ()
144
- f1 .setAttributes (["foo" , 123 , 321 ])
144
+ f1 .setAttributes (["foo" , 123 , 321 , QDateTime ( QDate ( 2010 , 1 , 1 )) ])
145
145
f1 .setGeometry (QgsGeometry .fromPointXY (QgsPointXY (1 , 1 )))
146
146
f2 = QgsFeature ()
147
- f2 .setAttributes (["bar" , 456 , 654 ])
147
+ f2 .setAttributes (["bar" , 456 , 654 , QDateTime ( QDate ( 2020 , 1 , 1 )) ])
148
148
f2 .setGeometry (QgsGeometry .fromPointXY (QgsPointXY (2 , 2 )))
149
149
f3 = QgsFeature ()
150
- f3 .setAttributes (["qar" , 457 , 111 ])
150
+ f3 .setAttributes (["qar" , 457 , 111 , None ])
151
151
f3 .setGeometry (QgsGeometry .fromPointXY (QgsPointXY (2 , 2 )))
152
152
f4 = QgsFeature ()
153
- f4 .setAttributes (["a" , 458 , 19 ])
153
+ f4 .setAttributes (["a" , 458 , 19 , QDateTime ( QDate ( 2012 , 1 , 1 )) ])
154
154
f4 .setGeometry (QgsGeometry .fromPointXY (QgsPointXY (2 , 2 )))
155
155
assert pr .addFeatures ([f1 , f2 , f3 , f4 ])
156
156
assert joinLayer .featureCount () == 4
@@ -1611,11 +1611,11 @@ def test_join(self):
1611
1611
layer .addJoin (join2 )
1612
1612
1613
1613
flds = layer .fields ()
1614
- self .assertEqual (len (flds ), 6 )
1614
+ self .assertEqual (len (flds ), 8 )
1615
1615
self .assertEqual (flds [2 ].name (), "joinlayer_x" )
1616
1616
self .assertEqual (flds [3 ].name (), "joinlayer_z" )
1617
- self .assertEqual (flds [4 ].name (), "custom-prefix_x" )
1618
- self .assertEqual (flds [5 ].name (), "custom-prefix_z" )
1617
+ self .assertEqual (flds [5 ].name (), "custom-prefix_x" )
1618
+ self .assertEqual (flds [6 ].name (), "custom-prefix_z" )
1619
1619
self .assertEqual (flds .fieldOrigin (0 ), QgsFields .OriginProvider )
1620
1620
self .assertEqual (flds .fieldOrigin (2 ), QgsFields .OriginJoin )
1621
1621
self .assertEqual (flds .fieldOrigin (3 ), QgsFields .OriginJoin )
@@ -1627,15 +1627,15 @@ def test_join(self):
1627
1627
fi = layer .getFeatures ()
1628
1628
self .assertTrue (fi .nextFeature (f ))
1629
1629
attrs = f .attributes ()
1630
- self .assertEqual (len (attrs ), 6 )
1630
+ self .assertEqual (len (attrs ), 8 )
1631
1631
self .assertEqual (attrs [0 ], "test" )
1632
1632
self .assertEqual (attrs [1 ], 123 )
1633
1633
self .assertEqual (attrs [2 ], "foo" )
1634
1634
self .assertEqual (attrs [3 ], 321 )
1635
1635
self .assertFalse (fi .nextFeature (f ))
1636
1636
1637
1637
f2 = next (layer .getFeatures (QgsFeatureRequest (f .id ())))
1638
- self .assertEqual (len (f2 .attributes ()), 6 )
1638
+ self .assertEqual (len (f2 .attributes ()), 8 )
1639
1639
self .assertEqual (f2 [2 ], "foo" )
1640
1640
self .assertEqual (f2 [3 ], 321 )
1641
1641
@@ -1653,8 +1653,19 @@ def test_JoinStats(self):
1653
1653
layer .addJoin (join )
1654
1654
1655
1655
# stats on joined fields should only include values present by join
1656
+
1657
+ # strings
1658
+ self .assertEqual (layer .minimumValue (2 ), "foo" )
1659
+ self .assertEqual (layer .maximumValue (2 ), "qar" )
1660
+
1661
+ # numbers
1656
1662
self .assertEqual (layer .minimumValue (3 ), 111 )
1657
1663
self .assertEqual (layer .maximumValue (3 ), 321 )
1664
+
1665
+ # dates (maximumValue also tests we properly handle null values by skipping those)
1666
+ self .assertEqual (layer .minimumValue (4 ), QDateTime (QDate (2010 , 1 , 1 )))
1667
+ self .assertEqual (layer .maximumValue (4 ), QDateTime (QDate (2010 , 1 , 1 )))
1668
+
1658
1669
self .assertEqual (set (layer .uniqueValues (3 )), set ([111 , 321 ]))
1659
1670
1660
1671
def test_valid_join_when_opening_project (self ):
@@ -1872,6 +1883,28 @@ def testMaxValue(self):
1872
1883
self .assertTrue (layer .changeAttributeValue (f1_id , 1 , 1001 ))
1873
1884
self .assertEqual (layer .maximumValue (1 ), 1001 )
1874
1885
1886
+ def testMinMaxInVirtualField (self ):
1887
+ """
1888
+ Test minimum and maximum values in a virtual field
1889
+ """
1890
+ layer = QgsVectorLayer ("Point?field=fldstr:string" , "layer" , "memory" )
1891
+ pr = layer .dataProvider ()
1892
+
1893
+ int_values = ['2010-01-01' , None , '2020-01-01' ]
1894
+ features = []
1895
+ for i in int_values :
1896
+ f = QgsFeature ()
1897
+ f .setFields (layer .fields ())
1898
+ f .setAttributes ([i ])
1899
+ features .append (f )
1900
+ assert pr .addFeatures (features )
1901
+
1902
+ field = QgsField ('virtual' , QVariant .Date )
1903
+ layer .addExpressionField ('to_date("fldstr")' , field )
1904
+ self .assertEqual (len (layer .getFeature (1 ).attributes ()), 2 )
1905
+ self .assertEqual (layer .minimumValue (1 ), QDate (2010 , 1 , 1 ))
1906
+ self .assertEqual (layer .maximumValue (1 ), QDate (2020 , 1 , 1 ))
1907
+
1875
1908
def test_InvalidOperations (self ):
1876
1909
layer = createLayerWithOnePoint ()
1877
1910
0 commit comments