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
4- from Orange .preprocess import Discretize
5- from Orange .preprocess .preprocess import Preprocess
8+ from Orange .preprocess import Discretize , Continuize
69from Orange .widgets .data .owtransform import OWTransform
710from Orange .widgets .tests .base import WidgetTest
811from Orange .widgets .unsupervised .owpca import OWPCA
@@ -12,38 +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." , self .widget .preprocessor_label .text ())
39+ self .assertEqual ("No template data on input." ,
40+ self .widget .template_label .text ())
3741 self .assertEqual ("" , self .widget .output_label .text ())
3842
39- # send preprocessor
40- 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 )
4145 output = self .get_output (self .widget .Outputs .transformed_data )
42- self .assertIsInstance (output , Table )
43- 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." ,
4448 self .widget .input_label .text ())
45- self .assertEqual ("Preprocessor Discretize() applied." ,
46- self .widget .preprocessor_label .text ())
49+ self .assertEqual ("Template domain applied." ,
50+ self .widget .template_label .text ())
4751 self .assertEqual ("Output data includes 4 features." ,
4852 self .widget .output_label .text ())
4953
@@ -52,49 +56,63 @@ def test_output(self):
5256 output = self .get_output (self .widget .Outputs .transformed_data )
5357 self .assertIsNone (output )
5458 self .assertEqual ("No data on input." , self .widget .input_label .text ())
55- self .assertEqual ("Preprocessor Discretize() on input ." ,
56- self .widget .preprocessor_label .text ())
59+ self .assertEqual ("Template data includes 4 features ." ,
60+ self .widget .template_label .text ())
5761 self .assertEqual ("" , self .widget .output_label .text ())
5862
59- # remove preprocessor
60- self .send_signal (self .widget .Inputs .preprocessor , None )
63+ # remove template data
64+ self .send_signal (self .widget .Inputs .template_data , None )
6165 self .assertEqual ("No data on input." , self .widget .input_label .text ())
62- self .assertEqual ("No preprocessor on input." ,
63- self .widget .preprocessor_label .text ())
66+ self .assertEqual ("No template data on input." ,
67+ self .widget .template_label .text ())
6468 self .assertEqual ("" , self .widget .output_label .text ())
6569
66- 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 ):
6777 owpca = self .create_widget (OWPCA )
6878 self .send_signal (owpca .Inputs .data , self .data , widget = owpca )
6979 owpca .components_spin .setValue (2 )
70- pp = self .get_output (owpca .Outputs .preprocessor , widget = owpca )
71- self .assertIsNotNone (pp , Preprocess )
80+ pca_out = self .get_output (owpca .Outputs .transformed_data , widget = owpca )
7281
73- self .send_signal (self .widget .Inputs .data , self .data )
74- 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 )
7584 output = self .get_output (self .widget .Outputs .transformed_data )
76- self .assertIsInstance (output , Table )
77- self .assertEqual (output .X .shape , (len (self .data ), 2 ))
85+ npt .assert_array_equal (pca_out .X [::10 ], output .X )
7886
79- # test retain data functionality
80- self .widget .retain_all_data = True
81- self .widget .apply ()
87+ def test_retain_all_data (self ):
88+ data = Table ("zoo" )
89+ cont_data = Continuize ()(data )
90+ self .send_signal (self .widget .Inputs .data , data )
91+ self .send_signal (self .widget .Inputs .template_data , cont_data )
92+ self .widget .controls .retain_all_data .click ()
8293 output = self .get_output (self .widget .Outputs .transformed_data )
8394 self .assertIsInstance (output , Table )
84- self .assertEqual (output .X .shape , (len (self . data ), 4 ))
85- self .assertEqual (output .metas .shape , (len (self . data ), 2 ))
95+ self .assertEqual (output .X .shape , (len (data ), 16 ))
96+ self .assertEqual (output .metas .shape , (len (data ), 38 ))
8697
8798 def test_error_transforming (self ):
88- self .send_signal (self .widget .Inputs .data , self .data )
89- self .send_signal (self .widget .Inputs .preprocessor , Preprocess ())
90- 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 ())
91104 output = self .get_output (self .widget .Outputs .transformed_data )
92105 self .assertIsNone (output )
93106 self .send_signal (self .widget .Inputs .data , None )
94- self .assertFalse (self .widget .Error .pp_error .is_shown ())
107+ self .assertFalse (self .widget .Error .error .is_shown ())
95108
96109 def test_send_report (self ):
97110 self .send_signal (self .widget .Inputs .data , self .data )
98111 self .widget .report_button .click ()
99112 self .send_signal (self .widget .Inputs .data , None )
100113 self .widget .report_button .click ()
114+
115+
116+ if __name__ == "__main__" :
117+ import unittest
118+ unittest .main ()
0 commit comments