Skip to content
Snippets Groups Projects
Commit a83a0e36 authored by Cain, Payton David's avatar Cain, Payton David
Browse files

Merge branch 'no-extra-params' into 'master'

No extra params

Closes #58

See merge request !143
parents a9db65ee 591ac0ab
No related branches found
No related tags found
2 merge requests!166Merge branch master into production,!143No extra params
Pipeline #82516 passed
import os import os
from typing import List, Union from typing import List, Union
from fastapi import APIRouter, Depends, Query from fastapi import APIRouter, Depends, Query, Request
from obspy import UTCDateTime, Stream from obspy import UTCDateTime, Stream
from starlette.responses import Response from starlette.responses import Response
...@@ -48,6 +48,7 @@ def get_data_factory( ...@@ -48,6 +48,7 @@ def get_data_factory(
def get_data_query( def get_data_query(
request: Request,
id: str = Query(..., title="Observatory code"), id: str = Query(..., title="Observatory code"),
starttime: UTCDateTime = Query( starttime: UTCDateTime = Query(
None, None,
...@@ -102,6 +103,22 @@ def get_data_query( ...@@ -102,6 +103,22 @@ def get_data_query(
format format
output format output format
""" """
default_params = [
"id",
"starttime",
"endtime",
"elements",
"sampling_period",
"type",
"format",
]
invalid_params = []
for param in request.query_params.keys():
if param not in default_params:
invalid_params.append(param)
if len(invalid_params) > 0:
msg = ", ".join(invalid_params)
raise ValueError(f"Invalid query parameter(s): {msg}")
# parse query # parse query
query = DataApiQuery( query = DataApiQuery(
id=id, id=id,
......
from fastapi import Depends
from fastapi.testclient import TestClient
from numpy.testing import assert_equal from numpy.testing import assert_equal
from obspy import UTCDateTime from obspy import UTCDateTime
import pytest
from geomagio.api.ws import app
from geomagio.api.ws.data import get_data_query from geomagio.api.ws.data import get_data_query
from geomagio.api.ws.DataApiQuery import OutputFormat, SamplingPeriod from geomagio.api.ws.DataApiQuery import DataApiQuery, OutputFormat, SamplingPeriod
def test_get_data_query(): @pytest.fixture(scope="module")
query = get_data_query( def test_client():
id="BOU", @app.get("/query/", response_model=DataApiQuery)
starttime="2020-09-01T00:00:01", def get_query(query: DataApiQuery = Depends(get_data_query)):
endtime=None, return query
elements=["X,Y,Z,F"],
data_type="R1", client = TestClient(app)
sampling_period=60, yield client
format="iaga2002",
def test_get_data_query(test_client):
"""test.api_test.ws_test.data_test.test_get_data_query()"""
response = test_client.get(
"/query/?id=BOU&starttime=2020-09-01T00:00:01&elements=X,Y,Z,F&type=R1&sampling_period=60&format=iaga2002"
) )
query = DataApiQuery(**response.json())
assert_equal(query.id, "BOU") assert_equal(query.id, "BOU")
assert_equal(query.starttime, UTCDateTime("2020-09-01T00:00:01")) assert_equal(query.starttime, UTCDateTime("2020-09-01T00:00:01"))
assert_equal(query.endtime, UTCDateTime("2020-09-02T00:00:00.999")) assert_equal(query.endtime, UTCDateTime("2020-09-02T00:00:00.999"))
...@@ -22,3 +32,21 @@ def test_get_data_query(): ...@@ -22,3 +32,21 @@ def test_get_data_query():
assert_equal(query.sampling_period, SamplingPeriod.MINUTE) assert_equal(query.sampling_period, SamplingPeriod.MINUTE)
assert_equal(query.format, OutputFormat.IAGA2002) assert_equal(query.format, OutputFormat.IAGA2002)
assert_equal(query.data_type, "R1") assert_equal(query.data_type, "R1")
def test_get_data_query_extra_params(test_client):
"""test.api_test.ws_test.data_test.test_get_data_query_extra_params()"""
with pytest.raises(ValueError) as error:
test_client.get(
"/query/?id=BOU&starttime=2020-09-01T00:00:01&elements=X,Y,Z,F&type=variation&sampling_period=60&format=iaga2002&location=R1&network=NT"
)
assert error.message == "Invalid query parameter(s): location, network"
def test_get_data_query_bad_params(test_client):
"""test.api_test.ws_test.data_test.test_get_data_query_bad_params()"""
with pytest.raises(ValueError) as error:
test_client.get(
"/query/?id=BOU&startime=2020-09-01T00:00:01&elements=X,Y,Z,F&data_type=variation&sampling_period=60&format=iaga2002"
)
assert error.message == "Invalid query parameter(s): startime, data_type"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment