From ac52d6eec511762921def17dc21e83f578df7595 Mon Sep 17 00:00:00 2001 From: Brunobrno Date: Mon, 20 Oct 2025 21:05:31 +0200 Subject: [PATCH] last updates --- scraper.py => api.py | 0 app.py | 7 +++- docker-compose.yml | 6 +-- pytest.ini | 6 +++ templates/results.html | 51 +++++++++++++++++--------- tests/{test_scraper.py => test_api.py} | 22 ++++++++--- 6 files changed, 65 insertions(+), 27 deletions(-) rename scraper.py => api.py (100%) create mode 100644 pytest.ini rename tests/{test_scraper.py => test_api.py} (53%) diff --git a/scraper.py b/api.py similarity index 100% rename from scraper.py rename to api.py diff --git a/app.py b/app.py index 323c375..a60a824 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ from flask import Flask, render_template, request, send_file, jsonify from datetime import datetime -from scraper import get_google_first_page -import io, json, csv, yaml, os +from api import get_google_first_page +import io, json, csv, yaml, os, requests from dotenv import load_dotenv load_dotenv() @@ -10,6 +10,7 @@ app = Flask(__name__) @app.route("/", methods=["GET"]) def index(): + return render_template("index.html", results=[]) @app.route("/search", methods=["POST"]) @@ -17,6 +18,7 @@ def search(): query = request.form.get("q", "").strip() if not query: return render_template("index.html", error="Zadejte dotaz.") + try: results = get_google_first_page(query) # list of dicts except Exception as e: @@ -32,6 +34,7 @@ def search(): @app.route("/export", methods=["POST"]) def export(): data = request.get_json() + ext = data.get("format", "json") results = data.get("results", []) diff --git a/docker-compose.yml b/docker-compose.yml index ca5f8ae..45d7e9c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,6 @@ services: healthcheck: test: ["CMD-SHELL", "python -m pytest || exit 1"] interval: 1m - timeout: 20s - retries: 3 - start_period: 20s + timeout: 10s + retries: 1 + start_period: 10s diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..0db4d21 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +# Show logs during test runs (not only on failures) +log_cli = true +log_cli_level = INFO +log_format = %(asctime)s %(levelname)s %(name)s - %(message)s +log_date_format = %H:%M:%S diff --git a/templates/results.html b/templates/results.html index 7d1b6a1..3f0c470 100644 --- a/templates/results.html +++ b/templates/results.html @@ -6,25 +6,42 @@ Výsledky pro "{{ query }}" - vontor.cz diff --git a/tests/test_scraper.py b/tests/test_api.py similarity index 53% rename from tests/test_scraper.py rename to tests/test_api.py index 3f5d036..6ad14cb 100644 --- a/tests/test_scraper.py +++ b/tests/test_api.py @@ -1,8 +1,9 @@ import os +import logging import pytest from dotenv import load_dotenv -import scraper +import api def test_env_variables_present(): @@ -13,6 +14,10 @@ def test_env_variables_present(): 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" @@ -27,17 +32,24 @@ def test_integration_search_youtube(monkeypatch): 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(scraper, "RATE_SECONDS", 0) - monkeypatch.setattr(scraper, "last_api_call", 0) + monkeypatch.setattr(api, "RATE_SECONDS", 0) + monkeypatch.setattr(api, "last_api_call", 0) results = [] try: - results = scraper.get_google_first_page("youtube") + 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 - assert any("youtube.com" in (item.get("link") or "") for item in results) + 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