Commit 5c5db22a authored by Peter Evans's avatar Peter Evans
Browse files

Test ElementTree responses with '== None' instead of 'not'

- Seems like behaviour of ET changed around Python 3.6-3.9.
- Add test that dictionary has meaningful content.
parent 8f1f4fa4
......@@ -60,15 +60,14 @@ def fetch_fm(root: ET.Element, ns: dict, preferredfmID: str) -> list:
_perror("Oops: no <nodalPlanes> object seen")
break
d: List[dict] = []
d: List[dict] = [dict(), dict()]
for k in range(2):
plane = np.find("ns:nodalPlane" + str(k + 1), ns)
d[k] = dict()
if not plane:
if plane == None:
continue
for child in plane:
found = child.find("ns:value", ns)
if not found:
if found == None:
continue
v = found.text
tag = child.tag
......@@ -88,10 +87,10 @@ def fetch_magnitude(
continue
child = m.find("ns:mag", ns)
if not child:
if child == None:
return None
value = child.find("ns:value", ns)
if not value:
if value == None:
return None
v = value.text
try:
......@@ -197,17 +196,24 @@ def fetch_quakeml(path: str) -> Union[dict, None]:
try:
# e.g. "smi:org.gfz-potsdam.de/geofon/gfz2021ekhv"
evid = event.attrib["publicID"].split("/")[-1]
# print("INFO Event ID from response:", evid)
except AttributeError:
_perror("Oops, couldn't get event id from " + event.attrib["publicID"])
# descr = root[0][0].find("ns:description", ns)
# text = descr.find("ns:text", ns)
# print("INFO Found description:", text.text)
preferredoriginIDElem = root[0][0].find("ns:preferredOriginID", ns)
if not preferredoriginIDElem:
if preferredoriginIDElem == None:
_perror("Oops, couldn't find the preferred origin ID")
return None
preferredoriginID = preferredoriginIDElem.text
# print("DEBUG Found", preferredoriginID)
preferredmagID = None
preferredMagnitudeIDElem = root[0][0].find("ns:preferredMagnitudeID", ns)
if preferredMagnitudeIDElem:
if preferredMagnitudeIDElem != None:
try:
preferredmagID = preferredMagnitudeIDElem.text
except AttributeError:
......@@ -216,7 +222,7 @@ def fetch_quakeml(path: str) -> Union[dict, None]:
preferredfmID = None
try:
preferredfmIDElem = root[0][0].find("ns:preferredFocalMechanismID", ns)
if preferredfmIDElem:
if preferredfmIDElem != None:
preferredfmID = preferredfmIDElem.text
except AttributeError:
pass
......@@ -277,6 +283,8 @@ http://geofon.gfz-potsdam.de/eqinfo/list.php
sys.exit(1)
ev_info = fetch_quakeml(evid)
if ev_info == None:
print("Got no dictionary from QuakeML")
outfile = evid + ".yaml"
with open(outfile, "w") as fid:
fid.write(yaml.safe_dump(ev_info, default_flow_style=False))
......
......@@ -41,6 +41,8 @@ class QuakeMLReadTestCase(unittest.TestCase):
evid = "gfz2021ekhv"
result = fetch_quakeml(evid)
self.assertTrue(isinstance(result, dict))
self.assertGreater(len(result), 3)
self.assertEqual(result["id"], evid)
def test_read_no_eventid(self):
"""
......@@ -51,6 +53,22 @@ class QuakeMLReadTestCase(unittest.TestCase):
with self.assertRaises(IOError):
result = fetch_quakeml(infile)
def test_read_good_eventid2(self):
"""
Might fail if the GEOFON fdsnws-event webservice is unavailable.
Does the dictionary have the values expected for this event?
"""
evid = "gfz2021htpl"
result = fetch_quakeml(evid)
self.assertTrue(isinstance(result, dict))
self.assertGreater(len(result), 3)
self.assertEqual(result["date"], "2021-04-21")
self.assertEqual(len(result["focalmechanism"]), 2)
self.assertEqual(result["id"], "gfz2021htpl")
self.assertLess(abs(float(result["magnitude"]) - 4.465), 0.01)
self.assertEqual(len(result["origin"]), 3)
self.assertTrue(result["time"].startswith("08:08:23"))
def tearDown(self):
try:
os.unlink(self.outfile)
......
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