-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cae2b1
commit fc0f984
Showing
6 changed files
with
312 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Any | ||
|
||
from geoservercloud.models.common import EntityModel | ||
|
||
|
||
class Resource(EntityModel): | ||
def __init__( | ||
self, | ||
name: str, | ||
href: str, | ||
type: str, | ||
) -> None: | ||
self.name: str = name | ||
self.href: str = href | ||
self.type: str = type | ||
|
||
def is_image(self) -> bool: | ||
return self.type.startswith("image") | ||
|
||
|
||
class ResourceDirectory(EntityModel): | ||
def __init__(self, name: str, parent: Resource, children: list[Resource]) -> None: | ||
self.name: str = name | ||
self.parent: Resource = parent | ||
self.children: list[Resource] = children | ||
|
||
@classmethod | ||
def from_get_response_payload(cls, payload: dict[str, Any]) -> "ResourceDirectory": | ||
resource_directory = payload["ResourceDirectory"] | ||
parent = resource_directory["parent"] | ||
parent = Resource( | ||
name=parent["path"], | ||
href=parent["link"]["href"], | ||
type=parent["link"]["type"], | ||
) | ||
children = [ | ||
Resource(child["name"], child["link"]["href"], child["link"]["type"]) | ||
for child in resource_directory.get("children", {}).get("child", []) | ||
] | ||
return cls( | ||
name=resource_directory["name"], | ||
parent=parent, | ||
children=children, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from geoservercloud.models.resourcedirectory import Resource, ResourceDirectory | ||
|
||
|
||
def test_from_get_response_payload(): | ||
payload = { | ||
"ResourceDirectory": { | ||
"name": "test_resource_directory", | ||
"parent": { | ||
"path": "workspace/parent", | ||
"name": "parent", | ||
"link": {"href": "http://example.com", "type": "application/json"}, | ||
}, | ||
"children": { | ||
"child": [ | ||
{ | ||
"name": "child1.svg", | ||
"link": { | ||
"href": "http://example.com/child1", | ||
"type": "image/svg+xml", | ||
}, | ||
}, | ||
{ | ||
"name": "child2.xml", | ||
"link": { | ||
"href": "http://example.com/child2", | ||
"type": "application/xml", | ||
}, | ||
}, | ||
] | ||
}, | ||
} | ||
} | ||
|
||
resource_directory = ResourceDirectory.from_get_response_payload(payload) | ||
|
||
assert resource_directory.name == "test_resource_directory" | ||
assert resource_directory.parent.name == "workspace/parent" | ||
assert resource_directory.parent.href == "http://example.com" | ||
assert resource_directory.parent.type == "application/json" | ||
assert len(resource_directory.children) == 2 | ||
assert resource_directory.children[0].name == "child1.svg" | ||
assert resource_directory.children[0].href == "http://example.com/child1" | ||
assert resource_directory.children[0].type == "image/svg+xml" | ||
assert resource_directory.children[1].name == "child2.xml" | ||
assert resource_directory.children[1].href == "http://example.com/child2" | ||
assert resource_directory.children[1].type == "application/xml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import pytest | ||
import responses | ||
|
||
from geoservercloud.models.resourcedirectory import ResourceDirectory | ||
from geoservercloud.services.restservice import RestService | ||
|
||
|
||
@pytest.fixture | ||
def rest_service(): | ||
yield RestService("http://geoserver", auth=("test", "test")) | ||
|
||
|
||
@pytest.fixture | ||
def resource_dir_get_response(): | ||
yield { | ||
"ResourceDirectory": { | ||
"name": "test_resource_directory", | ||
"parent": { | ||
"path": "workspace/parent", | ||
"name": "parent", | ||
"link": {"href": "http://example.com", "type": "application/json"}, | ||
}, | ||
"children": { | ||
"child": [ | ||
{ | ||
"name": "child1.svg", | ||
"link": { | ||
"href": "http://example.com/child1", | ||
"type": "image/svg+xml", | ||
}, | ||
}, | ||
{ | ||
"name": "child2.xml", | ||
"link": { | ||
"href": "http://example.com/child2", | ||
"type": "application/xml", | ||
}, | ||
}, | ||
] | ||
}, | ||
} | ||
} | ||
|
||
|
||
def test_get_workspace_style_resource_directory( | ||
rest_service: RestService, resource_dir_get_response: dict | ||
): | ||
with responses.RequestsMock() as rsps: | ||
rsps.get( | ||
url=f"{rest_service.url}/rest/resource/workspaces/test/styles", | ||
status=200, | ||
json=resource_dir_get_response, | ||
match=[responses.matchers.header_matcher({"Accept": "application/json"})], | ||
) | ||
resource_dir, code = rest_service.get_resource_directory( | ||
path="styles", workspace_name="test" | ||
) | ||
assert isinstance(resource_dir, ResourceDirectory) | ||
assert len(resource_dir.children) == 2 | ||
|
||
|
||
def test_get_global_style_resource_directory( | ||
rest_service: RestService, resource_dir_get_response: dict | ||
): | ||
with responses.RequestsMock() as rsps: | ||
rsps.get( | ||
url=f"{rest_service.url}/rest/resource/styles", | ||
status=200, | ||
json=resource_dir_get_response, | ||
match=[responses.matchers.header_matcher({"Accept": "application/json"})], | ||
) | ||
resource_dir, code = rest_service.get_resource_directory(path="styles") | ||
assert isinstance(resource_dir, ResourceDirectory) | ||
assert len(resource_dir.children) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters