From f7df74fabc1ddb5bc7104eacfa03d36052ea40d5 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 14:38:43 +0200 Subject: [PATCH 1/9] Fix datetime import --- posttroll/tests/test_message.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posttroll/tests/test_message.py b/posttroll/tests/test_message.py index af97236..d0936e9 100644 --- a/posttroll/tests/test_message.py +++ b/posttroll/tests/test_message.py @@ -27,7 +27,7 @@ import os import sys import unittest -from datetime import datetime +import datetime as dt from posttroll.message import _MAGICK, Message @@ -36,7 +36,7 @@ DATADIR = HOME + "/data" -SOME_METADATA = {"timestamp": datetime(2010, 12, 3, 16, 28, 39), +SOME_METADATA = {"timestamp": dt.datetime(2010, 12, 3, 16, 28, 39), "satellite": "metop2", "uri": "file://data/my/path/to/hrpt/files/myfile", "orbit": 1222, From f1b37dfe9f6a51d8ee1c30fadc7d88a7504ae333 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 14:41:46 +0200 Subject: [PATCH 2/9] Replace .utcnow() with .now(dt.timezone.utc) --- posttroll/address_receiver.py | 6 +++--- posttroll/message.py | 2 +- posttroll/publisher.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/posttroll/address_receiver.py b/posttroll/address_receiver.py index 42c0d4c..ece7b84 100644 --- a/posttroll/address_receiver.py +++ b/posttroll/address_receiver.py @@ -128,11 +128,11 @@ def get(self, name=""): def _check_age(self, pub, min_interval=zero_seconds): """Check the age of the receiver.""" - now = dt.datetime.utcnow() + now = dt.datetime.now(dt.timezone.utc) if (now - self._last_age_check) <= min_interval: return - LOGGER.debug("%s - checking addresses", str(dt.datetime.utcnow())) + LOGGER.debug("%s - checking addresses", str(dt.datetime.now(dt.timezone.utc))) self._last_age_check = now to_del = [] with self._address_lock: @@ -232,7 +232,7 @@ def set_up_address_receiver(self, port): def _add(self, adr, metadata): """Add an address.""" with self._address_lock: - metadata["receive_time"] = dt.datetime.utcnow() + metadata["receive_time"] = dt.datetime.now(dt.timezone.utc) self._addresses[adr] = metadata diff --git a/posttroll/message.py b/posttroll/message.py index ab68484..97c0510 100644 --- a/posttroll/message.py +++ b/posttroll/message.py @@ -132,7 +132,7 @@ def __init__(self, subject="", atype="", data="", binary=False, rawstr=None): self.type = atype self.type = atype self.sender = _getsender() - self.time = dt.datetime.utcnow() + self.time = dt.datetime.now(dt.timezone.utc) self.data = data self.binary = binary self.version = _VERSION diff --git a/posttroll/publisher.py b/posttroll/publisher.py index dee85cc..c990fed 100644 --- a/posttroll/publisher.py +++ b/posttroll/publisher.py @@ -144,9 +144,9 @@ def __init__(self, publisher): def __call__(self, min_interval=0): if not min_interval or ( - (dt.datetime.utcnow() - self.lastbeat >= + (dt.datetime.now() - self.lastbeat >= dt.timedelta(seconds=min_interval))): - self.lastbeat = dt.datetime.utcnow() + self.lastbeat = dt.datetime.now(dt.timezone.UTC) LOGGER.debug("Publish heartbeat (min_interval is %.1f sec)", min_interval) self.publisher.send(Message(self.subject, "beat", {"min_interval": min_interval}).encode()) From 95e8e99c4eb0a310edfa030e527412ddc6d6360c Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 14:42:48 +0200 Subject: [PATCH 3/9] Use dt.datetime.fromisoformat() --- posttroll/__init__.py | 26 -------------------------- posttroll/message.py | 6 ++---- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/posttroll/__init__.py b/posttroll/__init__.py index df053e3..da8f4a1 100644 --- a/posttroll/__init__.py +++ b/posttroll/__init__.py @@ -24,9 +24,7 @@ """Posttroll packages.""" -import datetime as dt import logging -import sys from donfig import Config @@ -46,27 +44,3 @@ def get_context(): return get_context() else: raise NotImplementedError(f"No support for backend {backend} implemented (yet?).") - - -def strp_isoformat(strg): - """Decode an ISO formatted string to a datetime object. - - Allow a time-string without microseconds. - - We handle input like: 2011-11-14T12:51:25.123456 - """ - if isinstance(strg, dt.datetime): - return strg - if len(strg) < 19 or len(strg) > 26: - if len(strg) > 30: - strg = strg[:30] + "..." - raise ValueError("Invalid ISO formatted time string '%s'" % strg) - if strg.find(".") == -1: - strg += ".000000" - if sys.version[0:3] >= "2.6": - return dt.datetime.strptime(strg, "%Y-%m-%dT%H:%M:%S.%f") - else: - dat, mis = strg.split(".") - dat = dt.datetime.strptime(dat, "%Y-%m-%dT%H:%M:%S") - mis = int(float("." + mis) * 1000000) - return dat.replace(microsecond=mis) diff --git a/posttroll/message.py b/posttroll/message.py index 97c0510..038bb7a 100644 --- a/posttroll/message.py +++ b/posttroll/message.py @@ -46,8 +46,6 @@ except ImportError: import simplejson as json -from posttroll import strp_isoformat - _MAGICK = "pytroll:/" _VERSION = "v1.01" @@ -229,7 +227,7 @@ def datetime_decoder(dct): for key, val in pairs: if isinstance(val, str): try: - val = strp_isoformat(val) + val = dt.datetime.fromisoformat(val) except ValueError: pass elif isinstance(val, (dict, list)): @@ -252,7 +250,7 @@ def _decode(rawstr): msg = dict((("subject", raw[0].strip()), ("type", raw[1].strip()), ("sender", raw[2].strip()), - ("time", strp_isoformat(raw[3].strip())), + ("time", dt.datetime.fromisoformat(raw[3].strip())), ("version", version))) # Data part From 84d574215c5823e820ecbfe37c0461e4bbc49a91 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 14:53:57 +0200 Subject: [PATCH 4/9] Fix typo --- posttroll/publisher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posttroll/publisher.py b/posttroll/publisher.py index c990fed..1ac061b 100644 --- a/posttroll/publisher.py +++ b/posttroll/publisher.py @@ -146,7 +146,7 @@ def __call__(self, min_interval=0): if not min_interval or ( (dt.datetime.now() - self.lastbeat >= dt.timedelta(seconds=min_interval))): - self.lastbeat = dt.datetime.now(dt.timezone.UTC) + self.lastbeat = dt.datetime.now(dt.timezone.utc) LOGGER.debug("Publish heartbeat (min_interval is %.1f sec)", min_interval) self.publisher.send(Message(self.subject, "beat", {"min_interval": min_interval}).encode()) From 6311b2eb73569c1b01eb3e9099ac64575950d51a Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 15:01:31 +0200 Subject: [PATCH 5/9] Fix timedelta import --- posttroll/tests/test_nameserver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posttroll/tests/test_nameserver.py b/posttroll/tests/test_nameserver.py index f4fe81d..5312db8 100644 --- a/posttroll/tests/test_nameserver.py +++ b/posttroll/tests/test_nameserver.py @@ -1,10 +1,10 @@ """Tests for communication involving the nameserver for service discovery.""" +import datetime as dt import os import time import unittest from contextlib import contextmanager -from datetime import timedelta from threading import Thread from unittest import mock @@ -49,7 +49,7 @@ def create_nameserver_instance(max_age=3, multicast_enabled=True): """Create a nameserver instance.""" config.set(nameserver_port=free_port()) config.set(address_publish_port=free_port()) - ns = NameServer(max_age=timedelta(seconds=max_age), multicast_enabled=multicast_enabled) + ns = NameServer(max_age=dt.timedelta(seconds=max_age), multicast_enabled=multicast_enabled) thr = Thread(target=ns.run) thr.start() From 0136ca3c9acf4bdf2654e43e8f80c1dbb4fb0deb Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 15:03:14 +0200 Subject: [PATCH 6/9] Fix timezone unaware datetimes --- posttroll/address_receiver.py | 2 +- posttroll/ns.py | 6 +++--- posttroll/publisher.py | 4 ++-- posttroll/subscriber.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/posttroll/address_receiver.py b/posttroll/address_receiver.py index ece7b84..1da74b6 100644 --- a/posttroll/address_receiver.py +++ b/posttroll/address_receiver.py @@ -89,7 +89,7 @@ def __init__(self, max_age=ten_minutes, port=None, self._subject = "/address" self._do_heartbeat = do_heartbeat self._multicast_enabled = multicast_enabled - self._last_age_check = dt.datetime(1900, 1, 1) + self._last_age_check = dt.datetime(1900, 1, 1, tzinfo=dt.timezone.utc) self._do_run = False self._is_running = False self._thread = threading.Thread(target=self._run) diff --git a/posttroll/ns.py b/posttroll/ns.py index 1bf05b4..a63a6d3 100644 --- a/posttroll/ns.py +++ b/posttroll/ns.py @@ -48,7 +48,7 @@ def get_configured_nameserver_port(): try: port = int(os.environ["NAMESERVER_PORT"]) warnings.warn("NAMESERVER_PORT is pending deprecation, please use POSTTROLL_NAMESERVER_PORT instead.", - PendingDeprecationWarning) + PendingDeprecationWarning, stacklevel=2) except KeyError: port = DEFAULT_NAMESERVER_PORT return config.get("nameserver_port", port) @@ -68,8 +68,8 @@ def get_pub_addresses(names=None, timeout=10, nameserver="localhost"): if names is None: names = ["", ] for name in names: - then = dt.datetime.now() + dt.timedelta(seconds=timeout) - while dt.datetime.now() < then: + then = dt.datetime.now(dt.timezone.utc) + dt.timedelta(seconds=timeout) + while dt.datetime.now(dt.timezone.utc) < then: addrs += get_pub_address(name, nameserver=nameserver, timeout=timeout) if addrs: break diff --git a/posttroll/publisher.py b/posttroll/publisher.py index 1ac061b..4cd1313 100644 --- a/posttroll/publisher.py +++ b/posttroll/publisher.py @@ -140,11 +140,11 @@ class _PublisherHeartbeat: def __init__(self, publisher): self.publisher = publisher self.subject = "/heartbeat/" + publisher.name - self.lastbeat = dt.datetime(1900, 1, 1) + self.lastbeat = dt.datetime(1900, 1, 1, tzinfo=dt.timezone.utc) def __call__(self, min_interval=0): if not min_interval or ( - (dt.datetime.now() - self.lastbeat >= + (dt.datetime.now(dt.timezone.utc) - self.lastbeat >= dt.timedelta(seconds=min_interval))): self.lastbeat = dt.datetime.now(dt.timezone.utc) LOGGER.debug("Publish heartbeat (min_interval is %.1f sec)", min_interval) diff --git a/posttroll/subscriber.py b/posttroll/subscriber.py index fc3a8c1..b6a3e52 100644 --- a/posttroll/subscriber.py +++ b/posttroll/subscriber.py @@ -196,8 +196,8 @@ def start(self): """Start the subscriber.""" def _get_addr_loop(service, timeout): """Try to get the address of *service* until for *timeout* seconds.""" - then = dt.datetime.now() + dt.timedelta(seconds=timeout) - while dt.datetime.now() < then: + then = dt.datetime.now(dt.timezone.utc) + dt.timedelta(seconds=timeout) + while dt.datetime.now(dt.timezone.utc) < then: addrs = get_pub_address(service, self._timeout, nameserver=self._nameserver) if addrs: return [addr["URI"] for addr in addrs] From 53a25f0c8b8d6c7d1ce611f277bbfa1a0089fab4 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 15:20:33 +0200 Subject: [PATCH 7/9] Switch to pytest --- posttroll/tests/test_message.py | 213 ++++++++++++++++---------------- 1 file changed, 104 insertions(+), 109 deletions(-) diff --git a/posttroll/tests/test_message.py b/posttroll/tests/test_message.py index d0936e9..78f3a65 100644 --- a/posttroll/tests/test_message.py +++ b/posttroll/tests/test_message.py @@ -26,7 +26,6 @@ import copy import os import sys -import unittest import datetime as dt from posttroll.message import _MAGICK, Message @@ -44,113 +43,109 @@ "afloat": 1.2345} -class Test(unittest.TestCase): - """Test class.""" - - def test_encode_decode(self): - """Test the encoding/decoding of the message class.""" - msg1 = Message("/test/whatup/doc", "info", data="not much to say") - - sender = "%s@%s" % (msg1.user, msg1.host) - assert sender == msg1.sender, "Messaging, decoding user, host from sender failed" - msg2 = Message.decode(msg1.encode()) - assert str(msg2) == str(msg1), "Messaging, encoding, decoding failed" - - def test_decode(self): - """Test the decoding of a message.""" - rawstr = (_MAGICK + - r"/test/1/2/3 info ras@hawaii 2008-04-11T22:13:22.123000 v1.01" + - r' text/ascii "what' + r"'" + r's up doc"') - msg = Message.decode(rawstr) - - assert str(msg) == rawstr, "Messaging, decoding of message failed" - - def test_encode(self): - """Test the encoding of a message.""" - subject = "/test/whatup/doc" - atype = "info" - data = "not much to say" - msg1 = Message(subject, atype, data=data) - sender = "%s@%s" % (msg1.user, msg1.host) - full_message = (_MAGICK + subject + " " + atype + " " + sender + " " + - str(msg1.time.isoformat()) + " " + msg1.version + " " + "text/ascii" + " " + data) - assert full_message == msg1.encode() - - def test_unicode(self): - """Test handling of unicode.""" - try: - msg = ('pytroll://PPS-monitorplot/3/norrköping/utv/polar/direct_readout/ file ' - 'safusr.u@lxserv1096.smhi.se 2018-11-16T12:19:29.934025 v1.01 application/json' - ' {"start_time": "2018-11-16T12:02:43.700000"}') - assert msg == str(Message(rawstr=msg)) - except UnicodeDecodeError: - self.fail("Unexpected unicode decoding error") - - try: - msg = (u'pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN 2019-01-07T12:52:19.872171' - r' v1.01 application/json {"station": "norrk\u00f6ping"}') - try: - assert msg == str(Message(rawstr=msg)).decode("utf-8") - except AttributeError: - assert msg == str(Message(rawstr=msg)) - except UnicodeDecodeError: - self.fail("Unexpected unicode decoding error") - - def test_iso(self): - """Test handling of iso-8859-1.""" - msg = ('pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN ' - '2019-01-07T12:52:19.872171 v1.01 application/json {"station": "norrköping"}') - try: - iso_msg = msg.decode("utf-8").encode("iso-8859-1") - except AttributeError: - iso_msg = msg.encode("iso-8859-1") - try: - Message(rawstr=iso_msg) - except UnicodeDecodeError: - self.fail("Unexpected iso decoding error") - - def test_pickle(self): - """Test pickling.""" - import pickle - msg1 = Message("/test/whatup/doc", "info", data="not much to say") - try: - fp_ = open("pickle.message", "wb") - pickle.dump(msg1, fp_) - fp_.close() - fp_ = open("pickle.message", "rb") - msg2 = pickle.load(fp_) - - fp_.close() - assert str(msg1) == str(msg2), "Messaging, pickle failed" - finally: - try: - os.remove("pickle.message") - except OSError: - pass - - def test_metadata(self): - """Test metadata encoding/decoding.""" - metadata = copy.copy(SOME_METADATA) - msg = Message.decode(Message("/sat/polar/smb/level1", "file", - data=metadata).encode()) - - assert msg.data == metadata, "Messaging, metadata decoding / encoding failed" - - def test_serialization(self): - """Test json serialization.""" - compare_file = "/message_metadata.dumps" - import json - metadata = copy.copy(SOME_METADATA) - metadata["timestamp"] = metadata["timestamp"].isoformat() - fp_ = open(DATADIR + compare_file) - dump = fp_.read() +def test_encode_decode(): + """Test the encoding/decoding of the message class.""" + msg1 = Message("/test/whatup/doc", "info", data="not much to say") + + sender = "%s@%s" % (msg1.user, msg1.host) + assert sender == msg1.sender, "Messaging, decoding user, host from sender failed" + msg2 = Message.decode(msg1.encode()) + assert str(msg2) == str(msg1), "Messaging, encoding, decoding failed" + + +def test_decode(): + """Test the decoding of a message.""" + rawstr = (_MAGICK + + r"/test/1/2/3 info ras@hawaii 2008-04-11T22:13:22.123000 v1.01" + + r' text/ascii "what' + r"'" + r's up doc"') + msg = Message.decode(rawstr) + + assert str(msg) == rawstr, "Messaging, decoding of message failed" + + +def test_encode(): + """Test the encoding of a message.""" + subject = "/test/whatup/doc" + atype = "info" + data = "not much to say" + msg1 = Message(subject, atype, data=data) + sender = "%s@%s" % (msg1.user, msg1.host) + full_message = (_MAGICK + subject + " " + atype + " " + sender + " " + + str(msg1.time.isoformat()) + " " + msg1.version + " " + "text/ascii" + " " + data) + assert full_message == msg1.encode() + + +def test_unicode(): + """Test handling of unicode.""" + msg = ('pytroll://PPS-monitorplot/3/norrköping/utv/polar/direct_readout/ file ' + 'safusr.u@lxserv1096.smhi.se 2018-11-16T12:19:29.934025 v1.01 application/json' + ' {"start_time": "2018-11-16T12:02:43.700000"}') + assert msg == str(Message(rawstr=msg)) + + msg = (u'pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN 2019-01-07T12:52:19.872171' + r' v1.01 application/json {"station": "norrk\u00f6ping"}') + try: + assert msg == str(Message(rawstr=msg)).decode("utf-8") + except AttributeError: + assert msg == str(Message(rawstr=msg)) + + +def test_iso(): + """Test handling of iso-8859-1.""" + msg = ('pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN ' + '2019-01-07T12:52:19.872171 v1.01 application/json {"station": "norrköping"}') + try: + iso_msg = msg.decode("utf-8").encode("iso-8859-1") + except AttributeError: + iso_msg = msg.encode("iso-8859-1") + + Message(rawstr=iso_msg) + + +def test_pickle(): + """Test pickling.""" + import pickle + msg1 = Message("/test/whatup/doc", "info", data="not much to say") + try: + fp_ = open("pickle.message", "wb") + pickle.dump(msg1, fp_) fp_.close() - local_dump = json.dumps(metadata) - - msg = json.loads(dump) - for key, val in msg.items(): - assert val == metadata.get(key) + fp_ = open("pickle.message", "rb") + msg2 = pickle.load(fp_) - msg = json.loads(local_dump) - for key, val in msg.items(): - assert val == metadata.get(key) + fp_.close() + assert str(msg1) == str(msg2), "Messaging, pickle failed" + finally: + try: + os.remove("pickle.message") + except OSError: + pass + + +def test_metadata(): + """Test metadata encoding/decoding.""" + metadata = copy.copy(SOME_METADATA) + msg = Message.decode(Message("/sat/polar/smb/level1", "file", + data=metadata).encode()) + + assert msg.data == metadata, "Messaging, metadata decoding / encoding failed" + + +def test_serialization(): + """Test json serialization.""" + compare_file = "/message_metadata.dumps" + import json + metadata = copy.copy(SOME_METADATA) + metadata["timestamp"] = metadata["timestamp"].isoformat() + fp_ = open(DATADIR + compare_file) + dump = fp_.read() + fp_.close() + local_dump = json.dumps(metadata) + + msg = json.loads(dump) + for key, val in msg.items(): + assert val == metadata.get(key) + + msg = json.loads(local_dump) + for key, val in msg.items(): + assert val == metadata.get(key) From 4d29cc976532fd0ccf6686b66426a5cde7e27db5 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 15:29:10 +0200 Subject: [PATCH 8/9] Add tests with tz-aware datetimes --- posttroll/tests/test_message.py | 51 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/posttroll/tests/test_message.py b/posttroll/tests/test_message.py index 78f3a65..85a5e51 100644 --- a/posttroll/tests/test_message.py +++ b/posttroll/tests/test_message.py @@ -28,6 +28,8 @@ import sys import datetime as dt +import pytest + from posttroll.message import _MAGICK, Message HOME = os.path.dirname(__file__) or "." @@ -35,12 +37,18 @@ DATADIR = HOME + "/data" -SOME_METADATA = {"timestamp": dt.datetime(2010, 12, 3, 16, 28, 39), - "satellite": "metop2", - "uri": "file://data/my/path/to/hrpt/files/myfile", - "orbit": 1222, - "format": "hrpt", - "afloat": 1.2345} +TZ_UNAWARE_METADATA = {"timestamp": dt.datetime(2010, 12, 3, 16, 28, 39), + "satellite": "metop2", + "uri": "file://data/my/path/to/hrpt/files/myfile", + "orbit": 1222, + "format": "hrpt", + "afloat": 1.2345} +TZ_AWARE_METADATA = {"timestamp": dt.datetime(2010, 12, 3, 16, 28, 39, tzinfo=dt.timezone.utc), + "satellite": "metop2", + "uri": "file://data/my/path/to/hrpt/files/myfile", + "orbit": 1222, + "format": "hrpt", + "afloat": 1.2345} def test_encode_decode(): @@ -53,10 +61,11 @@ def test_encode_decode(): assert str(msg2) == str(msg1), "Messaging, encoding, decoding failed" -def test_decode(): +@pytest.mark.parametrize("dstr", (r"2008-04-11T22:13:22.123000", r"2008-04-11T22:13:22.123000+00:00")) +def test_decode(dstr): """Test the decoding of a message.""" rawstr = (_MAGICK + - r"/test/1/2/3 info ras@hawaii 2008-04-11T22:13:22.123000 v1.01" + + r"/test/1/2/3 info ras@hawaii " + dstr + r" v1.01" + r' text/ascii "what' + r"'" + r's up doc"') msg = Message.decode(rawstr) @@ -75,14 +84,15 @@ def test_encode(): assert full_message == msg1.encode() -def test_unicode(): +@pytest.mark.parametrize("dstr", (r"2008-04-11T22:13:22.123000", r"2008-04-11T22:13:22.123000+00:00")) +def test_unicode(dstr): """Test handling of unicode.""" msg = ('pytroll://PPS-monitorplot/3/norrköping/utv/polar/direct_readout/ file ' - 'safusr.u@lxserv1096.smhi.se 2018-11-16T12:19:29.934025 v1.01 application/json' - ' {"start_time": "2018-11-16T12:02:43.700000"}') + 'safusr.u@lxserv1096.smhi.se ' + dstr + ' v1.01 application/json' + ' {"start_time": "' + dstr + '"}') assert msg == str(Message(rawstr=msg)) - msg = (u'pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN 2019-01-07T12:52:19.872171' + msg = (u'pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN ' + dstr + r' v1.01 application/json {"station": "norrk\u00f6ping"}') try: assert msg == str(Message(rawstr=msg)).decode("utf-8") @@ -90,10 +100,11 @@ def test_unicode(): assert msg == str(Message(rawstr=msg)) -def test_iso(): +@pytest.mark.parametrize("dstr", (r"2008-04-11T22:13:22.123000", r"2008-04-11T22:13:22.123000+00:00")) +def test_iso(dstr): """Test handling of iso-8859-1.""" - msg = ('pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN ' - '2019-01-07T12:52:19.872171 v1.01 application/json {"station": "norrköping"}') + msg = ('pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN ' + dstr + + ' v1.01 application/json {"station": "norrköping"}') try: iso_msg = msg.decode("utf-8").encode("iso-8859-1") except AttributeError: @@ -122,20 +133,22 @@ def test_pickle(): pass -def test_metadata(): +@pytest.mark.parametrize("mda", (TZ_UNAWARE_METADATA, TZ_AWARE_METADATA)) +def test_metadata(mda): """Test metadata encoding/decoding.""" - metadata = copy.copy(SOME_METADATA) + metadata = copy.copy(mda) msg = Message.decode(Message("/sat/polar/smb/level1", "file", data=metadata).encode()) assert msg.data == metadata, "Messaging, metadata decoding / encoding failed" -def test_serialization(): +@pytest.mark.parametrize("mda", (TZ_UNAWARE_METADATA, TZ_AWARE_METADATA)) +def test_serialization(mda): """Test json serialization.""" compare_file = "/message_metadata.dumps" import json - metadata = copy.copy(SOME_METADATA) + metadata = copy.copy(mda) metadata["timestamp"] = metadata["timestamp"].isoformat() fp_ = open(DATADIR + compare_file) dump = fp_.read() From 02fd7ec6553c99db80ee5959c90b4ee420db5c95 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Fri, 15 Nov 2024 15:48:25 +0200 Subject: [PATCH 9/9] Add aware/unaware comparison files --- posttroll/tests/data/message_metadata_aware.dumps | 1 + ...metadata.dumps => message_metadata_unaware.dumps} | 0 posttroll/tests/test_message.py | 12 ++++++------ 3 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 posttroll/tests/data/message_metadata_aware.dumps rename posttroll/tests/data/{message_metadata.dumps => message_metadata_unaware.dumps} (100%) diff --git a/posttroll/tests/data/message_metadata_aware.dumps b/posttroll/tests/data/message_metadata_aware.dumps new file mode 100644 index 0000000..850a6cc --- /dev/null +++ b/posttroll/tests/data/message_metadata_aware.dumps @@ -0,0 +1 @@ +{"satellite": "metop2", "format": "hrpt", "timestamp": "2010-12-03T16:28:39+00:00", "afloat": 1.2344999999999999, "uri": "file://data/my/path/to/hrpt/files/myfile", "orbit": 1222} \ No newline at end of file diff --git a/posttroll/tests/data/message_metadata.dumps b/posttroll/tests/data/message_metadata_unaware.dumps similarity index 100% rename from posttroll/tests/data/message_metadata.dumps rename to posttroll/tests/data/message_metadata_unaware.dumps diff --git a/posttroll/tests/test_message.py b/posttroll/tests/test_message.py index 85a5e51..c677eb2 100644 --- a/posttroll/tests/test_message.py +++ b/posttroll/tests/test_message.py @@ -143,16 +143,16 @@ def test_metadata(mda): assert msg.data == metadata, "Messaging, metadata decoding / encoding failed" -@pytest.mark.parametrize("mda", (TZ_UNAWARE_METADATA, TZ_AWARE_METADATA)) -def test_serialization(mda): +@pytest.mark.parametrize(("mda", "compare_file"), + ((TZ_UNAWARE_METADATA, "/message_metadata_unaware.dumps"), + ((TZ_AWARE_METADATA, "/message_metadata_aware.dumps")))) +def test_serialization(mda, compare_file): """Test json serialization.""" - compare_file = "/message_metadata.dumps" import json metadata = copy.copy(mda) metadata["timestamp"] = metadata["timestamp"].isoformat() - fp_ = open(DATADIR + compare_file) - dump = fp_.read() - fp_.close() + with open(DATADIR + compare_file) as fp_: + dump = fp_.read() local_dump = json.dumps(metadata) msg = json.loads(dump)