@@ -38,9 +38,14 @@ def empty_results():
3838 return empty_results
3939
4040
41- def test_create_project ():
41+ @pytest .fixture
42+ def model ():
43+ with patch ("rascal2.ui.model.os.chdir" , autospec = True ):
44+ yield MainWindowModel ()
45+
46+
47+ def test_create_project (model ):
4248 """The project should be set up with the desired name and default objets when a new project is created."""
43- model = MainWindowModel ()
4449 assert model .project is None
4550 assert model .controls is None
4651 assert model .results is None
@@ -53,8 +58,7 @@ def test_create_project():
5358 assert model .save_path == "C:/test"
5459
5560
56- def test_save_project (empty_results ):
57- model = MainWindowModel ()
61+ def test_save_project (empty_results , model ):
5862 model .project = Project (calculation = "domains" , name = "test project" )
5963 model .controls = Controls (procedure = "dream" , resampleMinAngle = 0.5 )
6064 model .results = empty_results
@@ -73,9 +77,8 @@ def test_save_project(empty_results):
7377 assert '"fitParams": []' in results
7478
7579
76- def test_load_project (empty_results ):
80+ def test_load_project (empty_results , model ):
7781 """The load function should load the correct controls object from JSON."""
78- model = MainWindowModel ()
7982 project = Project (name = "test project" , calculation = "domains" )
8083
8184 with TemporaryDirectory () as tmpdir :
@@ -91,20 +94,17 @@ def test_load_project(empty_results):
9194
9295
9396@patch ("ratapi.utils.convert.r1_to_project" )
94- def test_load_r1_project (mock_r1_class ):
97+ def test_load_r1_project (mock_r1_class , model ):
9598 """load_r1_project should call the conversion function and set the path correctly."""
96- model = MainWindowModel ()
9799 model .load_r1_project ("test_path/r1project.mat" )
98100
99101 mock_r1_class .assert_called_once ()
100102 assert model .save_path == "test_path"
101103
102104
103105@pytest .mark .parametrize ("bad_json" , ['{"field1":3' , '{"procedure":"fry eggs"}' ])
104- def test_load_controls_error (bad_json ):
106+ def test_load_controls_error (bad_json , model ):
105107 """The project load function should raise an error if the controls JSON is invalid or the parameters are invalid."""
106- model = MainWindowModel ()
107-
108108 with pytest .raises ( # noqa (for nested with's: pytest.raises breaks if not by itself)
109109 ValueError ,
110110 match = "The controls.json file for this project is not valid.\n "
@@ -116,10 +116,8 @@ def test_load_controls_error(bad_json):
116116
117117
118118@pytest .mark .parametrize ("bad_json" , ['{"calculation":"Do}' , '{i"m not a good project file' ])
119- def test_load_project_decode_error (bad_json ):
119+ def test_load_project_decode_error (bad_json , model ):
120120 """The project load function should raise an error if the project JSON is invalid JSON."""
121- model = MainWindowModel ()
122-
123121 with pytest .raises ( # noqa (for nested with's: pytest.raises breaks if not by itself)
124122 ValueError , match = "The project.json file for this project contains invalid JSON."
125123 ):
@@ -132,10 +130,8 @@ def test_load_project_decode_error(bad_json):
132130@pytest .mark .parametrize (
133131 "bad_json" , ['{"calculation":"guessing"}' , '{"parameters":[{"name":"parameter 1","thickness":0.51}]}' ]
134132)
135- def test_load_project_value_error (bad_json ):
133+ def test_load_project_value_error (bad_json , model ):
136134 """The project load function should raise an error if the values are not valid."""
137- model = MainWindowModel ()
138-
139135 with pytest .raises ( # noqa (for nested with's: pytest.raises breaks if not by itself)
140136 ValueError , match = "The project.json file for this project is not valid."
141137 ):
0 commit comments