diff --git a/geoservercloud/geoservercloud.py b/geoservercloud/geoservercloud.py index 11e99d5..fe3bf11 100644 --- a/geoservercloud/geoservercloud.py +++ b/geoservercloud/geoservercloud.py @@ -12,6 +12,8 @@ from geoservercloud import utils from geoservercloud.models import ( DataStores, + FeatureType, + FeatureTypes, KeyDollarListDict, PostGisDataStore, Style, @@ -304,6 +306,30 @@ def create_wmts_store( self.rest_endpoints.wmtsstore(workspace_name, name), json=payload ) + def get_feature_types( + self, workspace_name: str, datastore_name: str + ) -> dict[str, Any]: + """ + Get all feature types for a given workspace and datastore + """ + featuretypes = FeatureTypes.from_response( + self.get_request( + self.rest_endpoints.featuretypes(workspace_name, datastore_name) + ) + ) + return featuretypes + + def get_feature_type( + self, workspace_name: str, datastore_name: str, feature_type_name: str + ) -> dict[str, Any]: + return FeatureType.from_response( + self.get_request( + self.rest_endpoints.featuretype( + workspace_name, datastore_name, feature_type_name + ) + ) + ) + def create_feature_type( self, layer: str, diff --git a/geoservercloud/models/__init__.py b/geoservercloud/models/__init__.py index 47b3230..c28cea9 100644 --- a/geoservercloud/models/__init__.py +++ b/geoservercloud/models/__init__.py @@ -1,6 +1,8 @@ -from .common import KeyDollarListDict -from .dataStore import PostGisDataStore -from .dataStores import DataStores +from .common import I18N, KeyDollarListDict +from .datastore import PostGisDataStore +from .datastores import DataStores +from .featuretype import FeatureType +from .featuretypes import FeatureTypes from .style import Style from .styles import Styles from .workspace import Workspace @@ -9,6 +11,9 @@ __all__ = [ "DataStores", "KeyDollarListDict", + "FeatureType", + "FeatureTypes", + "I18N", "PostGisDataStore", "Style", "Styles", diff --git a/geoservercloud/models/common.py b/geoservercloud/models/common.py index bdcae12..f86f8bf 100644 --- a/geoservercloud/models/common.py +++ b/geoservercloud/models/common.py @@ -1,11 +1,18 @@ import json import logging +from typing import Any log = logging.getLogger() class KeyDollarListDict(dict): - def __init__(self, input_list=None, input_dict=None, *args, **kwargs): + def __init__( + self, + input_list: list | None = None, + input_dict: dict | None = None, + *args, + **kwargs + ): super().__init__(*args, **kwargs) self.key_prefix = "@key" self.value_prefix = "$" @@ -15,7 +22,7 @@ def __init__(self, input_list=None, input_dict=None, *args, **kwargs): self.update(input_dict) log.debug(self) - def deserialize(self, input_list): + def deserialize(self, input_list: list): for item in input_list: key = item[self.key_prefix] if self.value_prefix in item: @@ -39,3 +46,42 @@ def __str__(self): def update(self, other: dict): # type: ignore for key, value in other.items(): super().__setitem__(key, value) + + +class I18N: + """ + Class to handle internationalization of strings + items like title, abstract, etc. that can be internationalized + become a dictionary with the key being the language code + and their key in the payload changes to internationalizedTitle, internationalizedAbstract, etc. + """ + + def __init__(self, keys: tuple[str, Any], value: str | dict) -> None: + self._str_key = keys[0] + self._i18n_key = keys[1] + self._value = value + if isinstance(value, str): + self._payload = (self.str_key, self._value) + elif isinstance(value, dict): + self._payload = (self._i18n_key, self._value) # type: ignore + else: + raise ValueError("Invalid value type") + + @property + def str_key(self): + return self._str_key + + @property + def i18n_key(self): + return self._i18n_key + + @property + def value(self): + return self._value + + @property + def payload_tuple(self): + return self._payload + + def __repr__(self): + return json.dumps({self._payload[0]: self._payload[1]}, indent=4) diff --git a/geoservercloud/models/dataStore.py b/geoservercloud/models/datastore.py similarity index 96% rename from geoservercloud/models/dataStore.py rename to geoservercloud/models/datastore.py index f2cd9f4..4d22a09 100644 --- a/geoservercloud/models/dataStore.py +++ b/geoservercloud/models/datastore.py @@ -1,6 +1,8 @@ import json import logging +from requests.models import Response + from . import KeyDollarListDict log = logging.getLogger() @@ -49,7 +51,7 @@ def put_payload(self): return payload @classmethod - def from_response(cls, response): + def from_response(cls, response: Response): if response.status_code == 404: return None json_data = response.json() diff --git a/geoservercloud/models/dataStores.py b/geoservercloud/models/datastores.py similarity index 90% rename from geoservercloud/models/dataStores.py rename to geoservercloud/models/datastores.py index d0aa3f4..19b476d 100644 --- a/geoservercloud/models/dataStores.py +++ b/geoservercloud/models/datastores.py @@ -1,5 +1,7 @@ import logging +from requests.models import Response + log = logging.getLogger() @@ -14,7 +16,7 @@ def datastores(self): return self._datastores @classmethod - def from_response(cls, response): + def from_response(cls, response: Response): json_data = response.json() datastores = [] workspace_name = ( diff --git a/geoservercloud/models/featuretype.py b/geoservercloud/models/featuretype.py new file mode 100644 index 0000000..3ca163a --- /dev/null +++ b/geoservercloud/models/featuretype.py @@ -0,0 +1,134 @@ +import json + +from requests.models import Response + +from geoservercloud.models import I18N + + +# TODO: import more default values from Templates +class FeatureType: + def __init__( + self, + namespace_name: str, + name: str, + native_name: str, + srs: str = "EPSG:4326", + title: str | dict = "New Layer", + abstract: str | dict = "New Layer", + keywords={}, + metadata_url=None, + metadata_type="TC211", + metadata_format="text/xml", + attributes: dict | None = None, + ) -> None: + self._namespace_name = namespace_name + self._name = name + self._title = I18N(("title", "internationalTitle"), title) + self._abstract = I18N(("abstract", "internationalAbstract"), abstract) + self._native_name = native_name + self._srs = srs + self._keywords = keywords + self._attributes = attributes + self.create_metadata_link(metadata_url, metadata_type, metadata_format) + + @property + def namespace_name(self): + return self._namespace_name + + @property + def name(self): + return self._name + + @property + def title(self): + return self._title + + @property + def abstract(self): + return self._abstract + + @property + def native_name(self): + return self._native_name + + @property + def srs(self): + return self._srs + + @property + def keywords(self): + return self._keywords + + @property + def metadataLink(self): + return self._metadataLink + + @property + def attributes(self): + return self._attributes + + def post_payload(self): + payload = { + "featureType": { + "name": self.name, + "nativeName": self.native_name, + self._title.payload_tuple[0]: self._title.payload_tuple[1], + self._abstract.payload_tuple[0]: self._abstract.payload_tuple[1], + "srs": self.srs, + "keywords": self.keywords, + } + } + if self.metadataLink != {}: + payload["featureType"]["metadataLinks"] = self.metadataLink + if self.attributes: + payload["featureType"]["attributes"] = self.attributes + return payload + + def create_metadata_link( + self, metadata_url=None, metadata_type="TC211", metadata_format="text/xml" + ): + self._metadataLink = {} + if metadata_url: + self._metadataLink["metadataLink"] = { + "type": metadata_format, + "metadataType": metadata_type, + "content": metadata_url, + } + + @classmethod + def from_response(cls, response: Response): + json_data = response.json() + try: + abstract = json_data["featureType"]["abstract"] + except KeyError: + abstract = json_data["featureType"]["internationalAbstract"] + try: + title = json_data["featureType"]["title"] + except KeyError: + title = json_data["featureType"]["internationalTitle"] + + return cls( + namespace_name=json_data["featureType"]["namespace"]["name"], + name=json_data["featureType"]["name"], + native_name=json_data["featureType"]["nativeName"], + title=title, + abstract=abstract, + srs=json_data["featureType"]["srs"], + keywords=json_data["featureType"]["keywords"], + attributes=json_data["featureType"].get("attributes", None), + metadata_url=json_data["featureType"] + .get("metadataLinks", {}) + .get("metadataLink", {}) + .get("content"), + metadata_type=json_data["featureType"] + .get("metadataLinks", {}) + .get("metadataLink", {}) + .get("metadataType"), + metadata_format=json_data["featureType"] + .get("metadataLinks", {}) + .get("metadataLink", {}) + .get("type"), + ) + + def __repr__(self): + return json.dumps(self.post_payload(), indent=4) diff --git a/geoservercloud/models/featuretypes.py b/geoservercloud/models/featuretypes.py new file mode 100644 index 0000000..85eef01 --- /dev/null +++ b/geoservercloud/models/featuretypes.py @@ -0,0 +1,24 @@ +import json + +from requests.models import Response + + +class FeatureTypes: + + def __init__(self, featuretypes: list = []) -> None: + self._featuretypes = featuretypes + + @property + def featuretypes(self): + return self._featuretypes + + @classmethod + def from_response(cls, response: Response): + featuretypes = [] + json_data = response.json() + for featuretype in json_data.get("featureTypes", {}).get("featureType", []): + featuretypes.append(featuretype["name"]) + return cls(featuretypes) + + def __repr__(self): + return json.dumps(self._featuretypes, indent=4) diff --git a/geoservercloud/models/style.py b/geoservercloud/models/style.py index ce3ebb6..ebc9734 100644 --- a/geoservercloud/models/style.py +++ b/geoservercloud/models/style.py @@ -1,6 +1,7 @@ import json import xmltodict +from requests.models import Response class Style: @@ -16,8 +17,8 @@ def __init__( date_modified: str | None = None, legend_url: str | None = None, legend_format: str | None = None, - legend_width: str | None = None, - legend_height: str | None = None, + legend_width: int | None = None, + legend_height: int | None = None, ) -> None: self._workspace = workspace self._name = name @@ -26,7 +27,9 @@ def __init__( self._filename = filename self._date_created = date_created self._date_modified = date_modified - self.create_legend(legend_url, legend_format, legend_width, legend_height) + self._legend = self.create_legend( + legend_url, legend_format, legend_width, legend_height + ) # create one property for each attribute @property @@ -61,19 +64,26 @@ def date_modified(self): def legend(self): return self._legend - def create_legend(self, url, image_format, width, height): + def create_legend( + self, + url: str | None, + image_format: str | None, + width: int | None, + height: int | None, + ): if any([url, image_format, width, height]): - self._legend = {} + legend: dict = {} if url: - self.legend["onlineResource"] = url + legend["onlineResource"] = url if image_format: - self.legend["format"] = image_format + legend["format"] = image_format if width: - self.legend["width"] = width + legend["width"] = width if height: - self.legend["height"] = height + legend["height"] = height else: - self._legend = None + legend = None # type: ignore + return legend def put_payload(self): payload = { @@ -92,7 +102,7 @@ def post_payload(self): return self.put_payload() @classmethod - def from_response(cls, response): + def from_response(cls, response: Response): json_data = response.json() style_data = json_data.get("style", {}) return cls( diff --git a/geoservercloud/models/styles.py b/geoservercloud/models/styles.py index 9112946..f8589e6 100644 --- a/geoservercloud/models/styles.py +++ b/geoservercloud/models/styles.py @@ -1,3 +1,6 @@ +from requests.models import Response + + class Styles: def __init__(self, styles: list[str], workspace: str | None = None) -> None: @@ -13,7 +16,7 @@ def styles(self): return self._styles @classmethod - def from_response(cls, response): + def from_response(cls, response: Response): json_data = response.json() styles = [] try: diff --git a/geoservercloud/models/workspace.py b/geoservercloud/models/workspace.py index 7af82bd..eff4789 100644 --- a/geoservercloud/models/workspace.py +++ b/geoservercloud/models/workspace.py @@ -1,12 +1,14 @@ import json import logging +from requests.models import Response + log = logging.getLogger() class Workspace: - def __init__(self, name, isolated: bool = False) -> None: + def __init__(self, name: str, isolated: bool = False) -> None: self.name = name self.isolated = isolated @@ -20,7 +22,7 @@ def post_payload(self): return self.put_payload() @classmethod - def from_response(cls, response): + def from_response(cls, response: Response): json_data = response.json() return cls( json_data.get("workspace", {}).get("name", None), diff --git a/geoservercloud/models/workspaces.py b/geoservercloud/models/workspaces.py index 05c4fbd..b27dc3e 100644 --- a/geoservercloud/models/workspaces.py +++ b/geoservercloud/models/workspaces.py @@ -1,23 +1,16 @@ import logging +from requests.models import Response + log = logging.getLogger() class Workspaces: - def __init__(self, workspaces: dict = {}) -> None: + def __init__(self, workspaces: list = []) -> None: self._workspaces = workspaces - @classmethod - def validate(self, response): - try: - jsonschema.validate(response, self.response_schema) - except jsonschema.exceptions.ValidationError as err: - print(err) - return False - return True - - def find(self, workspace_name): + def find(self, workspace_name: str): return self.workspaces.get(workspace_name, None) @property @@ -25,7 +18,7 @@ def workspaces(self): return self._workspaces @classmethod - def from_response(cls, response): + def from_response(cls, response: Response): # Parse the JSON response json_data = response.json() diff --git a/tests/models/__init__.py b/tests/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/models/test_common.py b/tests/models/test_common.py new file mode 100644 index 0000000..73f3bde --- /dev/null +++ b/tests/models/test_common.py @@ -0,0 +1,148 @@ +import json + +import pytest + +from geoservercloud.models import ( # Adjust import based on your module location + I18N, + KeyDollarListDict, +) + + +# Test initialization with input_list (deserialization) +def test_keydollarlistdict_initialization_with_input_list(): + input_list = [{"@key": "host", "$": "localhost"}, {"@key": "port", "$": "5432"}] + + kdl_dict = KeyDollarListDict(input_list) + + assert kdl_dict["host"] == "localhost" + assert kdl_dict["port"] == "5432" + + +# Test initialization without input_list +def test_keydollarlistdict_initialization_without_input_list(): + kdl_dict = KeyDollarListDict() + + assert len(kdl_dict) == 0 # Should be an empty dictionary + + +# Test deserialization of input_list +def test_keydollarlistdict_deserialization(): + input_list = [ + {"@key": "username", "$": "admin"}, + {"@key": "password", "$": "secret"}, + ] + + kdl_dict = KeyDollarListDict(input_list) + + assert kdl_dict["username"] == "admin" + assert kdl_dict["password"] == "secret" + + +# Test serialization method +def test_keydollarlistdict_serialization(): + kdl_dict = KeyDollarListDict() + kdl_dict["host"] = "localhost" + kdl_dict["port"] = "5432" + + expected_output = [ + {"@key": "host", "$": "localhost"}, + {"@key": "port", "$": "5432"}, + ] + + assert kdl_dict.serialize() == expected_output + + +# Test __repr__ method +def test_keydollarlistdict_repr(): + kdl_dict = KeyDollarListDict([{"@key": "db", "$": "postgres"}]) + + expected_repr = "[{'@key': 'db', '$': 'postgres'}]" + + assert repr(kdl_dict) == expected_repr + + +# Test __str__ method +def test_keydollarlistdict_str(): + kdl_dict = KeyDollarListDict([{"@key": "db", "$": "postgres"}]) + + expected_str = json.dumps([{"@key": "db", "$": "postgres"}]) + + assert str(kdl_dict) == expected_str + + +# Test initialization with a string value +def test_i18n_initialization_string(): + keys = ("title", "internationalizedTitle") + value = "Test Title" + + i18n_instance = I18N(keys, value) + + assert i18n_instance.str_key == "title" + assert i18n_instance.i18n_key == "internationalizedTitle" + assert i18n_instance.value == value + assert i18n_instance._payload == ("title", value) + + +# Test initialization with a dictionary value (internationalized) +def test_i18n_initialization_dict(): + keys = ("abstract", "internationalizedAbstract") + value = {"en": "Test Abstract", "fr": "Résumé Test"} + + i18n_instance = I18N(keys, value) + + assert i18n_instance.str_key == "abstract" + assert i18n_instance.i18n_key == "internationalizedAbstract" + assert i18n_instance.value == value + assert i18n_instance._payload == ("internationalizedAbstract", value) + + +# Test invalid value type (neither string nor dictionary) +def test_i18n_invalid_value_type(): + keys = ("title", "internationalizedTitle") + value = 123 # Invalid value type + + with pytest.raises(ValueError, match="Invalid value type"): + I18N(keys, value) + + +# Test str_key property +def test_i18n_str_key_property(): + keys = ("title", "internationalizedTitle") + value = "Test Title" + i18n_instance = I18N(keys, value) + + assert i18n_instance.str_key == "title" + + +# Test i18n_key property +def test_i18n_i18n_key_property(): + keys = ("abstract", "internationalizedAbstract") + value = {"en": "Test Abstract", "fr": "Résumé Test"} + i18n_instance = I18N(keys, value) + + assert i18n_instance.i18n_key == "internationalizedAbstract" + + +# Test value property +def test_i18n_value_property(): + keys = ("title", "internationalizedTitle") + value = "Test Title" + i18n_instance = I18N(keys, value) + + assert i18n_instance.value == "Test Title" + + +def test_i18n_payload_tuple_property(): + keys = ("title", "internationalizedTitle") + value = "Test Title" + i18n_instance = I18N(keys, value) + + assert i18n_instance.payload_tuple == ("title", "Test Title") + + +def test_i18n_repr(): + keys = ("title", "internationalizedTitle") + value = "Test Title" + i18n_instance = I18N(keys, value) + + assert repr(i18n_instance) == json.dumps({"title": "Test Title"}, indent=4) diff --git a/tests/test_models_datastore.py b/tests/models/test_datastore.py similarity index 100% rename from tests/test_models_datastore.py rename to tests/models/test_datastore.py diff --git a/tests/test_models_datastores.py b/tests/models/test_datastores.py similarity index 100% rename from tests/test_models_datastores.py rename to tests/models/test_datastores.py diff --git a/tests/models/test_featuretype.py b/tests/models/test_featuretype.py new file mode 100644 index 0000000..fa0fab3 --- /dev/null +++ b/tests/models/test_featuretype.py @@ -0,0 +1,137 @@ +import json +from unittest.mock import Mock + +from geoservercloud.models import I18N, FeatureType + + +# Test initialization of FeatureType class +def test_featuretype_initialization(): + feature_type = FeatureType( + namespace_name="test_namespace", + name="test_name", + native_name="test_native_name", + srs="EPSG:4326", + title="Test Title", + abstract="Test Abstract", + keywords={"keyword1": "test_keyword"}, + attributes={"attribute1": "value1"}, + ) + + assert feature_type.namespace_name == "test_namespace" + assert feature_type.name == "test_name" + assert feature_type.native_name == "test_native_name" + assert feature_type.srs == "EPSG:4326" + assert feature_type.title.payload_tuple[1] == "Test Title" + assert feature_type.abstract.payload_tuple[1] == "Test Abstract" + assert feature_type.keywords == {"keyword1": "test_keyword"} + assert feature_type.attributes == {"attribute1": "value1"} + + +# Test post_payload method +def test_featuretype_post_payload(): + feature_type = FeatureType( + namespace_name="test_namespace", + name="test_name", + native_name="test_native_name", + srs="EPSG:4326", + title={"de": "Test Title"}, + abstract={"de": "Test Abstract"}, + keywords={"keyword1": "test_keyword"}, + attributes={"attribute1": "value1"}, + ) + + expected_payload = { + "featureType": { + "name": "test_name", + "nativeName": "test_native_name", + "internationalTitle": {"de": "Test Title"}, + "internationalAbstract": {"de": "Test Abstract"}, + "srs": "EPSG:4326", + "keywords": {"keyword1": "test_keyword"}, + "attributes": {"attribute1": "value1"}, + } + } + + assert feature_type.post_payload() == expected_payload + + +# Test create_metadata_link method +def test_featuretype_create_metadata_link(): + feature_type = FeatureType( + namespace_name="test_namespace", + name="test_name", + native_name="test_native_name", + metadata_url="http://example.com/metadata.xml", + metadata_type="TC211", + metadata_format="text/xml", + ) + + expected_metadata_link = { + "metadataLink": { + "type": "text/xml", + "metadataType": "TC211", + "content": "http://example.com/metadata.xml", + } + } + + assert feature_type.metadataLink == expected_metadata_link + + +# Test from_response method +def test_featuretype_from_response(): + mock_response = Mock() + mock_response.json.return_value = { + "featureType": { + "namespace": {"name": "test_namespace"}, + "name": "test_name", + "nativeName": "test_native_name", + "srs": "EPSG:4326", + "title": "Test Title", + "abstract": "Test Abstract", + "keywords": {"keyword1": "test_keyword"}, + "attributes": {"attribute1": "value1"}, + "metadataLinks": { + "metadataLink": { + "type": "text/xml", + "metadataType": "TC211", + "content": "http://example.com/metadata.xml", + } + }, + } + } + + feature_type = FeatureType.from_response(mock_response) + + assert feature_type.namespace_name == "test_namespace" + assert feature_type.name == "test_name" + assert feature_type.native_name == "test_native_name" + assert feature_type.srs == "EPSG:4326" + assert feature_type.title.payload_tuple[1] == "Test Title" + assert feature_type.abstract.payload_tuple[1] == "Test Abstract" + assert feature_type.keywords == {"keyword1": "test_keyword"} + assert feature_type.attributes == {"attribute1": "value1"} + assert feature_type.metadataLink == { + "metadataLink": { + "type": "text/xml", + "metadataType": "TC211", + "content": "http://example.com/metadata.xml", + } + } + + +# Test __repr__ method +def test_featuretype_repr(): + feature_type = FeatureType( + namespace_name="test_namespace", + name="test_name", + native_name="test_native_name", + srs="EPSG:4326", + title="Test Title", + abstract="Test Abstract", + keywords={"keyword1": "test_keyword"}, + attributes={"attribute1": "value1"}, + ) + + expected_repr = json.dumps(feature_type.post_payload(), indent=4) + + assert repr(feature_type) == expected_repr diff --git a/tests/models/test_featuretypes.py b/tests/models/test_featuretypes.py new file mode 100644 index 0000000..4a623ff --- /dev/null +++ b/tests/models/test_featuretypes.py @@ -0,0 +1,37 @@ +import json +from unittest.mock import Mock + +import pytest + +from geoservercloud.models import FeatureTypes # Replace with the correct module path + + +# Test initialization of FeatureTypes class +def test_featuretypes_initialization(): + featuretypes = ["feature1", "feature2"] + feature_types_instance = FeatureTypes(featuretypes) + + assert feature_types_instance.featuretypes == featuretypes + + +# Test from_response method with a valid response +def test_featuretypes_from_response_valid(): + mock_response = Mock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "featureTypes": {"featureType": [{"name": "feature1"}, {"name": "feature2"}]} + } + + feature_types_instance = FeatureTypes.from_response(mock_response) + + assert feature_types_instance.featuretypes == ["feature1", "feature2"] + + +# Test __repr__ method +def test_featuretypes_repr(): + featuretypes = ["feature1", "feature2"] + feature_types_instance = FeatureTypes(featuretypes) + + expected_repr = json.dumps(featuretypes, indent=4) + + assert repr(feature_types_instance) == expected_repr diff --git a/tests/test_models_style.py b/tests/models/test_style.py similarity index 100% rename from tests/test_models_style.py rename to tests/models/test_style.py diff --git a/tests/test_models_styles.py b/tests/models/test_styles.py similarity index 100% rename from tests/test_models_styles.py rename to tests/models/test_styles.py diff --git a/tests/test_models_workspace.py b/tests/models/test_workspace.py similarity index 100% rename from tests/test_models_workspace.py rename to tests/models/test_workspace.py diff --git a/tests/test_models_workspaces.py b/tests/models/test_workspaces.py similarity index 100% rename from tests/test_models_workspaces.py rename to tests/models/test_workspaces.py diff --git a/tests/test_datastore.py b/tests/test_datastore.py index eef45e5..e622e4e 100644 --- a/tests/test_datastore.py +++ b/tests/test_datastore.py @@ -113,7 +113,7 @@ def test_get_postgis_datastore_valid( ) -> None: with responses.RequestsMock() as rsps: rsps.get( - url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + url=f"{geoserver.url}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", json=pg_payload, status=200, ) diff --git a/tests/test_feature_type.py b/tests/test_feature_type.py index 276a5c0..4ddf20e 100644 --- a/tests/test_feature_type.py +++ b/tests/test_feature_type.py @@ -5,6 +5,12 @@ from geoservercloud.geoservercloud import GeoServerCloud +# TODO: add tests for +# - geoservercloud.get_featuretypes() +# - geoservercloud.get_featuretype() +# for the moment just import them as import tests +from geoservercloud.models import FeatureType, FeatureTypes + LAYER = "test_layer" WORKSPACE = "test_workspace" STORE = "test_store" @@ -54,53 +60,3 @@ def feature_type_payload() -> dict[str, dict[str, Any]]: }, } } - - -def test_create_feature_type( - geoserver: GeoServerCloud, feature_type_payload: dict[str, dict[str, Any]] -) -> None: - with responses.RequestsMock() as rsps: - rsps.get( - f"{geoserver.url}/rest/workspaces/{WORKSPACE}/datastores/{STORE}/featuretypes/{LAYER}.json", - status=404, - ) - rsps.post( - f"{geoserver.url}/rest/workspaces/{WORKSPACE}/datastores/{STORE}/featuretypes.json", - match=[responses.matchers.json_params_matcher(feature_type_payload)], - status=201, - ) - response = geoserver.create_feature_type( - workspace_name=WORKSPACE, - datastore=STORE, - layer=LAYER, - title={"en": "English"}, - abstract={"en": "English"}, - ) - - assert response - assert response.status_code == 201 - - -def test_update_feature_type( - geoserver: GeoServerCloud, feature_type_payload: dict[str, dict[str, Any]] -) -> None: - with responses.RequestsMock() as rsps: - rsps.get( - f"{geoserver.url}/rest/workspaces/{WORKSPACE}/datastores/{STORE}/featuretypes/{LAYER}.json", - status=200, - ) - rsps.put( - f"{geoserver.url}/rest/workspaces/{WORKSPACE}/datastores/{STORE}/featuretypes/{LAYER}.json", - match=[responses.matchers.json_params_matcher(feature_type_payload)], - status=200, - ) - response = geoserver.create_feature_type( - workspace_name=WORKSPACE, - datastore=STORE, - layer=LAYER, - title={"en": "English"}, - abstract={"en": "English"}, - ) - - assert response - assert response.status_code == 200 diff --git a/tests/test_models_common.py b/tests/test_models_common.py deleted file mode 100644 index 59538d9..0000000 --- a/tests/test_models_common.py +++ /dev/null @@ -1,69 +0,0 @@ -import json - -import pytest - -from geoservercloud.models import ( # Adjust import based on your module location - KeyDollarListDict, -) - - -# Test initialization with input_list (deserialization) -def test_keydollarlistdict_initialization_with_input_list(): - input_list = [{"@key": "host", "$": "localhost"}, {"@key": "port", "$": "5432"}] - - kdl_dict = KeyDollarListDict(input_list) - - assert kdl_dict["host"] == "localhost" - assert kdl_dict["port"] == "5432" - - -# Test initialization without input_list -def test_keydollarlistdict_initialization_without_input_list(): - kdl_dict = KeyDollarListDict() - - assert len(kdl_dict) == 0 # Should be an empty dictionary - - -# Test deserialization of input_list -def test_keydollarlistdict_deserialization(): - input_list = [ - {"@key": "username", "$": "admin"}, - {"@key": "password", "$": "secret"}, - ] - - kdl_dict = KeyDollarListDict(input_list) - - assert kdl_dict["username"] == "admin" - assert kdl_dict["password"] == "secret" - - -# Test serialization method -def test_keydollarlistdict_serialization(): - kdl_dict = KeyDollarListDict() - kdl_dict["host"] = "localhost" - kdl_dict["port"] = "5432" - - expected_output = [ - {"@key": "host", "$": "localhost"}, - {"@key": "port", "$": "5432"}, - ] - - assert kdl_dict.serialize() == expected_output - - -# Test __repr__ method -def test_keydollarlistdict_repr(): - kdl_dict = KeyDollarListDict([{"@key": "db", "$": "postgres"}]) - - expected_repr = "[{'@key': 'db', '$': 'postgres'}]" - - assert repr(kdl_dict) == expected_repr - - -# Test __str__ method -def test_keydollarlistdict_str(): - kdl_dict = KeyDollarListDict([{"@key": "db", "$": "postgres"}]) - - expected_str = json.dumps([{"@key": "db", "$": "postgres"}]) - - assert str(kdl_dict) == expected_str