Skip to content

Commit 0c2c1f2

Browse files
authored
Merge pull request #8 from cgs-earth/jsonld
Update to use jsonld in tests
2 parents 5a6b8b1 + 92a3bbe commit 0c2c1f2

3 files changed

+208
-143
lines changed

tests/runner.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,48 @@
22
import json
33
from jinja2 import Environment, FileSystemLoader
44

5-
template_dir = 'templates'
5+
"""Test runner to go through each template and try to template each associated json file"""
6+
7+
8+
def to_json(dict_: dict, pretty: bool = False) -> str:
9+
"""Mimics the to_json filter in pygeoapi"""
10+
return json.dumps(dict_, indent=4 if pretty else None, separators=(",", ":"))
11+
12+
13+
template_dir = "templates"
614
env = Environment(loader=FileSystemLoader(template_dir))
15+
env.filters["to_json"] = to_json
16+
717

818
def apply_template_to_json(template_path, json_path):
919
template = env.get_template(template_path)
1020

1121
with open(json_path) as json_file:
12-
data = json.load(json_file)
13-
# pygeoapi templates use data as the key where all the properties are stored
14-
# so we just rename the key
15-
data["data"] = data["properties"]
22+
data = {"data": json.load(json_file)}
1623

1724
result = template.render(data)
1825

1926
return result
2027

28+
# For every template, try to apply it to every corresponding json file
2129
for template_filename in os.listdir(template_dir):
22-
if template_filename.endswith('.j2'):
23-
template_name = template_filename.replace('.j2', '')
24-
25-
test_dir = os.path.abspath(os.path.join('tests', template_name))
30+
if template_filename.endswith(".j2"):
31+
template_name = template_filename.replace(".j2", "")
32+
33+
test_dir = os.path.abspath(os.path.join("tests", template_name))
2634

2735
if not os.path.isdir(test_dir):
2836
print(f"No test directory found for template {template_name}")
37+
print("-" * 40)
2938
continue
3039

3140
for file_name in os.listdir(test_dir):
32-
if file_name.endswith('.json'):
41+
if file_name.endswith(".json"):
3342
json_path = os.path.join(test_dir, file_name)
3443
template_path = template_filename
3544

3645
result = apply_template_to_json(template_path, json_path)
3746

3847
print(f"Result for {file_name} using {template_filename}:")
3948
print(result)
40-
print("-" * 40)
49+
print("Successfully ran all tests!")

tests/usgs-location-oriented/collections-usgs-things-items-'USCE-07335390'.json

-132
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
{
2+
"@context":[
3+
{
4+
"schema":"https://schema.org/",
5+
"type":"@type",
6+
"gsp":"http://www.opengis.net/ont/geosparql#"
7+
}
8+
],
9+
"type":"schema:Place",
10+
"id":"'AR008-331856091114601'",
11+
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')",
12+
"name":"AR008-331856091114601",
13+
"description":"Well",
14+
"Locations":[
15+
{
16+
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Locations('8416841e-2d1b-11ec-ac1b-a32405a27bb1')",
17+
"@iot.id":"8416841e-2d1b-11ec-ac1b-a32405a27bb1",
18+
"name":"AR008-331856091114601",
19+
"description":"Well",
20+
"encodingType":"application/vnd.geo+json",
21+
"location":{
22+
"type":"Point",
23+
"coordinates":[
24+
-91.1969333333333,
25+
33.3169611111111
26+
]
27+
}
28+
}
29+
],
30+
"Datastreams":[
31+
{
32+
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Datastreams('177000bd9f6c45e5909c1729e4aab7b0')",
33+
"@iot.id":"177000bd9f6c45e5909c1729e4aab7b0",
34+
"name":"177000bd9f6c45e5909c1729e4aab7b0",
35+
"description":"Flow rate, well / AR008-331856091114601-177000bd9f6c45e5909c1729e4aab7b0",
36+
"observationType":"Instantaneous",
37+
"unitOfMeasurement":{
38+
"name":"UNKNOWN",
39+
"symbol":"US Gal/min",
40+
"definition":""
41+
},
42+
"observedArea":{
43+
"type":"Point",
44+
"coordinates":[
45+
-91.1969333,
46+
33.3169611
47+
]
48+
},
49+
"phenomenonTime":"2024-09-20T09:15:00Z/2024-09-30T16:00:00Z",
50+
"properties":{
51+
"Thresholds":[
52+
{
53+
"Name":"Operational limit (minimum)",
54+
"Type":"ThresholdBelow",
55+
"Periods":[
56+
{
57+
"EndTime":"9999-12-31T23:59:59.9999999+00:00",
58+
"StartTime":"0001-01-01T00:00:00.0000000+00:00",
59+
"SuppressData":true,
60+
"ReferenceValue":-0.01,
61+
"ReferenceValueToTriggerDisplay":null
62+
}
63+
],
64+
"ReferenceCode":"Operational limit - low-Public"
65+
},
66+
{
67+
"Name":"Operational limit (maximum)",
68+
"Type":"ThresholdAbove",
69+
"Periods":[
70+
{
71+
"EndTime":"9999-12-31T23:59:59.9999999+00:00",
72+
"StartTime":"0001-01-01T00:00:00.0000000+00:00",
73+
"SuppressData":true,
74+
"ReferenceValue":20000,
75+
"ReferenceValueToTriggerDisplay":15000
76+
}
77+
],
78+
"ReferenceCode":"Operational limit - high-Public"
79+
}
80+
],
81+
"ParameterCode":"00058",
82+
"StatisticCode":"00011"
83+
}
84+
},
85+
{
86+
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Datastreams('9aa77f35cea344a4a0e5715e05d057c3')",
87+
"@iot.id":"9aa77f35cea344a4a0e5715e05d057c3",
88+
"name":"9aa77f35cea344a4a0e5715e05d057c3",
89+
"description":"Precipitation, total, inches / AR008-331856091114601-9aa77f35cea344a4a0e5715e05d057c3",
90+
"observationType":"Instantaneous",
91+
"unitOfMeasurement":{
92+
"name":"in",
93+
"symbol":"in",
94+
"definition":""
95+
},
96+
"observedArea":{
97+
"type":"Point",
98+
"coordinates":[
99+
-91.1969333,
100+
33.3169611
101+
]
102+
},
103+
"phenomenonTime":"2024-09-20T09:15:00Z/2024-09-30T16:00:00Z",
104+
"properties":{
105+
"ParameterCode":"00045"
106+
}
107+
}
108+
],
109+
"Locations@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')/Locations",
110+
"HistoricalLocations@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')/HistoricalLocations",
111+
"Datastreams@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')/Datastreams",
112+
"state":"Arkansas",
113+
"county":"Chicot County",
114+
"country":"United States of America",
115+
"mapScale":24000,
116+
"stateCode":"AR",
117+
"wellDepth":"80.0",
118+
"agencyCode":"AR008",
119+
"countryFIPS":"US",
120+
"hydrologicUnit":"08050002",
121+
"decimalLatitude":33.3169611111111,
122+
"decimalLongitude":-91.1969333333333,
123+
"monitoringLocationUrl":"https://waterdata.usgs.gov/monitoring-location/331856091114601",
124+
"monitoringLocationName":"16S01W10CC1 CH-32 WU",
125+
"monitoringLocationType":"Well",
126+
"monitoringLocationNumber":"331856091114601",
127+
"locationHUCTwelveDigitCode":"080500020302",
128+
"decimalLatitudeStandardized":33.3169611111111,
129+
"decimalLongitudeStandardized":-91.1969333333333,
130+
"geometry":{
131+
"type":"Point",
132+
"coordinates":[
133+
-91.1969333333333,
134+
33.3169611111111
135+
]
136+
},
137+
"gsp:hasGeometry":{
138+
"@type":"http://www.opengis.net/ont/sf#Point",
139+
"gsp:asWKT":{
140+
"@type":"http://www.opengis.net/ont/geosparql#wktLiteral",
141+
"@value":"POINT (-91.1969333333333 33.3169611111111)"
142+
}
143+
},
144+
"schema:geo":{
145+
"@type":"schema:GeoCoordinates",
146+
"schema:longitude":-91.1969333333333,
147+
"schema:latitude":33.3169611111111
148+
},
149+
"@id":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'",
150+
"links":[
151+
{
152+
"type":"application/json",
153+
"rel":"root",
154+
"title":"The landing page of this server as JSON",
155+
"href":"https://api.iodh.internetofwater.app?f=json"
156+
},
157+
{
158+
"type":"text/html",
159+
"rel":"root",
160+
"title":"The landing page of this server as HTML",
161+
"href":"https://api.iodh.internetofwater.app?f=html"
162+
},
163+
{
164+
"rel":"alternate",
165+
"type":"application/geo+json",
166+
"title":"This document as JSON",
167+
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'?f=json"
168+
},
169+
{
170+
"rel":"self",
171+
"type":"application/ld+json",
172+
"title":"This document as RDF (JSON-LD)",
173+
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'?f=jsonld"
174+
},
175+
{
176+
"rel":"alternate",
177+
"type":"text/html",
178+
"title":"This document as HTML",
179+
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'?f=html"
180+
},
181+
{
182+
"rel":"collection",
183+
"type":"application/json",
184+
"title":"United States Geologic Survey SensorThings API",
185+
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta"
186+
}
187+
]
188+
}

0 commit comments

Comments
 (0)