Skip to content

Commit

Permalink
update to use jsonld after playground discussion; refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Sep 30, 2024
1 parent 5a6b8b1 commit 92a3bbe
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 143 deletions.
31 changes: 20 additions & 11 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@
import json
from jinja2 import Environment, FileSystemLoader

template_dir = 'templates'
"""Test runner to go through each template and try to template each associated json file"""


def to_json(dict_: dict, pretty: bool = False) -> str:
"""Mimics the to_json filter in pygeoapi"""
return json.dumps(dict_, indent=4 if pretty else None, separators=(",", ":"))


template_dir = "templates"
env = Environment(loader=FileSystemLoader(template_dir))
env.filters["to_json"] = to_json


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

with open(json_path) as json_file:
data = json.load(json_file)
# pygeoapi templates use data as the key where all the properties are stored
# so we just rename the key
data["data"] = data["properties"]
data = {"data": json.load(json_file)}

result = template.render(data)

return result

# For every template, try to apply it to every corresponding json file
for template_filename in os.listdir(template_dir):
if template_filename.endswith('.j2'):
template_name = template_filename.replace('.j2', '')
test_dir = os.path.abspath(os.path.join('tests', template_name))
if template_filename.endswith(".j2"):
template_name = template_filename.replace(".j2", "")

test_dir = os.path.abspath(os.path.join("tests", template_name))

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

for file_name in os.listdir(test_dir):
if file_name.endswith('.json'):
if file_name.endswith(".json"):
json_path = os.path.join(test_dir, file_name)
template_path = template_filename

result = apply_template_to_json(template_path, json_path)

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"@context":[
{
"schema":"https://schema.org/",
"type":"@type",
"gsp":"http://www.opengis.net/ont/geosparql#"
}
],
"type":"schema:Place",
"id":"'AR008-331856091114601'",
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')",
"name":"AR008-331856091114601",
"description":"Well",
"Locations":[
{
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Locations('8416841e-2d1b-11ec-ac1b-a32405a27bb1')",
"@iot.id":"8416841e-2d1b-11ec-ac1b-a32405a27bb1",
"name":"AR008-331856091114601",
"description":"Well",
"encodingType":"application/vnd.geo+json",
"location":{
"type":"Point",
"coordinates":[
-91.1969333333333,
33.3169611111111
]
}
}
],
"Datastreams":[
{
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Datastreams('177000bd9f6c45e5909c1729e4aab7b0')",
"@iot.id":"177000bd9f6c45e5909c1729e4aab7b0",
"name":"177000bd9f6c45e5909c1729e4aab7b0",
"description":"Flow rate, well / AR008-331856091114601-177000bd9f6c45e5909c1729e4aab7b0",
"observationType":"Instantaneous",
"unitOfMeasurement":{
"name":"UNKNOWN",
"symbol":"US Gal/min",
"definition":""
},
"observedArea":{
"type":"Point",
"coordinates":[
-91.1969333,
33.3169611
]
},
"phenomenonTime":"2024-09-20T09:15:00Z/2024-09-30T16:00:00Z",
"properties":{
"Thresholds":[
{
"Name":"Operational limit (minimum)",
"Type":"ThresholdBelow",
"Periods":[
{
"EndTime":"9999-12-31T23:59:59.9999999+00:00",
"StartTime":"0001-01-01T00:00:00.0000000+00:00",
"SuppressData":true,
"ReferenceValue":-0.01,
"ReferenceValueToTriggerDisplay":null
}
],
"ReferenceCode":"Operational limit - low-Public"
},
{
"Name":"Operational limit (maximum)",
"Type":"ThresholdAbove",
"Periods":[
{
"EndTime":"9999-12-31T23:59:59.9999999+00:00",
"StartTime":"0001-01-01T00:00:00.0000000+00:00",
"SuppressData":true,
"ReferenceValue":20000,
"ReferenceValueToTriggerDisplay":15000
}
],
"ReferenceCode":"Operational limit - high-Public"
}
],
"ParameterCode":"00058",
"StatisticCode":"00011"
}
},
{
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Datastreams('9aa77f35cea344a4a0e5715e05d057c3')",
"@iot.id":"9aa77f35cea344a4a0e5715e05d057c3",
"name":"9aa77f35cea344a4a0e5715e05d057c3",
"description":"Precipitation, total, inches / AR008-331856091114601-9aa77f35cea344a4a0e5715e05d057c3",
"observationType":"Instantaneous",
"unitOfMeasurement":{
"name":"in",
"symbol":"in",
"definition":""
},
"observedArea":{
"type":"Point",
"coordinates":[
-91.1969333,
33.3169611
]
},
"phenomenonTime":"2024-09-20T09:15:00Z/2024-09-30T16:00:00Z",
"properties":{
"ParameterCode":"00045"
}
}
],
"Locations@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')/Locations",
"HistoricalLocations@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')/HistoricalLocations",
"Datastreams@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('AR008-331856091114601')/Datastreams",
"state":"Arkansas",
"county":"Chicot County",
"country":"United States of America",
"mapScale":24000,
"stateCode":"AR",
"wellDepth":"80.0",
"agencyCode":"AR008",
"countryFIPS":"US",
"hydrologicUnit":"08050002",
"decimalLatitude":33.3169611111111,
"decimalLongitude":-91.1969333333333,
"monitoringLocationUrl":"https://waterdata.usgs.gov/monitoring-location/331856091114601",
"monitoringLocationName":"16S01W10CC1 CH-32 WU",
"monitoringLocationType":"Well",
"monitoringLocationNumber":"331856091114601",
"locationHUCTwelveDigitCode":"080500020302",
"decimalLatitudeStandardized":33.3169611111111,
"decimalLongitudeStandardized":-91.1969333333333,
"geometry":{
"type":"Point",
"coordinates":[
-91.1969333333333,
33.3169611111111
]
},
"gsp:hasGeometry":{
"@type":"http://www.opengis.net/ont/sf#Point",
"gsp:asWKT":{
"@type":"http://www.opengis.net/ont/geosparql#wktLiteral",
"@value":"POINT (-91.1969333333333 33.3169611111111)"
}
},
"schema:geo":{
"@type":"schema:GeoCoordinates",
"schema:longitude":-91.1969333333333,
"schema:latitude":33.3169611111111
},
"@id":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'",
"links":[
{
"type":"application/json",
"rel":"root",
"title":"The landing page of this server as JSON",
"href":"https://api.iodh.internetofwater.app?f=json"
},
{
"type":"text/html",
"rel":"root",
"title":"The landing page of this server as HTML",
"href":"https://api.iodh.internetofwater.app?f=html"
},
{
"rel":"alternate",
"type":"application/geo+json",
"title":"This document as JSON",
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'?f=json"
},
{
"rel":"self",
"type":"application/ld+json",
"title":"This document as RDF (JSON-LD)",
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'?f=jsonld"
},
{
"rel":"alternate",
"type":"text/html",
"title":"This document as HTML",
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta/items/'AR008-331856091114601'?f=html"
},
{
"rel":"collection",
"type":"application/json",
"title":"United States Geologic Survey SensorThings API",
"href":"https://api.iodh.internetofwater.app/collections/usgs-sta"
}
]
}

0 comments on commit 92a3bbe

Please sign in to comment.