-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest_factories.py
301 lines (263 loc) · 12.1 KB
/
test_factories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""test endpoint factories."""
from fastapi import FastAPI
from starlette.testclient import TestClient
def test_features_factory():
"""test OGC Feature Factory."""
# We import the factory here to make sure they do not mess with the env setting set in conftest
# ref: https://github.com/developmentseed/tipg/issues/38
from tipg.factory import OGCFeaturesFactory
endpoints = OGCFeaturesFactory()
assert endpoints.with_common
assert endpoints.title == "OGC API"
assert len(endpoints.router.routes) == 7
assert len(endpoints.conforms_to) == 6
app = FastAPI()
app.include_router(endpoints.router)
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json()["title"] == "OGC API"
links = response.json()["links"]
assert len(links) == 9 # 5 from features + 4 from common
landing_link = [link for link in links if link["title"] == "Landing Page"][0]
assert landing_link["href"] == "http://testserver/"
queryables_link = [
link
for link in links
if link["title"] == "Collection queryables (Template URL)"
][0]
assert (
queryables_link["href"]
== "http://testserver/collections/{collectionId}/queryables"
)
doc_link = [link for link in links if link["title"] == "the API documentation"][
0
]
assert doc_link["href"] == "http://testserver/docs"
response = client.get("/conformance")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()["conformsTo"]
assert len(body) > 6
assert "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core" in body
endpoints = OGCFeaturesFactory(
router_prefix="/features", title="OGC Features API", with_common=True
)
assert endpoints.router_prefix == "/features"
assert endpoints.with_common
assert endpoints.title == "OGC Features API"
assert len(endpoints.router.routes) == 7
app = FastAPI()
app.include_router(endpoints.router, prefix="/features")
with TestClient(app) as client:
response = client.get("/features/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json()["title"] == "OGC Features API"
links = response.json()["links"]
assert len(links) == 9
landing_link = [link for link in links if link["title"] == "Landing Page"][0]
assert landing_link["href"] == "http://testserver/features/"
queryables_link = [
link
for link in links
if link["title"] == "Collection queryables (Template URL)"
][0]
assert (
queryables_link["href"]
== "http://testserver/features/collections/{collectionId}/queryables"
)
doc_link = [link for link in links if link["title"] == "the API documentation"][
0
]
assert doc_link["href"] == "http://testserver/docs"
response = client.get("/features/conformance")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()["conformsTo"]
assert len(body) > 6
assert "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core" in body
endpoints = OGCFeaturesFactory(title="OGC Features API", with_common=False)
assert not endpoints.with_common
assert endpoints.title == "OGC Features API"
assert len(endpoints.router.routes) == 5
assert len(endpoints.conforms_to) == 6
app = FastAPI()
app.include_router(endpoints.router)
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 404
response = client.get("/conformance")
assert response.status_code == 404
def test_tiles_factory():
"""test OGC Tiles Factory."""
# We import the factory here to make sure they do not mess with the env setting set in conftest
# ref: https://github.com/developmentseed/tipg/issues/38
from tipg.factory import OGCTilesFactory
endpoints = OGCTilesFactory()
assert endpoints.with_common
assert endpoints.title == "OGC API"
assert len(endpoints.router.routes) == 10
assert len(endpoints.conforms_to) == 5
app = FastAPI()
app.include_router(endpoints.router)
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json()["title"] == "OGC API"
links = response.json()["links"]
assert len(links) == 10 # 6 from tiles + 4 from common
landing_link = [link for link in links if link["title"] == "Landing Page"][0]
assert landing_link["href"] == "http://testserver/"
tms_link = [link for link in links if link["title"] == "TileMatrixSets"][0]
assert tms_link["href"] == "http://testserver/tileMatrixSets"
doc_link = [link for link in links if link["title"] == "the API documentation"][
0
]
assert doc_link["href"] == "http://testserver/docs"
response = client.get("/conformance")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()["conformsTo"]
assert len(body) > 3
assert "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core" in body
endpoints = OGCTilesFactory(
router_prefix="/map", title="OGC Tiles API", with_common=True
)
assert endpoints.router_prefix == "/map"
assert endpoints.with_common
assert endpoints.title == "OGC Tiles API"
assert len(endpoints.router.routes) == 10
app = FastAPI()
app.include_router(endpoints.router, prefix="/map")
with TestClient(app) as client:
response = client.get("/map/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json()["title"] == "OGC Tiles API"
links = response.json()["links"]
assert len(links) == 10
landing_link = [link for link in links if link["title"] == "Landing Page"][0]
assert landing_link["href"] == "http://testserver/map/"
tms_link = [link for link in links if link["title"] == "TileMatrixSets"][0]
assert tms_link["href"] == "http://testserver/map/tileMatrixSets"
doc_link = [link for link in links if link["title"] == "the API documentation"][
0
]
assert doc_link["href"] == "http://testserver/docs"
response = client.get("/map/conformance")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()["conformsTo"]
assert len(body) > 6
assert "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core" in body
endpoints = OGCTilesFactory(title="OGC Tiles API", with_common=False)
assert not endpoints.with_common
assert endpoints.title == "OGC Tiles API"
assert len(endpoints.router.routes) == 8
assert len(endpoints.conforms_to) == 5
app = FastAPI()
app.include_router(endpoints.router)
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 404
response = client.get("/conformance")
assert response.status_code == 404
def test_endpoints_factory():
"""test OGC Features+Tiles Factory."""
# We import the factory here to make sure they do not mess with the env setting set in conftest
# ref: https://github.com/developmentseed/tipg/issues/38
from tipg.factory import Endpoints
endpoints = Endpoints()
assert endpoints.with_common
assert endpoints.title == "OGC API"
assert len(endpoints.router.routes) == 15
assert len(endpoints.conforms_to) == 11 # 5 from tiles + 6 from features
app = FastAPI()
app.include_router(endpoints.router)
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json()["title"] == "OGC API"
links = response.json()["links"]
assert len(links) == 15 # 6 from tiles + 5 from features + 4 from common
landing_link = [link for link in links if link["title"] == "Landing Page"][0]
assert landing_link["href"] == "http://testserver/"
queryables_link = [
link
for link in links
if link["title"] == "Collection queryables (Template URL)"
][0]
assert (
queryables_link["href"]
== "http://testserver/collections/{collectionId}/queryables"
)
tms_link = [link for link in links if link["title"] == "TileMatrixSets"][0]
assert tms_link["href"] == "http://testserver/tileMatrixSets"
doc_link = [link for link in links if link["title"] == "the API documentation"][
0
]
assert doc_link["href"] == "http://testserver/docs"
response = client.get("/conformance")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()["conformsTo"]
assert len(body) > 9 # 3 from tiles + 6 from features
assert "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core" in body
endpoints = Endpoints(router_prefix="/ogc", title="OGC Full API", with_common=True)
assert endpoints.router_prefix == "/ogc"
assert endpoints.with_common
assert endpoints.title == "OGC Full API"
assert len(endpoints.router.routes) == 15
assert not endpoints.ogc_features.with_common
assert endpoints.ogc_features.router_prefix == "/ogc"
assert not endpoints.ogc_tiles.with_common
assert endpoints.ogc_tiles.router_prefix == "/ogc"
app = FastAPI()
app.include_router(endpoints.router, prefix="/ogc")
with TestClient(app) as client:
response = client.get("/ogc/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json()["title"] == "OGC Full API"
links = response.json()["links"]
assert len(links) == 15
landing_link = [link for link in links if link["title"] == "Landing Page"][0]
assert landing_link["href"] == "http://testserver/ogc/"
queryables_link = [
link
for link in links
if link["title"] == "Collection queryables (Template URL)"
][0]
assert (
queryables_link["href"]
== "http://testserver/ogc/collections/{collectionId}/queryables"
)
tms_link = [link for link in links if link["title"] == "TileMatrixSets"][0]
assert tms_link["href"] == "http://testserver/ogc/tileMatrixSets"
doc_link = [link for link in links if link["title"] == "the API documentation"][
0
]
assert doc_link["href"] == "http://testserver/docs"
response = client.get("/ogc/conformance")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
body = response.json()["conformsTo"]
assert len(body) > 9
assert "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core" in body
# Create Endpoints without landing and conformance
endpoints = Endpoints(title="Tiles and Features API", with_common=False)
assert not endpoints.with_common
assert endpoints.title == "Tiles and Features API"
assert len(endpoints.router.routes) == 13 # 8 from tiles + 5 from features
assert len(endpoints.conforms_to) == 11 # 4 from tiles + 6 from features
app = FastAPI()
app.include_router(endpoints.router)
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 404
response = client.get("/conformance")
assert response.status_code == 404