/
test_federator_routing_client.py
343 lines (305 loc) · 14.3 KB
/
test_federator_routing_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:copyright:
The ObsPy Development Team (devs@obspy.org)
:license:
GNU Lesser General Public License, Version 3
(https://www.gnu.org/copyleft/lesser.html)
"""
import collections
from unittest import mock
from packaging.version import parse as parse_version
import pytest
import obspy
from obspy.clients.fdsn.routing.federator_routing_client import \
FederatorRoutingClient
_DummyResponse = collections.namedtuple("_DummyResponse", ["content"])
pytestmark = pytest.mark.network
class TestFederatorRoutingClient():
@classmethod
def setup_class(cls):
cls.client = FederatorRoutingClient()
cls._cls = ("obspy.clients.fdsn.routing.federator_routing_client."
"FederatorRoutingClient")
def test_get_service_version(self):
# At the time of test writing the version is 1.1.1. Here we just
# make sure it is larger.
assert parse_version(self.client.get_service_version()) >= \
parse_version("1.1.1")
def test_response_splitting(self):
data = """
RANDOM_KEY=true
DATACENTER=GEOFON,http://geofon.gfz-potsdam.de
DATASELECTSERVICE=http://geofon.gfz-potsdam1.de/fdsnws/dataselect/1/
STATIONSERVICE=http://geofon.gfz-potsdam2.de/fdsnws/station/1/
AF CER -- BHE 2007-03-15T00:47:00 2599-12-31T23:59:59
AF CER -- BHN 2007-03-15T00:47:00 2599-12-31T23:59:59
DATACENTER=INGV,http://www.ingv.it
DATASELECTSERVICE=http://webservices1.rm.ingv.it/fdsnws/dataselect/1/
STATIONSERVICE=http://webservices2.rm.ingv.it/fdsnws/station/1/
EVENTSERVICE=http://webservices.rm.ingv.it/fdsnws/event/1/
AC PUK -- HHE 2009-05-29T00:00:00 2009-12-22T00:00:00
"""
assert FederatorRoutingClient._split_routing_response(
data, "dataselect") == \
{"http://geofon.gfz-potsdam1.de": (
"AF CER -- BHE 2007-03-15T00:47:00 2599-12-31T23:59:59\n"
"AF CER -- BHN 2007-03-15T00:47:00 2599-12-31T23:59:59"),
"http://webservices1.rm.ingv.it": (
"AC PUK -- HHE 2009-05-29T00:00:00 2009-12-22T00:00:00")}
assert FederatorRoutingClient._split_routing_response(
data, "station") == \
{"http://geofon.gfz-potsdam2.de": (
"AF CER -- BHE 2007-03-15T00:47:00 2599-12-31T23:59:59\n"
"AF CER -- BHN 2007-03-15T00:47:00 2599-12-31T23:59:59"),
"http://webservices2.rm.ingv.it": (
"AC PUK -- HHE 2009-05-29T00:00:00 2009-12-22T00:00:00")}
# Error handling.
msg = "Service must be 'dataselect' or 'station'."
with pytest.raises(ValueError, match=msg):
FederatorRoutingClient._split_routing_response(data, "random")
def test_response_splitting_fdsnws_subdomain(self):
data = """
DATACENTER=NOA,http://bbnet.gein.noa.gr/HL/
DATASELECTSERVICE=http://eida.gein.noa.gr/fdsnws/dataselect/1/
STATIONSERVICE=http://eida.gein.noa.gr/fdsnws/station/1/
HP LTHK * * 2017-10-20T00:00:00 2599-12-31T23:59:59
DATACENTER=RASPISHAKE,http://raspberryshake.net/
DATASELECTSERVICE=http://fdsnws.raspberryshakedata.com/fdsnws/dataselect/1/
STATIONSERVICE=http://fdsnws.raspberryshakedata.com/fdsnws/station/1/
EVENTSERVICE=http://fdsnws.raspberryshakedata.com/fdsnws/event/1/
AM RA14E * * 2017-10-20T00:00:00 2599-12-31T23:59:59
"""
assert FederatorRoutingClient._split_routing_response(
data, "station") == \
{"http://eida.gein.noa.gr":
"HP LTHK * * 2017-10-20T00:00:00 2599-12-31T23:59:59",
"http://fdsnws.raspberryshakedata.com":
"AM RA14E * * 2017-10-20T00:00:00 2599-12-31T23:59:59"}
def test_get_waveforms(self):
"""
This just dispatches to the get_waveforms_bulk() method - so no need
to also test it explicitly.
"""
with mock.patch(self._cls + ".get_waveforms_bulk") as p:
p.return_value = "1234"
st = self.client.get_waveforms(
network="XX", station="XXXXX", location="XX",
channel="XXX", starttime=obspy.UTCDateTime(2017, 1, 1),
endtime=obspy.UTCDateTime(2017, 1, 2),
latitude=1.0, longitude=2.0,
longestonly=True, minimumlength=2)
assert st == "1234"
assert p.call_count == 1
assert p.call_args[0][0][0] == \
["XX", "XXXXX", "XX", "XXX", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 2)]
# SNCLs + times should be filtered out.
assert p.call_args[1] == \
{"longestonly": True, "minimumlength": 2, "latitude": 1.0,
"longitude": 2.0}
# Don't pass in the SNCLs.
with mock.patch(self._cls + ".get_waveforms_bulk") as p:
p.return_value = "1234"
st = self.client.get_waveforms(
starttime=obspy.UTCDateTime(2017, 1, 1),
endtime=obspy.UTCDateTime(2017, 1, 2),
latitude=1.0, longitude=2.0,
longestonly=True, minimumlength=2)
assert st == "1234"
assert p.call_count == 1
assert p.call_args[0][0][0] == \
["*", "*", "*", "*", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 2)]
assert p.call_args[1] == \
{"longestonly": True, "minimumlength": 2, "latitude": 1.0,
"longitude": 2.0}
def test_get_waveforms_bulk(self):
# Some mock routing response.
content = """
DATACENTER=GEOFON,http://geofon.gfz-potsdam.de
DATASELECTSERVICE=http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/
STATIONSERVICE=http://geofon.gfz-potsdam.de/fdsnws/station/1/
AF CER -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
AF CVNA -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
DATACENTER=IRISDMC,http://ds.iris.edu
DATASELECTSERVICE=http://service.iris.edu/fdsnws/dataselect/1/
STATIONSERVICE=http://service.iris.edu/fdsnws/station/1/
EVENTSERVICE=http://service.iris.edu/fdsnws/event/1/
SACPZSERVICE=http://service.iris.edu/irisws/sacpz/1/
RESPSERVICE=http://service.iris.edu/irisws/resp/1/
AF CNG -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
AK CAPN -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
"""
if hasattr(content, "encode"):
content = content.encode()
with mock.patch(self._cls + "._download") as p1, \
mock.patch(self._cls + "._download_waveforms") as p2:
p1.return_value = _DummyResponse(content=content)
p2.return_value = "1234"
st = self.client.get_waveforms_bulk(
[["A*", "C*", "", "LHZ", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 2)]],
longestonly=True, minimumlength=2)
assert st == "1234"
assert p1.call_count == 1
assert p1.call_args[0][0] == \
"http://service.iris.edu/irisws/fedcatalog/1/query"
assert p1.call_args[1]["data"] == (
b"format=request\n"
b"A* C* -- LHZ 2017-01-01T00:00:00.000000 "
b"2017-01-02T00:00:00.000000")
assert p2.call_args[0][0] == {
"http://geofon.gfz-potsdam.de": (
"AF CER -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00\n"
"AF CVNA -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00"),
"http://service.iris.edu": (
"AF CNG -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00\n"
"AK CAPN -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00")}
assert p2.call_args[1] == {"longestonly": True, "minimumlength": 2}
def test_get_waveforms_error_handling(self):
# Some parameters should not be passed explicitly.
msg = ("`network` must not be part of the optional parameters in a "
"bulk request.")
with pytest.raises(ValueError, match=msg):
self.client.get_waveforms_bulk([[
"AA", "BB", "", "LHZ", obspy.UTCDateTime(2016, 1, 1),
obspy.UTCDateTime(2016, 1, 2)]], network="BB")
def test_get_stations(self):
"""
This just dispatches to the get_waveforms_bulk() method - so no need
to also test it explicitly.
"""
with mock.patch(self._cls + ".get_stations_bulk") as p:
p.return_value = "1234"
st = self.client.get_stations(
network="XX", station="XXXXX", location="XX",
channel="XXX", starttime=obspy.UTCDateTime(2017, 1, 1),
endtime=obspy.UTCDateTime(2017, 1, 2),
latitude=1.0, longitude=2.0,
maximumradius=1.0, level="network")
assert st == "1234"
assert p.call_count == 1
assert p.call_args[0][0][0] == \
["XX", "XXXXX", "XX", "XXX", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 2)]
# SNCLs + times should be filtered out.
assert p.call_args[1] == \
{"latitude": 1.0, "longitude": 2.0,
"maximumradius": 1.0, "level": "network"}
# Don't pass in the SNCLs.
with mock.patch(self._cls + ".get_stations_bulk") as p:
p.return_value = "1234"
st = self.client.get_stations(
starttime=obspy.UTCDateTime(2017, 1, 1),
endtime=obspy.UTCDateTime(2017, 1, 2),
latitude=1.0, longitude=2.0,
maximumradius=1.0, level="network")
assert st == "1234"
assert p.call_count == 1
assert p.call_args[0][0][0] == \
["*", "*", "*", "*", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 2)]
assert p.call_args[1] == \
{"latitude": 1.0, "longitude": 2.0,
"maximumradius": 1.0, "level": "network"}
def test_get_stations_bulk(self):
# Some mock routing response.
content = """
DATACENTER=GEOFON,http://geofon.gfz-potsdam.de
DATASELECTSERVICE=http://geofon.gfz-potsdam.de/fdsnws/dataselect/1/
STATIONSERVICE=http://geofon.gfz-potsdam.de/fdsnws/station/1/
AF CER -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
AF CVNA -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
DATACENTER=IRISDMC,http://ds.iris.edu
DATASELECTSERVICE=http://service.iris.edu/fdsnws/dataselect/1/
STATIONSERVICE=http://service.iris.edu/fdsnws/station/1/
EVENTSERVICE=http://service.iris.edu/fdsnws/event/1/
SACPZSERVICE=http://service.iris.edu/irisws/sacpz/1/
RESPSERVICE=http://service.iris.edu/irisws/resp/1/
AF CNG -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
AK CAPN -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00
"""
if hasattr(content, "encode"):
content = content.encode()
with mock.patch(self._cls + "._download") as p1, \
mock.patch(self._cls + "._download_stations") as p2:
p1.return_value = _DummyResponse(content=content)
p2.return_value = "1234"
st = self.client.get_stations_bulk(
[["A*", "C*", "", "LHZ", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 2)]],
level="network")
assert st == "1234"
assert p1.call_count == 1
assert p1.call_args[0][0] == \
"http://service.iris.edu/irisws/fedcatalog/1/query"
assert p1.call_args[1]["data"] == (
b"level=network\n"
b"format=request\n"
b"A* C* -- LHZ 2017-01-01T00:00:00.000000 "
b"2017-01-02T00:00:00.000000")
assert p2.call_args[0][0] == {
"http://geofon.gfz-potsdam.de": (
"AF CER -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00\n"
"AF CVNA -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00"),
"http://service.iris.edu": (
"AF CNG -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00\n"
"AK CAPN -- LHZ 2017-01-01T00:00:00 2017-01-02T00:00:00")}
assert p2.call_args[1] == {"level": "network"}
def test_get_stations_error_handling(self):
# Some parameters should not be passed explicitly.
msg = ("`network` must not be part of the optional parameters in a "
"bulk request.")
with pytest.raises(ValueError, match=msg):
self.client.get_stations_bulk([[
"AA", "BB", "", "LHZ", obspy.UTCDateTime(2016, 1, 1),
obspy.UTCDateTime(2016, 1, 2)]], network="BB")
def test_get_waveforms_integration_test(self):
"""
Integration test that does not mock anything but actually downloads
things.
"""
st = self.client.get_waveforms(
starttime=obspy.UTCDateTime(2017, 1, 1),
endtime=obspy.UTCDateTime(2017, 1, 1, 0, 1),
latitude=35.0, longitude=-110, maxradius=0.3,
channel="LHZ")
# This yields 1 channel at the time of writing this test - I assume
# it is unlikely to every yield less. So this test should be fairly
# stable.
assert len(st) >= 1
# Same with the bulk request.
st2 = self.client.get_waveforms_bulk(
[["*", "*", "*", "LHZ", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 1, 0, 1)]],
latitude=35.0, longitude=-110, maxradius=0.3)
assert len(st2) >= 1
assert st == st2
def test_get_stations_integration_test(self):
"""
Integration test that does not mock anything but actually downloads
things.
"""
inv = self.client.get_stations(
starttime=obspy.UTCDateTime(2017, 1, 1),
endtime=obspy.UTCDateTime(2017, 1, 1, 0, 1),
latitude=35.0, longitude=-110, maxradius=0.3, network="TA",
channel="LHZ", level="station")
# This yields 1 network at the time of writing this test - I assume
# it is unlikely to every yield less. So this test should be fairly
# stable.
assert len(inv) >= 1
# Again repeat with the bulk request.
inv2 = self.client.get_stations_bulk(
[["*", "*", "*", "LHZ", obspy.UTCDateTime(2017, 1, 1),
obspy.UTCDateTime(2017, 1, 1, 0, 1)]],
latitude=35.0, longitude=-110, maxradius=0.3,
level="station")
assert len(inv2) >= 1
inv2 = inv2.select(network="TA")
# The results should be basically identical - they will still differ
# because times stamps and also order might change slightly.
# But the get_contents() method should be safe enough.
assert inv.get_contents() == inv2.get_contents()