Commit 4c8345e1 authored by Derek Homeier's avatar Derek Homeier
Browse files

Tests for all sdc Attribute queries

parent 3a6efd3f
......@@ -352,6 +352,8 @@ class Theta(_attr.Range):
thetamax : `u.Quantity`
The maximum value of the surface normal angle to search between.
"""
type_name = "theta" # `attr.Range` has no default `type_name`.?
def __init__(self, thetamin: u.deg, thetamax: u.deg):
super().__init__(thetamin, thetamax)
self.min = thetamin.to_value(u.deg)
......@@ -374,6 +376,8 @@ class Mu(_attr.Range):
mumax : float
The maximum value of the surface normal angle to search between.
"""
type_name = "mu"
def __init__(self, mumin, mumax):
super().__init__(mumin, mumax)
......
......@@ -105,8 +105,8 @@ def _update_range(dictionary, key, values, block='description'):
query = (f"{{'{block}.{key}MIN':{{'$le':{{{_str_val(values[1])}}}}}}},"
f"{{'{block}.{key}MAX':{{'$ge':{{{_str_val(values[0])}}}}}}}")
else:
query = (f"{{'{block}.{key}':{{'$ge':{{'{_str_val(values[0])}'}},"
f"'$le':{{'{_str_val(values[1])}'}}}}}}")
query = (f"{{'{block}.{key}':{{'$ge':{{{_str_val(values[0])}}},"
f"'$le':{{{_str_val(values[1])}}}}}}}")
return dictionary.update({key: query})
......
......@@ -3,6 +3,7 @@ from urllib.error import HTTPError, URLError
import pytest
import astropy.units as u
from sunpy.net import attrs as a
import client as kisclient
......@@ -25,22 +26,89 @@ def client():
def test_search(client):
"""Test conversion of Attrs to query string."""
assert not client._can_handle_query(a.Time("2019/01/01", "2021/01/01"))
with pytest.raises(AttributeError, match=r"Query not possible: "
r"No 'Instrument' found in Attributes"):
client.search(a.Time("2019/01/01", "2021/01/01"))
assert not client._can_handle_query(a.Instrument("UVES"), a.Time("2019/01/01", "2021/01/01"))
with pytest.raises(AttributeError, match=r"Query not possible: "
r"Instrument UVES not in registered list"):
client.search(a.Instrument("UVES") & a.Time("2019/01/01", "2021/01/01"))
query = a.Instrument("BBI") & a.Time("2019/01/01", "2021/01/01")
# TODO: Verify the actual return from the RESTHeart server.
assert client._can_handle_query(query)
if HAS_DOCKERTEST:
res = client.search(a.Time("2019/01/01", "2021/01/01") & a.Instrument("GRIS"))
res = client.search(query)
else:
with pytest.raises(URLError, match=r"Unable to execute search "
r".http://dockertest:8083/sdc/gris_observations.filter="
r"{'.and':.{'description.INSTRUMENT':'gris'},"
r"{'description.DATE.BEG':{'.le':{'2021-01-01T00:00:00.000'}}},"
r".http://dockertest:8083/sdc/bbi_observations.filter="
r"{'.and':.{'description.INSTRUMENT':'bbi'},"
r"{'description.DATE_BEG':{'.le':{'2021-01-01T00:00:00.000'}}},"
r"{'description.DATE_END':{'.ge':{'2019-01-01T00:00:00.000'}}}.}"
rf".*Confirm that RESTHeart is running on {_BASE_URL} and connected"):
client.search(a.Instrument("GRIS") & a.Time("2019/01/01", "2021/01/01"))
client.search(query)
query = a.Instrument("LARS") & a.sdc.HelioProjLat(-10*u.arcsec, 0.2*u.arcmin)
assert client._can_handle_query(query)
if HAS_DOCKERTEST:
res = client.search(query)
else:
with pytest.raises(URLError, match=r"Unable to execute search "
r".http://dockertest:8083/sdc/lars_observations.filter="
r"{'.and':.{'description.INSTRUMENT':'lars'},"
r"{'description.HPLT_TAN_MIN':{'.le':{12\}}},"
r"{'description.HPLT_TAN_MAX':{'.ge':{-10\}}}"):
client.search(query)
query = a.Instrument("Zimpol") & (a.sdc.Theta(85*u.deg, 600*u.arcmin) | a.sdc.PolStates('iq'))
assert client._can_handle_query(query)
if HAS_DOCKERTEST:
res = client.search(query)
else:
with pytest.raises(URLError, match=r"Unable to execute search .http://dockertest") as exc:
client.search(query)
# Will raise on first of multi-part OR queries.
assert "8083/sdc/zimpol_observations?filter=" in str(exc.value)
assert "{'description.THETA':{'$ge':{10},'$le':{85}}}" in str(exc.value)
assert "{'description.POL_STATES':" not in str(exc.value)
@pytest.mark.parametrize("query", ((a.Instrument("GRIS") & a.Level(3)),
(a.Instrument("ChroTel") & a.Physobs("perspective.vortex")),
(a.Level(0) & a.Instrument("Bob")),
(a.Instrument("LARS") & a.sdc.Telescope("Leviathan"))))
def test_cant_handle_query(client, query):
"""Some examples of invalid queries with exceptions."""
assert not client._can_handle_query(*query.attrs)
with pytest.raises(AttributeError, match=r"Query not possible: "
rf"[ILPT][a-z]* {query.attrs[1].value} not in [rs]"):
client.search(query)
@pytest.mark.parametrize("query", (a.Level(1), a.Wavelength(3200*u.AA, 1.6*u.micron),
a.sdc.DataProduct('cube'), a.sdc.ObsName('gris_211031_12'),
a.sdc.Date('2021/10/31'), a.sdc.Filter('I'),
a.sdc.PolStates('IQUV'), a.sdc.Telescope('Gregor'),
a.sdc.Target('Sunspot_22'), a.sdc.AtmosR0(*([10, 200]*u.cm)),
a.sdc.Theta(20*u.arcmin, 89*u.deg), a.sdc.Mu(0.1, 1),
a.sdc.ExposureTime(*([5, 60]*u.min)),
a.sdc.HelioProjLon(5*u.arcsec), a.sdc.HelioProjLat(9*u.arcsec),
a.sdc.SpatialResolution(0.1*u.arcsec, 0.8*u.arcsec),
a.sdc.SpectralResolution(600, 20000),
a.sdc.TemporalResolution(2*u.s, 30*u.s),
a.sdc.NDimensions(2, 3), a.sdc.PolXel(2, 4),
a.sdc.SpatialXel1(200, 3000), a.sdc.SpatialXel2(100, 4000),
a.sdc.SpectralXel(320, 4096), a.sdc.TimeXel(60, 86400)))
def test_all_queries(client, query):
"""Test an example of all supported query attributes."""
assert client._can_handle_query(a.Instrument("GRIS"), query)
if HAS_DOCKERTEST:
res = client.search(a.Instrument("GRIS") & query)
else:
with pytest.raises(URLError, match=r"Unable to execute search "
r".http://dockertest:8083/sdc/gris_observations.filter="
r"{'.and':.{'description.INSTRUMENT':'gris'},"
rf"{{'description.*{query.type_name.upper()}"):
client.search(a.Instrument("GRIS") & query)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment