42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
|
|
import unittest
|
||
|
|
from unittest.mock import call, patch
|
||
|
|
|
||
|
|
from app.models import Video
|
||
|
|
from app.worker import generate_articles_with_quality_retries
|
||
|
|
|
||
|
|
|
||
|
|
class GenerateArticlesWithQualityRetriesTest(unittest.TestCase):
|
||
|
|
@patch("app.worker.append_progress")
|
||
|
|
@patch("app.worker.articles_from_payload")
|
||
|
|
@patch("app.worker.generate_article_payload")
|
||
|
|
def test_retries_invalid_json_and_passes_feedback(
|
||
|
|
self,
|
||
|
|
generate_article_payload_mock,
|
||
|
|
articles_from_payload_mock,
|
||
|
|
append_progress_mock,
|
||
|
|
):
|
||
|
|
error = "LLM вернул невалидный JSON: Expecting ',' delimiter в строке 11, колонке 17."
|
||
|
|
valid_payload = {"articles": [{"title": "Готовая статья"}]}
|
||
|
|
expected_articles = [object()]
|
||
|
|
generate_article_payload_mock.side_effect = [RuntimeError(error), valid_payload]
|
||
|
|
articles_from_payload_mock.return_value = expected_articles
|
||
|
|
video = Video(id=213, title="Тест", description="", external_id="test", source_url="")
|
||
|
|
|
||
|
|
result = generate_articles_with_quality_retries(video, "Текст", [])
|
||
|
|
|
||
|
|
self.assertEqual(result, expected_articles)
|
||
|
|
self.assertEqual(generate_article_payload_mock.call_count, 2)
|
||
|
|
self.assertEqual(
|
||
|
|
generate_article_payload_mock.call_args_list,
|
||
|
|
[
|
||
|
|
call(video, "Текст", chapter=None, quality_feedback=""),
|
||
|
|
call(video, "Текст", chapter=None, quality_feedback=error),
|
||
|
|
],
|
||
|
|
)
|
||
|
|
append_progress_mock.assert_called_once()
|
||
|
|
articles_from_payload_mock.assert_called_once_with(video, valid_payload, [])
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|