Example #1
0
    async def test__to_song__w_album(self):
        mock_album = Album("Sweetener",
                           "spotify:album:3tx8gQqWbGwqIGZHqDNrGe",
                           [mock_spotify_artist_factory("Ariana Grande")],
                           songs=[],
                           popularity=100)
        mock_spotify_track = mock_spotify_track_factory(
            "successful",
            "spotify:track:5YeHLHDdQ4nKHk81XFWhCU",
            [mock_spotify_artist_factory("Ariana Grande")],
            popularity=99,
            album=mock_album,
        )
        self.spotify_client._to_album = AsyncMock(return_value=mock_album)

        song = await self.spotify_client._to_song(mock_spotify_track)

        self.assertEqual("successful", song.name)
        self.assertEqual("spotify:track:5YeHLHDdQ4nKHk81XFWhCU",
                         song.spotify_uri)
        self.assertEqual(["Ariana Grande"], song.artists)
        self.assertEqual(99, song.popularity)
        self.assertIsNotNone(song.album)
        self.assertEqual("Sweetener", song.album.name)
        self.assertEqual("spotify:album:3tx8gQqWbGwqIGZHqDNrGe",
                         song.album.spotify_uri)
        self.assertEqual(100, song.album.popularity)
        self.assertEqual([], song.album.songs)
Example #2
0
    async def test_find_song__multiple_artists(self, mock_inner_client_ctor):
        song = "bad guy"
        mock_spotify_tracks = [
            mock_spotify_track_factory(
                name="bad guy",
                artists=[
                    mock_spotify_artist_factory(name="Billie Eilish"),
                    mock_spotify_artist_factory(name="Finneas O'Connell")
                ],
                uri="spotify:track:2Fxmhks0bxGSBdJ92vM42m",
                popularity=None,
                album=None,
            ),
        ]
        inner_cli = self._get_inner_client(mock_inner_client_ctor)
        inner_cli.search = AsyncMock(return_value=MagicMock(
            tracks=mock_spotify_tracks))

        songs = await self.spotify_client.find_song(song)

        inner_cli.search.assert_called_once_with("bad guy", types=["track"])
        self.assertEqual(len(songs), 1)
        songs_list = list(songs)
        self.assertEqual(songs_list[0].name, "bad guy")
        self.assertEqual(songs_list[0].spotify_uri,
                         "spotify:track:2Fxmhks0bxGSBdJ92vM42m")
        self.assertEqual(songs_list[0].artists,
                         ["Billie Eilish", "Finneas O'Connell"])
Example #3
0
    async def test_find_song__ignores_partial_matches(self,
                                                      mock_inner_client_ctor):
        track = "Mock Track"
        mock_spotify_tracks = [
            mock_spotify_track_factory(
                "Mock Track Partial Match",
                "Mock URI",
                [mock_spotify_artist_factory("Mock Artist")],
                popularity=None,
                album=None,
            )
        ]
        inner_cli = self._get_inner_client(mock_inner_client_ctor)
        inner_cli.search = AsyncMock(return_value=MagicMock(
            tracks=mock_spotify_tracks))

        def _strip_song_metadata(track_name):
            return track_name

        self.spotify_client._strip_song_metadata = MagicMock(
            side_effect=_strip_song_metadata)

        results = list(await self.spotify_client.find_song(track))

        self.assertEqual(get_num_times_called(mock_inner_client_ctor), 1)
        inner_cli.search.assert_called_once_with("Mock Track", types=["track"])
        self.assertEqual(len(results), 0)
Example #4
0
    async def test_find_song__two_songs(self, mock_inner_client_ctor):
        song = "Sorry"
        mock_spotify_tracks = [
            mock_spotify_track_factory(
                "Sorry",
                "spotify:track:09CtPGIpYB4BrO8qb1RGsF",
                [mock_spotify_artist_factory("Justin Bieber")],
                popularity=1,
                album=None,
            ),
            mock_spotify_track_factory(
                "Sorry",
                "spotify:track:6rAXHPd18PZ6W8m9EectzH",
                [mock_spotify_artist_factory("Nothing But Thieves")],
                popularity=2,
                album=None,
            ),
        ]
        inner_cli = self._get_inner_client(mock_inner_client_ctor)
        inner_cli.search = AsyncMock(return_value=MagicMock(
            tracks=mock_spotify_tracks))

        songs = await self.spotify_client.find_song(song)

        inner_cli.search.assert_called_once_with("Sorry", types=["track"])
        self.assertEqual(len(songs), 2)
        songs_list = list(songs)
        self.assertEqual(songs_list[0].name, "Sorry")
        self.assertEqual(songs_list[1].name, "Sorry")
        self.assertIn(songs_list[0].spotify_uri, [
            "spotify:track:09CtPGIpYB4BrO8qb1RGsF",
            "spotify:track:6rAXHPd18PZ6W8m9EectzH"
        ])
        self.assertIn(songs_list[1].spotify_uri, [
            "spotify:track:09CtPGIpYB4BrO8qb1RGsF",
            "spotify:track:6rAXHPd18PZ6W8m9EectzH"
        ])
        self.assertIn(songs_list[0].artists[0],
                      ["Justin Bieber", "Nothing But Thieves"])
        self.assertIn(songs_list[1].artists[0],
                      ["Justin Bieber", "Nothing But Thieves"])
        self.assertIn(songs_list[0].popularity, [1, 2])
        self.assertIn(songs_list[1].popularity, [1, 2])
Example #5
0
    async def test_find_song__exact_and_partial_matches__keeps_exact_match_and_skips_partial_match(
            self, mock_inner_client_ctor):
        track = "Mock Track Exact Match"
        mock_spotify_tracks = [
            mock_spotify_track_factory(
                "Mock Track Exact Match",
                "Mock URI",
                [mock_spotify_artist_factory("Mock Artist")],
                popularity=None,
                album=None,
            ),
            mock_spotify_track_factory(
                "Mock Track Partial Match",
                "Mock URI",
                [mock_spotify_artist_factory("Mock Artist")],
                popularity=None,
                album=None,
            )
        ]
        inner_cli = self._get_inner_client(mock_inner_client_ctor)
        inner_cli.search = AsyncMock(return_value=MagicMock(
            tracks=mock_spotify_tracks))

        def _strip_song_metadata(track_name):
            return track_name

        self.spotify_client._strip_song_metadata = MagicMock(
            side_effect=_strip_song_metadata)

        results = list(await self.spotify_client.find_song(track))

        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].name, "Mock Track Exact Match")
        self.assertEqual(results[0].spotify_uri, "Mock URI")
        self.assertEqual(len(results[0].artists), 1)
        self.assertEqual(results[0].artists[0], "Mock Artist")
Example #6
0
    async def test__to_song__no_album(self):
        mock_spotify_track = mock_spotify_track_factory(
            "Sorry",
            "spotify:track:09CtPGIpYB4BrO8qb1RGsF",
            [mock_spotify_artist_factory("Justin Bieber")],
            popularity=51,
            album=None,
        )

        song = await self.spotify_client._to_song(mock_spotify_track)

        self.assertEqual("Sorry", song.name)
        self.assertEqual("spotify:track:09CtPGIpYB4BrO8qb1RGsF",
                         song.spotify_uri)
        self.assertEqual(["Justin Bieber"], song.artists)
        self.assertEqual(51, song.popularity)
        self.assertIsNone(song.album)
Example #7
0
    async def test_find_album(self, mock_inner_client_ctor):
        album = "Sweetener"
        mock_spotify_albums = [
            mock_spotify_album_factory(
                "Sweetener",
                "spotify:album:3tx8gQqWbGwqIGZHqDNrGe",
                [mock_spotify_artist_factory("Ariana Grande")],
                songs=[],
                popularity=1)
        ]
        inner_cli = self._get_inner_client(mock_inner_client_ctor)
        inner_cli.search = AsyncMock(return_value=MagicMock(
            albums=mock_spotify_albums))
        inner_cli.get_albums = AsyncMock(return_value=mock_spotify_albums)

        albums = list(await self.spotify_client.find_album(album))

        self.assertEqual(len(albums), 1)
        albums_list = list(albums)
        self.assertEqual(albums_list[0].name, "Sweetener")
        self.assertIn(albums_list[0].spotify_uri,
                      "spotify:album:3tx8gQqWbGwqIGZHqDNrGe")
        self.assertEqual(albums_list[0].popularity, 1)