Files
MoneyPrinterTurbo/test/services/test_serial_material_search.py
TanishqandHarry 4644b56f8e fix(material): log why Pexels and Coverr searches fail, such as a wrong or missing API key
With a wrong key, Pexels and Coverr answer HTTP 401, and the log said
only "pexels video search returned an unsupported response" (7ddb11d
replaced the line that printed the response). An answer that is not
JSON, such as an HTML error page or a plain-text 429, logged the raw
"Expecting value: line 1 column 1 (char 0)". With no key and the
default material_concurrency = 1, the serial search logged only
"error=ValueError", so "pexels_api_keys is not set" no longer reached
the log. Each time, the task then stopped with "failed to download
video materials from pexels".

Pexels and Coverr responses now go through _stock_search_results(),
which reports a failed search the way the Pixabay search does: a 401
logs "pexels rejected the API key: status=401. Check pexels_api_keys in
config.toml.", a 429 logs the rate limit and Retry-After, other errors
log the status and content type, and a body that is not JSON says so.
Neither the response body nor the key is logged. The serial search now
logs the exception text, as the parallel search does.
2026-11-02 10:29:36 +08:00

45 lines
1.9 KiB
Python

from unittest.mock import Mock, patch
import pytest
from app.models.schema import MaterialInfo, VideoAspect
from app.services import material
@pytest.mark.parametrize("workers", [1, 2])
def test_failed_term_does_not_abort_remaining_stock_search(monkeypatch, workers):
monkeypatch.setattr(material, "_get_material_concurrency", lambda: workers)
healthy = MaterialInfo(url="https://example.test/clip.mp4", duration=5)
def find_term(search_term, *args, **kwargs):
if search_term == "bad term":
raise ValueError("malformed provider response")
return [healthy]
search = Mock(side_effect=find_term)
result = material._search_terms_in_parallel(
["bad term", "healthy term"], search, 5, VideoAspect.portrait
)
assert result == [("bad term", []), ("healthy term", [healthy])]
assert search.call_count == 2
@pytest.mark.parametrize("workers", [1, 2])
def test_failed_term_log_keeps_the_error_detail(monkeypatch, workers):
monkeypatch.setattr(material, "_get_material_concurrency", lambda: workers)
monkeypatch.delitem(material.config.app, "pexels_api_keys", raising=False)
with patch.object(material.logger, "error") as error:
result = material._search_terms_in_parallel(
["ocean", "beach"], material.search_videos_pexels, 5, VideoAspect.portrait
)
assert result == [("ocean", []), ("beach", [])]
assert error.call_count == 2
for call in error.call_args_list:
assert "pexels_api_keys is not set" in call.args[0]
def test_serial_success_preserves_order_and_empty_results(monkeypatch):
monkeypatch.setattr(material, "_get_material_concurrency", lambda: 1)
healthy = MaterialInfo(url="https://example.test/clip.mp4", duration=5)
assert material._search_terms_in_parallel(
["empty", "healthy"], Mock(side_effect=[[], [healthy]]), 5, VideoAspect.portrait
) == [("empty", []), ("healthy", [healthy])]