Add tests for date_time formatting with Unix time and datetime objects

This commit is contained in:
JRoot Junior 2026-03-02 21:50:45 +02:00
parent ebfab22d64
commit 50e7b3f755
No known key found for this signature in database
GPG key ID: 738964250D5FF6E2
2 changed files with 82 additions and 0 deletions

View file

@ -1,3 +1,5 @@
from datetime import datetime, timezone
import pytest
from aiogram.enums import MessageEntityType
@ -120,6 +122,10 @@ class TestNode:
DateTime("test", unix_time=42, date_time_format="yMd"),
'<tg-time unix="42" format="yMd">test</tg-time>',
],
[
DateTime("test", unix_time=42),
'<tg-time unix="42">test</tg-time>',
],
],
)
def test_render_plain_only(self, node: Text, result: str):
@ -379,6 +385,22 @@ class TestUtils:
assert node._params["unix_time"] == 42
assert node._params["date_time_format"] == "yMd"
def test_date_time_with_datetime_object(self):
dt = datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
node = DateTime("test", unix_time=dt)
assert isinstance(node, DateTime)
assert node._params["unix_time"] == 1704067200
def test_date_time_with_datetime_and_format(self):
dt = datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
node = DateTime("test", unix_time=dt, date_time_format="yMd")
assert node._params["unix_time"] == 1704067200
assert node._params["date_time_format"] == "yMd"
def test_date_time_as_markdown(self):
node = DateTime("test", unix_time=42, date_time_format="yMd")
assert node.as_markdown() == "![test](tg://time?unix=42&format=yMd)"
def test_as_line(self):
node = as_line("test", "test", "test")
assert isinstance(node, Text)

View file

@ -1,3 +1,5 @@
from datetime import datetime, timezone
import pytest
from aiogram.types import MessageEntity, User
@ -146,6 +148,16 @@ class TestTextDecoration:
),
"![test](tg://time?unix=42&format=yMd)",
],
[
html_decoration,
MessageEntity(type="date_time", offset=0, length=5, unix_time=42),
'<tg-time unix="42">test</tg-time>',
],
[
markdown_decoration,
MessageEntity(type="date_time", offset=0, length=5, unix_time=42),
"![test](tg://time?unix=42)",
],
],
)
def test_apply_single_entity(
@ -153,6 +165,38 @@ class TestTextDecoration:
):
assert decorator.apply_entity(entity, "test") == result
@pytest.mark.parametrize(
"decorator,date_time_format,expected",
[
(
html_decoration,
None,
'<tg-time unix="1704067200">test</tg-time>',
),
(
html_decoration,
"yMd",
'<tg-time unix="1704067200" format="yMd">test</tg-time>',
),
(
markdown_decoration,
None,
"![test](tg://time?unix=1704067200)",
),
(
markdown_decoration,
"yMd",
"![test](tg://time?unix=1704067200&format=yMd)",
),
],
)
def test_date_time_with_datetime_object(
self, decorator: TextDecoration, date_time_format: str | None, expected: str
):
dt = datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
result = decorator.date_time("test", unix_time=dt, date_time_format=date_time_format)
assert result == expected
def test_unknown_apply_entity(self):
assert (
html_decoration.apply_entity(
@ -318,6 +362,22 @@ class TestTextDecoration:
],
"<b>test@example.com</b>",
],
[
html_decoration,
"test",
[MessageEntity(type="date_time", offset=0, length=4, unix_time=42)],
'<tg-time unix="42">test</tg-time>',
],
[
html_decoration,
"test",
[
MessageEntity(
type="date_time", offset=0, length=4, unix_time=42, date_time_format="yMd"
)
],
'<tg-time unix="42" format="yMd">test</tg-time>',
],
],
)
def test_unparse(