121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
"""Тесты для UsageDB."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
from decimal import Decimal
|
|
|
|
from voice_dictation.core.models import UsageRecord
|
|
from voice_dictation.core.paths import AppPaths
|
|
from voice_dictation.core.usage_db import UsageDB
|
|
|
|
|
|
def _record(when: datetime, duration: float = 5.0, cost: str = "0.001") -> UsageRecord:
|
|
return UsageRecord(
|
|
id=None,
|
|
created_at=when,
|
|
duration_sec=duration,
|
|
cost_usd=Decimal(cost),
|
|
model="gpt-4o-mini-transcribe",
|
|
text_preview="hello",
|
|
)
|
|
|
|
|
|
def test_insert_assigns_id(tmp_paths: AppPaths) -> None:
|
|
db = UsageDB(tmp_paths.usage_db)
|
|
record = db.insert(_record(datetime.now(UTC)))
|
|
assert record.id is not None
|
|
assert record.id >= 1
|
|
|
|
|
|
def test_today_total_aggregates(tmp_paths: AppPaths) -> None:
|
|
db = UsageDB(tmp_paths.usage_db)
|
|
now = datetime.now(UTC)
|
|
db.insert(_record(now, duration=10.0, cost="0.001"))
|
|
db.insert(_record(now, duration=20.0, cost="0.002"))
|
|
db.insert(_record(now - timedelta(days=2), duration=999.0, cost="0.5"))
|
|
|
|
today = db.today_total(tz=UTC)
|
|
assert today.count == 2
|
|
assert today.total_duration_sec == 30.0
|
|
assert today.total_cost_usd == Decimal("0.003000")
|
|
|
|
|
|
def test_recent_returns_newest_first(tmp_paths: AppPaths) -> None:
|
|
db = UsageDB(tmp_paths.usage_db)
|
|
base = datetime.now(UTC)
|
|
for i in range(5):
|
|
db.insert(_record(base + timedelta(seconds=i)))
|
|
|
|
recent = db.recent(limit=3)
|
|
assert len(recent) == 3
|
|
assert recent[0].id is not None
|
|
assert recent[0].id > recent[1].id > recent[2].id
|
|
|
|
|
|
def test_all_time_includes_old_records(tmp_paths: AppPaths) -> None:
|
|
db = UsageDB(tmp_paths.usage_db)
|
|
db.insert(_record(datetime.now(UTC) - timedelta(days=400), duration=60.0, cost="0.01"))
|
|
db.insert(_record(datetime.now(UTC), duration=30.0, cost="0.005"))
|
|
|
|
total = db.all_time()
|
|
assert total.count == 2
|
|
assert total.total_duration_sec == 90.0
|
|
assert total.total_cost_usd == Decimal("0.015000")
|
|
|
|
|
|
def test_month_stats_filters_correctly(tmp_paths: AppPaths) -> None:
|
|
db = UsageDB(tmp_paths.usage_db)
|
|
in_month = datetime(2026, 4, 15, 12, 0, tzinfo=UTC)
|
|
other_month = datetime(2026, 3, 15, 12, 0, tzinfo=UTC)
|
|
db.insert(_record(in_month, duration=10.0))
|
|
db.insert(_record(other_month, duration=999.0))
|
|
|
|
stats = db.month_stats(2026, 4, tz=UTC)
|
|
assert stats.count == 1
|
|
assert stats.total_duration_sec == 10.0
|
|
|
|
|
|
def test_v1_migration_retains_old_rows_and_unknown_processing(tmp_path):
|
|
import sqlite3
|
|
|
|
from voice_dictation.core.usage_db import _SCHEMA_V1
|
|
|
|
path = tmp_path / "legacy.db"
|
|
with sqlite3.connect(path) as connection:
|
|
connection.executescript(_SCHEMA_V1)
|
|
connection.execute("PRAGMA user_version = 1")
|
|
connection.execute(
|
|
"INSERT INTO usage(created_at, duration_sec, cost_usd, model, text_preview) "
|
|
"VALUES (?, ?, ?, ?, ?)",
|
|
(datetime.now(UTC).isoformat(), 5, "0.01", "legacy", "old snippet"),
|
|
)
|
|
db = UsageDB(path)
|
|
old = db.recent()[0]
|
|
assert old.text_preview == "old snippet"
|
|
assert old.full_text == ""
|
|
assert old.processing_sec is None
|
|
assert old.audio_path == ""
|
|
assert UsageDB(path).recent() == [old] # repeat initialization is idempotent
|
|
with sqlite3.connect(path) as connection:
|
|
assert connection.execute("PRAGMA user_version").fetchone()[0] == 2
|
|
|
|
|
|
def test_complete_history_record_roundtrips_untruncated(tmp_paths):
|
|
from dataclasses import replace
|
|
|
|
db = UsageDB(tmp_paths.usage_db)
|
|
full_text = "Полный итоговый текст. " * 100
|
|
record = replace(
|
|
_record(datetime.now(UTC)),
|
|
full_text=full_text,
|
|
text_preview=full_text,
|
|
processing_sec=2.75,
|
|
audio_path="/synthetic/session/source.ogg",
|
|
)
|
|
inserted = db.insert(record)
|
|
assert inserted == db.recent()[0]
|
|
assert inserted.full_text == full_text
|
|
assert inserted.text_preview == full_text[:80]
|
|
assert inserted.processing_sec == 2.75
|
|
assert inserted.audio_path == "/synthetic/session/source.ogg"
|