Download tests/test_dynamics.py from isold/MeloonTop: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/isold/MeloonTop/resolve/main/tests/test_dynamics.py
- Command line
-
hf download hf://spaces/isold/MeloonTop/tests/test_dynamics.py
-
curl -L -o test_dynamics.py https://huggingface.co/spaces/isold/MeloonTop/resolve/main/tests/test_dynamics.py
5.14 kB
| import pytest | |
| from fastapi.testclient import TestClient | |
| from unittest.mock import patch, MagicMock | |
| from app.main import app | |
| client = TestClient(app) | |
| # --- Helper Classes bridging MagicMock chains --- | |
| class MockResponse: | |
| def __init__(self, data, count=None): | |
| self.data = data | |
| self.count = count if count is not None else len(data) | |
| def mock_supabase(): | |
| """Mocks the Supabase client used in the dynamics router.""" | |
| with patch("app.routers.dynamics.supabase") as mock_db: | |
| mock_client = MagicMock() | |
| mock_db.table.return_value = mock_client | |
| yield mock_client | |
| def test_get_dynamics_empty(mock_supabase): | |
| """GET /api/dynamics/ falls back to scraper if the dynamics table is empty.""" | |
| # count query returns 0 | |
| mock_supabase.select.return_value.execute.return_value = MockResponse(data=[], count=0) | |
| response = client.get("/api/dynamics/") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| assert len(body["items"]) > 0 # Should contain mock items from scraper | |
| assert body["total"] > 0 | |
| def test_get_dynamics_with_data(mock_supabase): | |
| """GET /api/dynamics/ returns paginated items with correct camelCase mapping.""" | |
| mock_data = [ | |
| { | |
| "id": "1", | |
| "title": "Test Dynamics", | |
| "content": "Content", | |
| "url": "http://test.com", | |
| "source": "Test", | |
| "category": "Product", | |
| "published_at": "2024-01-01T00:00:00Z" | |
| } | |
| ] | |
| # count query | |
| mock_supabase.select.return_value.execute.return_value = MockResponse(data=mock_data, count=1) | |
| # paginated data query | |
| mock_supabase.select.return_value.order.return_value.range.return_value.execute.return_value = MockResponse(data=mock_data, count=1) | |
| response = client.get("/api/dynamics/") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| assert body["total"] == 1 | |
| assert body["page"] == 1 | |
| assert len(body["items"]) == 1 | |
| assert body["items"][0]["title"] == "Test Dynamics" | |
| assert body["items"][0]["publishedAt"] == "2024-01-01T00:00:00Z" | |
| def test_get_dynamics_fallback_ids(mock_supabase): | |
| """GET /api/dynamics/ falls back to scraper when DB is down; UUID IDs expected.""" | |
| mock_supabase.select.side_effect = Exception("DB Down") | |
| response = client.get("/api/dynamics/") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| assert len(body["items"]) > 0 | |
| for item in body["items"]: | |
| assert item["id"] is not None | |
| assert "-" in item["id"] # UUID format | |
| assert "publishedAt" in item | |
| assert "published_at" not in item | |
| def test_get_dynamics_pagination(mock_supabase): | |
| """GET /api/dynamics/?page=2&limit=1 returns the correct page.""" | |
| mock_data = [{"id": "2", "title": "Page 2 Item", "content": "C", "url": None, | |
| "source": None, "category": None, "published_at": "2024-01-02T00:00:00Z"}] | |
| # count query returns total=5 | |
| mock_supabase.select.return_value.execute.return_value = MockResponse(data=[], count=5) | |
| # paginated range query | |
| mock_supabase.select.return_value.order.return_value.range.return_value.execute.return_value = MockResponse(data=mock_data, count=5) | |
| response = client.get("/api/dynamics/?page=2&limit=1") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| assert body["page"] == 2 | |
| assert body["limit"] == 1 | |
| assert body["totalPages"] == 5 | |
| assert len(body["items"]) == 1 | |
| def test_sync_dynamics(mock_supabase): | |
| """POST /api/dynamics/sync inserts scraped items and returns success message.""" | |
| with patch("app.routers.dynamics.AIScraper") as MockScraper: | |
| mock_scraper_instance = MockScraper.return_value | |
| async def mock_scrape_all(): | |
| return [{"title": "Scraped", "content": "Content", "published_at": "2024-01-01T00:00:00Z"}] | |
| mock_scraper_instance.scrape_all = mock_scrape_all | |
| mock_supabase.insert.return_value.execute.return_value = MockResponse(data=[{"id": "1", "title": "Scraped"}]) | |
| response = client.post("/api/dynamics/sync") | |
| assert response.status_code == 200 | |
| assert "Successfully synced 1 items." in response.json()["message"] | |
| def test_sync_dynamics_table_missing(mock_supabase): | |
| """POST /api/dynamics/sync returns 200 warning (not 500) if table is missing (PGRST205).""" | |
| with patch("app.routers.dynamics.AIScraper") as MockScraper: | |
| mock_scraper_instance = MockScraper.return_value | |
| async def mock_scrape_all(): | |
| return [{"title": "Scraped", "content": "Content", "published_at": "2024-01-01T00:00:00Z"}] | |
| mock_scraper_instance.scrape_all = mock_scrape_all | |
| # Simulate PGRST205 error from Supabase | |
| mock_supabase.insert.side_effect = Exception("PGRST205: Could not find the table") | |
| response = client.post("/api/dynamics/sync") | |
| assert response.status_code == 200 | |
| # The returned message should mention the table missing situation | |
| msg = response.json()["message"] | |
| assert "simulation mode" in msg or "does not exist" in msg | |