Skip to content

Commit 554512e

Browse files
committed
update tests
1 parent b525152 commit 554512e

File tree

3 files changed

+137
-10
lines changed

3 files changed

+137
-10
lines changed

pycsw/ogc/api/records.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,28 @@ def collections(self, headers_, args, collection=False):
337337
response = {
338338
'collections': [collection_info]
339339
}
340-
else:
341-
response = collection_info
342-
343-
if not collection:
344340
template = 'collections.html'
341+
url_base = f"{self.config['server']['url']}/collections"
345342
else:
343+
response = collection_info
346344
template = 'collection.html'
345+
url_base = f"{self.config['server']['url']}/collections/metadata:main"
346+
347+
is_html = headers_['Content-Type'] == 'text/html'
348+
349+
response['links'] = [{
350+
'rel': 'self' if not is_html else 'alternate',
351+
'type': 'application/json',
352+
'title': 'This document as JSON',
353+
'href': f"{url_base}?f=json",
354+
'hreflang': self.config['server']['language']
355+
}, {
356+
'rel': 'self' if is_html else 'alternate',
357+
'type': 'text/html',
358+
'title': 'This document as HTML',
359+
'href': f"{url_base}?f=html",
360+
'hreflang': self.config['server']['language']
361+
}]
347362

348363
return self.get_response(200, headers_, template, response)
349364

@@ -634,7 +649,7 @@ def record2json(record):
634649
record_dict['properties']['formats'] = [record.format]
635650

636651
if record.keywords:
637-
record_dict['keywords'] = [x for x in record.keywords.split(',')]
652+
record_dict['properties']['keywords'] = [x for x in record.keywords.split(',')]
638653

639654
if record.links:
640655
record_dict['associations'] = []

tests/unittests/oarec-default.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
[server]
3131
home=/var/www/pycsw
32-
url=http://localhost/pycsw/csw.py
32+
url=http://localhost/pycsw/oarec
3333
mimetype=application/xml; charset=UTF-8
3434
encoding=UTF-8
3535
language=en-US

tests/unittests/test_oarec.py

+116-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import os
33

4+
import pytest
5+
46
from pycsw.core.util import parse_ini_config
57
from pycsw.ogc.api.records import API
68

@@ -14,15 +16,125 @@ def get_test_file_path(filename):
1416
return f'tests/unittests/{filename}'
1517

1618

17-
def test_landing_page():
19+
@pytest.fixture()
20+
def config():
21+
return parse_ini_config(get_test_file_path('oarec-default.cfg'))
22+
1823

19-
api_ = API(parse_ini_config(get_test_file_path('oarec-default.cfg')))
24+
@pytest.fixture()
25+
def api(config):
26+
return API(config)
2027

21-
headers, status, content = api_.landing_page({}, {'f': 'json'})
28+
29+
def test_landing_page(api):
30+
headers, status, content = api.landing_page({}, {'f': 'json'})
2231
content = json.loads(content)
2332

2433
assert headers['Content-Type'] == 'application/json'
2534
assert status == 200
2635
assert len(content['links']) == 10
2736

28-
headers, status, content = api_.landing_page({}, {'f': 'json'})
37+
for link in content['links']:
38+
assert link['href'].startswith(api.config['server']['url'])
39+
40+
headers, status, content = api.landing_page({}, {'f': 'html'})
41+
assert status == 200
42+
assert headers['Content-Type'] == 'text/html'
43+
44+
45+
def test_conformance(api):
46+
content = json.loads(api.conformance({}, {})[2])
47+
48+
assert len(content['conformsTo']) == 6
49+
50+
51+
def test_collections(api):
52+
content = json.loads(api.collections({}, {})[2])
53+
54+
assert len(content['links']) == 2
55+
assert len(content['collections']) == 1
56+
57+
content = json.loads(api.collections({}, {}, True)[2])
58+
assert len(content['links']) == 2
59+
assert content['id'] == 'metadata:main'
60+
assert content['title'] == 'pycsw Geospatial Catalogue'
61+
assert content['description'] == 'pycsw is an OGC CSW server implementation written in Python' # noqa
62+
assert content['itemType'] == 'record'
63+
64+
65+
def test_queryables(api):
66+
content = json.loads(api.queryables({}, {})[2])
67+
68+
assert content['type'] == 'object'
69+
assert content['title'] == 'pycsw Geospatial Catalogue'
70+
assert content['$id'] == 'http://localhost/pycsw/oarec/collections/metadata:main/queryables' # noqa
71+
assert content['$schema'] == 'http://json-schema.org/draft/2019-09/schema'
72+
73+
assert len(content['properties']) == 59
74+
75+
76+
def test_items(api):
77+
content = json.loads(api.items({}, {})[2])
78+
79+
assert content['type'] == 'FeatureCollection'
80+
assert len(content['links']) == 4
81+
assert content['numberMatched'] == 12
82+
assert content['numberReturned'] == 10
83+
assert len(content['features']) == 10
84+
assert len(content['features']) == content['numberReturned']
85+
86+
params = {'q': 'Lorem'}
87+
content = json.loads(api.items({}, params)[2])
88+
assert content['numberMatched'] == 5
89+
assert content['numberReturned'] == 5
90+
assert len(content['features']) == content['numberReturned']
91+
92+
params = {'bbox': '-50,0,50,80'}
93+
content = json.loads(api.items({}, params)[2])
94+
assert content['numberMatched'] == 3
95+
assert content['numberReturned'] == 3
96+
assert len(content['features']) == content['numberReturned']
97+
98+
params = {'bbox': '-50,0,50,80', 'q': 'Lorem'}
99+
content = json.loads(api.items({}, params)[2])
100+
assert content['numberMatched'] == 1
101+
assert content['numberReturned'] == 1
102+
assert len(content['features']) == content['numberReturned']
103+
104+
params = {'filter': 'title LIKE "%%Lorem%%"'}
105+
content = json.loads(api.items({}, params)[2])
106+
assert content['numberMatched'] == 2
107+
assert content['numberReturned'] == 2
108+
assert len(content['features']) == content['numberReturned']
109+
110+
params = {'filter': 'title LIKE "%%Lorem%%"', 'q': 'iPsUm'}
111+
content = json.loads(api.items({}, params)[2])
112+
assert content['numberMatched'] == 2
113+
assert content['numberReturned'] == 2
114+
assert len(content['features']) == content['numberReturned']
115+
116+
params = {'limit': 4}
117+
content = json.loads(api.items({}, params)[2])
118+
assert content['numberMatched'] == 12
119+
assert content['numberReturned'] == 4
120+
assert len(content['features']) == content['numberReturned']
121+
122+
params = {'limit': 4, 'startindex': 10}
123+
content = json.loads(api.items({}, params)[2])
124+
assert content['numberMatched'] == 12
125+
assert content['numberReturned'] == 2
126+
assert len(content['features']) == content['numberReturned']
127+
128+
129+
def test_item(api):
130+
item = 'urn:uuid:19887a8a-f6b0-4a63-ae56-7fba0e17801f'
131+
content = json.loads(api.item({}, {}, item)[2])
132+
133+
assert content['id'] == item
134+
assert content['type'] == 'Feature'
135+
assert content['properties']['title'] == 'Lorem ipsum'
136+
assert content['properties']['keywords'] == ['Tourism--Greece']
137+
138+
item = 'urn:uuid:19887a8a-f6b0-4a63-ae56-7fba0e17801f'
139+
params = {'f': 'xml'}
140+
content = api.item({}, params, item)[2]

0 commit comments

Comments
 (0)