11# Test methods with long descriptive names can omit docstrings
22# pylint: disable=missing-docstring
3+ import time
4+
35from AnyQt .QtCore import QLocale , Qt
46from AnyQt .QtTest import QTest
57from AnyQt .QtWidgets import QLineEdit , QComboBox
68
79import numpy as np
810
911from Orange .data import (
10- Table , ContinuousVariable , StringVariable , DiscreteVariable )
12+ Table , ContinuousVariable , StringVariable , DiscreteVariable , Domain )
1113from Orange .widgets .data .owselectrows import (
1214 OWSelectRows , FilterDiscreteType , SelectRowsContextHandler )
1315from Orange .widgets .tests .base import WidgetTest , datasets
1618from Orange .widgets .tests .utils import simulate , override_locale
1719from Orange .widgets .utils .annotated_data import ANNOTATED_DATA_FEATURE_NAME
1820from Orange .tests import test_filename
21+ from orangewidget .settings import VERSION_KEY
1922
2023CFValues = {
2124 FilterContinuous .Equal : ["5.4" ],
@@ -132,22 +135,22 @@ def test_stores_settings_in_invariant_locale(self):
132135 context = self .widget .current_context
133136 self .send_signal (self .widget .Inputs .data , None )
134137 saved_condition = context .values ["conditions" ][0 ]
135- self .assertEqual (saved_condition [2 ][0 ], 5.2 )
138+ self .assertEqual (saved_condition [3 ][0 ], 5.2 )
136139
137140 @override_locale (QLocale .C )
138141 def test_restores_continuous_filter_in_c_locale (self ):
139142 iris = Table ("iris" )[:5 ]
140143 # Settings with string value
141144 self .widget = self .widget_with_context (
142- iris .domain , [["sepal length" , 2 , ("5.2" ,)]])
145+ iris .domain , [["sepal length" , 102 , 2 , ("5.2" ,)]])
143146 self .send_signal (self .widget .Inputs .data , iris )
144147
145148 values = self .widget .conditions [0 ][2 ]
146149 self .assertTrue (values [0 ].startswith ("5.2" ))
147150
148151 # Settings with float value
149152 self .widget = self .widget_with_context (
150- iris .domain , [["sepal length" , 2 , (5.2 ,)]])
153+ iris .domain , [["sepal length" , 102 , 2 , (5.2 ,)]])
151154 self .send_signal (self .widget .Inputs .data , iris )
152155
153156 values = self .widget .conditions [0 ][2 ]
@@ -158,20 +161,33 @@ def test_restores_continuous_filter_in_sl_SI_locale(self):
158161 iris = Table ("iris" )[:5 ]
159162 # Settings with string value
160163 self .widget = self .widget_with_context (
161- iris .domain , [["sepal length" , 2 , ("5.2" ,)]])
164+ iris .domain , [["sepal length" , 102 , 2 , ("5.2" ,)]])
162165 self .send_signal (self .widget .Inputs .data , iris )
163166
164167 values = self .widget .conditions [0 ][2 ]
165168 self .assertTrue (values [0 ].startswith ("5,2" ))
166169
167170 # Settings with float value
168171 self .widget = self .widget_with_context (
169- iris .domain , [["sepal length" , 2 , (5.2 ,)]])
172+ iris .domain , [["sepal length" , 102 , 2 , (5.2 ,)]])
170173 self .send_signal (self .widget .Inputs .data , iris )
171174
172175 values = self .widget .conditions [0 ][2 ]
173176 self .assertTrue (values [0 ].startswith ("5,2" ))
174177
178+ @override_locale (QLocale .C )
179+ def test_partial_matches (self ):
180+ iris = Table ("iris" )
181+ domain = iris .domain
182+ self .widget = self .widget_with_context (
183+ domain , [[domain [0 ].name , 2 , ("5.2" ,)]])
184+ iris2 = iris .transform (Domain (domain .attributes [:2 ], None ))
185+ self .send_signal (self .widget .Inputs .data , iris2 )
186+ condition = self .widget .conditions [0 ]
187+ self .assertEqual (condition [0 ], "sepal length" )
188+ self .assertEqual (condition [1 ], 2 )
189+ self .assertTrue (condition [2 ][0 ].startswith ("5.2" ))
190+
175191 def test_load_settings (self ):
176192 iris = Table ("iris" )[:5 ]
177193 self .send_signal (self .widget .Inputs .data , iris )
@@ -242,10 +258,65 @@ def test_annotated_data(self):
242258 np .testing .assert_equal (annotations [:50 ], True )
243259 np .testing .assert_equal (annotations [50 :], False )
244260
261+ def test_change_var_type (self ):
262+ iris = Table ("iris" )
263+ domain = iris .domain
264+
265+ self .send_signal (self .widget .Inputs .data , iris )
266+ self .widget .remove_all_button .click ()
267+ self .enterFilter (domain [0 ], "is below" , "5.2" )
268+
269+ var0vals = list ({str (x ) for x in iris .X [:, 0 ]})
270+ new_domain = Domain (
271+ (DiscreteVariable (domain [0 ].name , values = var0vals ), )
272+ + domain .attributes [1 :],
273+ domain .class_var )
274+ new_iris = iris .transform (new_domain )
275+ self .send_signal (self .widget .Inputs .data , new_iris )
276+
277+ # Uncomment this on 2022/2/2
278+ #
279+ # def test_migration_to_version_1(self):
280+ # iris = Table("iris")
281+ #
282+ # ch = SelectRowsContextHandler()
283+ # context = ch.new_context(iris.domain, *ch.encode_domain(iris.domain))
284+ # context.values = dict(conditions=[["petal length", 2, (5.2,)]])
285+ # settings = dict(context_settings=[context])
286+ # widget = self.create_widget(OWSelectRows, settings)
287+ # self.assertEqual(widget.conditions, [])
288+
289+ @override_locale (QLocale .C )
290+ def test_support_old_settings (self ):
291+ iris = Table ("iris" )
292+ self .widget = self .widget_with_context (
293+ iris .domain , [["sepal length" , 2 , ("5.2" ,)]])
294+ self .send_signal (self .widget .Inputs .data , iris )
295+ condition = self .widget .conditions [0 ]
296+ self .assertEqual (condition [0 ], "sepal length" )
297+ self .assertEqual (condition [1 ], 2 )
298+ self .assertTrue (condition [2 ][0 ].startswith ("5.2" ))
299+
300+ def test_end_support_for_version_1 (self ):
301+ if time .gmtime () >= (2022 , 2 , 2 ):
302+ self .fail ("""
303+ Happy 22/2/2!
304+
305+ Now remove support for version==None settings in
306+ SelectRowsContextHandler.decode_setting and SelectRowsContextHandler.match,
307+ and uncomment OWSelectRows.migrate.
308+
309+ In tests, uncomment test_migration_to_version_1,
310+ and remove test_support_old_settings and this test.
311+
312+ Basically, revert this commit.
313+ """ )
314+
245315 def widget_with_context (self , domain , conditions ):
246316 ch = SelectRowsContextHandler ()
247317 context = ch .new_context (domain , * ch .encode_domain (domain ))
248- context .values = dict (conditions = conditions )
318+ context .values = {"conditions" : conditions ,
319+ VERSION_KEY : OWSelectRows .settings_version }
249320 settings = dict (context_settings = [context ])
250321
251322 return self .create_widget (OWSelectRows , settings )
0 commit comments