Skip to content

Commit 5010666

Browse files
authored
Add tests to BaseProvider class (#2024)
* Add tests to the base provider class * Fix flake8
1 parent 8a74159 commit 5010666

File tree

2 files changed

+503
-0
lines changed

2 files changed

+503
-0
lines changed

tests/conftest.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Authors: Bernhard Mallinger <bernhard.mallinger@eox.at>
44
#
55
# Copyright (c) 2024 Bernhard Mallinger
6+
# Copyright (c) 2025 Francesco Bartoli
67
#
78
# Permission is hereby granted, free of charge, to any person
89
# obtaining a copy of this software and associated documentation
@@ -26,10 +27,14 @@
2627
# OTHER DEALINGS IN THE SOFTWARE.
2728
#
2829
# =================================================================
30+
import io
31+
import json
32+
import sys
2933

3034
import pytest
3135

3236
from pygeoapi.api import API
37+
from pygeoapi.provider.base import BaseProvider, ProviderItemNotFoundError
3338
from pygeoapi.util import yaml_load
3439

3540
from tests.util import get_test_file_path
@@ -50,3 +55,99 @@ def openapi():
5055
@pytest.fixture()
5156
def api_(config, openapi):
5257
return API(config, openapi)
58+
59+
60+
@pytest.fixture
61+
def basic_provider_def():
62+
"""Basic provider definition for testing."""
63+
return {
64+
"name": "test_provider",
65+
"type": "feature",
66+
"data": "/path/to/data.geojson"
67+
}
68+
69+
70+
@pytest.fixture
71+
def extended_provider_def():
72+
"""Extended provider definition with all optional fields."""
73+
return {
74+
"name": "test_provider",
75+
"type": "feature",
76+
"data": "/path/to/data.geojson",
77+
"editable": True,
78+
"options": {"some_option": "value"},
79+
"id_field": "feature_id",
80+
"uri_field": "uri",
81+
"x_field": "longitude",
82+
"y_field": "latitude",
83+
"time_field": "timestamp",
84+
"title_field": "title",
85+
"properties": ["prop1", "prop2"],
86+
"file_types": [".geojson", ".json"]
87+
}
88+
89+
90+
@pytest.fixture
91+
def basic_provider(basic_provider_def):
92+
"""Basic BaseProvider instance."""
93+
return BaseProvider(basic_provider_def)
94+
95+
96+
@pytest.fixture
97+
def extended_provider(extended_provider_def):
98+
"""Extended BaseProvider instance."""
99+
return BaseProvider(extended_provider_def)
100+
101+
102+
@pytest.fixture
103+
def mock_provider_with_get():
104+
"""Mock provider that implements get() method."""
105+
class MockProvider(BaseProvider):
106+
def get(self, identifier, **kwargs):
107+
if identifier == "mock_id":
108+
return {"type": "Feature", "id": identifier}
109+
else:
110+
raise ProviderItemNotFoundError("Not found")
111+
112+
provider_def = {
113+
"name": "mock_provider",
114+
"type": "feature",
115+
"data": "/path/to/data.geojson"
116+
}
117+
return MockProvider(provider_def)
118+
119+
120+
@pytest.fixture
121+
def valid_geojson_item():
122+
"""Valid GeoJSON item for testing."""
123+
return json.dumps({
124+
"type": "Feature",
125+
"id": "test_id",
126+
"geometry": {"type": "Point", "coordinates": [0, 0]},
127+
"properties": {"name": "Test Feature"}
128+
})
129+
130+
131+
@pytest.fixture
132+
def geojson_item_with_props_id():
133+
"""GeoJSON item with identifier in properties."""
134+
return json.dumps({
135+
"type": "Feature",
136+
"geometry": {"type": "Point", "coordinates": [0, 0]},
137+
"properties": {"identifier": "props_id", "name": "Test"}
138+
})
139+
140+
141+
@pytest.fixture
142+
def remove_stdout():
143+
"""Fixture to remove standard output during tests."""
144+
class RemoveStdout:
145+
def __enter__(self):
146+
self.original_stdout = sys.stdout
147+
sys.stdout = io.StringIO()
148+
return self
149+
150+
def __exit__(self, exc_type, exc_val, exc_tb):
151+
sys.stdout = self.original_stdout
152+
153+
return RemoveStdout

0 commit comments

Comments
 (0)