1414
1515@pytest .fixture
1616def app_state_fixture ():
17+ """A pytest fixture which returns an instance of AppState class with no errors.
18+
19+ Returns:
20+ AppState: An object of the AppState class
21+ """
1722 settings = {"error" : None }
1823 app_state = AppState (settings = settings )
1924 return app_state
2025
2126
2227@pytest .fixture
2328def mocker_os_patching_fixture (mocker , platform ):
29+ """A pytest fixture which patches the is_* functions in system.py module
30+
31+ Args:
32+ mocker : Built in pytest fixture
33+ platform (str): A string representing "windows", "linux" or "mac"
34+
35+ Returns:
36+ mocker: Built in pytest fixture with patched calls to system.py module.
37+ """
2438 mocker .patch ("matlab_proxy.app_state.system.is_linux" , return_value = False )
2539 mocker .patch ("matlab_proxy.app_state.system.is_windows" , return_value = False )
2640 mocker .patch ("matlab_proxy.app_state.system.is_mac" , return_value = False )
@@ -35,11 +49,15 @@ def mocker_os_patching_fixture(mocker, platform):
3549
3650@dataclass (frozen = True )
3751class Mock_xvfb :
52+ """An immutable dataclass representing a mocked Xvfb process"""
53+
3854 returncode : Optional [int ]
3955
4056
4157@dataclass (frozen = True )
4258class Mock_matlab :
59+ """An immutable dataclass representing a mocked MATLAB process"""
60+
4361 returncode : Optional [int ]
4462
4563
@@ -74,32 +92,67 @@ class Mock_matlab:
7492 ],
7593)
7694def test_is_licensed (app_state_fixture , licensing , expected ):
95+ """Test to check is_licensed()
96+
97+ Args:
98+ app_state_fixture (AppState): Object of AppState class with defaults set
99+ licensing (dict): Represents licensing information
100+ expected (bool): Expected return value.
101+ """
102+ # Arrange
103+ # Nothing to arrange
104+
105+ # Act
77106 app_state_fixture .licensing = licensing
107+
108+ # Assert
78109 assert app_state_fixture .is_licensed () == expected
79110
80111
81112@pytest .mark .parametrize (
82- "err, licensing, expected_err" ,
113+ "err, expected_err" ,
83114 [
84- (MatlabError (message = "dummy error" ), None , MatlabError (message = "dummy" )),
85- (LicensingError (message = "license issue" ), None , None ),
115+ (MatlabError (message = "dummy error" ), MatlabError (message = "dummy" )),
116+ (LicensingError (message = "license issue" ), None ),
86117 ],
87118 ids = ["Any error except licensing error" , "licensing error" ],
88119)
89- def test_unset_licensing (err , licensing , expected_err ):
120+ def test_unset_licensing (err , expected_err ):
121+ """Test to check unset_liecnsing removes licensing from the AppState object
122+
123+ Args:
124+ err (Exception): Custom exceptions defined in exceptions.py
125+ licensing (bool): Whether licensing info is removed
126+ expected_err (Exception): Expected exception
127+ """
128+ # Arrange
90129 settings = {"error" : err }
91130 app_state = AppState (settings = settings )
131+
132+ # Act
92133 app_state .unset_licensing ()
93- assert app_state .licensing == licensing
134+
135+ # Assert
136+ assert app_state .licensing == None
94137 assert type (app_state .error ) is type (expected_err )
95138
96139
97140# config file is deleted when licensing info is not set i.e. set to None
98141def test_persist_licensing_when_licensing_info_is_not_set (tmp_path ):
142+ """Test to check if data is not persisted to a file if licensing info is not present
143+
144+ Args:
145+ tmp_path (Path): Built in pytest fixture
146+ """
147+ # Arrange
99148 tmp_file = tmp_path / "tmp_file.json"
100149 settings = {"matlab_config_file" : tmp_file , "error" : None }
101150 app_state = AppState (settings = settings )
151+
152+ # Act
102153 app_state .persist_licensing ()
154+
155+ # Assert
103156 assert os .path .exists (tmp_file ) is False
104157
105158
@@ -121,13 +174,24 @@ def test_persist_licensing_when_licensing_info_is_not_set(tmp_path):
121174 ids = ["nlm type" , "mhlm type" , "existing license type" ],
122175)
123176def test_persist_licensing (data : dict , tmp_path ):
177+ """Test to check if persist_licensing() writes data to the file system
178+
179+ Args:
180+ data (dict): Represents matlab-proxy licensing data
181+ tmp_path : Built-in pytest fixture.
182+ """
183+ # Arrange
124184 tmp_file = tmp_path / "parent_1" / "parent_2" / "tmp_file.json"
125185 settings = {"matlab_config_file" : tmp_file , "error" : None }
126186 app_state = AppState (settings = settings )
127187 app_state .licensing = data
188+
189+ # Act
128190 app_state .persist_licensing ()
129191 with open (tmp_file , "r" ) as file :
130192 got = file .read ()
193+
194+ # Assert
131195 assert json .loads (got ) == data
132196
133197
@@ -169,7 +233,23 @@ def test_persist_licensing(data: dict, tmp_path):
169233def test_are_required_processes_ready (
170234 app_state_fixture , mocker_os_patching_fixture , matlab , xvfb , expected
171235):
172- assert app_state_fixture ._are_required_processes_ready (matlab , xvfb ) == expected
236+ """Test to check if required processes are ready
237+
238+ Args:
239+ app_state_fixture (AppState): Object of AppState class with defaults set
240+ mocker_os_patching_fixture (mocker): Custom pytest fixture for mocking
241+ matlab (Mock_matlab): Represents a mocked MATLAB process
242+ xvfb (Mock_xvfb): Represents a mocked Xvfb process
243+ expected (bool): Expected return value based on process return code
244+ """
245+ # Arrange
246+ # Nothing to arrange
247+
248+ # Act
249+ actual = app_state_fixture ._are_required_processes_ready (matlab , xvfb )
250+
251+ # Assert
252+ assert actual == expected
173253
174254
175255get_matlab_status_based_on_connector_status_test_data = [
@@ -187,6 +267,15 @@ def test_are_required_processes_ready(
187267async def test_get_matlab_status_based_on_connector_status (
188268 mocker , connector_status , ready_file_present , matlab_status
189269):
270+ """Test to check matlab status based on connector status
271+
272+ Args:
273+ mocker : Built in pytest fixture.
274+ connector_status (str): Status of Embedded Connector.
275+ ready_file_present (bool): Represents if the ready file has been created or not.
276+ matlab_status (str): Represents the status of MATLAB process.
277+ """
278+ # Arrange
190279 mocker .patch (
191280 "matlab_proxy.app_state.mwi.embedded_connector.request.get_state" ,
192281 return_value = connector_status ,
@@ -199,7 +288,12 @@ async def test_get_matlab_status_based_on_connector_status(
199288 }
200289 app_state = AppState (settings = settings )
201290 app_state .matlab_session_files ["matlab_ready_file" ] = Path ("dummy" )
202- assert await app_state ._get_matlab_connector_status () == matlab_status
291+
292+ # Act
293+ actual_matlab_status = await app_state ._get_matlab_connector_status ()
294+
295+ # Assert
296+ assert actual_matlab_status == matlab_status
203297
204298
205299@pytest .mark .parametrize (
@@ -218,6 +312,16 @@ async def test_get_matlab_status_based_on_connector_status(
218312async def test_get_matlab_state (
219313 app_state_fixture , mocker , valid_processes , connector_status , expected
220314):
315+ """Test to check get_matlab_state returns the correct MATLAB state based on the connector status
316+
317+ Args:
318+ app_state_fixture (AppState): Object of AppState class with defaults set
319+ mocker : Built in pytest fixture
320+ valid_processes (bool): Represents if the processes are valid or not
321+ connector_status (str): Status of Embedded Connector.
322+ expected (str): Expected status of MATLAB process.
323+ """
324+ # Arrange
221325 mocker .patch .object (
222326 AppState ,
223327 "_are_required_processes_ready" ,
@@ -228,11 +332,24 @@ async def test_get_matlab_state(
228332 "_get_matlab_connector_status" ,
229333 return_value = connector_status ,
230334 )
231- assert await app_state_fixture .get_matlab_state () == expected
335+
336+ # Act
337+ actual_state = await app_state_fixture .get_matlab_state ()
338+
339+ # Assert
340+ assert actual_state == expected
232341
233342
234343@pytest .mark .parametrize ("platform" , [("linux" ), ("windows" ), ("mac" )])
235344async def test_track_embedded_connector (mocker_os_patching_fixture , app_state_fixture ):
345+ """Test to check track_embedded_connector task
346+
347+ Args:
348+ mocker_os_patching_fixture (mocker): Custom pytest fixture for mocking
349+ app_state_fixture (AppState): Object of AppState class with defaults set
350+ """
351+
352+ # Arrange
236353 # patching embedded_connector_start_time to EPOCH+1 seconds and state to be "down"
237354 mocker_os_patching_fixture .patch .object (
238355 app_state_fixture , "embedded_connector_start_time" , new = float (1.0 )
@@ -243,7 +360,11 @@ async def test_track_embedded_connector(mocker_os_patching_fixture, app_state_fi
243360
244361 # verify that stop_matlab() is called once
245362 spy = mocker_os_patching_fixture .spy (app_state_fixture , "stop_matlab" )
363+
364+ # Act
246365 await app_state_fixture ._AppState__track_embedded_connector_state ()
366+
367+ # Assert
247368 spy .assert_called_once ()
248369
249370
@@ -255,10 +376,22 @@ async def test_track_embedded_connector(mocker_os_patching_fixture, app_state_fi
255376def test_env_variables_filtration_for_xvfb_process (
256377 monkeypatch , env_var_name , filter_prefix , is_filtered
257378):
379+ """Test to check if __filter_env_variables filters environment variables with a certain prefix correctly.
380+
381+ Args:
382+ monkeypatch (Object): Built-in pytest fixture for monkeypatching
383+ env_var_name (str): Name of the environment variable
384+ filter_prefix (str): Prefix to check for filtering
385+ is_filtered (bool): To check if the env variable with specified prefix is filtered.
386+ """
387+ # Arrange
258388 env_var = env_var_name
259389 monkeypatch .setenv (env_var , "foo" )
260390
391+ # Act
261392 filtered_env_vars : dict = AppState ._AppState__filter_env_variables (
262393 os .environ , filter_prefix
263394 )
395+
396+ # Assert
264397 assert filtered_env_vars .get (env_var ) == is_filtered
0 commit comments