1
1
import sys
2
2
from random import uniform
3
- from os .path import join , isfile
3
+ from os import makedirs
4
+ from os .path import join , isfile , isdir
4
5
import mock
5
6
6
7
import pytest
10
11
11
12
from pyleecan .GUI .Dialog .DMachineSetup .DMachineSetup import DMachineSetup
12
13
from pyleecan .GUI .Dialog .DMachineSetup .SPreview .SPreview import SPreview
13
- from Tests import TEST_DATA_DIR as data_test
14
+ from Tests import TEST_DATA_DIR as data_test , save_gui_path
14
15
from pyleecan .definitions import MAIN_DIR
15
16
from pyleecan .Functions .load import load_matlib
16
17
48
49
49
50
50
51
class TestSPreview (object ):
51
- @pytest .fixture
52
- def setup (self ):
53
- """Run at the begining of every test to setup the gui"""
54
52
53
+ @classmethod
54
+ def setup_class (cls ):
55
+ """Start the app for the test"""
56
+ print ("\n Start Test TestSPreview" )
55
57
if not QtWidgets .QApplication .instance ():
56
- self .app = QtWidgets .QApplication (sys .argv )
58
+ cls .app = QtWidgets .QApplication (sys .argv )
57
59
else :
58
- self .app = QtWidgets .QApplication .instance ()
60
+ cls .app = QtWidgets .QApplication .instance ()
59
61
62
+ def setup_method (self ):
63
+ """Run at the begining of every test to setup the gui"""
60
64
# MatLib widget
61
65
material_dict = load_matlib (matlib_path = matlib_path )
62
- widget = DMachineSetup (material_dict = material_dict , machine_path = machine_path )
66
+ self . widget = DMachineSetup (material_dict = material_dict , machine_path = machine_path )
63
67
64
- yield {"widget" : widget }
65
-
66
- self .app .quit ()
68
+ @classmethod
69
+ def teardown_class (cls ):
70
+ """Exit the app after the test"""
71
+ cls .app .quit ()
67
72
68
73
@pytest .mark .parametrize ("test_dict" , load_preview_test )
69
- def test_load (self , setup , test_dict ):
74
+ def test_load (self , test_dict ):
70
75
"""Check that you can load a machine"""
71
76
assert isfile (test_dict ["file_path" ])
72
77
@@ -75,20 +80,46 @@ def test_load(self, setup, test_dict):
75
80
"PySide2.QtWidgets.QFileDialog.getOpenFileName" , return_value = return_value
76
81
):
77
82
# To trigger the slot
78
- setup [ " widget" ] .b_load .clicked .emit ()
83
+ self . widget .b_load .clicked .emit ()
79
84
80
85
# Check load MachineType
81
- assert type (setup [ " widget" ] .w_step ) is SPreview
86
+ assert type (self . widget .w_step ) is SPreview
82
87
# Check the table
83
88
assert (
84
- setup [ " widget" ] .w_step .tab_machine .tab_param .rowCount () == test_dict ["Nrow" ]
89
+ self . widget .w_step .tab_machine .tab_param .rowCount () == test_dict ["Nrow" ]
85
90
)
86
91
for ii , content in enumerate (test_dict ["table" ]):
87
92
assert (
88
- setup [ " widget" ] .w_step .tab_machine .tab_param .item (ii , 0 ).text ()
93
+ self . widget .w_step .tab_machine .tab_param .item (ii , 0 ).text ()
89
94
== content [0 ]
90
95
)
91
96
assert (
92
- setup [ " widget" ] .w_step .tab_machine .tab_param .item (ii , 1 ).text ()
97
+ self . widget .w_step .tab_machine .tab_param .item (ii , 1 ).text ()
93
98
== content [1 ]
94
99
)
100
+ # Check Draw FEMM
101
+ FEMM_dir = join (save_gui_path , "Draw_FEMM" )
102
+ if not isdir (FEMM_dir ):
103
+ makedirs (FEMM_dir )
104
+ femm_path = join (FEMM_dir , self .widget .machine .name + ".fem" )
105
+ assert not isfile (femm_path )
106
+
107
+ return_value = (
108
+ femm_path ,
109
+ "FEMM (*.fem)" ,
110
+ )
111
+ with mock .patch (
112
+ "PySide2.QtWidgets.QFileDialog.getSaveFileName" , return_value = return_value
113
+ ):
114
+ self .widget .w_step .tab_machine .b_FEMM .clicked .emit ()
115
+ assert isfile (femm_path )
116
+
117
+ if __name__ == "__main__" :
118
+ a = TestSPreview ()
119
+ a .setup_class ()
120
+ a .setup_method ()
121
+ for ii , test_dict in enumerate (load_preview_test ):
122
+ print (ii )
123
+ a .test_load (test_dict )
124
+ # a.test_load(load_preview_test[0])
125
+ print ("Done" )
0 commit comments