Files
inizio-web-search/tests/test_api.py
2025-10-20 21:05:31 +02:00

56 lines
1.9 KiB
Python

import os
import logging
import pytest
from dotenv import load_dotenv
import api
def test_env_variables_present():
"""
Test jestli env proměnné existují a mají obsah
"""
load_dotenv()
key = (os.getenv("GOOGLE_DEVELOPER_KEY") or "").strip()
cx = (os.getenv("GOOGLE_CSE_ID") or "").strip()
# Log presence without exposing secrets
logging.info("GOOGLE_DEVELOPER_KEY present: %s", bool(key))
logging.info("GOOGLE_CSE_ID present: %s", bool(cx))
assert key, "GOOGLE_DEVELOPER_KEY is missing or empty"
assert cx, "GOOGLE_CSE_ID is missing or empty"
def test_integration_search_youtube(monkeypatch):
"""
Test výsledku vyhledávání "youtube".
"""
load_dotenv()
key = (os.getenv("GOOGLE_DEVELOPER_KEY") or "").strip()
cx = (os.getenv("GOOGLE_CSE_ID") or "").strip()
if not key or not cx:
logging.warning("Skipping integration test: missing GOOGLE_DEVELOPER_KEY or GOOGLE_CSE_ID")
pytest.skip("Integration test skipped: GOOGLE_DEVELOPER_KEY/GOOGLE_CSE_ID not set")
# Speed up: don't wait during the test
monkeypatch.setattr(api, "RATE_SECONDS", 0)
monkeypatch.setattr(api, "last_api_call", 0)
results = []
try:
logging.info("Calling get_google_first_page for query 'youtube'")
results = api.get_google_first_page("youtube")
logging.info("Received %d results. First title: %s", len(results), (results[0].get("title") if results else None))
except RuntimeError as e:
logging.error("API error during integration test: %s", e)
return pytest.skip(f"Integration test skipped due to API error: {e}")
assert isinstance(results, list) and len(results) > 0
has_youtube = any("youtube.com" in (item.get("link") or "") for item in results)
logging.info("Contains youtube.com link: %s", has_youtube)
assert has_youtube