Merge pull request #330 from pylast/add-album-and-image-to-get_now_playing

This commit is contained in:
Hugo van Kemenade 2020-06-22 21:50:22 +03:00 committed by GitHub
commit 108e3dda44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 279 additions and 328 deletions

View file

@ -4,39 +4,34 @@ on: [push, pull_request]
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-18.04
strategy:
matrix:
python-version: [3.8]
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: pip cache - name: Cache
uses: actions/cache@v1 uses: actions/cache@v2
with: with:
path: ~/.cache/pip path: |
key: lint-pip-${{ hashFiles('**/setup.py') }} ~/.cache/pip
~/.cache/pre-commit
key:
lint-v2-${{ hashFiles('**/setup.py') }}-${{
hashFiles('**/.pre-commit-config.yaml') }}
restore-keys: | restore-keys: |
lint-pip- lint-v2-
- name: pre-commit cache - name: Set up Python
uses: actions/cache@v1 uses: actions/setup-python@v2
with: with:
path: ~/.cache/pre-commit python-version: 3.8
key: lint-pre-commit-v1-${{ hashFiles('**/.pre-commit-config.yaml') }}
restore-keys: |
lint-pre-commit-v1-
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies - name: Install dependencies
run: | run: |
python -m pip install --upgrade pip python -m pip install -U pip
python -m pip install --upgrade tox python -m pip install -U tox
- name: Lint - name: Lint
run: tox -e lint run: tox -e lint
env:
PRE_COMMIT_COLOR: always

View file

@ -1,6 +1,6 @@
repos: repos:
- repo: https://github.com/asottile/pyupgrade - repo: https://github.com/asottile/pyupgrade
rev: v2.4.1 rev: v2.4.4
hooks: hooks:
- id: pyupgrade - id: pyupgrade
args: ["--py3-plus"] args: ["--py3-plus"]
@ -15,7 +15,7 @@ repos:
types: [] types: []
- repo: https://gitlab.com/pycqa/flake8 - repo: https://gitlab.com/pycqa/flake8
rev: 3.8.0a2 rev: 3.8.2
hooks: hooks:
- id: flake8 - id: flake8
additional_dependencies: [flake8-2020, flake8-implicit-str-concat] additional_dependencies: [flake8-2020, flake8-implicit-str-concat]
@ -36,7 +36,7 @@ repos:
- id: python-check-blanket-noqa - id: python-check-blanket-noqa
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0 rev: v3.1.0
hooks: hooks:
- id: check-merge-conflict - id: check-merge-conflict
- id: check-yaml - id: check-yaml

View file

@ -2128,6 +2128,8 @@ class Track(_Opus):
def get_album(self): def get_album(self):
"""Returns the album object of this track.""" """Returns the album object of this track."""
if "album" in self.info and self.info["album"] is not None:
return Album(self.artist, self.info["album"], self.network)
doc = self._request(self.ws_prefix + ".getInfo", True) doc = self._request(self.ws_prefix + ".getInfo", True)
@ -2338,8 +2340,9 @@ class User(_BaseObject, _Chartable):
artist = _extract(e, "artist") artist = _extract(e, "artist")
title = _extract(e, "name") title = _extract(e, "name")
info = {"album": _extract(e, "album"), "image": _extract_all(e, "image")}
return Track(artist, title, self.network, self.name) return Track(artist, title, self.network, self.name, info=info)
def get_recent_tracks(self, limit=10, cacheable=True, time_from=None, time_to=None): def get_recent_tracks(self, limit=10, cacheable=True, time_from=None, time_to=None):
""" """

View file

@ -2,8 +2,6 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import unittest
import pylast import pylast
from .test_pylast import TestPyLastWithLastFm from .test_pylast import TestPyLastWithLastFm
@ -18,8 +16,8 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
tags = album.get_top_tags(limit=1) tags = album.get_top_tags(limit=1)
# Assert # Assert
self.assertGreater(len(tags), 0) assert len(tags) > 0
self.assertIsInstance(tags[0], pylast.TopItem) assert isinstance(tags[0], pylast.TopItem)
def test_album_is_hashable(self): def test_album_is_hashable(self):
# Arrange # Arrange
@ -37,7 +35,7 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
track = lastfm_user.get_recent_tracks(limit=2)[0] track = lastfm_user.get_recent_tracks(limit=2)[0]
# Assert # Assert
self.assertTrue(hasattr(track, "album")) assert hasattr(track, "album")
def test_album_wiki_content(self): def test_album_wiki_content(self):
# Arrange # Arrange
@ -47,8 +45,8 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
wiki = album.get_wiki_content() wiki = album.get_wiki_content()
# Assert # Assert
self.assertIsNotNone(wiki) assert wiki is not None
self.assertGreaterEqual(len(wiki), 1) assert len(wiki) >= 1
def test_album_wiki_published_date(self): def test_album_wiki_published_date(self):
# Arrange # Arrange
@ -58,8 +56,8 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
wiki = album.get_wiki_published_date() wiki = album.get_wiki_published_date()
# Assert # Assert
self.assertIsNotNone(wiki) assert wiki is not None
self.assertGreaterEqual(len(wiki), 1) assert len(wiki) >= 1
def test_album_wiki_summary(self): def test_album_wiki_summary(self):
# Arrange # Arrange
@ -69,8 +67,8 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
wiki = album.get_wiki_summary() wiki = album.get_wiki_summary()
# Assert # Assert
self.assertIsNotNone(wiki) assert wiki is not None
self.assertGreaterEqual(len(wiki), 1) assert len(wiki) >= 1
def test_album_eq_none_is_false(self): def test_album_eq_none_is_false(self):
# Arrange # Arrange
@ -78,7 +76,7 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
album2 = pylast.Album("Test Artist", "Test Album", self.network) album2 = pylast.Album("Test Artist", "Test Album", self.network)
# Act / Assert # Act / Assert
self.assertNotEqual(album1, album2) assert album1 != album2
def test_album_ne_none_is_true(self): def test_album_ne_none_is_true(self):
# Arrange # Arrange
@ -86,7 +84,7 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
album2 = pylast.Album("Test Artist", "Test Album", self.network) album2 = pylast.Album("Test Artist", "Test Album", self.network)
# Act / Assert # Act / Assert
self.assertNotEqual(album1, album2) assert album1 != album2
def test_get_cover_image(self): def test_get_cover_image(self):
# Arrange # Arrange
@ -98,7 +96,3 @@ class TestPyLastAlbum(TestPyLastWithLastFm):
# Assert # Assert
self.assert_startswith(image, "https://") self.assert_startswith(image, "https://")
self.assert_endswith(image, ".png") self.assert_endswith(image, ".png")
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -2,8 +2,6 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import unittest
import pylast import pylast
import pytest import pytest
@ -19,13 +17,13 @@ class TestPyLastArtist(TestPyLastWithLastFm):
representation = repr(artist) representation = repr(artist)
# Assert # Assert
self.assertTrue(representation.startswith("pylast.Artist('Test Artist',")) assert representation.startswith("pylast.Artist('Test Artist',")
def test_artist_is_hashable(self): def test_artist_is_hashable(self):
# Arrange # Arrange
test_artist = self.network.get_artist("Test Artist") test_artist = self.network.get_artist("Test Artist")
artist = test_artist.get_similar(limit=2)[0].item artist = test_artist.get_similar(limit=2)[0].item
self.assertIsInstance(artist, pylast.Artist) assert isinstance(artist, pylast.Artist)
# Act/Assert # Act/Assert
self.helper_is_thing_hashable(artist) self.helper_is_thing_hashable(artist)
@ -38,8 +36,8 @@ class TestPyLastArtist(TestPyLastWithLastFm):
bio = artist.get_bio_published_date() bio = artist.get_bio_published_date()
# Assert # Assert
self.assertIsNotNone(bio) assert bio is not None
self.assertGreaterEqual(len(bio), 1) assert len(bio) >= 1
def test_bio_content(self): def test_bio_content(self):
# Arrange # Arrange
@ -49,8 +47,8 @@ class TestPyLastArtist(TestPyLastWithLastFm):
bio = artist.get_bio_content(language="en") bio = artist.get_bio_content(language="en")
# Assert # Assert
self.assertIsNotNone(bio) assert bio is not None
self.assertGreaterEqual(len(bio), 1) assert len(bio) >= 1
def test_bio_content_none(self): def test_bio_content_none(self):
# Arrange # Arrange
@ -61,7 +59,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
bio = artist.get_bio_content() bio = artist.get_bio_content()
# Assert # Assert
self.assertIsNone(bio) assert bio is None
def test_bio_summary(self): def test_bio_summary(self):
# Arrange # Arrange
@ -71,8 +69,8 @@ class TestPyLastArtist(TestPyLastWithLastFm):
bio = artist.get_bio_summary(language="en") bio = artist.get_bio_summary(language="en")
# Assert # Assert
self.assertIsNotNone(bio) assert bio is not None
self.assertGreaterEqual(len(bio), 1) assert len(bio) >= 1
def test_artist_top_tracks(self): def test_artist_top_tracks(self):
# Arrange # Arrange
@ -106,7 +104,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
things = artist.get_top_albums(limit=limit) things = artist.get_top_albums(limit=limit)
# Assert # Assert
self.assertEqual(len(things), 1) assert len(things) == 1
def test_artist_top_albums_limit_50(self): def test_artist_top_albums_limit_50(self):
# Arrange # Arrange
@ -118,7 +116,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
things = artist.get_top_albums(limit=limit) things = artist.get_top_albums(limit=limit)
# Assert # Assert
self.assertEqual(len(things), 50) assert len(things) == 50
def test_artist_top_albums_limit_100(self): def test_artist_top_albums_limit_100(self):
# Arrange # Arrange
@ -130,7 +128,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
things = artist.get_top_albums(limit=limit) things = artist.get_top_albums(limit=limit)
# Assert # Assert
self.assertEqual(len(things), 100) assert len(things) == 100
def test_artist_listener_count(self): def test_artist_listener_count(self):
# Arrange # Arrange
@ -140,8 +138,8 @@ class TestPyLastArtist(TestPyLastWithLastFm):
count = artist.get_listener_count() count = artist.get_listener_count()
# Assert # Assert
self.assertIsInstance(count, int) assert isinstance(count, int)
self.assertGreater(count, 0) assert count > 0
def test_tag_artist(self): def test_tag_artist(self):
# Arrange # Arrange
@ -153,13 +151,13 @@ class TestPyLastArtist(TestPyLastWithLastFm):
# Assert # Assert
tags = artist.get_tags() tags = artist.get_tags()
self.assertGreater(len(tags), 0) assert len(tags) > 0
found = False found = False
for tag in tags: for tag in tags:
if tag.name == "testing": if tag.name == "testing":
found = True found = True
break break
self.assertTrue(found) assert found
def test_remove_tag_of_type_text(self): def test_remove_tag_of_type_text(self):
# Arrange # Arrange
@ -177,7 +175,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
if tag.name == "testing": if tag.name == "testing":
found = True found = True
break break
self.assertFalse(found) assert not found
def test_remove_tag_of_type_tag(self): def test_remove_tag_of_type_tag(self):
# Arrange # Arrange
@ -195,7 +193,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
if tag.name == "testing": if tag.name == "testing":
found = True found = True
break break
self.assertFalse(found) assert not found
def test_remove_tags(self): def test_remove_tags(self):
# Arrange # Arrange
@ -210,15 +208,15 @@ class TestPyLastArtist(TestPyLastWithLastFm):
# Assert # Assert
tags_after = artist.get_tags() tags_after = artist.get_tags()
self.assertEqual(len(tags_after), len(tags_before) - 2) assert len(tags_after) == len(tags_before) - 2
found1, found2 = False, False found1, found2 = False, False
for tag in tags_after: for tag in tags_after:
if tag.name == "removetag1": if tag.name == "removetag1":
found1 = True found1 = True
elif tag.name == "removetag2": elif tag.name == "removetag2":
found2 = True found2 = True
self.assertFalse(found1) assert not found1
self.assertFalse(found2) assert not found2
def test_set_tags(self): def test_set_tags(self):
# Arrange # Arrange
@ -233,16 +231,16 @@ class TestPyLastArtist(TestPyLastWithLastFm):
# Assert # Assert
tags_after = artist.get_tags() tags_after = artist.get_tags()
self.assertNotEqual(tags_before, tags_after) assert tags_before != tags_after
self.assertEqual(len(tags_after), 2) assert len(tags_after) == 2
found1, found2 = False, False found1, found2 = False, False
for tag in tags_after: for tag in tags_after:
if tag.name == "settag1": if tag.name == "settag1":
found1 = True found1 = True
elif tag.name == "settag2": elif tag.name == "settag2":
found2 = True found2 = True
self.assertTrue(found1) assert found1
self.assertTrue(found2) assert found2
def test_artists(self): def test_artists(self):
# Arrange # Arrange
@ -259,13 +257,13 @@ class TestPyLastArtist(TestPyLastWithLastFm):
name_cap = artist1.get_name(properly_capitalized=True) name_cap = artist1.get_name(properly_capitalized=True)
# Assert # Assert
self.assertIn("https", image) assert "https" in image
self.assertGreater(playcount, 1) assert playcount > 1
self.assertNotEqual(artist1, artist2) assert artist1 != artist2
self.assertEqual(name.lower(), name_cap.lower()) assert name.lower() == name_cap.lower()
self.assertEqual(url, "https://www.last.fm/music/radiohead") assert url == "https://www.last.fm/music/radiohead"
self.assertEqual(mbid, "a74b1b7f-71a5-4011-9441-d0b5e4122711") assert mbid == "a74b1b7f-71a5-4011-9441-d0b5e4122711"
self.assertIsInstance(streamable, bool) assert isinstance(streamable, bool)
def test_artist_eq_none_is_false(self): def test_artist_eq_none_is_false(self):
# Arrange # Arrange
@ -273,7 +271,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
artist2 = pylast.Artist("Test Artist", self.network) artist2 = pylast.Artist("Test Artist", self.network)
# Act / Assert # Act / Assert
self.assertNotEqual(artist1, artist2) assert artist1 != artist2
def test_artist_ne_none_is_true(self): def test_artist_ne_none_is_true(self):
# Arrange # Arrange
@ -281,7 +279,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
artist2 = pylast.Artist("Test Artist", self.network) artist2 = pylast.Artist("Test Artist", self.network)
# Act / Assert # Act / Assert
self.assertNotEqual(artist1, artist2) assert artist1 != artist2
def test_artist_get_correction(self): def test_artist_get_correction(self):
# Arrange # Arrange
@ -291,7 +289,7 @@ class TestPyLastArtist(TestPyLastWithLastFm):
corrected_artist_name = artist.get_correction() corrected_artist_name = artist.get_correction()
# Assert # Assert
self.assertEqual(corrected_artist_name, "Guns N' Roses") assert corrected_artist_name == "Guns N' Roses"
@pytest.mark.xfail @pytest.mark.xfail
def test_get_userplaycount(self): def test_get_userplaycount(self):
@ -302,8 +300,4 @@ class TestPyLastArtist(TestPyLastWithLastFm):
playcount = artist.get_userplaycount() playcount = artist.get_userplaycount()
# Assert # Assert
self.assertGreaterEqual(playcount, 0) assert playcount >= 0
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -2,8 +2,6 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import unittest
import pylast import pylast
from .test_pylast import TestPyLastWithLastFm from .test_pylast import TestPyLastWithLastFm
@ -28,13 +26,9 @@ class TestPyLastCountry(TestPyLastWithLastFm):
url = country1.get_url() url = country1.get_url()
# Assert # Assert
self.assertIn("Italy", rep) assert "Italy" in rep
self.assertIn("pylast.Country", rep) assert "pylast.Country" in rep
self.assertEqual(text, "Italy") assert text == "Italy"
self.assertEqual(country1, country1) assert country1 == country1
self.assertNotEqual(country1, country2) assert country1 != country2
self.assertEqual(url, "https://www.last.fm/place/italy") assert url == "https://www.last.fm/place/italy"
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -2,8 +2,6 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import unittest
import pylast import pylast
from .test_pylast import TestPyLastWithLastFm from .test_pylast import TestPyLastWithLastFm
@ -53,8 +51,4 @@ class TestPyLastLibrary(TestPyLastWithLastFm):
library_user = library.get_user() library_user = library.get_user()
# Assert # Assert
self.assertEqual(library_user, user_to_get) assert library_user == user_to_get
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -2,8 +2,6 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import unittest
import pylast import pylast
from flaky import flaky from flaky import flaky
@ -26,7 +24,7 @@ class TestPyLastWithLibreFm(PyLastTestCase):
name = artist.get_name() name = artist.get_name()
# Assert # Assert
self.assertEqual(name, "Radiohead") assert name == "Radiohead"
def test_repr(self): def test_repr(self):
# Arrange # Arrange
@ -40,7 +38,3 @@ class TestPyLastWithLibreFm(PyLastTestCase):
# Assert # Assert
self.assert_startswith(representation, "pylast.LibreFMNetwork(") self.assert_startswith(representation, "pylast.LibreFMNetwork(")
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -2,16 +2,17 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import re
import time import time
import unittest
import pylast import pylast
import pytest
from .test_pylast import PY37, TestPyLastWithLastFm from .test_pylast import PY37, TestPyLastWithLastFm
class TestPyLastNetwork(TestPyLastWithLastFm): class TestPyLastNetwork(TestPyLastWithLastFm):
@unittest.skipUnless(PY37, "Only run on Python 3.7 to avoid collisions") @pytest.mark.skipif(not PY37, reason="Only run on Python 3.7 to avoid collisions")
def test_scrobble(self): def test_scrobble(self):
# Arrange # Arrange
artist = "test artist" artist = "test artist"
@ -26,8 +27,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
# Assert # Assert
# limit=2 to ignore now-playing: # limit=2 to ignore now-playing:
last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0] last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0]
self.assertEqual(str(last_scrobble.track.artist).lower(), artist) assert str(last_scrobble.track.artist).lower() == artist
self.assertEqual(str(last_scrobble.track.title).lower(), title) assert str(last_scrobble.track.title).lower() == title
def test_update_now_playing(self): def test_update_now_playing(self):
# Arrange # Arrange
@ -44,13 +45,18 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
# Assert # Assert
current_track = lastfm_user.get_now_playing() current_track = lastfm_user.get_now_playing()
self.assertIsNotNone(current_track) assert current_track is not None
self.assertEqual(str(current_track.title).lower(), "test title") assert str(current_track.title).lower() == "test title"
self.assertEqual(str(current_track.artist).lower(), "test artist") assert str(current_track.artist).lower() == "test artist"
assert current_track.info["album"] == "Test Album"
assert current_track.get_album().title == "Test Album"
assert len(current_track.info["image"])
assert re.search(r"^http.+$", current_track.info["image"][pylast.SIZE_LARGE])
def test_enable_rate_limiting(self): def test_enable_rate_limiting(self):
# Arrange # Arrange
self.assertFalse(self.network.is_rate_limited()) assert not self.network.is_rate_limited()
# Act # Act
self.network.enable_rate_limit() self.network.enable_rate_limit()
@ -62,13 +68,13 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
now = time.time() now = time.time()
# Assert # Assert
self.assertTrue(self.network.is_rate_limited()) assert self.network.is_rate_limited()
self.assertGreaterEqual(now - then, 0.2) assert now - then >= 0.2
def test_disable_rate_limiting(self): def test_disable_rate_limiting(self):
# Arrange # Arrange
self.network.enable_rate_limit() self.network.enable_rate_limit()
self.assertTrue(self.network.is_rate_limited()) assert self.network.is_rate_limited()
# Act # Act
self.network.disable_rate_limit() self.network.disable_rate_limit()
@ -78,14 +84,14 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
self.network.get_top_artists() self.network.get_top_artists()
# Assert # Assert
self.assertFalse(self.network.is_rate_limited()) assert not self.network.is_rate_limited()
def test_lastfm_network_name(self): def test_lastfm_network_name(self):
# Act # Act
name = str(self.network) name = str(self.network)
# Assert # Assert
self.assertEqual(name, "Last.fm Network") assert name == "Last.fm Network"
def test_geo_get_top_artists(self): def test_geo_get_top_artists(self):
# Arrange # Arrange
@ -93,9 +99,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
artists = self.network.get_geo_top_artists(country="United Kingdom", limit=1) artists = self.network.get_geo_top_artists(country="United Kingdom", limit=1)
# Assert # Assert
self.assertEqual(len(artists), 1) assert len(artists) == 1
self.assertIsInstance(artists[0], pylast.TopItem) assert isinstance(artists[0], pylast.TopItem)
self.assertIsInstance(artists[0].item, pylast.Artist) assert isinstance(artists[0].item, pylast.Artist)
def test_geo_get_top_tracks(self): def test_geo_get_top_tracks(self):
# Arrange # Arrange
@ -105,9 +111,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
) )
# Assert # Assert
self.assertEqual(len(tracks), 1) assert len(tracks) == 1
self.assertIsInstance(tracks[0], pylast.TopItem) assert isinstance(tracks[0], pylast.TopItem)
self.assertIsInstance(tracks[0].item, pylast.Track) assert isinstance(tracks[0].item, pylast.Track)
def test_network_get_top_artists_with_limit(self): def test_network_get_top_artists_with_limit(self):
# Arrange # Arrange
@ -182,12 +188,12 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
url = thing.get_url() url = thing.get_url()
# Assert # Assert
self.assertEqual(stringed, "Test Artist - Test Album") assert stringed == "Test Artist - Test Album"
self.assertIn("pylast.Album('Test Artist', 'Test Album',", rep) assert "pylast.Album('Test Artist', 'Test Album'," in rep
self.assertEqual(title, name) assert title == name
self.assertIsInstance(playcount, int) assert isinstance(playcount, int)
self.assertGreater(playcount, 1) assert playcount > 1
self.assertEqual("https://www.last.fm/music/test%2bartist/test%2balbum", url) assert "https://www.last.fm/music/test%2bartist/test%2balbum" == url
def test_track_data(self): def test_track_data(self):
# Arrange # Arrange
@ -202,15 +208,13 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
url = thing.get_url(pylast.DOMAIN_FRENCH) url = thing.get_url(pylast.DOMAIN_FRENCH)
# Assert # Assert
self.assertEqual(stringed, "Test Artist - test title") assert stringed == "Test Artist - test title"
self.assertIn("pylast.Track('Test Artist', 'test title',", rep) assert "pylast.Track('Test Artist', 'test title'," in rep
self.assertEqual(title, "test title") assert title == "test title"
self.assertEqual(title, name) assert title == name
self.assertIsInstance(playcount, int) assert isinstance(playcount, int)
self.assertGreater(playcount, 1) assert playcount > 1
self.assertEqual( assert "https://www.last.fm/fr/music/test%2bartist/_/test%2btitle" == url
"https://www.last.fm/fr/music/test%2bartist/_/test%2btitle", url
)
def test_country_top_artists(self): def test_country_top_artists(self):
# Arrange # Arrange
@ -232,10 +236,10 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
tags2 = user.get_top_tags(limit=1, cacheable=True) tags2 = user.get_top_tags(limit=1, cacheable=True)
# Assert # Assert
self.assertTrue(self.network.is_caching_enabled()) assert self.network.is_caching_enabled()
self.assertEqual(tags1, tags2) assert tags1 == tags2
self.network.disable_caching() self.network.disable_caching()
self.assertFalse(self.network.is_caching_enabled()) assert not self.network.is_caching_enabled()
def test_album_mbid(self): def test_album_mbid(self):
# Arrange # Arrange
@ -246,9 +250,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
album_mbid = album.get_mbid() album_mbid = album.get_mbid()
# Assert # Assert
self.assertIsInstance(album, pylast.Album) assert isinstance(album, pylast.Album)
self.assertEqual(album.title.lower(), "test") assert album.title.lower() == "test"
self.assertEqual(album_mbid, mbid) assert album_mbid == mbid
def test_artist_mbid(self): def test_artist_mbid(self):
# Arrange # Arrange
@ -258,8 +262,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
artist = self.network.get_artist_by_mbid(mbid) artist = self.network.get_artist_by_mbid(mbid)
# Assert # Assert
self.assertIsInstance(artist, pylast.Artist) assert isinstance(artist, pylast.Artist)
self.assertEqual(artist.name, "MusicBrainz Test Artist") assert artist.name == "MusicBrainz Test Artist"
def test_track_mbid(self): def test_track_mbid(self):
# Arrange # Arrange
@ -270,9 +274,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
track_mbid = track.get_mbid() track_mbid = track.get_mbid()
# Assert # Assert
self.assertIsInstance(track, pylast.Track) assert isinstance(track, pylast.Track)
self.assertEqual(track.title, "first") assert track.title == "first"
self.assertEqual(track_mbid, mbid) assert track_mbid == mbid
def test_init_with_token(self): def test_init_with_token(self):
# Arrange/Act # Arrange/Act
@ -287,7 +291,7 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
msg = str(exc) msg = str(exc)
# Assert # Assert
self.assertEqual(msg, "Unauthorized Token - This token has not been issued") assert msg == "Unauthorized Token - This token has not been issued"
def test_proxy(self): def test_proxy(self):
# Arrange # Arrange
@ -296,11 +300,11 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
# Act / Assert # Act / Assert
self.network.enable_proxy(host, port) self.network.enable_proxy(host, port)
self.assertTrue(self.network.is_proxy_enabled()) assert self.network.is_proxy_enabled()
self.assertEqual(self.network._get_proxy(), ["https://example.com", 1234]) assert self.network._get_proxy() == ["https://example.com", 1234]
self.network.disable_proxy() self.network.disable_proxy()
self.assertFalse(self.network.is_proxy_enabled()) assert not self.network.is_proxy_enabled()
def test_album_search(self): def test_album_search(self):
# Arrange # Arrange
@ -311,8 +315,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
results = search.get_next_page() results = search.get_next_page()
# Assert # Assert
self.assertIsInstance(results, list) assert isinstance(results, list)
self.assertIsInstance(results[0], pylast.Album) assert isinstance(results[0], pylast.Album)
def test_album_search_images(self): def test_album_search_images(self):
# Arrange # Arrange
@ -324,15 +328,15 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
images = results[0].info["image"] images = results[0].info["image"]
# Assert # Assert
self.assertEqual(len(images), 4) assert len(images) == 4
self.assert_startswith(images[pylast.SIZE_SMALL], "https://") self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
self.assert_endswith(images[pylast.SIZE_SMALL], ".png") self.assert_endswith(images[pylast.SIZE_SMALL], ".png")
self.assertIn("/34s/", images[pylast.SIZE_SMALL]) assert "/34s/" in images[pylast.SIZE_SMALL]
self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://") self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://")
self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png") self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png")
self.assertIn("/300x300/", images[pylast.SIZE_EXTRA_LARGE]) assert "/300x300/" in images[pylast.SIZE_EXTRA_LARGE]
def test_artist_search(self): def test_artist_search(self):
# Arrange # Arrange
@ -343,8 +347,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
results = search.get_next_page() results = search.get_next_page()
# Assert # Assert
self.assertIsInstance(results, list) assert isinstance(results, list)
self.assertIsInstance(results[0], pylast.Artist) assert isinstance(results[0], pylast.Artist)
def test_artist_search_images(self): def test_artist_search_images(self):
# Arrange # Arrange
@ -356,15 +360,15 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
images = results[0].info["image"] images = results[0].info["image"]
# Assert # Assert
self.assertEqual(len(images), 5) assert len(images) == 5
self.assert_startswith(images[pylast.SIZE_SMALL], "https://") self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
self.assert_endswith(images[pylast.SIZE_SMALL], ".png") self.assert_endswith(images[pylast.SIZE_SMALL], ".png")
self.assertIn("/34s/", images[pylast.SIZE_SMALL]) assert "/34s/" in images[pylast.SIZE_SMALL]
self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://") self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://")
self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png") self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png")
self.assertIn("/300x300/", images[pylast.SIZE_EXTRA_LARGE]) assert "/300x300/" in images[pylast.SIZE_EXTRA_LARGE]
def test_track_search(self): def test_track_search(self):
# Arrange # Arrange
@ -376,8 +380,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
results = search.get_next_page() results = search.get_next_page()
# Assert # Assert
self.assertIsInstance(results, list) assert isinstance(results, list)
self.assertIsInstance(results[0], pylast.Track) assert isinstance(results[0], pylast.Track)
def test_track_search_images(self): def test_track_search_images(self):
# Arrange # Arrange
@ -390,15 +394,15 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
images = results[0].info["image"] images = results[0].info["image"]
# Assert # Assert
self.assertEqual(len(images), 4) assert len(images) == 4
self.assert_startswith(images[pylast.SIZE_SMALL], "https://") self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
self.assert_endswith(images[pylast.SIZE_SMALL], ".png") self.assert_endswith(images[pylast.SIZE_SMALL], ".png")
self.assertIn("/34s/", images[pylast.SIZE_SMALL]) assert "/34s/" in images[pylast.SIZE_SMALL]
self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://") self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://")
self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png") self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png")
self.assertIn("/300x300/", images[pylast.SIZE_EXTRA_LARGE]) assert "/300x300/" in images[pylast.SIZE_EXTRA_LARGE]
def test_search_get_total_result_count(self): def test_search_get_total_result_count(self):
# Arrange # Arrange
@ -410,8 +414,4 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
total = search.get_total_result_count() total = search.get_total_result_count()
# Assert # Assert
self.assertGreater(int(total), 10000) assert int(total) > 10000
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -5,7 +5,6 @@ Integration (not unit) tests for pylast.py
import os import os
import sys import sys
import time import time
import unittest
import pylast import pylast
import pytest import pytest
@ -33,12 +32,12 @@ def load_secrets():
return doc return doc
class PyLastTestCase(unittest.TestCase): class PyLastTestCase:
def assert_startswith(self, str, prefix, start=None, end=None): def assert_startswith(self, str, prefix, start=None, end=None):
self.assertTrue(str.startswith(prefix, start, end)) assert str.startswith(prefix, start, end)
def assert_endswith(self, str, suffix, start=None, end=None): def assert_endswith(self, str, suffix, start=None, end=None):
self.assertTrue(str.endswith(suffix, start, end)) assert str.endswith(suffix, start, end)
@flaky(max_runs=3, min_passes=1) @flaky(max_runs=3, min_passes=1)
@ -49,20 +48,21 @@ class TestPyLastWithLastFm(PyLastTestCase):
def unix_timestamp(self): def unix_timestamp(self):
return int(time.time()) return int(time.time())
def setUp(self): @classmethod
if self.__class__.secrets is None: def setup_class(cls):
self.__class__.secrets = load_secrets() if cls.secrets is None:
cls.secrets = load_secrets()
self.username = self.__class__.secrets["username"] cls.username = cls.secrets["username"]
password_hash = self.__class__.secrets["password_hash"] password_hash = cls.secrets["password_hash"]
api_key = self.__class__.secrets["api_key"] api_key = cls.secrets["api_key"]
api_secret = self.__class__.secrets["api_secret"] api_secret = cls.secrets["api_secret"]
self.network = pylast.LastFMNetwork( cls.network = pylast.LastFMNetwork(
api_key=api_key, api_key=api_key,
api_secret=api_secret, api_secret=api_secret,
username=self.username, username=cls.username,
password_hash=password_hash, password_hash=password_hash,
) )
@ -74,19 +74,19 @@ class TestPyLastWithLastFm(PyLastTestCase):
things.add(thing) things.add(thing)
# Assert # Assert
self.assertIsNotNone(thing) assert thing is not None
self.assertEqual(len(things), 1) assert len(things) == 1
def helper_validate_results(self, a, b, c): def helper_validate_results(self, a, b, c):
# Assert # Assert
self.assertIsNotNone(a) assert a is not None
self.assertIsNotNone(b) assert b is not None
self.assertIsNotNone(c) assert c is not None
self.assertGreaterEqual(len(a), 0) assert len(a) >= 0
self.assertGreaterEqual(len(b), 0) assert len(b) >= 0
self.assertGreaterEqual(len(c), 0) assert len(c) >= 0
self.assertEqual(a, b) assert a == b
self.assertEqual(b, c) assert b == c
def helper_validate_cacheable(self, thing, function_name): def helper_validate_cacheable(self, thing, function_name):
# Arrange # Arrange
@ -103,35 +103,31 @@ class TestPyLastWithLastFm(PyLastTestCase):
def helper_at_least_one_thing_in_top_list(self, things, expected_type): def helper_at_least_one_thing_in_top_list(self, things, expected_type):
# Assert # Assert
self.assertGreater(len(things), 1) assert len(things) > 1
self.assertIsInstance(things, list) assert isinstance(things, list)
self.assertIsInstance(things[0], pylast.TopItem) assert isinstance(things[0], pylast.TopItem)
self.assertIsInstance(things[0].item, expected_type) assert isinstance(things[0].item, expected_type)
def helper_only_one_thing_in_top_list(self, things, expected_type): def helper_only_one_thing_in_top_list(self, things, expected_type):
# Assert # Assert
self.assertEqual(len(things), 1) assert len(things) == 1
self.assertIsInstance(things, list) assert isinstance(things, list)
self.assertIsInstance(things[0], pylast.TopItem) assert isinstance(things[0], pylast.TopItem)
self.assertIsInstance(things[0].item, expected_type) assert isinstance(things[0].item, expected_type)
def helper_only_one_thing_in_list(self, things, expected_type): def helper_only_one_thing_in_list(self, things, expected_type):
# Assert # Assert
self.assertEqual(len(things), 1) assert len(things) == 1
self.assertIsInstance(things, list) assert isinstance(things, list)
self.assertIsInstance(things[0], expected_type) assert isinstance(things[0], expected_type)
def helper_two_different_things_in_top_list(self, things, expected_type): def helper_two_different_things_in_top_list(self, things, expected_type):
# Assert # Assert
self.assertEqual(len(things), 2) assert len(things) == 2
thing1 = things[0] thing1 = things[0]
thing2 = things[1] thing2 = things[1]
self.assertIsInstance(thing1, pylast.TopItem) assert isinstance(thing1, pylast.TopItem)
self.assertIsInstance(thing2, pylast.TopItem) assert isinstance(thing2, pylast.TopItem)
self.assertIsInstance(thing1.item, expected_type) assert isinstance(thing1.item, expected_type)
self.assertIsInstance(thing2.item, expected_type) assert isinstance(thing2.item, expected_type)
self.assertNotEqual(thing1, thing2) assert thing1 != thing2
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -2,8 +2,6 @@
""" """
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import unittest
import pylast import pylast
from .test_pylast import TestPyLastWithLastFm from .test_pylast import TestPyLastWithLastFm
@ -49,14 +47,10 @@ class TestPyLastTag(TestPyLastWithLastFm):
url = tag1.get_url() url = tag1.get_url()
# Assert # Assert
self.assertEqual("blues", tag_str) assert "blues" == tag_str
self.assertIn("pylast.Tag", tag_repr) assert "pylast.Tag" in tag_repr
self.assertIn("blues", tag_repr) assert "blues" in tag_repr
self.assertEqual("blues", name) assert "blues" == name
self.assertEqual(tag1, tag1) assert tag1 == tag1
self.assertNotEqual(tag1, tag2) assert tag1 != tag2
self.assertEqual(url, "https://www.last.fm/tag/blues") assert url == "https://www.last.fm/tag/blues"
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -3,9 +3,9 @@
Integration (not unit) tests for pylast.py Integration (not unit) tests for pylast.py
""" """
import time import time
import unittest
import pylast import pylast
import pytest
from .test_pylast import PY37, TestPyLastWithLastFm from .test_pylast import PY37, TestPyLastWithLastFm
@ -23,10 +23,10 @@ class TestPyLastTrack(TestPyLastWithLastFm):
# Assert # Assert
loved = lastfm_user.get_loved_tracks(limit=1) loved = lastfm_user.get_loved_tracks(limit=1)
self.assertEqual(str(loved[0].track.artist).lower(), "test artist") assert str(loved[0].track.artist).lower() == "test artist"
self.assertEqual(str(loved[0].track.title).lower(), "test title") assert str(loved[0].track.title).lower() == "test title"
@unittest.skipUnless(PY37, "Only run on Python 3.7 to avoid collisions") @pytest.mark.skipif(not PY37, reason="Only run on Python 3.7 to avoid collisions")
def test_unlove(self): def test_unlove(self):
# Arrange # Arrange
artist = pylast.Artist("Test Artist", self.network) artist = pylast.Artist("Test Artist", self.network)
@ -42,8 +42,8 @@ class TestPyLastTrack(TestPyLastWithLastFm):
# Assert # Assert
loved = lastfm_user.get_loved_tracks(limit=1) loved = lastfm_user.get_loved_tracks(limit=1)
if len(loved): # OK to be empty but if not: if len(loved): # OK to be empty but if not:
self.assertNotEqual(str(loved[0].track.artist), "Test Artist") assert str(loved[0].track.artist) != "Test Artist"
self.assertNotEqual(str(loved[0].track.title), "test title") assert str(loved[0].track.title) != "test title"
def test_user_play_count_in_track_info(self): def test_user_play_count_in_track_info(self):
# Arrange # Arrange
@ -57,7 +57,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
count = track.get_userplaycount() count = track.get_userplaycount()
# Assert # Assert
self.assertGreaterEqual(count, 0) assert count >= 0
def test_user_loved_in_track_info(self): def test_user_loved_in_track_info(self):
# Arrange # Arrange
@ -71,15 +71,15 @@ class TestPyLastTrack(TestPyLastWithLastFm):
loved = track.get_userloved() loved = track.get_userloved()
# Assert # Assert
self.assertIsNotNone(loved) assert loved is not None
self.assertIsInstance(loved, bool) assert isinstance(loved, bool)
self.assertNotIsInstance(loved, str) assert not isinstance(loved, str)
def test_track_is_hashable(self): def test_track_is_hashable(self):
# Arrange # Arrange
artist = self.network.get_artist("Test Artist") artist = self.network.get_artist("Test Artist")
track = artist.get_top_tracks()[0].item track = artist.get_top_tracks()[0].item
self.assertIsInstance(track, pylast.Track) assert isinstance(track, pylast.Track)
# Act/Assert # Act/Assert
self.helper_is_thing_hashable(track) self.helper_is_thing_hashable(track)
@ -92,8 +92,8 @@ class TestPyLastTrack(TestPyLastWithLastFm):
wiki = track.get_wiki_content() wiki = track.get_wiki_content()
# Assert # Assert
self.assertIsNotNone(wiki) assert wiki is not None
self.assertGreaterEqual(len(wiki), 1) assert len(wiki) >= 1
def test_track_wiki_summary(self): def test_track_wiki_summary(self):
# Arrange # Arrange
@ -103,8 +103,8 @@ class TestPyLastTrack(TestPyLastWithLastFm):
wiki = track.get_wiki_summary() wiki = track.get_wiki_summary()
# Assert # Assert
self.assertIsNotNone(wiki) assert wiki is not None
self.assertGreaterEqual(len(wiki), 1) assert len(wiki) >= 1
def test_track_get_duration(self): def test_track_get_duration(self):
# Arrange # Arrange
@ -114,7 +114,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
duration = track.get_duration() duration = track.get_duration()
# Assert # Assert
self.assertGreaterEqual(duration, 200000) assert duration >= 200000
def test_track_is_streamable(self): def test_track_is_streamable(self):
# Arrange # Arrange
@ -124,7 +124,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
streamable = track.is_streamable() streamable = track.is_streamable()
# Assert # Assert
self.assertFalse(streamable) assert not streamable
def test_track_is_fulltrack_available(self): def test_track_is_fulltrack_available(self):
# Arrange # Arrange
@ -134,7 +134,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
fulltrack_available = track.is_fulltrack_available() fulltrack_available = track.is_fulltrack_available()
# Assert # Assert
self.assertFalse(fulltrack_available) assert not fulltrack_available
def test_track_get_album(self): def test_track_get_album(self):
# Arrange # Arrange
@ -144,7 +144,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
album = track.get_album() album = track.get_album()
# Assert # Assert
self.assertEqual(str(album), "Nirvana - Nevermind") assert str(album) == "Nirvana - Nevermind"
def test_track_get_similar(self): def test_track_get_similar(self):
# Arrange # Arrange
@ -159,17 +159,17 @@ class TestPyLastTrack(TestPyLastWithLastFm):
if str(track.item) == "Madonna - Vogue": if str(track.item) == "Madonna - Vogue":
found = True found = True
break break
self.assertTrue(found) assert found
def test_track_get_similar_limits(self): def test_track_get_similar_limits(self):
# Arrange # Arrange
track = pylast.Track("Cher", "Believe", self.network) track = pylast.Track("Cher", "Believe", self.network)
# Act/Assert # Act/Assert
self.assertEqual(len(track.get_similar(limit=20)), 20) assert len(track.get_similar(limit=20)) == 20
self.assertLessEqual(len(track.get_similar(limit=10)), 10) assert len(track.get_similar(limit=10)) <= 10
self.assertGreaterEqual(len(track.get_similar(limit=None)), 23) assert len(track.get_similar(limit=None)) >= 23
self.assertGreaterEqual(len(track.get_similar(limit=0)), 23) assert len(track.get_similar(limit=0)) >= 23
def test_tracks_notequal(self): def test_tracks_notequal(self):
# Arrange # Arrange
@ -178,7 +178,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
# Act # Act
# Assert # Assert
self.assertNotEqual(track1, track2) assert track1 != track2
def test_track_title_prop_caps(self): def test_track_title_prop_caps(self):
# Arrange # Arrange
@ -188,7 +188,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
title = track.get_title(properly_capitalized=True) title = track.get_title(properly_capitalized=True)
# Assert # Assert
self.assertEqual(title, "Test Title") assert title == "Test Title"
def test_track_listener_count(self): def test_track_listener_count(self):
# Arrange # Arrange
@ -198,7 +198,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
count = track.get_listener_count() count = track.get_listener_count()
# Assert # Assert
self.assertGreater(count, 21) assert count > 21
def test_album_tracks(self): def test_album_tracks(self):
# Arrange # Arrange
@ -209,10 +209,10 @@ class TestPyLastTrack(TestPyLastWithLastFm):
url = tracks[0].get_url() url = tracks[0].get_url()
# Assert # Assert
self.assertIsInstance(tracks, list) assert isinstance(tracks, list)
self.assertIsInstance(tracks[0], pylast.Track) assert isinstance(tracks[0], pylast.Track)
self.assertEqual(len(tracks), 1) assert len(tracks) == 1
self.assertTrue(url.startswith("https://www.last.fm/music/test")) assert url.startswith("https://www.last.fm/music/test")
def test_track_eq_none_is_false(self): def test_track_eq_none_is_false(self):
# Arrange # Arrange
@ -220,7 +220,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
track2 = pylast.Track("Test Artist", "test title", self.network) track2 = pylast.Track("Test Artist", "test title", self.network)
# Act / Assert # Act / Assert
self.assertNotEqual(track1, track2) assert track1 != track2
def test_track_ne_none_is_true(self): def test_track_ne_none_is_true(self):
# Arrange # Arrange
@ -228,7 +228,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
track2 = pylast.Track("Test Artist", "test title", self.network) track2 = pylast.Track("Test Artist", "test title", self.network)
# Act / Assert # Act / Assert
self.assertNotEqual(track1, track2) assert track1 != track2
def test_track_get_correction(self): def test_track_get_correction(self):
# Arrange # Arrange
@ -238,7 +238,7 @@ class TestPyLastTrack(TestPyLastWithLastFm):
corrected_track_name = track.get_correction() corrected_track_name = track.get_correction()
# Assert # Assert
self.assertEqual(corrected_track_name, "Mr. Brownstone") assert corrected_track_name == "Mr. Brownstone"
def test_track_with_no_mbid(self): def test_track_with_no_mbid(self):
# Arrange # Arrange
@ -248,8 +248,4 @@ class TestPyLastTrack(TestPyLastWithLastFm):
mbid = track.get_mbid() mbid = track.get_mbid()
# Assert # Assert
self.assertIsNone(mbid) assert mbid is None
if __name__ == "__main__":
unittest.main(failfast=True)

View file

@ -5,7 +5,7 @@ Integration (not unit) tests for pylast.py
import calendar import calendar
import datetime as dt import datetime as dt
import os import os
import unittest import re
import warnings import warnings
import pylast import pylast
@ -33,7 +33,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
string = str(user) string = str(user)
# Assert # Assert
self.assertEqual(string, "RJ") assert string == "RJ"
def test_equality(self): def test_equality(self):
# Arrange # Arrange
@ -43,9 +43,9 @@ class TestPyLastUser(TestPyLastWithLastFm):
not_a_user = self.network not_a_user = self.network
# Act / Assert # Act / Assert
self.assertEqual(user_1a, user_1b) assert user_1a == user_1b
self.assertNotEqual(user_1a, user_2) assert user_1a != user_2
self.assertNotEqual(user_1a, not_a_user) assert user_1a != not_a_user
def test_get_name(self): def test_get_name(self):
# Arrange # Arrange
@ -55,7 +55,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
name = user.get_name(properly_capitalized=True) name = user.get_name(properly_capitalized=True)
# Assert # Assert
self.assertEqual(name, "RJ") assert name == "RJ"
def test_get_user_registration(self): def test_get_user_registration(self):
# Arrange # Arrange
@ -67,11 +67,11 @@ class TestPyLastUser(TestPyLastWithLastFm):
# Assert # Assert
if int(registered): if int(registered):
# Last.fm API broken? Used to be yyyy-mm-dd not Unix timestamp # Last.fm API broken? Used to be yyyy-mm-dd not Unix timestamp
self.assertEqual(registered, "1037793040") assert registered == "1037793040"
else: else:
# Old way # Old way
# Just check date because of timezones # Just check date because of timezones
self.assertIn("2002-11-20 ", registered) assert "2002-11-20 " in registered
def test_get_user_unixtime_registration(self): def test_get_user_unixtime_registration(self):
# Arrange # Arrange
@ -82,7 +82,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
# Assert # Assert
# Just check date because of timezones # Just check date because of timezones
self.assertEqual(unixtime_registered, 1037793040) assert unixtime_registered == 1037793040
def test_get_countryless_user(self): def test_get_countryless_user(self):
# Arrange # Arrange
@ -93,7 +93,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
country = lastfm_user.get_country() country = lastfm_user.get_country()
# Assert # Assert
self.assertIsNone(country) assert country is None
def test_user_get_country(self): def test_user_get_country(self):
# Arrange # Arrange
@ -103,7 +103,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
country = lastfm_user.get_country() country = lastfm_user.get_country()
# Assert # Assert
self.assertEqual(str(country), "United Kingdom") assert str(country) == "United Kingdom"
def test_user_equals_none(self): def test_user_equals_none(self):
# Arrange # Arrange
@ -113,7 +113,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
value = lastfm_user is None value = lastfm_user is None
# Assert # Assert
self.assertFalse(value) assert not value
def test_user_not_equal_to_none(self): def test_user_not_equal_to_none(self):
# Arrange # Arrange
@ -123,7 +123,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
value = lastfm_user is not None value = lastfm_user is not None
# Assert # Assert
self.assertTrue(value) assert value
def test_now_playing_user_with_no_scrobbles(self): def test_now_playing_user_with_no_scrobbles(self):
# Arrange # Arrange
@ -134,7 +134,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
current_track = user.get_now_playing() current_track = user.get_now_playing()
# Assert # Assert
self.assertIsNone(current_track) assert current_track is None
def test_love_limits(self): def test_love_limits(self):
# Arrange # Arrange
@ -142,10 +142,10 @@ class TestPyLastUser(TestPyLastWithLastFm):
user = self.network.get_user("test-user") user = self.network.get_user("test-user")
# Act/Assert # Act/Assert
self.assertEqual(len(user.get_loved_tracks(limit=20)), 20) assert len(user.get_loved_tracks(limit=20)) == 20
self.assertLessEqual(len(user.get_loved_tracks(limit=100)), 100) assert len(user.get_loved_tracks(limit=100)) <= 100
self.assertGreaterEqual(len(user.get_loved_tracks(limit=None)), 23) assert len(user.get_loved_tracks(limit=None)) >= 23
self.assertGreaterEqual(len(user.get_loved_tracks(limit=0)), 23) assert len(user.get_loved_tracks(limit=0)) >= 23
def test_user_is_hashable(self): def test_user_is_hashable(self):
# Arrange # Arrange
@ -183,7 +183,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
os.remove(filename) os.remove(filename)
# Assert # Assert
self.assertEqual(lastfm_user, loaded_user) assert lastfm_user == loaded_user
@pytest.mark.xfail @pytest.mark.xfail
def test_cacheable_user(self): def test_cacheable_user(self):
@ -217,10 +217,10 @@ class TestPyLastUser(TestPyLastWithLastFm):
def helper_assert_chart(self, chart, expected_type): def helper_assert_chart(self, chart, expected_type):
# Assert # Assert
self.assertIsNotNone(chart) assert chart is not None
self.assertGreater(len(chart), 0) assert len(chart) > 0
self.assertIsInstance(chart[0], pylast.TopItem) assert isinstance(chart[0], pylast.TopItem)
self.assertIsInstance(chart[0].item, expected_type) assert isinstance(chart[0].item, expected_type)
def helper_get_assert_charts(self, thing, date): def helper_get_assert_charts(self, thing, date):
# Arrange # Arrange
@ -241,10 +241,10 @@ class TestPyLastUser(TestPyLastWithLastFm):
def helper_dates_valid(self, dates): def helper_dates_valid(self, dates):
# Assert # Assert
self.assertGreaterEqual(len(dates), 1) assert len(dates) >= 1
self.assertIsInstance(dates[0], tuple) assert isinstance(dates[0], tuple)
(start, end) = dates[0] (start, end) = dates[0]
self.assertLess(start, end) assert start < end
def test_user_charts(self): def test_user_charts(self):
# Arrange # Arrange
@ -276,8 +276,8 @@ class TestPyLastUser(TestPyLastWithLastFm):
self.helper_only_one_thing_in_top_list(albums, pylast.Album) self.helper_only_one_thing_in_top_list(albums, pylast.Album)
top_album = albums[0].item top_album = albums[0].item
self.assertTrue(len(top_album.info["image"])) assert len(top_album.info["image"])
self.assertRegex(top_album.info["image"][pylast.SIZE_LARGE], r"^http.+$") assert re.search(r"^http.+$", top_album.info["image"][pylast.SIZE_LARGE])
def test_user_tagged_artists(self): def test_user_tagged_artists(self):
# Arrange # Arrange
@ -328,8 +328,8 @@ class TestPyLastUser(TestPyLastWithLastFm):
non_subscriber_is_subscriber = non_subscriber.is_subscriber() non_subscriber_is_subscriber = non_subscriber.is_subscriber()
# Assert # Assert
self.assertTrue(subscriber_is_subscriber) assert subscriber_is_subscriber
self.assertFalse(non_subscriber_is_subscriber) assert not non_subscriber_is_subscriber
def test_user_get_image(self): def test_user_get_image(self):
# Arrange # Arrange
@ -349,7 +349,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
library = user.get_library() library = user.get_library()
# Assert # Assert
self.assertIsInstance(library, pylast.Library) assert isinstance(library, pylast.Library)
def test_get_recent_tracks_from_to(self): def test_get_recent_tracks_from_to(self):
# Arrange # Arrange
@ -364,9 +364,9 @@ class TestPyLastUser(TestPyLastWithLastFm):
tracks = lastfm_user.get_recent_tracks(time_from=utc_start, time_to=utc_end) tracks = lastfm_user.get_recent_tracks(time_from=utc_start, time_to=utc_end)
# Assert # Assert
self.assertEqual(len(tracks), 1) assert len(tracks) == 1
self.assertEqual(str(tracks[0].track.artist), "Johnny Cash") assert str(tracks[0].track.artist) == "Johnny Cash"
self.assertEqual(str(tracks[0].track.title), "Ring of Fire") assert str(tracks[0].track.title) == "Ring of Fire"
def test_get_recent_tracks_limit_none(self): def test_get_recent_tracks_limit_none(self):
# Arrange # Arrange
@ -383,9 +383,9 @@ class TestPyLastUser(TestPyLastWithLastFm):
) )
# Assert # Assert
self.assertEqual(len(tracks), 11) assert len(tracks) == 11
self.assertEqual(str(tracks[0].track.artist), "Seun Kuti & Egypt 80") assert str(tracks[0].track.artist) == "Seun Kuti & Egypt 80"
self.assertEqual(str(tracks[0].track.title), "Struggles Sounds") assert str(tracks[0].track.title) == "Struggles Sounds"
def test_get_playcount(self): def test_get_playcount(self):
# Arrange # Arrange
@ -395,7 +395,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
playcount = user.get_playcount() playcount = user.get_playcount()
# Assert # Assert
self.assertGreaterEqual(playcount, 128387) assert playcount >= 128387
def test_get_image(self): def test_get_image(self):
# Arrange # Arrange
@ -416,7 +416,7 @@ class TestPyLastUser(TestPyLastWithLastFm):
url = user.get_url() url = user.get_url()
# Assert # Assert
self.assertEqual(url, "https://www.last.fm/user/rj") assert url == "https://www.last.fm/user/rj"
def test_get_weekly_artist_charts(self): def test_get_weekly_artist_charts(self):
# Arrange # Arrange
@ -427,8 +427,8 @@ class TestPyLastUser(TestPyLastWithLastFm):
artist, weight = charts[0] artist, weight = charts[0]
# Assert # Assert
self.assertIsNotNone(artist) assert artist is not None
self.assertIsInstance(artist.network, pylast.LastFMNetwork) assert isinstance(artist.network, pylast.LastFMNetwork)
def test_get_weekly_track_charts(self): def test_get_weekly_track_charts(self):
# Arrange # Arrange
@ -439,8 +439,8 @@ class TestPyLastUser(TestPyLastWithLastFm):
track, weight = charts[0] track, weight = charts[0]
# Assert # Assert
self.assertIsNotNone(track) assert track is not None
self.assertIsInstance(track.network, pylast.LastFMNetwork) assert isinstance(track.network, pylast.LastFMNetwork)
def test_user_get_track_scrobbles(self): def test_user_get_track_scrobbles(self):
# Arrange # Arrange
@ -452,9 +452,9 @@ class TestPyLastUser(TestPyLastWithLastFm):
scrobbles = user.get_track_scrobbles(artist, title) scrobbles = user.get_track_scrobbles(artist, title)
# Assert # Assert
self.assertGreater(len(scrobbles), 0) assert len(scrobbles) > 0
self.assertEqual(str(scrobbles[0].track.artist), "France Gall") assert str(scrobbles[0].track.artist) == "France Gall"
self.assertEqual(scrobbles[0].track.title, "Laisse Tomber Les Filles") assert scrobbles[0].track.title == "Laisse Tomber Les Filles"
def test_cacheable_user_get_track_scrobbles(self): def test_cacheable_user_get_track_scrobbles(self):
# Arrange # Arrange
@ -475,12 +475,9 @@ class TestPyLastUser(TestPyLastWithLastFm):
lastfm_user = self.network.get_user(self.username) lastfm_user = self.network.get_user(self.username)
# Act / Assert # Act / Assert
with warnings.catch_warnings(), self.assertRaisesRegex( with warnings.catch_warnings(), pytest.raises(
pylast.WSError, "Deprecated - This type of request is no longer supported" pylast.WSError,
match="Deprecated - This type of request is no longer supported",
): ):
warnings.filterwarnings("ignore", category=DeprecationWarning) warnings.filterwarnings("ignore", category=DeprecationWarning)
lastfm_user.get_artist_tracks(artist="Test Artist") lastfm_user.get_artist_tracks(artist="Test Artist")
if __name__ == "__main__":
unittest.main(failfast=True)