test fix
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import time
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
@@ -188,45 +188,56 @@ class TestAudioRepositoryNewSchema:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_timestamp_conversion(self, audio_repository):
|
||||
"""Тест преобразования UNIX timestamp в читаемую дату"""
|
||||
test_timestamp = 1642248600 # 2022-01-17 10:30:00
|
||||
"""Тест преобразования UNIX timestamp в читаемую дату (UTC)"""
|
||||
test_timestamp = 1642248600 # 2022-01-15 12:10:00 UTC
|
||||
audio_repository._execute_query_with_result.return_value = [(test_timestamp,)]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
# Должна вернуться читаемая дата в формате dd.mm.yyyy HH:MM
|
||||
assert result == "15.01.2022 15:10"
|
||||
expected = datetime.fromtimestamp(test_timestamp, tz=timezone.utc).strftime(
|
||||
"%d.%m.%Y %H:%M"
|
||||
)
|
||||
assert result == expected
|
||||
assert isinstance(result, str)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_different_timestamp(self, audio_repository):
|
||||
"""Тест преобразования другого timestamp в читаемую дату"""
|
||||
test_timestamp = 1705312800 # 2024-01-16 12:00:00
|
||||
"""Тест преобразования другого timestamp в читаемую дату (UTC)"""
|
||||
test_timestamp = 1705312800 # 2024-01-16 12:00:00 UTC
|
||||
audio_repository._execute_query_with_result.return_value = [(test_timestamp,)]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
assert result == "15.01.2024 13:00"
|
||||
expected = datetime.fromtimestamp(test_timestamp, tz=timezone.utc).strftime(
|
||||
"%d.%m.%Y %H:%M"
|
||||
)
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_midnight(self, audio_repository):
|
||||
"""Тест преобразования timestamp для полуночи"""
|
||||
test_timestamp = 1705190400 # 2024-01-15 00:00:00
|
||||
"""Тест преобразования timestamp для полуночи (UTC)"""
|
||||
test_timestamp = 1705190400 # 2024-01-14 00:00:00 UTC
|
||||
audio_repository._execute_query_with_result.return_value = [(test_timestamp,)]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
assert result == "14.01.2024 03:00"
|
||||
expected = datetime.fromtimestamp(test_timestamp, tz=timezone.utc).strftime(
|
||||
"%d.%m.%Y %H:%M"
|
||||
)
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_year_end(self, audio_repository):
|
||||
"""Тест преобразования timestamp для конца года"""
|
||||
test_timestamp = 1704067200 # 2023-12-31 23:59:59
|
||||
"""Тест преобразования timestamp для конца года (UTC)"""
|
||||
test_timestamp = 1704067200 # 2023-12-31 00:00:00 UTC
|
||||
audio_repository._execute_query_with_result.return_value = [(test_timestamp,)]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
assert result == "01.01.2024 03:00"
|
||||
expected = datetime.fromtimestamp(test_timestamp, tz=timezone.utc).strftime(
|
||||
"%d.%m.%Y %H:%M"
|
||||
)
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_foreign_keys_enabled_called(self, audio_repository):
|
||||
@@ -288,18 +299,20 @@ class TestAudioRepositoryNewSchema:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_logging_format(self, audio_repository):
|
||||
"""Тест формата лога при получении даты"""
|
||||
test_timestamp = 1642248600 # 2022-01-17 10:30:00
|
||||
"""Тест формата лога при получении даты (UTC)"""
|
||||
test_timestamp = 1642248600 # 2022-01-15 12:10:00 UTC
|
||||
audio_repository._execute_query_with_result.return_value = [(test_timestamp,)]
|
||||
|
||||
await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
# Проверяем формат лога
|
||||
expected = datetime.fromtimestamp(test_timestamp, tz=timezone.utc).strftime(
|
||||
"%d.%m.%Y %H:%M"
|
||||
)
|
||||
log_call = audio_repository.logger.info.call_args
|
||||
log_message = log_call[0][0]
|
||||
|
||||
assert "Получена дата" in log_message
|
||||
assert "15.01.2022 15:10" in log_message
|
||||
assert expected in log_message
|
||||
assert "test_audio.ogg" in log_message
|
||||
|
||||
|
||||
@@ -400,30 +413,38 @@ class TestAudioRepositoryEdgeCases:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_zero_timestamp(self, audio_repository):
|
||||
"""Тест получения даты для timestamp = 0 (1970-01-01)"""
|
||||
"""Тест получения даты для timestamp = 0 (1970-01-01 UTC)"""
|
||||
audio_repository._execute_query_with_result.return_value = [(0,)]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
assert result == "01.01.1970 03:00"
|
||||
expected = datetime.fromtimestamp(0, tz=timezone.utc).strftime("%d.%m.%Y %H:%M")
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_negative_timestamp(self, audio_repository):
|
||||
"""Тест получения даты для отрицательного timestamp"""
|
||||
audio_repository._execute_query_with_result.return_value = [
|
||||
(-3600,)
|
||||
] # 1969-12-31 23:00:00
|
||||
"""Тест получения даты для отрицательного timestamp (UTC)"""
|
||||
ts = -3600 # 1969-12-31 23:00:00 UTC
|
||||
audio_repository._execute_query_with_result.return_value = [(ts,)]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
assert result == "01.01.1970 02:00"
|
||||
expected = datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%d.%m.%Y %H:%M")
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_date_by_file_name_future_timestamp(self, audio_repository):
|
||||
"""Тест получения даты для будущего timestamp"""
|
||||
future_timestamp = int(datetime(2030, 12, 31, 23, 59, 59).timestamp())
|
||||
audio_repository._execute_query_with_result.return_value = [(future_timestamp,)]
|
||||
"""Тест получения даты для будущего timestamp (UTC, без зависимости от локали)"""
|
||||
future_timestamp = int(
|
||||
datetime(2030, 12, 31, 23, 59, 59, tzinfo=timezone.utc).timestamp()
|
||||
)
|
||||
audio_repository._execute_query_with_result.return_value = [
|
||||
(future_timestamp,)
|
||||
]
|
||||
|
||||
result = await audio_repository.get_date_by_file_name("test_audio.ogg")
|
||||
|
||||
assert result == "31.12.2030 23:59"
|
||||
expected = datetime.fromtimestamp(
|
||||
future_timestamp, tz=timezone.utc
|
||||
).strftime("%d.%m.%Y %H:%M")
|
||||
assert result == expected
|
||||
|
||||
Reference in New Issue
Block a user