Skip to content

Commit

Permalink
Create style definition before posting style
Browse files Browse the repository at this point in the history
The style definition must exist before posting the style content (SLD or zip) otherwise GeoServer throws a 500
  • Loading branch information
vuilleumierc committed Nov 19, 2024
1 parent 7da9f56 commit 8500c27
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions geoservercloud/geoservercloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,13 @@ def create_style_from_file(
) -> tuple[str, int]:
"""Create a style (SLD) from a file, or update it if it already exists.
Supported file extensions are .sld and .zip."""
file_base_name = Path(file).name
file_ext = Path(file).suffix
content, code = self.create_style_definition(
style_name, f"{file_base_name}.sld", workspace_name
)
if code >= 400:
return content, code
if file_ext == ".sld":
file_format = "sld"
elif file_ext == ".zip":
Expand Down
34 changes: 33 additions & 1 deletion tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ def test_create_style_definition(mocker, geoserver: GeoServerCloud) -> None:
def test_create_style_from_file(geoserver: GeoServerCloud) -> None:
file_path = (Path(__file__).parent / "resources/style.sld").resolve()
with responses.RequestsMock() as rsps:
rsps.get(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}.sld",
status=201,
Expand All @@ -178,6 +186,14 @@ def test_create_style_from_file(geoserver: GeoServerCloud) -> None:
def test_update_style_from_file(geoserver: GeoServerCloud) -> None:
file_path = (Path(__file__).parent / "resources/style.sld").resolve()
with responses.RequestsMock() as rsps:
rsps.get(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}.sld",
status=200,
Expand All @@ -202,6 +218,14 @@ def test_update_style_from_file(geoserver: GeoServerCloud) -> None:
def test_create_style_from_file_zip(geoserver: GeoServerCloud) -> None:
file_path = (Path(__file__).parent / "resources/style.zip").resolve()
with responses.RequestsMock() as rsps:
rsps.get(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=201,
Expand All @@ -222,7 +246,15 @@ def test_create_style_from_file_zip(geoserver: GeoServerCloud) -> None:


def test_create_style_from_file_unsupported_format(geoserver: GeoServerCloud) -> None:
with responses.RequestsMock():
with responses.RequestsMock() as rsps:
rsps.get(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
rsps.put(
url=f"{geoserver.url}/rest/styles/{STYLE}",
status=200,
)
with pytest.raises(ValueError) as excinfo:
geoserver.create_style_from_file(
style_name=STYLE,
Expand Down

0 comments on commit 8500c27

Please sign in to comment.