import pytest from fastapi.testclient import TestClient from src.backend.main import app from src.backend.routers.audits.query import ( ROUTE, queries, top_label_artists_by_yt_revenue_cache, ) from src.backend.users.manage import verify_auth0 from tests_backend.conftest import TEST_LABEL_ID ROUTE = f"/api{ROUTE}" # Dependency override to mock the Auth0 verifier, so that tests can run without # needing to authenticate with Auth0. app.dependency_overrides[verify_auth0] = lambda: {"mock_auth0": "credentials"} class TestTopLabelArtistsByYouTubeRevenueLast12Months: label: int = TEST_LABEL_ID limit: int = 3 def call(self): with TestClient(app) as client: response = client.get( f"{ROUTE}/top_label_artists_by_yt_revenue", params={"label": self.label, "limit": self.limit}, ) assert response.status_code == 200, "Request failed" return response @pytest.fixture(autouse=True) def clear_top_label_artists_by_yt_revenue_cache(self): top_label_artists_by_yt_revenue_cache.clear() def test_top_label_artists_by_youtube_revenue_last_12_months(self): """Verify that the endpoint returns the expected data.""" response = self.call() data = response.json()["data"] expected_keys = { "id", "name", } assert len(data) == self.limit, "Wrong number of artists returned" assert all( set(artist.keys()) == expected_keys for artist in data ), "Wrong keys in artist objects" def test_top_label_artists_by_youtube_revenue_last_12_months_cached(self, mocker): """Verify that the query is cached after the first call.""" spy_queries = mocker.spy(queries, "get_top_label_artists_by_revenue") for _ in range(5): self.call() assert ( spy_queries.call_count == 1 ), "Query was called more than once but should be cached after the first call"