From 4f651e0392a4154668fc220d3439d63f9c5942a3 Mon Sep 17 00:00:00 2001 From: aadityasinha-dotcom Date: Mon, 24 Jun 2024 22:03:19 +0530 Subject: [PATCH 1/3] updated tests to use context managers Signed-off-by: aadityasinha-dotcom --- tests/integration/test_zos_console.py | 3 ++- tests/integration/test_zos_files.py | 3 ++- tests/integration/test_zos_jobs.py | 3 ++- tests/integration/test_zos_tso.py | 3 ++- tests/integration/test_zosmf.py | 3 ++- tests/unit/core/test_sdk_api.py | 11 ++++++++++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/integration/test_zos_console.py b/tests/integration/test_zos_console.py index 33e8d804..c30cb0c7 100644 --- a/tests/integration/test_zos_console.py +++ b/tests/integration/test_zos_console.py @@ -11,7 +11,8 @@ class TestConsoleIntegration(unittest.TestCase): def setUp(self): """Setup fixtures for Console class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") - self.console = Console(test_profile) + with Console(test_profile) as console: + self.console = console def test_console_command_time_should_return_time(self): """Test the execution of the time command should return the current time""" diff --git a/tests/integration/test_zos_files.py b/tests/integration/test_zos_files.py index 549cae8d..7a569c09 100644 --- a/tests/integration/test_zos_files.py +++ b/tests/integration/test_zos_files.py @@ -22,7 +22,8 @@ def setUp(self): self.user_name = test_profile["user"] with open(FILES_FIXTURES_PATH, "r") as fixtures_json: self.files_fixtures = json.load(fixtures_json) - self.files = Files(test_profile) + with Files(test_profile) as files: + self.files = files self.test_member_jcl = f'{self.files_fixtures["TEST_PDS"]}({self.files_fixtures["TEST_MEMBER"]})' self.test_member_generic = f'{self.files_fixtures["TEST_PDS"]}(TEST)' self.test_ds_upload = f'{self.files_fixtures["TEST_PDS"]}({self.files_fixtures["TEST_MEMBER_NEW"]})' diff --git a/tests/integration/test_zos_jobs.py b/tests/integration/test_zos_jobs.py index be32e324..5276eecc 100644 --- a/tests/integration/test_zos_jobs.py +++ b/tests/integration/test_zos_jobs.py @@ -19,7 +19,8 @@ def setUp(self): test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") with open(JOBS_FIXTURES_JSON_JSON_PATH, "r") as fixtures_json: self.jobs_fixtures_json = json.load(fixtures_json) - self.jobs = Jobs(test_profile) + with Jobs(test_profile) as jobs: + self.jobs = jobs def test_get_job_status_should_return_the_status_of_a_job(self): """Executing the get_job_status method should return the status of a given job""" diff --git a/tests/integration/test_zos_tso.py b/tests/integration/test_zos_tso.py index 5eb2bbef..a8455daa 100644 --- a/tests/integration/test_zos_tso.py +++ b/tests/integration/test_zos_tso.py @@ -12,7 +12,8 @@ class TestTsoIntegration(unittest.TestCase): def setUp(self): """Setup fixtures for Tso class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") - self.tso = Tso(test_profile, {"account": "IZUACCT"}) + with Tso(test_profile, {"account": "IZUACCT"}) as tso: + self.tso = tso def test_issue_command_should_return_valid_response(self): """Executing the issue_command method should return a valid response from TSO""" diff --git a/tests/integration/test_zosmf.py b/tests/integration/test_zosmf.py index 87b93c1b..6dfc1117 100644 --- a/tests/integration/test_zosmf.py +++ b/tests/integration/test_zosmf.py @@ -11,7 +11,8 @@ class TestZosmfIntegration(unittest.TestCase): def setUp(self): """Setup fixtures for Zosmf class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") - self.zosmf = Zosmf(test_profile) + with Zosmf(test_profile) as zosmf: + self.zosmf = zosmf def test_get_info_should_return_valid_response(self): """Executing the get_info method should return a valid response.""" diff --git a/tests/unit/core/test_sdk_api.py b/tests/unit/core/test_sdk_api.py index eda522ef..741cd77c 100644 --- a/tests/unit/core/test_sdk_api.py +++ b/tests/unit/core/test_sdk_api.py @@ -29,6 +29,15 @@ def test_object_should_be_instance_of_class(self): """Created object should be instance of SdkApi class.""" sdk_api = SdkApi(self.basic_props, self.default_url) self.assertIsInstance(sdk_api, SdkApi) + + @mock.patch('requests.Session.close') + def test_context_manager_closes_session(self, mock_close_request): + + mock_close_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200) + with SdkApi(self.basic_props, self.default_url) as api: + pass + + mock_close_request.assert_called_once() @mock.patch("logging.Logger.error") def test_session_no_host_logger(self, mock_logger_error: mock.MagicMock): @@ -90,4 +99,4 @@ def test_encode_uri_component(self): actual_none = sdk_api._encode_uri_component(None) expected_none = None - self.assertEqual(actual_none, expected_none) \ No newline at end of file + self.assertEqual(actual_none, expected_none) From 3b1b9c05049c11625577855da44b66839824b1cf Mon Sep 17 00:00:00 2001 From: aadityasinha-dotcom Date: Fri, 28 Jun 2024 19:03:40 +0530 Subject: [PATCH 2/3] changes add cleanup for requests.Session Signed-off-by: aadityasinha-dotcom --- tests/integration/test_zos_console.py | 4 ++-- tests/integration/test_zos_files.py | 4 ++-- tests/integration/test_zos_jobs.py | 4 ++-- tests/integration/test_zos_tso.py | 4 ++-- tests/integration/test_zosmf.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/integration/test_zos_console.py b/tests/integration/test_zos_console.py index c30cb0c7..e41a3677 100644 --- a/tests/integration/test_zos_console.py +++ b/tests/integration/test_zos_console.py @@ -11,8 +11,8 @@ class TestConsoleIntegration(unittest.TestCase): def setUp(self): """Setup fixtures for Console class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") - with Console(test_profile) as console: - self.console = console + self.console = Console(test_profile) + self.addCleanup(lambda: self.console.__exit(None, None, None)) def test_console_command_time_should_return_time(self): """Test the execution of the time command should return the current time""" diff --git a/tests/integration/test_zos_files.py b/tests/integration/test_zos_files.py index 7a569c09..003db8ce 100644 --- a/tests/integration/test_zos_files.py +++ b/tests/integration/test_zos_files.py @@ -22,8 +22,8 @@ def setUp(self): self.user_name = test_profile["user"] with open(FILES_FIXTURES_PATH, "r") as fixtures_json: self.files_fixtures = json.load(fixtures_json) - with Files(test_profile) as files: - self.files = files + self.files = Files(test_profile) + self.addCleanup(lambda: self.files.__exit__(None, None, None)) self.test_member_jcl = f'{self.files_fixtures["TEST_PDS"]}({self.files_fixtures["TEST_MEMBER"]})' self.test_member_generic = f'{self.files_fixtures["TEST_PDS"]}(TEST)' self.test_ds_upload = f'{self.files_fixtures["TEST_PDS"]}({self.files_fixtures["TEST_MEMBER_NEW"]})' diff --git a/tests/integration/test_zos_jobs.py b/tests/integration/test_zos_jobs.py index 5276eecc..1b4f9ea4 100644 --- a/tests/integration/test_zos_jobs.py +++ b/tests/integration/test_zos_jobs.py @@ -19,8 +19,8 @@ def setUp(self): test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") with open(JOBS_FIXTURES_JSON_JSON_PATH, "r") as fixtures_json: self.jobs_fixtures_json = json.load(fixtures_json) - with Jobs(test_profile) as jobs: - self.jobs = jobs + self.jobs = Jobs(test_profile) + self.addCleanup(lambda: self.jobs.__exit__(None, None, None)) def test_get_job_status_should_return_the_status_of_a_job(self): """Executing the get_job_status method should return the status of a given job""" diff --git a/tests/integration/test_zos_tso.py b/tests/integration/test_zos_tso.py index a8455daa..81191f49 100644 --- a/tests/integration/test_zos_tso.py +++ b/tests/integration/test_zos_tso.py @@ -12,8 +12,8 @@ class TestTsoIntegration(unittest.TestCase): def setUp(self): """Setup fixtures for Tso class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") - with Tso(test_profile, {"account": "IZUACCT"}) as tso: - self.tso = tso + self.tso = Tso(test_profile, {"account": "IZUACCT"}) + self.addCleanup(lambda: self.tso.__exit__(None, None, None)) def test_issue_command_should_return_valid_response(self): """Executing the issue_command method should return a valid response from TSO""" diff --git a/tests/integration/test_zosmf.py b/tests/integration/test_zosmf.py index 6dfc1117..70f0e785 100644 --- a/tests/integration/test_zosmf.py +++ b/tests/integration/test_zosmf.py @@ -11,8 +11,8 @@ class TestZosmfIntegration(unittest.TestCase): def setUp(self): """Setup fixtures for Zosmf class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") - with Zosmf(test_profile) as zosmf: - self.zosmf = zosmf + self.zosmf = Zosmf(test_profile) + self.addCleanup(lambda: self.zosmf.__exit__(None, None, None)) def test_get_info_should_return_valid_response(self): """Executing the get_info method should return a valid response.""" From c4b538f1e305a34873268a7cff3df6fa131e0db9 Mon Sep 17 00:00:00 2001 From: Aaditya Sinha <75474786+aadityasinha-dotcom@users.noreply.github.com> Date: Mon, 1 Jul 2024 23:09:54 +0530 Subject: [PATCH 3/3] Update tests/integration/test_zos_console.py Co-authored-by: Fernando Rijo Cedeno <37381190+zFernand0@users.noreply.github.com> Signed-off-by: Aaditya Sinha <75474786+aadityasinha-dotcom@users.noreply.github.com> --- tests/integration/test_zos_console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_zos_console.py b/tests/integration/test_zos_console.py index e41a3677..64928006 100644 --- a/tests/integration/test_zos_console.py +++ b/tests/integration/test_zos_console.py @@ -12,7 +12,7 @@ def setUp(self): """Setup fixtures for Console class.""" test_profile = ProfileManager(show_warnings=False).load(profile_type="zosmf") self.console = Console(test_profile) - self.addCleanup(lambda: self.console.__exit(None, None, None)) + self.addCleanup(lambda: self.console.__exit__(None, None, None)) def test_console_command_time_should_return_time(self): """Test the execution of the time command should return the current time"""