diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index fedd1a3..34eb91a 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -39,9 +39,6 @@ jobs: - name: Install run: make install - - name: Build - run: make build - - name: Tests run: make tests diff --git a/Makefile b/Makefile index 84e8c20..e6dea5d 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,10 @@ help: ## Display this help message .PHONY: install install: ## Install package + poetry lock --no-update poetry install -.PHONY: build -build: ## Build package - poetry build - .PHONY: tests -tests: ## Run unit tests - poetry run pytest -vvv tests --color=yes +tests: ## Run unit tests with coverage + poetry run coverage run --source=geoservercloud -m pytest tests -vvv --color=yes + poetry run coverage report diff --git a/geoservercloud/geoservercloud.py b/geoservercloud/geoservercloud.py index f7764a6..90c5559 100644 --- a/geoservercloud/geoservercloud.py +++ b/geoservercloud/geoservercloud.py @@ -102,12 +102,14 @@ def delete_workspace(self, workspace: str) -> Response: def recreate_workspace( self, workspace: str, set_default_workspace: bool = False - ) -> None: + ) -> Response: """ Create a workspace in GeoServer, and first delete it if it already exists. """ self.delete_workspace(workspace) - self.create_workspace(workspace, set_default_workspace=set_default_workspace) + return self.create_workspace( + workspace, set_default_workspace=set_default_workspace + ) def publish_workspace(self, workspace) -> Response: path: str = f"{self.workspace_wms_settings_path(workspace)}" @@ -333,7 +335,7 @@ def publish_gwc_layer( self.post_request( "/gwc/rest/reload", headers={"Content-Type": "application/json"}, - data="reload_configuration=1", + data="reload_configuration=1", # type: ignore ) payload = Templates.gwc_layer(workspace, layer, f"EPSG:{epsg}") return self.put_request( diff --git a/poetry.lock b/poetry.lock index b011960..24ad1b2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -124,6 +124,91 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "coverage" +version = "7.6.1" +description = "Code coverage measurement for Python" +category = "dev" +optional = false +python-versions = ">=3.8" +files = [ + {file = "coverage-7.6.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:b06079abebbc0e89e6163b8e8f0e16270124c154dc6e4a47b413dd538859af16"}, + {file = "coverage-7.6.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:cf4b19715bccd7ee27b6b120e7e9dd56037b9c0681dcc1adc9ba9db3d417fa36"}, + {file = "coverage-7.6.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e61c0abb4c85b095a784ef23fdd4aede7a2628478e7baba7c5e3deba61070a02"}, + {file = "coverage-7.6.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fd21f6ae3f08b41004dfb433fa895d858f3f5979e7762d052b12aef444e29afc"}, + {file = "coverage-7.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8f59d57baca39b32db42b83b2a7ba6f47ad9c394ec2076b084c3f029b7afca23"}, + {file = "coverage-7.6.1-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:a1ac0ae2b8bd743b88ed0502544847c3053d7171a3cff9228af618a068ed9c34"}, + {file = "coverage-7.6.1-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:e6a08c0be454c3b3beb105c0596ebdc2371fab6bb90c0c0297f4e58fd7e1012c"}, + {file = "coverage-7.6.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:f5796e664fe802da4f57a168c85359a8fbf3eab5e55cd4e4569fbacecc903959"}, + {file = "coverage-7.6.1-cp310-cp310-win32.whl", hash = "sha256:7bb65125fcbef8d989fa1dd0e8a060999497629ca5b0efbca209588a73356232"}, + {file = "coverage-7.6.1-cp310-cp310-win_amd64.whl", hash = "sha256:3115a95daa9bdba70aea750db7b96b37259a81a709223c8448fa97727d546fe0"}, + {file = "coverage-7.6.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:7dea0889685db8550f839fa202744652e87c60015029ce3f60e006f8c4462c93"}, + {file = "coverage-7.6.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:ed37bd3c3b063412f7620464a9ac1314d33100329f39799255fb8d3027da50d3"}, + {file = "coverage-7.6.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d85f5e9a5f8b73e2350097c3756ef7e785f55bd71205defa0bfdaf96c31616ff"}, + {file = "coverage-7.6.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:9bc572be474cafb617672c43fe989d6e48d3c83af02ce8de73fff1c6bb3c198d"}, + {file = "coverage-7.6.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0c0420b573964c760df9e9e86d1a9a622d0d27f417e1a949a8a66dd7bcee7bc6"}, + {file = "coverage-7.6.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:1f4aa8219db826ce6be7099d559f8ec311549bfc4046f7f9fe9b5cea5c581c56"}, + {file = "coverage-7.6.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:fc5a77d0c516700ebad189b587de289a20a78324bc54baee03dd486f0855d234"}, + {file = "coverage-7.6.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:b48f312cca9621272ae49008c7f613337c53fadca647d6384cc129d2996d1133"}, + {file = "coverage-7.6.1-cp311-cp311-win32.whl", hash = "sha256:1125ca0e5fd475cbbba3bb67ae20bd2c23a98fac4e32412883f9bcbaa81c314c"}, + {file = "coverage-7.6.1-cp311-cp311-win_amd64.whl", hash = "sha256:8ae539519c4c040c5ffd0632784e21b2f03fc1340752af711f33e5be83a9d6c6"}, + {file = "coverage-7.6.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:95cae0efeb032af8458fc27d191f85d1717b1d4e49f7cb226cf526ff28179778"}, + {file = "coverage-7.6.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:5621a9175cf9d0b0c84c2ef2b12e9f5f5071357c4d2ea6ca1cf01814f45d2391"}, + {file = "coverage-7.6.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:260933720fdcd75340e7dbe9060655aff3af1f0c5d20f46b57f262ab6c86a5e8"}, + {file = "coverage-7.6.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:07e2ca0ad381b91350c0ed49d52699b625aab2b44b65e1b4e02fa9df0e92ad2d"}, + {file = "coverage-7.6.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c44fee9975f04b33331cb8eb272827111efc8930cfd582e0320613263ca849ca"}, + {file = "coverage-7.6.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:877abb17e6339d96bf08e7a622d05095e72b71f8afd8a9fefc82cf30ed944163"}, + {file = "coverage-7.6.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:3e0cadcf6733c09154b461f1ca72d5416635e5e4ec4e536192180d34ec160f8a"}, + {file = "coverage-7.6.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:c3c02d12f837d9683e5ab2f3d9844dc57655b92c74e286c262e0fc54213c216d"}, + {file = "coverage-7.6.1-cp312-cp312-win32.whl", hash = "sha256:e05882b70b87a18d937ca6768ff33cc3f72847cbc4de4491c8e73880766718e5"}, + {file = "coverage-7.6.1-cp312-cp312-win_amd64.whl", hash = "sha256:b5d7b556859dd85f3a541db6a4e0167b86e7273e1cdc973e5b175166bb634fdb"}, + {file = "coverage-7.6.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:a4acd025ecc06185ba2b801f2de85546e0b8ac787cf9d3b06e7e2a69f925b106"}, + {file = "coverage-7.6.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:a6d3adcf24b624a7b778533480e32434a39ad8fa30c315208f6d3e5542aeb6e9"}, + {file = "coverage-7.6.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d0c212c49b6c10e6951362f7c6df3329f04c2b1c28499563d4035d964ab8e08c"}, + {file = "coverage-7.6.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6e81d7a3e58882450ec4186ca59a3f20a5d4440f25b1cff6f0902ad890e6748a"}, + {file = "coverage-7.6.1-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:78b260de9790fd81e69401c2dc8b17da47c8038176a79092a89cb2b7d945d060"}, + {file = "coverage-7.6.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a78d169acd38300060b28d600344a803628c3fd585c912cacc9ea8790fe96862"}, + {file = "coverage-7.6.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:2c09f4ce52cb99dd7505cd0fc8e0e37c77b87f46bc9c1eb03fe3bc9991085388"}, + {file = "coverage-7.6.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:6878ef48d4227aace338d88c48738a4258213cd7b74fd9a3d4d7582bb1d8a155"}, + {file = "coverage-7.6.1-cp313-cp313-win32.whl", hash = "sha256:44df346d5215a8c0e360307d46ffaabe0f5d3502c8a1cefd700b34baf31d411a"}, + {file = "coverage-7.6.1-cp313-cp313-win_amd64.whl", hash = "sha256:8284cf8c0dd272a247bc154eb6c95548722dce90d098c17a883ed36e67cdb129"}, + {file = "coverage-7.6.1-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:d3296782ca4eab572a1a4eca686d8bfb00226300dcefdf43faa25b5242ab8a3e"}, + {file = "coverage-7.6.1-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:502753043567491d3ff6d08629270127e0c31d4184c4c8d98f92c26f65019962"}, + {file = "coverage-7.6.1-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6a89ecca80709d4076b95f89f308544ec8f7b4727e8a547913a35f16717856cb"}, + {file = "coverage-7.6.1-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a318d68e92e80af8b00fa99609796fdbcdfef3629c77c6283566c6f02c6d6704"}, + {file = "coverage-7.6.1-cp313-cp313t-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:13b0a73a0896988f053e4fbb7de6d93388e6dd292b0d87ee51d106f2c11b465b"}, + {file = "coverage-7.6.1-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:4421712dbfc5562150f7554f13dde997a2e932a6b5f352edcce948a815efee6f"}, + {file = "coverage-7.6.1-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:166811d20dfea725e2e4baa71fffd6c968a958577848d2131f39b60043400223"}, + {file = "coverage-7.6.1-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:225667980479a17db1048cb2bf8bfb39b8e5be8f164b8f6628b64f78a72cf9d3"}, + {file = "coverage-7.6.1-cp313-cp313t-win32.whl", hash = "sha256:170d444ab405852903b7d04ea9ae9b98f98ab6d7e63e1115e82620807519797f"}, + {file = "coverage-7.6.1-cp313-cp313t-win_amd64.whl", hash = "sha256:b9f222de8cded79c49bf184bdbc06630d4c58eec9459b939b4a690c82ed05657"}, + {file = "coverage-7.6.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6db04803b6c7291985a761004e9060b2bca08da6d04f26a7f2294b8623a0c1a0"}, + {file = "coverage-7.6.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:f1adfc8ac319e1a348af294106bc6a8458a0f1633cc62a1446aebc30c5fa186a"}, + {file = "coverage-7.6.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a95324a9de9650a729239daea117df21f4b9868ce32e63f8b650ebe6cef5595b"}, + {file = "coverage-7.6.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:b43c03669dc4618ec25270b06ecd3ee4fa94c7f9b3c14bae6571ca00ef98b0d3"}, + {file = "coverage-7.6.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8929543a7192c13d177b770008bc4e8119f2e1f881d563fc6b6305d2d0ebe9de"}, + {file = "coverage-7.6.1-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:a09ece4a69cf399510c8ab25e0950d9cf2b42f7b3cb0374f95d2e2ff594478a6"}, + {file = "coverage-7.6.1-cp38-cp38-musllinux_1_2_i686.whl", hash = "sha256:9054a0754de38d9dbd01a46621636689124d666bad1936d76c0341f7d71bf569"}, + {file = "coverage-7.6.1-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:0dbde0f4aa9a16fa4d754356a8f2e36296ff4d83994b2c9d8398aa32f222f989"}, + {file = "coverage-7.6.1-cp38-cp38-win32.whl", hash = "sha256:da511e6ad4f7323ee5702e6633085fb76c2f893aaf8ce4c51a0ba4fc07580ea7"}, + {file = "coverage-7.6.1-cp38-cp38-win_amd64.whl", hash = "sha256:3f1156e3e8f2872197af3840d8ad307a9dd18e615dc64d9ee41696f287c57ad8"}, + {file = "coverage-7.6.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:abd5fd0db5f4dc9289408aaf34908072f805ff7792632250dcb36dc591d24255"}, + {file = "coverage-7.6.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:547f45fa1a93154bd82050a7f3cddbc1a7a4dd2a9bf5cb7d06f4ae29fe94eaf8"}, + {file = "coverage-7.6.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:645786266c8f18a931b65bfcefdbf6952dd0dea98feee39bd188607a9d307ed2"}, + {file = "coverage-7.6.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:9e0b2df163b8ed01d515807af24f63de04bebcecbd6c3bfeff88385789fdf75a"}, + {file = "coverage-7.6.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:609b06f178fe8e9f89ef676532760ec0b4deea15e9969bf754b37f7c40326dbc"}, + {file = "coverage-7.6.1-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:702855feff378050ae4f741045e19a32d57d19f3e0676d589df0575008ea5004"}, + {file = "coverage-7.6.1-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:2bdb062ea438f22d99cba0d7829c2ef0af1d768d1e4a4f528087224c90b132cb"}, + {file = "coverage-7.6.1-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:9c56863d44bd1c4fe2abb8a4d6f5371d197f1ac0ebdee542f07f35895fc07f36"}, + {file = "coverage-7.6.1-cp39-cp39-win32.whl", hash = "sha256:6e2cd258d7d927d09493c8df1ce9174ad01b381d4729a9d8d4e38670ca24774c"}, + {file = "coverage-7.6.1-cp39-cp39-win_amd64.whl", hash = "sha256:06a737c882bd26d0d6ee7269b20b12f14a8704807a01056c80bb881a4b2ce6ca"}, + {file = "coverage-7.6.1-pp38.pp39.pp310-none-any.whl", hash = "sha256:e9a6e0eb86070e8ccaedfbd9d38fec54864f3125ab95419970575b42af7541df"}, + {file = "coverage-7.6.1.tar.gz", hash = "sha256:953510dfb7b12ab69d20135a0662397f077c59b1e6379a768e97c59d852ee51d"}, +] + +[package.extras] +toml = ["tomli"] + [[package]] name = "exceptiongroup" version = "1.2.2" @@ -500,6 +585,26 @@ urllib3 = ">=1.21.1,<3" socks = ["PySocks (>=1.5.6,!=1.5.7)"] use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] +[[package]] +name = "responses" +version = "0.25.3" +description = "A utility library for mocking out the `requests` Python library." +category = "dev" +optional = false +python-versions = ">=3.8" +files = [ + {file = "responses-0.25.3-py3-none-any.whl", hash = "sha256:521efcbc82081ab8daa588e08f7e8a64ce79b91c39f6e62199b19159bea7dbcb"}, + {file = "responses-0.25.3.tar.gz", hash = "sha256:617b9247abd9ae28313d57a75880422d55ec63c29d33d629697590a034358dba"}, +] + +[package.dependencies] +pyyaml = "*" +requests = ">=2.30.0,<3.0" +urllib3 = ">=1.25.10,<3.0" + +[package.extras] +tests = ["coverage (>=6.0.0)", "flake8", "mypy", "pytest (>=7.0.0)", "pytest-asyncio", "pytest-cov", "pytest-httpserver", "tomli", "tomli-w", "types-PyYAML", "types-requests"] + [[package]] name = "six" version = "1.16.0" @@ -557,4 +662,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "27318ff060452f8aeeb973ef3022f2c4edf20d38d68e131ad8246fa0408bd93a" +content-hash = "9e26fae0681b5816d684a7fb0d769643ce305261d230ec84ee29157803e950b5" diff --git a/pyproject.toml b/pyproject.toml index cb5c17c..e261e35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ description = "Lightweight Python client to interact with GeoServer Cloud REST A authors = ["Camptocamp "] license = "BSD-2-Clause" readme = "README.md" -packages = [{include = "geoservercloud"}] +packages = [{ include = "geoservercloud" }] [tool.poetry.dependencies] python = ">=3.9,<4.0" @@ -15,12 +15,14 @@ xmltodict = "0.13.0" [tool.poetry.group.dev.dependencies] pytest = "8.3.2" +responses = "0.25.3" +coverage = "7.6.1" [build-system] requires = [ "poetry-core>=1.0.0", "poetry-dynamic-versioning[plugin]", - "poetry-plugin-tweak-dependencies-version" + "poetry-plugin-tweak-dependencies-version", ] build-backend = "poetry.core.masonry.api" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..1d21149 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,11 @@ +import pytest + +from geoservercloud import GeoServerCloud + +GEOSERVER_URL = "http://localhost:8080/geoserver" + + +@pytest.fixture(scope="session") +def geoserver(): + geoserver = GeoServerCloud(url=GEOSERVER_URL) + yield geoserver diff --git a/tests/test_cascaded_wmts.py b/tests/test_cascaded_wmts.py new file mode 100644 index 0000000..4bc5e9f --- /dev/null +++ b/tests/test_cascaded_wmts.py @@ -0,0 +1,68 @@ +from typing import Any + +import pytest +import responses + +from geoservercloud.geoservercloud import GeoServerCloud + +WORKSPACE = "test_workspace" +STORE = "test_wmtsstore" +CAPABILITIES_URL = "http://wms?request=GetCapabilities&service=WMS" + + +@pytest.fixture(scope="module") +def wmts_store_payload() -> dict[str, dict[str, Any]]: + return { + "wmtsStore": { + "name": STORE, + "type": "WMTS", + "capabilitiesURL": CAPABILITIES_URL, + "workspace": {"name": WORKSPACE}, + "enabled": True, + "metadata": {"entry": {"@key": "useConnectionPooling", "text": True}}, + } + } + + +def test_create_wmts_store( + geoserver: GeoServerCloud, wmts_store_payload: dict[str, dict[str, Any]] +) -> None: + with responses.RequestsMock() as rsps: + rsps.get( + f"{geoserver.url}/rest/workspaces/{WORKSPACE}/wmtsstores/{STORE}.json", + status=404, + ) + rsps.post( + f"{geoserver.url}/rest/workspaces/{WORKSPACE}/wmtsstores.json", + json=wmts_store_payload, + status=201, + ) + response = geoserver.create_wmts_store( + workspace=WORKSPACE, + name=STORE, + capabilities=CAPABILITIES_URL, + ) + assert response + assert response.status_code == 201 + + +def test_update_wmts_store( + geoserver: GeoServerCloud, wmts_store_payload: dict[str, dict[str, Any]] +) -> None: + with responses.RequestsMock() as rsps: + rsps.get( + f"{geoserver.url}/rest/workspaces/{WORKSPACE}/wmtsstores/{STORE}.json", + status=200, + ) + rsps.put( + f"{geoserver.url}/rest/workspaces/{WORKSPACE}/wmtsstores/{STORE}.json", + json=wmts_store_payload, + status=200, + ) + response = geoserver.create_wmts_store( + workspace=WORKSPACE, + name=STORE, + capabilities=CAPABILITIES_URL, + ) + assert response + assert response.status_code == 200 diff --git a/tests/test_datastore.py b/tests/test_datastore.py new file mode 100644 index 0000000..780fdee --- /dev/null +++ b/tests/test_datastore.py @@ -0,0 +1,184 @@ +from collections.abc import Generator +from typing import Any + +import pytest +import responses +from responses import matchers + +from geoservercloud.geoservercloud import GeoServerCloud +from tests.conftest import GEOSERVER_URL + +WORKSPACE = "test_workspace" +STORE = "test_store" +HOST = "localhost" +PORT = 5432 +DATABASE = "test_db" +USER = "test_user" +PASSWORD = "test_password" +SCHEMA = "test_schema" +JNDI = "java:comp/env/jdbc/data" +DESCRIPTION = "test description" + + +@pytest.fixture(scope="module") +def pg_payload() -> Generator[dict[str, dict[str, Any]], Any, None]: + yield { + "dataStore": { + "name": STORE, + "connectionParameters": { + "entry": [ + {"@key": "dbtype", "$": "postgis"}, + {"@key": "host", "$": HOST}, + {"@key": "port", "$": PORT}, + {"@key": "database", "$": DATABASE}, + {"@key": "user", "$": USER}, + {"@key": "passwd", "$": PASSWORD}, + {"@key": "schema", "$": SCHEMA}, + { + "@key": "namespace", + "$": f"http://{WORKSPACE}", + }, + {"@key": "Expose primary keys", "$": "true"}, + ] + }, + } + } + + +@pytest.fixture(scope="module") +def jndi_payload() -> Generator[dict[str, dict[str, Any]], Any, None]: + yield { + "dataStore": { + "name": STORE, + "description": DESCRIPTION, + "connectionParameters": { + "entry": [ + {"@key": "dbtype", "$": "postgis"}, + { + "@key": "jndiReferenceName", + "$": JNDI, + }, + { + "@key": "schema", + "$": SCHEMA, + }, + { + "@key": "namespace", + "$": f"http://{WORKSPACE}", + }, + {"@key": "Expose primary keys", "$": "true"}, + ] + }, + } + } + + +def test_create_pg_datastore( + geoserver: GeoServerCloud, pg_payload: dict[str, dict[str, Any]] +) -> None: + with responses.RequestsMock() as rsps: + rsps.get( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + status=404, + ) + rsps.post( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores.json", + status=201, + json={"workspace": {"name": WORKSPACE}}, + match=[matchers.json_params_matcher(pg_payload)], + ) + + response = geoserver.create_pg_datastore( + workspace=WORKSPACE, + datastore=STORE, + pg_host=HOST, + pg_port=PORT, + pg_db=DATABASE, + pg_user=USER, + pg_password=PASSWORD, + pg_schema=SCHEMA, + ) + + assert response + assert response.status_code == 201 + + +def test_update_pg_datastore( + geoserver: GeoServerCloud, pg_payload: dict[str, dict[str, Any]] +) -> None: + with responses.RequestsMock() as rsps: + rsps.get( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + status=200, + ) + rsps.put( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + status=200, + match=[matchers.json_params_matcher(pg_payload)], + ) + + response = geoserver.create_pg_datastore( + workspace=WORKSPACE, + datastore=STORE, + pg_host=HOST, + pg_port=PORT, + pg_db=DATABASE, + pg_user=USER, + pg_password=PASSWORD, + pg_schema=SCHEMA, + ) + + assert response + assert response.status_code == 200 + + +def test_create_jndi_datastore( + geoserver: GeoServerCloud, jndi_payload: dict[str, dict[str, Any]] +) -> None: + with responses.RequestsMock() as rsps: + rsps.get( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + status=404, + ) + rsps.post( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores.json", + status=201, + match=[matchers.json_params_matcher(jndi_payload)], + ) + + response = geoserver.create_jndi_datastore( + workspace=WORKSPACE, + datastore=STORE, + jndi_reference=JNDI, + pg_schema=SCHEMA, + description=DESCRIPTION, + ) + + assert response + assert response.status_code == 201 + + +def test_update_jndi_datastore( + geoserver: GeoServerCloud, jndi_payload: dict[str, dict[str, Any]] +) -> None: + with responses.RequestsMock() as rsps: + rsps.get( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + status=200, + ) + rsps.put( + url=f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{STORE}.json", + status=200, + match=[matchers.json_params_matcher(jndi_payload)], + ) + + response = geoserver.create_jndi_datastore( + workspace=WORKSPACE, + datastore=STORE, + jndi_reference=JNDI, + pg_schema=SCHEMA, + description=DESCRIPTION, + ) + + assert response + assert response.status_code == 200 diff --git a/tests/test_workspace.py b/tests/test_workspace.py new file mode 100644 index 0000000..473e6ce --- /dev/null +++ b/tests/test_workspace.py @@ -0,0 +1,80 @@ +import responses +from responses import matchers + +from geoservercloud.geoservercloud import GeoServerCloud +from tests.conftest import GEOSERVER_URL + + +def test_create_workspace(geoserver: GeoServerCloud) -> None: + workspace = "test_workspace" + isolated = True + + with responses.RequestsMock() as rsps: + rsps.post( + url=f"{GEOSERVER_URL}/rest/workspaces.json", + status=201, + match=[ + matchers.json_params_matcher( + { + "workspace": { + "name": workspace, + "isolated": isolated, + } + } + ) + ], + ) + + response = geoserver.create_workspace(workspace, isolated=isolated) + + assert response.status_code == 201 + + +def test_update_workspace(geoserver: GeoServerCloud) -> None: + workspace = "test_workspace" + + with responses.RequestsMock() as rsps: + rsps.post( + url=f"{GEOSERVER_URL}/rest/workspaces.json", + status=409, + ) + rsps.put( + url=f"{GEOSERVER_URL}/rest/workspaces/{workspace}.json", + status=200, + ) + + response = geoserver.create_workspace(workspace) + + assert response.status_code == 200 + + +def test_delete_workspace(geoserver: GeoServerCloud) -> None: + workspace = "test_workspace" + + with responses.RequestsMock() as rsps: + rsps.delete( + url=f"{GEOSERVER_URL}/rest/workspaces/{workspace}.json", + status=200, + ) + + response = geoserver.delete_workspace(workspace) + + assert response.status_code == 200 + + +def test_recreate_workspace(geoserver: GeoServerCloud) -> None: + workspace = "test_workspace" + + with responses.RequestsMock() as rsps: + rsps.delete( + url=f"{GEOSERVER_URL}/rest/workspaces/{workspace}.json", + status=200, + ) + rsps.post( + url=f"{GEOSERVER_URL}/rest/workspaces.json", + status=201, + ) + + response = geoserver.recreate_workspace(workspace) + + assert response.status_code == 201