11# Test methods with long descriptive names can omit docstrings
22# pylint: disable=missing-docstring
3+ import os
4+ from itertools import chain
5+ import unittest
36from unittest .mock import patch , Mock
47
58import numpy as np
69from AnyQt .QtCore import QRectF , QPointF
710
11+ from Orange .data import Table
12+ from Orange .misc import DistMatrix
813from Orange .distance import Euclidean
914from Orange .widgets .settings import Context
1015from Orange .widgets .unsupervised .owmds import OWMDS
@@ -22,6 +27,10 @@ def setUpClass(cls):
2227 cls .signal_data = Euclidean (cls .data )
2328 cls .same_input_output_domain = False
2429
30+ my_dir = os .path .dirname (__file__ )
31+ datasets_dir = os .path .join (my_dir , '..' , '..' , '..' , 'datasets' )
32+ cls .datasets_dir = os .path .realpath (datasets_dir )
33+
2534 def setUp (self ):
2635 self .widget = self .create_widget (
2736 OWMDS , stored_settings = {
@@ -30,6 +39,9 @@ def setUp(self):
3039 "initialization" : OWMDS .PCA ,
3140 }
3241 ) # type: OWMDS
42+ self .towns = DistMatrix .from_file (
43+ os .path .join (self .datasets_dir , "slovenian-towns.dst" ))
44+
3345
3446 def tearDown (self ):
3547 self .widget .onDeleteWidget ()
@@ -174,3 +186,106 @@ def test_migrate_settings_from_version_1(self):
174186 (g .jitter_size , 0.5 )):
175187 self .assertTrue (a , value )
176188 self .assertFalse (w .auto_commit )
189+
190+ def test_attr_label_from_dist_matrix_from_file (self ):
191+ w = self .widget
192+ # Don't run the MDS optimization to save time and to prevent the
193+ # widget be in a blocking state when trying to send the next signal
194+ w .start = Mock ()
195+ row_items = self .towns .row_items
196+
197+ # Distance matrix with labels
198+ self .send_signal (w .Inputs .distances , self .towns )
199+ self .assertIs (w .graph .attr_label , row_items .domain ["label" ])
200+
201+ # Distances matrix without labels
202+ self .towns .row_items = None
203+ self .send_signal (w .Inputs .distances , self .towns )
204+ self .assertIsNone (w .graph .attr_label )
205+
206+ # No data
207+ self .send_signal (w .Inputs .distances , None )
208+ self .assertIsNone (w .graph .attr_label )
209+
210+ # Distances matrix with labels again
211+ self .towns .row_items = row_items
212+ self .send_signal (w .Inputs .distances , self .towns )
213+ self .assertIs (w .graph .attr_label , row_items .domain ["label" ])
214+
215+ # Followed by no data
216+ self .towns .row_items = None
217+ self .send_signal (w .Inputs .distances , self .towns )
218+ self .assertIsNone (w .graph .attr_label )
219+
220+ def test_attr_label_from_dist_matrix_from_data (self ):
221+ w = self .widget
222+ # Don't run the MDS optimization to save time and to prevent the
223+ # widget be in a blocking state when trying to send the next signal
224+ w .start = Mock ()
225+
226+ data = Table ("zoo" )
227+ dist = Euclidean (data )
228+ self .send_signal (w .Inputs .distances , dist )
229+ self .send_signal (w .Inputs .data , data )
230+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
231+ < set (w .label_model ))
232+ self .assertIs (w .graph .attr_label , data .domain .metas [0 ])
233+
234+ def test_attr_label_from_data (self ):
235+ w = self .widget
236+ # Don't run the MDS optimization to save time and to prevent the
237+ # widget be in a blocking state when trying to send the next signal
238+ w .start = Mock ()
239+
240+ # Data with string attribute: all attributes present, string selected
241+ data = Table ("zoo" )
242+ dist = Euclidean (data )
243+ self .send_signal (w .Inputs .distances , dist )
244+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
245+ < set (w .label_model ))
246+ self .assertIs (w .graph .attr_label , data .domain .metas [0 ])
247+
248+ # Data without string attribute: all attributes present, none selected
249+ data = Table ("iris" )
250+ dist = Euclidean (data )
251+ self .send_signal (w .Inputs .distances , dist )
252+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
253+ < set (w .label_model ))
254+ self .assertIsNone (w .graph .attr_label )
255+
256+ def test_attr_label_matrix_and_data (self ):
257+ w = self .widget
258+ # Don't run the MDS optimization to save time and to prevent the
259+ # widget be in a blocking state when trying to send the next signal
260+ w .start = Mock ()
261+
262+ # Data and matrix
263+ data = Table ("zoo" )
264+ dist = Euclidean (data )
265+ self .send_signal (w .Inputs .distances , dist )
266+ self .send_signal (w .Inputs .data , data )
267+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
268+ < set (w .label_model ))
269+ self .assertIs (w .graph .attr_label , data .domain .metas [0 ])
270+
271+ # Has data, but receives a signal without data: has to keep the label
272+ dist .row_items = None
273+ self .send_signal (w .Inputs .distances , dist )
274+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
275+ < set (w .label_model ))
276+ self .assertIs (w .graph .attr_label , data .domain .metas [0 ])
277+
278+ # Has matrix without data, and loses the data: remove the label
279+ self .send_signal (w .Inputs .data , None )
280+ self .assertEqual (len (w .label_model ), 1 ) # just None
281+ self .assertIsNone (w .graph .attr_label )
282+
283+ # Has matrix without data, receives data: add attrs to combo, select
284+ self .send_signal (w .Inputs .data , data )
285+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
286+ < set (w .label_model ))
287+ self .assertIs (w .graph .attr_label , data .domain .metas [0 ])
288+
289+
290+ if __name__ == "__main__" :
291+ unittest .main ()
0 commit comments