import pytest from src.backend.connectors import scrapers @pytest.fixture(autouse=True) def mocker_get(mocker): return mocker.patch.object(scrapers.BaseScraper, "_get", autospec=True) class TestGenericScraper: _class = scrapers.GenericScraper @pytest.mark.parametrize("client_provided", [True, False]) def test_init(self, mocker, client_provided): mock_client = mocker.patch.object(scrapers.httpx, "AsyncClient", autospec=True) instance = self._class(client=mock_client() if client_provided else None) assert instance.client == mock_client.return_value class TestCSVScraper: _class = scrapers.CSVScraper @pytest.fixture def instance(self): return self._class() @pytest.mark.asyncio async def test_get_url_is_not_csv(self, instance): sample_url = "https://www.google.com/" with pytest.raises(ValueError): await instance.get(sample_url) class TestLastFM: _class = scrapers.LastFM @pytest.mark.asyncio async def test_dispatch(self, mocker_get): """Test that the dispatch quotes plus the URL.""" sample_url = "https://www.last.fm/music/Kanye West: The Track" await self._class()._dispatch(sample_url) called_with_url = mocker_get.call_args[0][1] assert "Kanye+West" in called_with_url assert " " not in called_with_url