Skip to content

Commit

Permalink
Move the new function to fmn.database to preserve the dependencies …
Browse files Browse the repository at this point in the history
…split

Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Nov 4, 2024
1 parent c9439d2 commit 704d09a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 45 deletions.
15 changes: 0 additions & 15 deletions fmn/core/amqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
15 changes: 14 additions & 1 deletion fmn/database/model/destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
28 changes: 1 addition & 27 deletions tests/core/test_amqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
28 changes: 26 additions & 2 deletions tests/database/model/test_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#
# SPDX-License-Identifier: MIT

from datetime import datetime
from datetime import datetime, timezone
from email.utils import format_datetime
from unittest import mock

import httpx
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
Expand Down Expand Up @@ -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

0 comments on commit 704d09a

Please sign in to comment.