Files
learningcircuit--local-deep…/tests/news/test_core_utils_comprehensive.py
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

181 lines
5.4 KiB
Python

"""
Comprehensive tests for core/utils.py module.
Tests utility functions like utc_now, date formatting, and helpers.
"""
from datetime import datetime, timezone, timedelta
class TestUtcNow:
"""Tests for utc_now function."""
def test_returns_datetime(self):
"""Test returns a datetime object."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
assert isinstance(result, datetime)
def test_returns_utc_timezone(self):
"""Test returns datetime with UTC timezone."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
assert result.tzinfo == timezone.utc
def test_is_close_to_current_time(self):
"""Test result is close to current time."""
from local_deep_research.news.core.utils import utc_now
before = datetime.now(timezone.utc)
result = utc_now()
after = datetime.now(timezone.utc)
assert before <= result <= after
def test_is_not_naive(self):
"""Test datetime is timezone-aware, not naive."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
assert result.tzinfo is not None
class TestFormatTimestamp:
"""Tests for format_timestamp function if it exists."""
def test_can_import_utc_now(self):
"""Test utc_now can be imported."""
from local_deep_research.news.core.utils import utc_now
assert utc_now is not None
assert callable(utc_now)
class TestDateTimeHelpers:
"""Tests for datetime helper functions."""
def test_utc_now_multiple_calls_are_sequential(self):
"""Test multiple utc_now calls return sequential times."""
from local_deep_research.news.core.utils import utc_now
import time
first = utc_now()
time.sleep(0.001) # Small delay
second = utc_now()
assert first <= second
def test_utc_now_returns_different_instances(self):
"""Test utc_now returns different instances each call."""
from local_deep_research.news.core.utils import utc_now
first = utc_now()
second = utc_now()
# They should be different objects
assert first is not second
def test_utc_now_has_all_datetime_attributes(self):
"""Test utc_now result has all datetime attributes."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
assert hasattr(result, "year")
assert hasattr(result, "month")
assert hasattr(result, "day")
assert hasattr(result, "hour")
assert hasattr(result, "minute")
assert hasattr(result, "second")
assert hasattr(result, "microsecond")
def test_utc_now_isoformat_works(self):
"""Test utc_now result can be converted to ISO format."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
iso = result.isoformat()
assert isinstance(iso, str)
assert "T" in iso # Has datetime separator
assert "+" in iso or "Z" in iso # Has timezone info
def test_utc_now_can_be_subtracted(self):
"""Test utc_now result can be used in time calculations."""
from local_deep_research.news.core.utils import utc_now
now = utc_now()
delta = timedelta(hours=1)
past = now - delta
assert past < now
def test_utc_now_can_compare_with_datetime(self):
"""Test utc_now result can compare with datetime."""
from local_deep_research.news.core.utils import utc_now
now = utc_now()
other = datetime.now(timezone.utc)
# Should be able to compare
_ = now < other or now > other or now == other
def test_utc_now_timestamp_method(self):
"""Test utc_now result has timestamp method."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
timestamp = result.timestamp()
assert isinstance(timestamp, float)
assert timestamp > 0
class TestUtilsImports:
"""Tests for module imports."""
def test_utc_now_is_callable(self):
"""Test utc_now is callable."""
from local_deep_research.news.core.utils import utc_now
assert callable(utc_now)
class TestUtilsEdgeCases:
"""Edge case tests for utils functions."""
def test_utc_now_handles_rapid_calls(self):
"""Test utc_now handles rapid sequential calls."""
from local_deep_research.news.core.utils import utc_now
results = [utc_now() for _ in range(100)]
# All should be datetime objects
assert all(isinstance(r, datetime) for r in results)
# Should be in non-decreasing order
for i in range(1, len(results)):
assert results[i] >= results[i - 1]
def test_utc_now_year_is_reasonable(self):
"""Test utc_now returns reasonable year."""
from local_deep_research.news.core.utils import utc_now
result = utc_now()
# Should be between 2024 and 2100 (reasonable for this codebase)
assert 2024 <= result.year <= 2100
def test_utc_now_works_in_list_comprehension(self):
"""Test utc_now works correctly in list comprehensions."""
from local_deep_research.news.core.utils import utc_now
timestamps = [utc_now().isoformat() for _ in range(5)]
assert len(timestamps) == 5
assert all(isinstance(t, str) for t in timestamps)