From 704d09ac9031ef743fcdb683104a6f0be95a515a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bompard?= Date: Mon, 28 Oct 2024 15:16:19 +0100 Subject: [PATCH] Move the new function to `fmn.database` to preserve the dependencies split MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Aurélien Bompard --- fmn/core/amqp.py | 15 ------------- fmn/database/model/destination.py | 15 ++++++++++++- tests/core/test_amqp.py | 28 +----------------------- tests/database/model/test_destination.py | 28 ++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/fmn/core/amqp.py b/fmn/core/amqp.py index 25783518f..5ead22564 100644 --- a/fmn/core/amqp.py +++ b/fmn/core/amqp.py @@ -4,11 +4,9 @@ import logging import ssl -from datetime import datetime, timezone from aio_pika.abc import SSLOptions from aio_pika.connection import URL -from fedora_messaging.message import Message log = logging.getLogger(__name__) @@ -26,16 +24,3 @@ def get_url_from_config(config: dict): ) ) return url - - -def get_sent_datetime(message: Message) -> datetime: - sent_at = message._headers.get("sent-at", None) - if sent_at: - # fromisoformat doesn't parse Z suffix (yet) see: - # https://discuss.python.org/t/parse-z-timezone-suffix-in-datetime/2220 - try: - return datetime.fromisoformat(sent_at.replace("Z", "+00:00")) - except ValueError: - log.exception("Failed to parse sent-at timestamp value") - # Default to now - return datetime.now(tz=timezone.utc) diff --git a/fmn/database/model/destination.py b/fmn/database/model/destination.py index 3db27397e..1f9b33071 100644 --- a/fmn/database/model/destination.py +++ b/fmn/database/model/destination.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: MIT import logging +from datetime import datetime, timezone from email.utils import format_datetime from typing import TYPE_CHECKING @@ -11,7 +12,6 @@ from sqlalchemy.ext.asyncio import async_object_session from sqlalchemy.orm import relationship -from ...core.amqp import get_sent_datetime from ...core.config import get_settings from ..main import Base from .generation_rule import GenerationRule @@ -82,3 +82,16 @@ async def get_extra(message: "Message"): else: return f"\n{response.text}\n" return "" + + +def get_sent_datetime(message: "Message") -> datetime: + sent_at = message._headers.get("sent-at", None) + if sent_at: + # fromisoformat doesn't parse Z suffix (yet) see: + # https://discuss.python.org/t/parse-z-timezone-suffix-in-datetime/2220 + try: + return datetime.fromisoformat(sent_at.replace("Z", "+00:00")) + except ValueError: + log.exception("Failed to parse sent-at timestamp value") + # Default to now + return datetime.now(tz=timezone.utc) diff --git a/tests/core/test_amqp.py b/tests/core/test_amqp.py index 93262bb4b..c46ddd6d8 100644 --- a/tests/core/test_amqp.py +++ b/tests/core/test_amqp.py @@ -3,12 +3,10 @@ # SPDX-License-Identifier: MIT import ssl -from datetime import datetime, timezone -import pytest from aio_pika.connection import URL -from fmn.core.amqp import get_sent_datetime, get_url_from_config +from fmn.core.amqp import get_url_from_config async def test_get_url_from_config_with_ssl(mocker): @@ -30,27 +28,3 @@ async def test_get_url_from_config_with_ssl(mocker): } ) assert get_url_from_config(config) == expected - - -FAKE_NOW = datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc) - - -@pytest.mark.parametrize( - "sent_at,expected", - [ - (None, FAKE_NOW), - ("2021-07-27T04:22:42Z", datetime(2021, 7, 27, 4, 22, 42, tzinfo=timezone.utc)), - ("2021-07-27T04:22:42JUNK", FAKE_NOW), - ], -) -def test_get_sent_datetime(make_mocked_message, mocker, sent_at, expected): - message = make_mocked_message( - topic="dummy", - body={"summary": "dummy summary"}, - ) - message._properties.headers["sent-at"] = sent_at - - fake_datetime = mocker.patch("fmn.core.amqp.datetime") - fake_datetime.now.return_value = FAKE_NOW - fake_datetime.fromisoformat = datetime.fromisoformat - assert get_sent_datetime(message) == expected diff --git a/tests/database/model/test_destination.py b/tests/database/model/test_destination.py index 2cfd54df9..9d098c85d 100644 --- a/tests/database/model/test_destination.py +++ b/tests/database/model/test_destination.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: MIT -from datetime import datetime +from datetime import datetime, timezone from email.utils import format_datetime from unittest import mock @@ -10,7 +10,7 @@ import pytest from fmn.database import model -from fmn.database.model.destination import Destination, get_extra +from fmn.database.model.destination import Destination, get_extra, get_sent_datetime @pytest.fixture @@ -145,3 +145,27 @@ async def test_get_extra_request_failure(make_mocked_message, respx_mocker): result = await get_extra(message) assert result == "" + + +FAKE_NOW = datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc) + + +@pytest.mark.parametrize( + "sent_at,expected", + [ + (None, FAKE_NOW), + ("2021-07-27T04:22:42Z", datetime(2021, 7, 27, 4, 22, 42, tzinfo=timezone.utc)), + ("2021-07-27T04:22:42JUNK", FAKE_NOW), + ], +) +def test_get_sent_datetime(make_mocked_message, mocker, sent_at, expected): + message = make_mocked_message( + topic="dummy", + body={"summary": "dummy summary"}, + ) + message._properties.headers["sent-at"] = sent_at + + fake_datetime = mocker.patch("fmn.database.model.destination.datetime") + fake_datetime.now.return_value = FAKE_NOW + fake_datetime.fromisoformat = datetime.fromisoformat + assert get_sent_datetime(message) == expected