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,91 @@ 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 .assertIn (row_items .domain ["label" ], w .label_model )
200+
201+ # Distances matrix without labels
202+ self .towns .row_items = None
203+ self .send_signal (w .Inputs .distances , self .towns )
204+ self .assertEqual (list (w .label_model ), [None ])
205+
206+ # No data
207+ self .send_signal (w .Inputs .distances , None )
208+ self .assertEqual (list (w .label_model ), [None ])
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 .assertIn (row_items .domain ["label" ], w .label_model )
214+
215+ # Followed by no data
216+ self .towns .row_items = None
217+ self .send_signal (w .Inputs .distances , self .towns )
218+ self .assertEqual (list (w .label_model ), [None ])
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+
233+ def test_attr_label_from_data (self ):
234+ w = self .widget
235+ # Don't run the MDS optimization to save time and to prevent the
236+ # widget be in a blocking state when trying to send the next signal
237+ w .start = Mock ()
238+
239+ data = Table ("zoo" )
240+ dist = Euclidean (data )
241+ self .send_signal (w .Inputs .distances , dist )
242+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
243+ < set (w .label_model ))
244+
245+ def test_attr_label_matrix_and_data (self ):
246+ w = self .widget
247+ # Don't run the MDS optimization to save time and to prevent the
248+ # widget be in a blocking state when trying to send the next signal
249+ w .start = Mock ()
250+
251+ # Data and matrix
252+ data = Table ("zoo" )
253+ dist = Euclidean (data )
254+ self .send_signal (w .Inputs .distances , dist )
255+ self .send_signal (w .Inputs .data , data )
256+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
257+ < set (w .label_model ))
258+
259+ # Has data, but receives a signal without data: has to keep the label
260+ dist .row_items = None
261+ self .send_signal (w .Inputs .distances , dist )
262+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
263+ < set (w .label_model ))
264+
265+ # Has matrix without data, and loses the data: remove the label
266+ self .send_signal (w .Inputs .data , None )
267+ self .assertEqual (list (w .label_model ), [None ])
268+
269+ # Has matrix without data, receives data: add attrs to combo, select
270+ self .send_signal (w .Inputs .data , data )
271+ self .assertTrue (set (chain (data .domain .variables , data .domain .metas ))
272+ < set (w .label_model ))
273+
274+
275+ if __name__ == "__main__" :
276+ unittest .main ()
0 commit comments