Skip to content

Commit

Permalink
Add Test for Mocking SOAR Server Downtime (#135)
Browse files Browse the repository at this point in the history
* Add Tests for Mocking SOAR Server Downtime

* pytest xfail removed

* catching json error in soarclient

* Apply suggestions from code review

Co-authored-by: Nabil Freij <[email protected]>

* apply suggestions

* Update sunpy_soar/client.py

---------

Co-authored-by: Nabil Freij <[email protected]>
  • Loading branch information
NucleonGodX and nabobalis authored Jul 19, 2024
1 parent ba3c90d commit 9575963
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tests = [
"pytest-doctestplus",
"pytest-xdist",
"pytest",
"responses>=0.20.0",
"sunpy[map,net]>=5.0",
]
docs = [
Expand Down
10 changes: 8 additions & 2 deletions sunpy_soar/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import pathlib
import re
from json.decoder import JSONDecodeError

import astropy.table
import astropy.units as u
Expand Down Expand Up @@ -170,12 +171,17 @@ def _do_search(query):
r = requests.get(f"{tap_endpoint}/sync", params=payload)
log.debug(f"Sent query: {r.url}")
r.raise_for_status()
try:
response_json = r.json()
except JSONDecodeError as err:
msg = "The SOAR server returned an invalid JSON response. It may be down or not functioning correctly."
raise RuntimeError(msg) from err

# Do some list/dict wrangling
names = [m["name"] for m in r.json()["metadata"]]
names = [m["name"] for m in response_json["metadata"]]
info = {name: [] for name in names}

for entry in r.json()["data"]:
for entry in response_json["data"]:
for i, name in enumerate(names):
info[name].append(entry[i])

Expand Down
24 changes: 24 additions & 0 deletions sunpy_soar/tests/test_sunpy_soar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pathlib import Path

import astropy.units as u
import pytest
import responses
import sunpy.map
from sunpy.net import Fido
from sunpy.net import attrs as a
Expand Down Expand Up @@ -268,3 +270,25 @@ def test_join_low_latency_query():
"+WHERE+h1.instrument='EUI'+AND+h1.begin_time>='2021-02-01+00:00:00'+AND+h1.begin_time<='2021-02-02+00:00:00'"
"+AND+h2.dimension_index='1'+AND+h1.level='LL01'+AND+h1.descriptor='eui-fsi174-image'"
)


@responses.activate
def test_soar_server_down():
# As the SOAR server is expected to be down in this test, a JSONDecodeError is expected
# to be raised due to the absence of a valid JSON response.
TAP_ENDPOINT = (
"http://soar.esac.esa.int/soar-sl-tap/tap/sync?REQUEST=doQuery&LANG=ADQL&FORMAT=json&QUERY=SELECT+*+FROM+v_ll_data_item+"
"WHERE+begin_time%3E='2020-11-13+00:00:00'+AND+begin_time%3C='2020-11-14+00:00:00'+AND+level='LL02'+AND+descriptor='mag'"
)
# We do not give any json data similar to the condition when the server is down.
responses.add(responses.GET, TAP_ENDPOINT, body="Invalid JSON response", status=200)

time = a.Time("2020-11-13", "2020-11-14")
level = a.Level("LL02")
product = a.soar.Product("mag")

with pytest.raises(
RuntimeError,
match=("The SOAR server returned an invalid JSON response. " "It may be down or not functioning correctly."),
):
Fido.search(time, level, product)

0 comments on commit 9575963

Please sign in to comment.