Skip to content

Commit eccbeb6

Browse files
committed
oweditdomain: Fix tests
1 parent d37ad92 commit eccbeb6

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Orange/widgets/data/oweditdomain.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,14 @@ class ContinuousVariableEditor(VariableEditor):
15751575
pass
15761576

15771577

1578+
class ComboBox(QComboBox):
1579+
# QComboBox.findData does not work for (named)tuples?
1580+
def findData(self, data, role=Qt.ItemDataRole.UserRole) -> int:
1581+
idx = findf(range(self.count()), lambda i: self.itemData(i, role) == data)
1582+
if idx is None:
1583+
idx = -1
1584+
return idx
1585+
15781586
class TimeVariableEditor(VariableEditor):
15791587
CUSTOM_FORMAT_LABEL = "Custom format"
15801588
FORMATS = [("Detect automatically", (None, 1, 1))] + list(
@@ -1598,7 +1606,7 @@ def __init__(self, parent=None, **kwargs):
15981606
super().__init__(parent, **kwargs)
15991607
form = self.layout().itemAt(0)
16001608

1601-
self.format_cb = QComboBox()
1609+
self.format_cb = ComboBox()
16021610
self._formats_model = create_list_model([
16031611
{Qt.DisplayRole: name, Qt.UserRole: StrpTime(name, *data)}
16041612
for name, data in self.FORMATS
@@ -1682,6 +1690,7 @@ def get_data(self):
16821690
trf = StrpTime(None, (custom_text,), have_date, have_time)
16831691
else:
16841692
trf = self.format_cb.currentData()
1693+
assert trf is not None
16851694
trs.insert(0, trf)
16861695
return var, trs
16871696

Orange/widgets/data/tests/test_oweditdomain.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,26 @@ def test_migrate_settings_4_to_5(self):
724724
},
725725
)
726726

727+
def transform_eq(first, second) -> bool:
728+
"""
729+
Specialize equality for transforms to simplify tests:
730+
* AsTime(None) == AsTime(DefaultStrpTime)
731+
* AsTime(None) == AsTime(DefaultTimeUnit)
732+
"""
733+
if isinstance(first, AsTime) and isinstance(second, AsTime):
734+
p1, p2 = first.param, second.param
735+
if (p1 is None) ^ (p2 is None):
736+
param = p1 or p2
737+
return param == DefaultStrpTime or param == DefaultTimeUnit
738+
return first == second
739+
740+
727741
class TestEditors(GuiTest):
742+
743+
def assertTransformsEqual(self, first, second):
744+
if not all(transform_eq(t1, t1) for t1, t2 in zip(first, second)):
745+
self.assertSequenceEqual(first, second)
746+
728747
def test_variable_editor(self):
729748
w = VariableEditor()
730749
self.assertEqual(w.get_data(), (None, []))
@@ -995,7 +1014,7 @@ def test_reinterpret_editor(self):
9951014
if not tr_[0]:
9961015
self.assertEqual(tr, self.ReinterpretTransforms[type(*v)])
9971016
else:
998-
self.assertListEqual(*tr_, [t() for t in tr])
1017+
self.assertTransformsEqual(*tr_, [t() for t in tr])
9991018

10001019
def test_reinterpret_editor_simulate(self):
10011020
w = ReinterpretVariableEditor()
@@ -1005,7 +1024,7 @@ def cb():
10051024
var, tr = var[0], tr[0]
10061025
type_ = tc.currentData()
10071026
if type_ is not type(var):
1008-
self.assertEqual(
1027+
self.assertTransformsEqual(
10091028
tr, [t() for t in self.ReinterpretTransforms[type_]] + [Rename("Z")]
10101029
)
10111030
else:
@@ -1383,6 +1402,9 @@ def _assertLookupEquals(self, first, second):
13831402
self.assertIs(first.variable, second.variable)
13841403
assert_array_equal(first.lookup_table, second.lookup_table)
13851404

1405+
DefaultStrpTime = StrpTime("Detect automatically", None, 1, 1)
1406+
DefaultTimeUnit = TimeUnit("Default", "s")
1407+
13861408

13871409
class TestReinterpretTransforms(TestCase):
13881410
@classmethod
@@ -1531,7 +1553,7 @@ def test_reinterpret_string(self):
15311553
tvars = []
15321554
for v in domain.metas:
15331555
for i, tr in enumerate(
1534-
[AsContinuous(), AsCategorical(), AsTime(StrpTime("Detect automatically", None, 1, 1)), AsString()]
1556+
[AsContinuous(), AsCategorical(), AsTime(DefaultStrpTime), AsString()]
15351557
):
15361558
vtr = apply_reinterpret(v, tr, table_column_data(table, v)).renamed(
15371559
f"{v.name}_{i}"

0 commit comments

Comments
 (0)