11# Test methods with long descriptive names can omit docstrings
22# pylint: disable=missing-docstring
3+ from unittest .mock import Mock
4+
5+ from numpy import testing as npt
6+
37from Orange .data import Table
48from Orange .preprocess import Discretize , Continuize
5- from Orange .preprocess .preprocess import Preprocess
69from Orange .widgets .data .owtransform import OWTransform
710from Orange .widgets .tests .base import WidgetTest
811from Orange .widgets .unsupervised .owpca import OWPCA
@@ -12,39 +15,39 @@ class TestOWTransform(WidgetTest):
1215 def setUp (self ):
1316 self .widget = self .create_widget (OWTransform )
1417 self .data = Table ("iris" )
15- self .preprocessor = Discretize ()
18+ self .disc_data = Discretize ()( self . data )
1619
1720 def test_output (self ):
18- # send data and preprocessor
19- self .send_signal (self .widget .Inputs .data , self .data )
20- self .send_signal (self .widget .Inputs .preprocessor , self .preprocessor )
21+ # send data and template data
22+ self .send_signal (self .widget .Inputs .data , self .data [:: 15 ] )
23+ self .send_signal (self .widget .Inputs .template_data , self .disc_data )
2124 output = self .get_output (self .widget .Outputs .transformed_data )
22- self .assertIsInstance (output , Table )
23- self .assertEqual ("Input data with 150 instances and 4 features." ,
25+ self .assertTableEqual (output , self . disc_data [:: 15 ] )
26+ self .assertEqual ("Input data with 10 instances and 4 features." ,
2427 self .widget .input_label .text ())
25- self .assertEqual ("Preprocessor Discretize() applied." ,
26- self .widget .preprocessor_label .text ())
28+ self .assertEqual ("Template domain applied." ,
29+ self .widget .template_label .text ())
2730 self .assertEqual ("Output data includes 4 features." ,
2831 self .widget .output_label .text ())
2932
30- # remove preprocessor
31- self .send_signal (self .widget .Inputs .preprocessor , None )
33+ # remove template data
34+ self .send_signal (self .widget .Inputs .template_data , None )
3235 output = self .get_output (self .widget .Outputs .transformed_data )
3336 self .assertIsNone (output )
34- self .assertEqual ("Input data with 150 instances and 4 features." ,
37+ self .assertEqual ("Input data with 10 instances and 4 features." ,
3538 self .widget .input_label .text ())
36- self .assertEqual ("No preprocessor on input." ,
37- self .widget .preprocessor_label .text ())
39+ self .assertEqual ("No template data on input." ,
40+ self .widget .template_label .text ())
3841 self .assertEqual ("" , self .widget .output_label .text ())
3942
40- # send preprocessor
41- self .send_signal (self .widget .Inputs .preprocessor , self .preprocessor )
43+ # send template data
44+ self .send_signal (self .widget .Inputs .template_data , self .disc_data )
4245 output = self .get_output (self .widget .Outputs .transformed_data )
43- self .assertIsInstance (output , Table )
44- self .assertEqual ("Input data with 150 instances and 4 features." ,
46+ self .assertTableEqual (output , self . disc_data [:: 15 ] )
47+ self .assertEqual ("Input data with 10 instances and 4 features." ,
4548 self .widget .input_label .text ())
46- self .assertEqual ("Preprocessor Discretize() applied." ,
47- self .widget .preprocessor_label .text ())
49+ self .assertEqual ("Template domain applied." ,
50+ self .widget .template_label .text ())
4851 self .assertEqual ("Output data includes 4 features." ,
4952 self .widget .output_label .text ())
5053
@@ -53,48 +56,55 @@ def test_output(self):
5356 output = self .get_output (self .widget .Outputs .transformed_data )
5457 self .assertIsNone (output )
5558 self .assertEqual ("No data on input." , self .widget .input_label .text ())
56- self .assertEqual ("Preprocessor Discretize() on input ." ,
57- self .widget .preprocessor_label .text ())
59+ self .assertEqual ("Template data includes 4 features ." ,
60+ self .widget .template_label .text ())
5861 self .assertEqual ("" , self .widget .output_label .text ())
5962
60- # remove preprocessor
61- self .send_signal (self .widget .Inputs .preprocessor , None )
63+ # remove template data
64+ self .send_signal (self .widget .Inputs .template_data , None )
6265 self .assertEqual ("No data on input." , self .widget .input_label .text ())
63- self .assertEqual ("No preprocessor on input." ,
64- self .widget .preprocessor_label .text ())
66+ self .assertEqual ("No template data on input." ,
67+ self .widget .template_label .text ())
6568 self .assertEqual ("" , self .widget .output_label .text ())
6669
67- def test_input_pca_preprocessor (self ):
70+ def assertTableEqual (self , table1 , table2 ):
71+ self .assertIs (table1 .domain , table2 .domain )
72+ npt .assert_array_equal (table1 .X , table2 .X )
73+ npt .assert_array_equal (table1 .Y , table2 .Y )
74+ npt .assert_array_equal (table1 .metas , table2 .metas )
75+
76+ def test_input_pca_output (self ):
6877 owpca = self .create_widget (OWPCA )
6978 self .send_signal (owpca .Inputs .data , self .data , widget = owpca )
7079 owpca .components_spin .setValue (2 )
71- pp = self .get_output (owpca .Outputs .preprocessor , widget = owpca )
72- self .assertIsNotNone (pp , Preprocess )
80+ pca_out = self .get_output (owpca .Outputs .transformed_data , widget = owpca )
7381
74- self .send_signal (self .widget .Inputs .data , self .data )
75- self .send_signal (self .widget .Inputs .preprocessor , pp )
82+ self .send_signal (self .widget .Inputs .data , self .data [:: 10 ] )
83+ self .send_signal (self .widget .Inputs .template_data , pca_out )
7684 output = self .get_output (self .widget .Outputs .transformed_data )
77- self .assertIsInstance (output , Table )
78- self .assertEqual (output .X .shape , (len (self .data ), 2 ))
85+ npt .assert_array_equal (pca_out .X [::10 ], output .X )
7986
8087 def test_retain_all_data (self ):
8188 data = Table ("zoo" )
89+ cont_data = Continuize ()(data )
8290 self .send_signal (self .widget .Inputs .data , data )
83- self .send_signal (self .widget .Inputs .preprocessor , Continuize () )
91+ self .send_signal (self .widget .Inputs .template_data , cont_data )
8492 self .widget .controls .retain_all_data .click ()
8593 output = self .get_output (self .widget .Outputs .transformed_data )
8694 self .assertIsInstance (output , Table )
8795 self .assertEqual (output .X .shape , (len (data ), 16 ))
88- self .assertEqual (output .metas .shape , (len (data ), 37 ))
96+ self .assertEqual (output .metas .shape , (len (data ), 38 ))
8997
9098 def test_error_transforming (self ):
91- self .send_signal (self .widget .Inputs .data , self .data )
92- self .send_signal (self .widget .Inputs .preprocessor , Preprocess ())
93- self .assertTrue (self .widget .Error .pp_error .is_shown ())
99+ data = self .data [::10 ]
100+ data .transform = Mock (side_effect = Exception ())
101+ self .send_signal (self .widget .Inputs .data , data )
102+ self .send_signal (self .widget .Inputs .template_data , self .disc_data )
103+ self .assertTrue (self .widget .Error .error .is_shown ())
94104 output = self .get_output (self .widget .Outputs .transformed_data )
95105 self .assertIsNone (output )
96106 self .send_signal (self .widget .Inputs .data , None )
97- self .assertFalse (self .widget .Error .pp_error .is_shown ())
107+ self .assertFalse (self .widget .Error .error .is_shown ())
98108
99109 def test_send_report (self ):
100110 self .send_signal (self .widget .Inputs .data , self .data )
0 commit comments