From 7b5acae22dfd7b9e5117aeacce969ac7eb846890 Mon Sep 17 00:00:00 2001 From: Jeremy Fee <jmfee@usgs.gov> Date: Tue, 17 Dec 2019 13:33:24 -0700 Subject: [PATCH] Fix Python3 RawInputClient error, add test --- geomagio/edge/RawInputClient.py | 8 +++-- test/edge_test/RawInputFactory_test.py | 47 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/edge_test/RawInputFactory_test.py diff --git a/geomagio/edge/RawInputClient.py b/geomagio/edge/RawInputClient.py index eeea3889b..436102f63 100644 --- a/geomagio/edge/RawInputClient.py +++ b/geomagio/edge/RawInputClient.py @@ -1,4 +1,5 @@ -from builtins import range +from __future__ import unicode_literals +from builtins import range, str import socket # noqa import struct @@ -132,7 +133,10 @@ class RawInputClient(): the correct length. We only expect observatory to ever be of an incorrect length. """ - return str(network + observatory.ljust(5) + channel + location) + return str.encode(network + + observatory.ljust(5) + + channel + + location) def forceout(self): """ force edge to recognize data diff --git a/test/edge_test/RawInputFactory_test.py b/test/edge_test/RawInputFactory_test.py new file mode 100644 index 000000000..c78d654ea --- /dev/null +++ b/test/edge_test/RawInputFactory_test.py @@ -0,0 +1,47 @@ +"""Tests for RawInputFactory.py""" + +import numpy +from obspy.core import Stats, Trace, UTCDateTime +from geomagio.edge import EdgeFactory, RawInputClient +from nose.tools import assert_equals + + +class TestRawInputClient(RawInputClient): + def __init__(self, **kwargs): + RawInputClient.__init__(self, **kwargs) + self.last_send = [] + + def _send(self, buf): + """stub out send method to capture data that would be sent.""" + self.last_send.append(buf) + + +def test_raw_input_client(): + """edge_test.RawInputClient_test.test_raw_input_client() + """ + network = 'NT' + station = 'BOU' + channel = 'MVH' + location = 'R0' + data = [0, 1, 2, 3, 4, 5] + starttime = UTCDateTime('2019-12-01') + + trace = Trace( + numpy.array(data, dtype=numpy.float64), + Stats({ + 'channel': channel, + 'delta': 60.0, + 'location': location, + 'network': network, + 'npts': len(data), + 'starttime': starttime, + 'station': station + })) + + client = TestRawInputClient(tag='tag', host='host', port='port', + station=station, channel=channel, + location=location, network=network) + trace_send = EdgeFactory()._convert_trace_to_int(trace.copy()) + client.send_trace('minute', trace_send) + # verify data was sent + assert_equals(len(client.last_send), 1) -- GitLab