153 lines
4.9 KiB
Python
153 lines
4.9 KiB
Python
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from src.models.article_dto import ArticleDTO, ArticleStatus
|
|
from src.models import AppConfig, ProcessingResult, SimplifyCommand
|
|
|
|
|
|
class TestArticleDTO:
|
|
|
|
def test_article_dto_creation(self):
|
|
article = ArticleDTO(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
title="Test Article",
|
|
raw_text="Test content",
|
|
status=ArticleStatus.PENDING,
|
|
created_at=datetime.now(timezone.utc),
|
|
)
|
|
|
|
assert article.url == "https://ru.ruwiki.ru/wiki/Test"
|
|
assert article.title == "Test Article"
|
|
assert article.raw_text == "Test content"
|
|
assert article.status == ArticleStatus.PENDING
|
|
assert article.id is None
|
|
assert article.simplified_text is None
|
|
assert article.updated_at is None
|
|
|
|
def test_article_dto_with_optional_fields(self):
|
|
now = datetime.now(timezone.utc)
|
|
article = ArticleDTO(
|
|
id=1,
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
title="Test Article",
|
|
raw_text="Test content",
|
|
simplified_text="Simplified content",
|
|
status=ArticleStatus.SIMPLIFIED,
|
|
created_at=now,
|
|
updated_at=now,
|
|
)
|
|
|
|
assert article.id == 1
|
|
assert article.simplified_text == "Simplified content"
|
|
assert article.status == ArticleStatus.SIMPLIFIED
|
|
assert article.updated_at == now
|
|
|
|
def test_article_status_enum(self):
|
|
assert ArticleStatus.PENDING.value == "pending"
|
|
assert ArticleStatus.SIMPLIFIED.value == "simplified"
|
|
assert ArticleStatus.FAILED.value == "failed"
|
|
|
|
def test_article_dto_dataclass_behavior(self):
|
|
article1 = ArticleDTO(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
title="Test",
|
|
raw_text="Content",
|
|
status=ArticleStatus.PENDING,
|
|
created_at=datetime.now(timezone.utc),
|
|
)
|
|
|
|
article2 = ArticleDTO(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
title="Test",
|
|
raw_text="Content",
|
|
status=ArticleStatus.PENDING,
|
|
created_at=article1.created_at,
|
|
)
|
|
|
|
assert article1 == article2
|
|
|
|
article2.title = "Modified"
|
|
assert article1 != article2
|
|
|
|
|
|
class TestAppConfig:
|
|
|
|
def test_app_config_defaults(self):
|
|
from pathlib import Path
|
|
|
|
import os
|
|
from unittest.mock import patch
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
config = AppConfig(openai_api_key="test-key")
|
|
|
|
assert isinstance(config.db_path, str)
|
|
assert Path(config.db_path).suffix == ".db"
|
|
assert isinstance(config.openai_model, str)
|
|
assert config.openai_model.startswith("gpt")
|
|
assert isinstance(config.chunk_size, int)
|
|
assert config.chunk_size > 0
|
|
assert isinstance(config.chunk_overlap, int)
|
|
assert config.chunk_overlap >= 0
|
|
assert isinstance(config.max_concurrent_llm, int)
|
|
assert config.max_concurrent_llm > 0
|
|
|
|
|
|
class TestSimplifyCommand:
|
|
|
|
def test_simplify_command_creation(self):
|
|
command = SimplifyCommand(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
force_reprocess=False,
|
|
)
|
|
|
|
assert command.url == "https://ru.ruwiki.ru/wiki/Test"
|
|
assert command.force_reprocess is False
|
|
|
|
def test_simplify_command_with_force(self):
|
|
command = SimplifyCommand(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
force_reprocess=True,
|
|
)
|
|
|
|
assert command.force_reprocess is True
|
|
|
|
|
|
class TestProcessingResult:
|
|
|
|
def test_success_result(self):
|
|
result = ProcessingResult.success_result(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
title="Test",
|
|
raw_text="Raw content",
|
|
simplified_text="Simplified content",
|
|
token_count_raw=100,
|
|
token_count_simplified=50,
|
|
processing_time_seconds=1.5,
|
|
)
|
|
|
|
assert result.success is True
|
|
assert result.url == "https://ru.ruwiki.ru/wiki/Test"
|
|
assert result.title == "Test"
|
|
assert result.simplified_text == "Simplified content"
|
|
assert result.token_count_raw == 100
|
|
assert result.token_count_simplified == 50
|
|
assert result.processing_time_seconds == 1.5
|
|
assert result.error_message is None
|
|
|
|
def test_failure_result(self):
|
|
result = ProcessingResult.failure_result(
|
|
url="https://ru.ruwiki.ru/wiki/Test",
|
|
error_message="Processing failed",
|
|
)
|
|
|
|
assert result.success is False
|
|
assert result.url == "https://ru.ruwiki.ru/wiki/Test"
|
|
assert result.error_message == "Processing failed"
|
|
assert result.title is None
|
|
assert result.simplified_text is None
|
|
assert result.token_count_raw is None
|
|
assert result.token_count_simplified is None
|
|
assert result.processing_time_seconds is None
|