import pytest from src.backend.connectors import scrapers SAMPLE_ARTIST: str = "Kanye West" class TestCSVScraper: _class = scrapers.CSVScraper sample_url = ( "https://raw.githubusercontent.com/lukes/" "ISO-3166-Countries-with-Regional-Codes/master/all/all.csv" ) @pytest.fixture def instance(self): return self._class() @pytest.mark.asyncio async def test_get(self, instance): result = await instance.get(self.sample_url) countries = (r["name"] for r in result) assert "France" in countries class TestLastFM: _class = scrapers.LastFM @pytest.fixture def instance(self): def _(client=None): return self._class(client=client) return _ def test_client(self, instance): instance = instance() assert instance.client == instance._client class TestArtistSearch: @pytest.mark.asyncio async def test_artist_search(self, instance): result = await instance().artist_search(SAMPLE_ARTIST) assert SAMPLE_ARTIST in result @pytest.mark.asyncio async def test_artist_search_no_matches(self, instance): result = await instance().artist_search("xefweewfjkwgefkhbdv") assert result == [] @pytest.mark.asyncio async def test_artist_search_name_is_empty(self, instance): with pytest.raises(ValueError): await instance().artist_search("") class TestGetArtistPhotos: @pytest.mark.asyncio @pytest.mark.parametrize("client_provided", [True, False]) async def test_get_artist_photos(self, instance, client_provided): """Test that the function returns a list of URLs to the artist's photos. Parametrized test to check that the function works with and without a client provided. """ limit = 5 instance = instance( client=scrapers.httpx.AsyncClient() if client_provided else None ) result = await instance.get_artist_photos( SAMPLE_ARTIST, limit=limit, ) assert len(result) == limit assert all(url.endswith(".jpg") for url in result) @pytest.mark.asyncio async def test_get_artist_photos_name_is_empty(self, instance): with pytest.raises(ValueError): await instance().get_artist_photos("") @pytest.mark.asyncio async def test_get_artist_photos_needs_pagination(self, instance): with pytest.raises(NotImplementedError): await instance().get_artist_photos(SAMPLE_ARTIST, limit=100) @pytest.mark.asyncio async def test_get_artist_photos_no_matches(self, instance): result = await instance().get_artist_photos("xefweewfjkwgefkhbdv") assert result == []