Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
geomag-algorithms
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ghsc
National Geomagnetism Program
geomag-algorithms
Commits
a83a0e36
Commit
a83a0e36
authored
3 years ago
by
Cain, Payton David
Browse files
Options
Downloads
Plain Diff
Merge branch 'no-extra-params' into 'master'
No extra params Closes
#58
See merge request
!143
parents
a9db65ee
591ac0ab
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!166
Merge branch master into production
,
!143
No extra params
Pipeline
#82516
passed
3 years ago
Stage: test
Stage: integration
Stage: scan
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
geomagio/api/ws/data.py
+18
-1
18 additions, 1 deletion
geomagio/api/ws/data.py
test/api_test/ws_test/data_test.py
+40
-12
40 additions, 12 deletions
test/api_test/ws_test/data_test.py
with
58 additions
and
13 deletions
geomagio/api/ws/data.py
+
18
−
1
View file @
a83a0e36
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
,
...
...
This diff is collapsed.
Click to expand it.
test/api_test/ws_test/data_test.py
+
40
−
12
View file @
a83a0e36
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
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment