-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce hard coding and make dev experience better (#5)
* Reduce hard coding and make dev experience better * change test dir name * few edits to comments * update comment in volume
- Loading branch information
Showing
5 changed files
with
243 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Run Jinja2 Tests | ||
|
||
on: [push] | ||
|
||
jobs: | ||
run-tests: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.12' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install jinja2 | ||
- name: Run tests | ||
run: | | ||
python ./tests/runner.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
import json | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
template_dir = 'templates' | ||
env = Environment(loader=FileSystemLoader(template_dir)) | ||
|
||
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"] | ||
|
||
result = template.render(data) | ||
|
||
return result | ||
|
||
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 not os.path.isdir(test_dir): | ||
print(f"No test directory found for template {template_name}") | ||
continue | ||
|
||
for file_name in os.listdir(test_dir): | ||
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) |
132 changes: 132 additions & 0 deletions
132
tests/usgs-location-oriented/collections-usgs-things-items-'USCE-07335390'.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{ | ||
"type":"Feature", | ||
"id":"'USCE-07335390'", | ||
"properties":{ | ||
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('USCE-07335390')", | ||
"name":"USCE-07335390", | ||
"description":"Lake, Reservoir, Impoundment", | ||
"Locations":[ | ||
{ | ||
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Locations('62f14fda-2d2a-11ec-ad43-63929dbd9073')", | ||
"@iot.id":"62f14fda-2d2a-11ec-ad43-63929dbd9073", | ||
"name":"USCE-07335390", | ||
"description":"Lake, Reservoir, Impoundment", | ||
"encodingType":"application/vnd.geo+json", | ||
"location":{ | ||
"type":"Point", | ||
"coordinates":[ | ||
-95.5446813444291, | ||
33.8528821051941 | ||
] | ||
} | ||
} | ||
], | ||
"Datastreams":[ | ||
{ | ||
"@iot.selfLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Datastreams('727586146fff4717b8d772eb32388d85')", | ||
"@iot.id":"727586146fff4717b8d772eb32388d85", | ||
"name":"727586146fff4717b8d772eb32388d85", | ||
"description":"Lake or reservoir water surface elevation above NGVD 1929, feet / USCE-07335390-727586146fff4717b8d772eb32388d85", | ||
"observationType":"Instantaneous", | ||
"unitOfMeasurement":{ | ||
"name":"Feet", | ||
"symbol":"ft", | ||
"definition":"" | ||
}, | ||
"observedArea":{ | ||
"type":"Point", | ||
"coordinates":[ | ||
-95.5446813, | ||
33.8528821 | ||
] | ||
}, | ||
"phenomenonTime":"2024-08-16T09:30:00Z/2024-08-26T15:00:00Z", | ||
"properties":{ | ||
"Thresholds":[ | ||
{ | ||
"Name":"Conservation Pool Elevation", | ||
"Type":"None", | ||
"Periods":[ | ||
{ | ||
"EndTime":"9999-12-31T23:59:59.9999999+00:00", | ||
"StartTime":"0001-01-01T00:00:00.0000000+00:00", | ||
"SuppressData":false, | ||
"ReferenceValue":451, | ||
"ReferenceValueToTriggerDisplay":null | ||
} | ||
], | ||
"ReferenceCode":"NWISWeb ref level - Public" | ||
} | ||
], | ||
"ParameterCode":"62614", | ||
"StatisticCode":"00011" | ||
} | ||
} | ||
], | ||
"Locations@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('USCE-07335390')/Locations", | ||
"HistoricalLocations@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('USCE-07335390')/HistoricalLocations", | ||
"Datastreams@iot.navigationLink":"https://labs.waterdata.usgs.gov/sta/v1.1/Things('USCE-07335390')/Datastreams", | ||
"state":"Texas", | ||
"county":"Lamar County", | ||
"country":"United States of America", | ||
"mapScale":24000, | ||
"stateCode":"TX", | ||
"agencyCode":"USCE", | ||
"countryFIPS":"US", | ||
"hydrologicUnit":"11140101", | ||
"decimalLatitude":33.8528821051941, | ||
"decimalLongitude":-95.5446813444291, | ||
"monitoringLocationUrl":"https://waterdata.usgs.gov/monitoring-location/07335390", | ||
"monitoringLocationName":"USCOE Pat Mayse Lk nr Chicota, TX", | ||
"monitoringLocationType":"Lake, Reservoir, Impoundment", | ||
"monitoringLocationNumber":"07335390", | ||
"locationHUCTwelveDigitCode":"111401010606", | ||
"decimalLatitudeStandardized":33.8528821051941, | ||
"decimalLongitudeStandardized":-95.5446813444291 | ||
}, | ||
"geometry":{ | ||
"type":"Point", | ||
"coordinates":[ | ||
-95.5446813444291, | ||
33.8528821051941 | ||
] | ||
}, | ||
"links":[ | ||
{ | ||
"type":"application/json", | ||
"rel":"root", | ||
"title":"The landing page of this server as JSON", | ||
"href":"http://localhost:5000?f=json" | ||
}, | ||
{ | ||
"type":"text/html", | ||
"rel":"root", | ||
"title":"The landing page of this server as HTML", | ||
"href":"http://localhost:5000?f=html" | ||
}, | ||
{ | ||
"rel":"self", | ||
"type":"application/geo+json", | ||
"title":"This document as JSON", | ||
"href":"http://localhost:5000/collections/USGS/Things/items/'USCE-07335390'?f=json" | ||
}, | ||
{ | ||
"rel":"alternate", | ||
"type":"application/ld+json", | ||
"title":"This document as RDF (JSON-LD)", | ||
"href":"http://localhost:5000/collections/USGS/Things/items/'USCE-07335390'?f=jsonld" | ||
}, | ||
{ | ||
"rel":"alternate", | ||
"type":"text/html", | ||
"title":"This document as HTML", | ||
"href":"http://localhost:5000/collections/USGS/Things/items/'USCE-07335390'?f=html" | ||
}, | ||
{ | ||
"rel":"collection", | ||
"type":"application/json", | ||
"title":"USGS Things", | ||
"href":"http://localhost:5000/collections/USGS/Things" | ||
} | ||
] | ||
} |