Skip to content

Commit

Permalink
fixed few workspace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnaglodha committed Nov 29, 2024
1 parent 71e4697 commit 05bc534
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 105 deletions.
82 changes: 60 additions & 22 deletions tests/_async/test_gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
90 changes: 66 additions & 24 deletions tests/_sync/test_gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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):
Expand Down
66 changes: 49 additions & 17 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import httpx
import pytest
from typer.testing import CliRunner

from geoserverx.cli.cli import app
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
30 changes: 0 additions & 30 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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"}
Expand Down
Loading

0 comments on commit 05bc534

Please sign in to comment.