From f77914be683b33c6d482a68bc915a738cdcc21e5 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 24 Apr 2024 10:26:52 -0600 Subject: [PATCH 1/4] stevencartavia:test-utils --- tests/test_utils.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index b7b4a90..ac1d784 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -5,7 +5,7 @@ from giza.schemas.endpoints import Endpoint, EndpointsList from giza.schemas.workspaces import Workspace -from giza_actions.utils import get_endpoint_uri, get_workspace_uri +from giza_actions.utils import get_endpoint_uri, get_workspace_uri, read_json @patch("giza.client.EndpointsClient.list") @@ -62,3 +62,23 @@ def test_get_workspace_uri_request_exception(mock_get): TestCase.assertRaises( TestCase, requests.exceptions.RequestException, get_workspace_uri ) + + +@mock.patch("builtins.open") +@mock.patch("json.load", mock.MagicMock(side_effect=[{"test": "test"}])) +def test_read_json_successful(*args): + """ + Tests when file is found + """ + response = read_json("path/to/open") + assert response == {"test": "test"} + assert response != None + + +@mock.patch("builtins.open", side_effect=FileNotFoundError) +def test_read_json_file_not_found(mock_open): + """ + Tests when the JSON file is not found. + """ + with TestCase.assertRaises(TestCase, FileNotFoundError): + read_json("/notFound/") From 638bdc6e349ea98e0777f50e63a4295d01e14d30 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 25 Apr 2024 09:34:50 -0600 Subject: [PATCH 2/4] changes with pytest.raises --- tests/test_utils.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index ac1d784..0b0595c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,10 +1,9 @@ from unittest import TestCase, mock from unittest.mock import patch - +import pytest import requests from giza.schemas.endpoints import Endpoint, EndpointsList from giza.schemas.workspaces import Workspace - from giza_actions.utils import get_endpoint_uri, get_workspace_uri, read_json @@ -59,13 +58,12 @@ def test_get_workspace_uri_request_exception(mock_get): """ mock_get.side_effect = requests.exceptions.RequestException - TestCase.assertRaises( - TestCase, requests.exceptions.RequestException, get_workspace_uri - ) + with pytest.raises(requests.exceptions.RequestException): + get_workspace_uri() @mock.patch("builtins.open") -@mock.patch("json.load", mock.MagicMock(side_effect=[{"test": "test"}])) +@mock.patch("json.load", side_effect=[{"test": "test"}]) def test_read_json_successful(*args): """ Tests when file is found @@ -80,5 +78,5 @@ def test_read_json_file_not_found(mock_open): """ Tests when the JSON file is not found. """ - with TestCase.assertRaises(TestCase, FileNotFoundError): + with pytest.raises(FileNotFoundError): read_json("/notFound/") From 54992f0a900b6532b9335dd8123c334ff82bdcd9 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 25 Apr 2024 09:44:12 -0600 Subject: [PATCH 3/4] fix --- tests/test_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0b0595c..98282d7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,11 @@ from unittest import TestCase, mock from unittest.mock import patch + import pytest import requests from giza.schemas.endpoints import Endpoint, EndpointsList from giza.schemas.workspaces import Workspace + from giza_actions.utils import get_endpoint_uri, get_workspace_uri, read_json From 15481c9a3933cb19af2d8926d2f2087e80f74b23 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 26 Apr 2024 08:55:46 -0600 Subject: [PATCH 4/4] fix --- tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 98282d7..e721c15 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,4 @@ -from unittest import TestCase, mock +from unittest import mock from unittest.mock import patch import pytest @@ -72,7 +72,7 @@ def test_read_json_successful(*args): """ response = read_json("path/to/open") assert response == {"test": "test"} - assert response != None + assert response is not None @mock.patch("builtins.open", side_effect=FileNotFoundError)