diff --git a/tests/_async/test_gsx.py b/tests/_async/test_gsx.py index afd8e52..a9d7286 100644 --- a/tests/_async/test_gsx.py +++ b/tests/_async/test_gsx.py @@ -30,14 +30,15 @@ async def test_error(): @pytest.mark.asyncio +@pytest_mark.parametrize("status_code,response_data", [(404, {"error": "not found"})]) async def test_get_all_workspaces_validation( - create_a_client, respx_mock, bad_workspaces_connection + create_a_client, respx_mock, response_data, status_code ): respx_mock.get(f"{baseUrl}workspaces").mock( - return_value=httpx.Response(404, json=bad_workspaces_connection) + return_value=httpx.Response(status_code, json=response_data) ) response = await create_a_client.get_all_workspaces() - assert response.code == 404 + assert response.code == status_code @pytest.mark.asyncio @@ -61,14 +62,26 @@ async def test_get_all_workspaces_NetworkError(create_a_client, respx_mock): # Test - get_workspace @pytest_mark.anyio +@pytest.mark.parametrize( + "workspace_name,status_code,response_data,expected_response", + [ + ("sfsf", 404, {"error": "not found"}, "Result not found"), + ], +) async def test_get_workspace_validation( - create_a_client, bad_workspace_connection, respx_mock + create_a_client, + respx_mock, + workspace_name, + status_code, + response_data, + expected_response, ): - respx_mock.get(f"{baseUrl}workspaces/sfsf").mock( - return_value=httpx.Response(404, json=bad_workspace_connection) + respx_mock.get(f"{baseUrl}workspaces/{workspace_name}").mock( + return_value=httpx.Response(status_code, json=response_data) ) - response = await create_a_client.get_workspace("sfsf") - assert response.response == "Result not found" + + response = await create_a_client.get_workspace(workspace_name) + assert response.response == expected_response @pytest_mark.anyio @@ -92,29 +105,54 @@ async def test_get_workspace_ConnectError(create_a_client, respx_mock): # Test - update_workspace @pytest_mark.anyio +@pytest.mark.parametrize( + "workspace_name,status_code,response_data,expected_response", + [ + ("tiger", 404, {"error": "not found"}, "Result not found"), + ], +) async def test_update_workspace_validation( - create_a_client, bad_update_workspace_connection, respx_mock + create_a_client, + respx_mock, + workspace_name, + status_code, + response_data, + expected_response, ): - respx_mock.put(f"{baseUrl}workspaces/tiger.json").mock( - return_value=httpx.Response(404, json=bad_update_workspace_connection) + respx_mock.put(f"{baseUrl}workspaces/{workspace_name}.json").mock( + return_value=httpx.Response(status_code, json=response_data) ) response = await create_a_client.update_workspace( - "tiger", UpdateWorkspaceInfo(isolated=True) + workspace_name, UpdateWorkspaceInfo(isolated=True) ) - assert response.response == "Result not found" + assert response.response == expected_response @pytest_mark.anyio +@pytest_mark.parametrize( + "workspace_name,workspace_info,status_code,response_data", + [ + ( + "tiger", + UpdateWorkspaceInfo(isolated=True), + 200, + {"workspace": {"isolated": True}}, + ) + ], +) async def test_update_workspace_success( - create_a_client, good_update_workspace_connection, respx_mock -): - respx_mock.put(f"{baseUrl}workspaces/tiger.json").mock( - return_value=httpx.Response(200, json=good_update_workspace_connection) - ) - response = await create_a_client.update_workspace( - "tiger", UpdateWorkspaceInfo(isolated=True) - ) - assert response.code == 200 + create_a_client, + respx_mock, + workspace_name, + workspace_info, + status_code, + response_data, +): + respx_mock.put(f"{baseUrl}workspaces/{workspace_name}.json").mock( + return_value=httpx.Response(status_code, json=response_data) + ) + response = await create_a_client.update_workspace(workspace_name, workspace_info) + assert response.code == status_code @pytest_mark.anyio diff --git a/tests/_sync/test_gsx.py b/tests/_sync/test_gsx.py index 9b0fdbd..dbde791 100644 --- a/tests/_sync/test_gsx.py +++ b/tests/_sync/test_gsx.py @@ -26,14 +26,15 @@ def test_error(): # Test - get_all_workspaces +@pytest_mark.parametrize("status_code,response_data", [(404, {"error": "not found"})]) def test_get_all_workspaces_validation( - client: SyncGeoServerX, bad_workspaces_connection, respx_mock + client: SyncGeoServerX, respx_mock, response_data, status_code ): respx_mock.get(f"{baseUrl}workspaces").mock( - return_value=httpx.Response(404, json=bad_workspaces_connection) + return_value=httpx.Response(status_code, json=response_data) ) response = client.get_all_workspaces() - assert response.code == 404 + assert response.code == status_code def test_get_all_workspaces_success( @@ -53,14 +54,26 @@ def test_get_all_workspaces_NetworkError(client: SyncGeoServerX, respx_mock): # Test - get_workspace +@pytest_mark.parametrize( + "workspace_name,status_code,response_data,expected_response", + [ + ("sfsf", 404, {"error": "not found"}, "Result not found"), + ], +) def test_get_workspace_validation( - client: SyncGeoServerX, bad_workspace_connection, respx_mock + client: SyncGeoServerX, + respx_mock, + workspace_name, + status_code, + response_data, + expected_response, ): - respx_mock.get(f"{baseUrl}workspaces/sfsf").mock( - return_value=httpx.Response(404, json=bad_workspace_connection) + respx_mock.get(f"{baseUrl}workspaces/{workspace_name}").mock( + return_value=httpx.Response(status_code, json=response_data) ) - response = client.get_workspace("sfsf") - assert response.response == "Result not found" + + response = client.get_workspace(workspace_name) + assert response.response == expected_response def test_get_workspace_success( @@ -80,24 +93,53 @@ def test_get_workspace_ConnectError(client: SyncGeoServerX, respx_mock): # Test - update_workspace +@pytest_mark.parametrize( + "workspace_name,status_code,response_data,expected_response", + [ + ("tiger", 404, {"error": "not found"}, "Result not found"), + ], +) def test_update_workspace_validation( - client: SyncGeoServerX, bad_update_workspace_connection, respx_mock -): - respx_mock.put(f"{baseUrl}workspaces/tiger.json").mock( - return_value=httpx.Response(404, json=bad_update_workspace_connection) - ) - response = client.update_workspace("tiger", UpdateWorkspaceInfo(isolated=True)) - assert response.response == "Result not found" - - + client: SyncGeoServerX, + respx_mock, + workspace_name, + status_code, + response_data, + expected_response, +): + respx_mock.put(f"{baseUrl}workspaces/{workspace_name}.json").mock( + return_value=httpx.Response(status_code, json=response_data) + ) + response = client.update_workspace( + workspace_name, UpdateWorkspaceInfo(isolated=True) + ) + assert response.response == expected_response + + +@pytest_mark.parametrize( + "workspace_name,workspace_info,status_code,response_data", + [ + ( + "tiger", + UpdateWorkspaceInfo(isolated=True), + 200, + {"workspace": {"isolated": True}}, + ) + ], +) def test_update_workspace_success( - client: SyncGeoServerX, good_update_workspace_connection, respx_mock -): - respx_mock.put(f"{baseUrl}workspaces/tiger.json").mock( - return_value=httpx.Response(200, json=good_update_workspace_connection) - ) - response = client.update_workspace("tiger", UpdateWorkspaceInfo(isolated=True)) - assert response.code == 200 + client: SyncGeoServerX, + respx_mock, + workspace_name, + workspace_info, + status_code, + response_data, +): + respx_mock.put(f"{baseUrl}workspaces/{workspace_name}.json").mock( + return_value=httpx.Response(status_code, json=response_data) + ) + response = client.update_workspace(workspace_name, workspace_info) + assert response.code == status_code def test_update_workspace_ConnectError(client: SyncGeoServerX, respx_mock): diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index 1fa6807..499d70a 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -1,4 +1,5 @@ import httpx +import pytest from typer.testing import CliRunner from geoserverx.cli.cli import app @@ -9,12 +10,13 @@ # Test - get_all_workspaces -def test_get_all_workspaces_validation(bad_workspaces_connection, respx_mock): +@pytest.mark.parametrize("status_code,response_data", [(404, {"error": "not found"})]) +def test_get_all_workspaces_validation(respx_mock, response_data, status_code): respx_mock.get(f"{baseUrl}workspaces").mock( - return_value=httpx.Response(404, json=bad_workspaces_connection) + return_value=httpx.Response(status_code, json=response_data) ) result = runner.invoke(app, ["workspaces"]) - assert "404" in result.stdout + assert str(status_code) in result.stdout def test_get_all_workspaces_success(good_workspaces_connection, respx_mock): @@ -32,12 +34,20 @@ def test_get_all_workspaces_NetworkError(respx_mock): # Test - get_workspace -def test_get_workspace_validation(bad_workspace_connection, respx_mock): - respx_mock.get(f"{baseUrl}workspaces/sfsf").mock( - return_value=httpx.Response(404, json=bad_workspace_connection) +@pytest.mark.parametrize( + "workspace_name,status_code,response_data,expected_response", + [ + ("sfsf", 404, {"error": "not found"}, "Result not found"), + ], +) +def test_get_workspace_validation( + workspace_name, status_code, response_data, expected_response, respx_mock +): + respx_mock.get(f"{baseUrl}workspaces/{workspace_name}").mock( + return_value=httpx.Response(status_code, json=response_data) ) result = runner.invoke(app, ["workspace", "sfsf"]) - assert "Result not found" in result.stdout + assert expected_response in result.stdout def test_get_workspace_success(good_workspace_connection, respx_mock): @@ -55,20 +65,42 @@ def test_get_workspace_ConnectError(respx_mock): # Test - update_workspace -def test_update_workspace_validation(bad_update_workspace_connection, respx_mock): - respx_mock.put(f"{baseUrl}workspaces/tiger.json").mock( - return_value=httpx.Response(404, json=bad_update_workspace_connection) +@pytest.mark.parametrize( + "workspace_name,status_code,response_data,expected_response", + [ + ("tiger", 404, {"error": "not found"}, "Result not found"), + ], +) +def test_update_workspace_validation( + respx_mock, workspace_name, status_code, response_data, expected_response +): + respx_mock.put(f"{baseUrl}workspaces/{workspace_name}.json").mock( + return_value=httpx.Response(status_code, json=response_data) ) - result = runner.invoke(app, ["update-workspace", "tiger", "--isolated"]) - assert "Result not found" in result.stdout + result = runner.invoke(app, ["update-workspace", workspace_name, "--isolated"]) + assert expected_response in result.stdout -def test_update_workspace_success(good_update_workspace_connection, respx_mock): - respx_mock.put(f"{baseUrl}workspaces/tiger.json").mock( - return_value=httpx.Response(200, json=good_update_workspace_connection) +@pytest.mark.parametrize( + "workspace_name,workspace_info,status_code,response_data", + [ + ( + "tiger", + "--isolated", + 200, + {"workspace": {"isolated": True}}, + ) + ], +) +def test_update_workspace_success( + respx_mock, workspace_name, workspace_info, status_code, response_data +): + respx_mock.put(f"{baseUrl}workspaces/{workspace_name}.json").mock( + return_value=httpx.Response(status_code, json=response_data) ) - result = runner.invoke(app, ["update-workspace", "tiger", "--isolated"]) - assert "200" in result.stdout + print(workspace_info) + result = runner.invoke(app, ["update-workspace", workspace_name, workspace_info]) + assert str(status_code) in result.stdout def test_update_workspace_ConnectError(respx_mock): diff --git a/tests/conftest.py b/tests/conftest.py index 46124c6..c113cc1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,12 +24,6 @@ def good_workspaces_connection() -> dict: return item -@pytest.fixture -def bad_workspaces_connection() -> dict: - item = {"code": 502} - return item - - @pytest.fixture def good_workspace_connection() -> dict: item = { @@ -46,30 +40,6 @@ def good_workspace_connection() -> dict: return item -@pytest.fixture -def bad_workspace_connection() -> dict: - item = {"code": 404, "response": "Result not found"} - return item - - -@pytest.fixture -def networkbad_workspace_connection() -> dict: - item = {"code": 503, "response": "Geoserver unavailable"} - return item - - -@pytest.fixture -def good_update_workspace_connection() -> dict: - item = {"workspace": {"isolated": True}} - return item - - -@pytest.fixture -def bad_update_workspace_connection() -> dict: - item = {"workspace": ""} - return item - - @pytest.fixture def invalid_update_workspace_connection() -> dict: item = {"code": 404, "response": "Result not found"} diff --git a/tests/test_models.py b/tests/test_models.py index 145e491..1fe098c 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -27,7 +27,6 @@ ) from geoserverx.models.workspace import ( NewWorkspace, - UpdateWorkspace, WorkspaceInBulk, WorkspaceModel, WorkspacesModel, @@ -288,14 +287,3 @@ def test_RulesResponse_connection(good_all_geofence_rules_connection): def test_RulesResponse_failure(bad_all_geofence_rules_connection): with pytest.raises(ValidationError): RulesResponse(**bad_all_geofence_rules_connection) - - -# Testing UpdateWorkspace -def test_UpdateWorkspace_connection(good_update_workspace_connection): - ds_connection = UpdateWorkspace(**good_update_workspace_connection) - assert ds_connection.workspace.isolated is True - - -def test_UpdateWorkspace_failure(bad_update_workspace_connection): - with pytest.raises(ValidationError): - UpdateWorkspace(**bad_update_workspace_connection)