Skip to content

Commit

Permalink
fixed CLI responses from workspace commands
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnaglodha committed Dec 16, 2024
1 parent b8bc7e6 commit 09b8081
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 29 deletions.
64 changes: 43 additions & 21 deletions src/geoserverx/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

import typer
from rich import print
from rich.console import Console
from rich.table import Table

from geoserverx._sync.gsx import SyncGeoServerX
from geoserverx.models.workspace import UpdateWorkspaceInfo

app = typer.Typer()
console = Console()


@app.callback()
Expand Down Expand Up @@ -40,17 +43,28 @@ def workspaces(
),
password: str = typer.Option("geoserver", help="Geoserver Password"),
username: str = typer.Option("admin", help="Geoserver username"),
output: str = typer.Option("table", help="Output format - 'json' or 'table'"),
):
"""
Get all workspaces in the Geoserver
looks like - gsx workspaces --url <url> --username <username> --password <password>
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.get_all_workspaces().model_dump_json()
result = client.get_all_workspaces()
if "code" in result:
typer.secho(result, fg=typer.colors.RED)
else:
print(result)
if output == "json":
print(json.dumps(result.model_dump(), indent=2))
else:
try:
table = Table("Name", "Link")
for workspace in result.workspaces.workspace:
table.add_row(workspace.name, workspace.href)
console.print(table)
except AttributeError:
typer.secho(result.response, fg=typer.colors.RED)
else:
typer.echo("Async support will be shortly")

Expand All @@ -65,17 +79,33 @@ def workspace(
),
password: str = typer.Option("geoserver", help="Geoserver Password"),
username: str = typer.Option("admin", help="Geoserver username"),
output: str = typer.Option("table", help="Output format - 'json' or 'table'"),
):
"""
Get workspace in the Geoserver
looks like - gsx workspace <workspacename> --url <url> --username <username> --password <password>
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.get_workspace(workspace).model_dump_json()
result = client.get_workspace(workspace)
if "code" in result:
typer.secho(result, fg=typer.colors.RED)
else:
print(result)
if output == "json":
print(json.dumps(result.model_dump(), indent=2))
else:
try:
table = Table("Column", "Value")
table.add_row("name", result.workspace.name)
table.add_row("isolated", str(result.workspace.isolated))
table.add_row("dateCreated", result.workspace.dateCreated)
table.add_row("dataStores", result.workspace.dataStores)
table.add_row("coverageStores", result.workspace.coverageStores)
table.add_row("wmsStores", result.workspace.wmsStores)
table.add_row("wmtsStores", result.workspace.wmtsStores)
console.print(table)
except AttributeError:
typer.secho(result.response, fg=typer.colors.RED)
else:
typer.echo("Async support will be shortly")

Expand All @@ -94,14 +124,12 @@ def delete_workspace(
):
"""
Delete workspace in the Geoserver
looks like - gsx delete-workspace <workspacename> --recurse/--no-recurse --url <url> --username <username> --password <password>
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.delete_workspace(workspace, recurse).model_dump_json()
if "code" in result:
typer.secho(result, fg=typer.colors.RED)
else:
print(result)
result = client.delete_workspace(workspace, recurse)
print(result.response)
else:
typer.echo("Async support will be shortly")

Expand All @@ -121,15 +149,12 @@ def create_workspace(
):
"""
Add workspace in the Geoserver
looks like - gsx create-workspace --workspace <workspacename> --default/--no-default --isolated/--no-isolated --username <username> --password <password>
looks like - gsx create-workspace <workspacename> --default/--no-default --isolated/--no-isolated --url <url> --username <username> --password <password>
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.create_workspace(workspace, default, isolated).model_dump_json()
if json.loads(result)["code"] == 201:
typer.secho(result, fg=typer.colors.GREEN)
else:
typer.secho(result, fg=typer.colors.RED)
result = client.create_workspace(workspace, default, isolated)
print(result.response)

else:
typer.echo("Async support will be shortly")
Expand All @@ -150,18 +175,15 @@ def update_workspace(
):
"""
Update existing workspace in the Geoserver
looks like - gsx create-workspace --workspace <workspacename> --default/--no-default --isolated/--no-isolated --username <username> --password <password>
looks like - gsx update-workspace <workspacename> --new-name <new-workspacename> --isolated/--no-isolated --username <username> --password <password>
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.update_workspace(
current_name,
UpdateWorkspaceInfo(name=new_name, isolated=isolated),
).model_dump()
if result["code"] == 200:
typer.secho(result, fg=typer.colors.GREEN)
else:
typer.secho(result, fg=typer.colors.RED)
)
print(result.response)

else:
typer.echo("Async support will be shortly")
Expand Down
44 changes: 36 additions & 8 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict

import httpx
import pytest
from typer.testing import CliRunner
Expand All @@ -7,14 +9,30 @@
runner = CliRunner()

baseUrl = "http://127.0.0.1:8080/geoserver/rest/"
# Test data
WORKSPACE_TEST_CASES = [
{"name": "aa", "href": "http://127.0.0.1:8080/geoserver/rest/workspaces/aa.json"},
{
"name": "aaba",
"href": "http://127.0.0.1:8080/geoserver/rest/workspaces/aaba.json",
},
]


@pytest.fixture
def workspace_response() -> Dict[str, Any]:
return {"workspaces": {"workspace": WORKSPACE_TEST_CASES}}

def test_get_all_workspaces_success(good_workspaces_connection, respx_mock):

def test_get_all_workspaces_success(workspace_response, respx_mock):
"""Test getting all workspaces"""
respx_mock.get(f"{baseUrl}workspaces").mock(
return_value=httpx.Response(200, json=good_workspaces_connection)
return_value=httpx.Response(200, json=workspace_response)
)
result = runner.invoke(app, ["workspaces"])
assert "pydad" in result.stdout
result = runner.invoke(app, ["workspaces", "--output", "json"])
assert result.exit_code == 0
for workspace in WORKSPACE_TEST_CASES:
assert workspace["name"] in result.output


def test_get_all_workspaces_NetworkError(respx_mock):
Expand All @@ -27,7 +45,12 @@ def test_get_all_workspaces_NetworkError(respx_mock):
@pytest.mark.parametrize(
"workspace_name,status_code,response_data,expected_response",
[
("sfsf", 404, {"error": "not found"}, "Result not found"),
(
"sfsf",
404,
{"code": 404, "response": "Result not found"},
"Result not found",
),
],
)
def test_get_workspace_validation(
Expand Down Expand Up @@ -58,7 +81,12 @@ def test_get_workspace_ConnectError(respx_mock):
@pytest.mark.parametrize(
"workspace_name,status_code,response_data,expected_response",
[
("tiger", 404, {"error": "not found"}, "Result not found"),
(
"tiger",
404,
{"code": 404, "response": "Result not found"},
"Result not found",
),
],
)
def test_update_workspace_validation(
Expand All @@ -78,7 +106,7 @@ def test_update_workspace_validation(
"tiger",
"--isolated",
200,
{"workspace": {"isolated": True}},
"Executed successfully",
)
],
)
Expand All @@ -90,7 +118,7 @@ def test_update_workspace_success(
)
print(workspace_info)
result = runner.invoke(app, ["update-workspace", workspace_name, workspace_info])
assert str(status_code) in result.stdout
assert response_data in result.stdout


def test_update_workspace_ConnectError(respx_mock):
Expand Down

0 comments on commit 09b8081

Please sign in to comment.