11# Test methods with long descriptive names can omit docstrings
22# pylint: disable=missing-docstring
3+ import unittest
34from unittest .mock import Mock
45
56import numpy as np
67
78from Orange .data import Table , Domain
89from Orange import distance
9- from Orange .widgets .unsupervised .owdistances import OWDistances , METRICS
10+ from Orange .widgets .unsupervised .owdistances import OWDistances , METRICS , \
11+ DistanceRunner
1012from Orange .widgets .tests .base import WidgetTest
1113
1214
15+ class TestDistanceRunner (unittest .TestCase ):
16+ @classmethod
17+ def setUpClass (cls ):
18+ super ().setUpClass ()
19+ cls .iris = Table ("iris" )[::5 ]
20+
21+ def test_run (self ):
22+ for _ , metric in METRICS :
23+ # between rows, normalized
24+ dist1 = DistanceRunner .run (self .iris , metric , True , 0 , Mock ())
25+ dist2 = metric (self .iris , axis = 1 , impute = True , normalize = True )
26+ np .testing .assert_array_equal (dist1 , dist2 )
27+
28+ # between rows, not normalized
29+ dist1 = DistanceRunner .run (self .iris , metric , False , 0 , Mock ())
30+ dist2 = metric (self .iris , axis = 1 , impute = True , normalize = False )
31+ np .testing .assert_array_equal (dist1 , dist2 )
32+
33+ # between columns, normalized
34+ dist1 = DistanceRunner .run (self .iris , metric , True , 1 , Mock ())
35+ dist2 = metric (self .iris , axis = 0 , impute = True , normalize = True )
36+ np .testing .assert_array_equal (dist1 , dist2 )
37+
38+ # between columns, not normalized
39+ dist1 = DistanceRunner .run (self .iris , metric , False , 1 , Mock ())
40+ dist2 = metric (self .iris , axis = 0 , impute = True , normalize = False )
41+ np .testing .assert_array_equal (dist1 , dist2 )
42+
43+
1344class TestOWDistances (WidgetTest ):
1445 @classmethod
1546 def setUpClass (cls ):
@@ -27,6 +58,7 @@ def test_distance_combo(self):
2758 for i , (_ , metric ) in enumerate (METRICS ):
2859 self .widget .metrics_combo .activated .emit (i )
2960 self .widget .metrics_combo .setCurrentIndex (i )
61+ self .wait_until_stop_blocking ()
3062 self .send_signal (self .widget .Inputs .data , self .iris )
3163 if metric .supports_normalization :
3264 expected = metric (self .iris , normalize = self .widget .normalized_dist )
@@ -42,8 +74,10 @@ def test_error_message(self):
4274 data is removed from input"""
4375 self .widget .metric_idx = 2
4476 self .send_signal (self .widget .Inputs .data , self .iris )
77+ self .wait_until_stop_blocking ()
4578 self .assertFalse (self .widget .Error .no_continuous_features .is_shown ())
4679 self .send_signal (self .widget .Inputs .data , self .titanic )
80+ self .wait_until_stop_blocking ()
4781 self .assertTrue (self .widget .Error .no_continuous_features .is_shown ())
4882 self .send_signal (self .widget .Inputs .data , None )
4983 self .assertFalse (self .widget .Error .no_continuous_features .is_shown ())
@@ -53,32 +87,39 @@ def test_jaccard_messages(self):
5387 if name == "Jaccard" :
5488 break
5589 self .send_signal (self .widget .Inputs .data , self .iris )
90+ self .wait_until_stop_blocking ()
5691 self .assertTrue (self .widget .Error .no_binary_features .is_shown ())
5792 self .assertFalse (self .widget .Warning .ignoring_nonbinary .is_shown ())
5893
5994 self .send_signal (self .widget .Inputs .data , None )
95+ self .wait_until_stop_blocking ()
6096 self .assertFalse (self .widget .Error .no_binary_features .is_shown ())
6197 self .assertFalse (self .widget .Warning .ignoring_nonbinary .is_shown ())
6298
6399 self .send_signal (self .widget .Inputs .data , self .titanic )
100+ self .wait_until_stop_blocking ()
64101 self .assertFalse (self .widget .Error .no_binary_features .is_shown ())
65102 self .assertTrue (self .widget .Warning .ignoring_nonbinary .is_shown ())
66103
67104 self .send_signal (self .widget .Inputs .data , None )
105+ self .wait_until_stop_blocking ()
68106 self .assertFalse (self .widget .Error .no_binary_features .is_shown ())
69107 self .assertFalse (self .widget .Warning .ignoring_nonbinary .is_shown ())
70108
71109 self .send_signal (self .widget .Inputs .data , self .titanic )
110+ self .wait_until_stop_blocking ()
72111 self .assertFalse (self .widget .Error .no_binary_features .is_shown ())
73112 self .assertTrue (self .widget .Warning .ignoring_nonbinary .is_shown ())
74113
75114 dom = self .titanic .domain
76115 dom = Domain (dom .attributes [1 :], dom .class_var )
77116 self .send_signal (self .widget .Inputs .data , self .titanic .transform (dom ))
117+ self .wait_until_stop_blocking ()
78118 self .assertFalse (self .widget .Error .no_binary_features .is_shown ())
79119 self .assertFalse (self .widget .Warning .ignoring_nonbinary .is_shown ())
80120
81121 self .send_signal (self .widget .Inputs .data , Table ("heart_disease" ))
122+ self .wait_until_stop_blocking ()
82123 self .assertFalse (self .widget .Error .no_binary_features .is_shown ())
83124 self .assertFalse (self .widget .Warning .ignoring_discrete .is_shown ())
84125
@@ -93,10 +134,12 @@ def test_too_big_array(self):
93134
94135 mock = Mock (side_effect = ValueError )
95136 self .widget .compute_distances (mock , self .iris )
137+ self .wait_until_stop_blocking ()
96138 self .assertTrue (self .widget .Error .distances_value_error .is_shown ())
97139
98140 mock = Mock (side_effect = MemoryError )
99141 self .widget .compute_distances (mock , self .iris )
142+ self .wait_until_stop_blocking ()
100143 self .assertEqual (len (self .widget .Error .active ), 1 )
101144 self .assertTrue (self .widget .Error .distances_memory_error .is_shown ())
102145
0 commit comments