Replace unittest with pytest
This commit is contained in:
parent
1160ee1513
commit
aae4bb3693
10 changed files with 253 additions and 305 deletions
|
@ -2,16 +2,17 @@
|
|||
"""
|
||||
Integration (not unit) tests for pylast.py
|
||||
"""
|
||||
import re
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import pylast
|
||||
import pytest
|
||||
|
||||
from .test_pylast import PY37, 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):
|
||||
# Arrange
|
||||
artist = "test artist"
|
||||
|
@ -26,8 +27,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
# Assert
|
||||
# limit=2 to ignore now-playing:
|
||||
last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0]
|
||||
self.assertEqual(str(last_scrobble.track.artist).lower(), artist)
|
||||
self.assertEqual(str(last_scrobble.track.title).lower(), title)
|
||||
assert str(last_scrobble.track.artist).lower() == artist
|
||||
assert str(last_scrobble.track.title).lower() == title
|
||||
|
||||
def test_update_now_playing(self):
|
||||
# Arrange
|
||||
|
@ -44,17 +45,17 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
|
||||
# Assert
|
||||
current_track = lastfm_user.get_now_playing()
|
||||
self.assertIsNotNone(current_track)
|
||||
self.assertEqual(str(current_track.title).lower(), "test title")
|
||||
self.assertEqual(str(current_track.artist).lower(), "test artist")
|
||||
self.assertEqual(current_track.info["album"], "Test Album")
|
||||
assert current_track is not None
|
||||
assert str(current_track.title).lower() == "test title"
|
||||
assert str(current_track.artist).lower() == "test artist"
|
||||
assert current_track.info["album"] == "Test Album"
|
||||
|
||||
self.assertTrue(len(current_track.info["image"]))
|
||||
self.assertRegex(current_track.info["image"][pylast.SIZE_LARGE], r"^http.+$")
|
||||
assert len(current_track.info["image"])
|
||||
assert re.search(r"^http.+$", current_track.info["image"][pylast.SIZE_LARGE])
|
||||
|
||||
def test_enable_rate_limiting(self):
|
||||
# Arrange
|
||||
self.assertFalse(self.network.is_rate_limited())
|
||||
assert not self.network.is_rate_limited()
|
||||
|
||||
# Act
|
||||
self.network.enable_rate_limit()
|
||||
|
@ -66,13 +67,13 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
now = time.time()
|
||||
|
||||
# Assert
|
||||
self.assertTrue(self.network.is_rate_limited())
|
||||
self.assertGreaterEqual(now - then, 0.2)
|
||||
assert self.network.is_rate_limited()
|
||||
assert now - then >= 0.2
|
||||
|
||||
def test_disable_rate_limiting(self):
|
||||
# Arrange
|
||||
self.network.enable_rate_limit()
|
||||
self.assertTrue(self.network.is_rate_limited())
|
||||
assert self.network.is_rate_limited()
|
||||
|
||||
# Act
|
||||
self.network.disable_rate_limit()
|
||||
|
@ -82,14 +83,14 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
self.network.get_top_artists()
|
||||
|
||||
# Assert
|
||||
self.assertFalse(self.network.is_rate_limited())
|
||||
assert not self.network.is_rate_limited()
|
||||
|
||||
def test_lastfm_network_name(self):
|
||||
# Act
|
||||
name = str(self.network)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(name, "Last.fm Network")
|
||||
assert name == "Last.fm Network"
|
||||
|
||||
def test_geo_get_top_artists(self):
|
||||
# Arrange
|
||||
|
@ -97,9 +98,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
artists = self.network.get_geo_top_artists(country="United Kingdom", limit=1)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(len(artists), 1)
|
||||
self.assertIsInstance(artists[0], pylast.TopItem)
|
||||
self.assertIsInstance(artists[0].item, pylast.Artist)
|
||||
assert len(artists) == 1
|
||||
assert isinstance(artists[0], pylast.TopItem)
|
||||
assert isinstance(artists[0].item, pylast.Artist)
|
||||
|
||||
def test_geo_get_top_tracks(self):
|
||||
# Arrange
|
||||
|
@ -109,9 +110,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(len(tracks), 1)
|
||||
self.assertIsInstance(tracks[0], pylast.TopItem)
|
||||
self.assertIsInstance(tracks[0].item, pylast.Track)
|
||||
assert len(tracks) == 1
|
||||
assert isinstance(tracks[0], pylast.TopItem)
|
||||
assert isinstance(tracks[0].item, pylast.Track)
|
||||
|
||||
def test_network_get_top_artists_with_limit(self):
|
||||
# Arrange
|
||||
|
@ -186,12 +187,12 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
url = thing.get_url()
|
||||
|
||||
# Assert
|
||||
self.assertEqual(stringed, "Test Artist - Test Album")
|
||||
self.assertIn("pylast.Album('Test Artist', 'Test Album',", rep)
|
||||
self.assertEqual(title, name)
|
||||
self.assertIsInstance(playcount, int)
|
||||
self.assertGreater(playcount, 1)
|
||||
self.assertEqual("https://www.last.fm/music/test%2bartist/test%2balbum", url)
|
||||
assert stringed == "Test Artist - Test Album"
|
||||
assert "pylast.Album('Test Artist', 'Test Album'," in rep
|
||||
assert title == name
|
||||
assert isinstance(playcount, int)
|
||||
assert playcount > 1
|
||||
assert "https://www.last.fm/music/test%2bartist/test%2balbum" == url
|
||||
|
||||
def test_track_data(self):
|
||||
# Arrange
|
||||
|
@ -206,15 +207,13 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
url = thing.get_url(pylast.DOMAIN_FRENCH)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(stringed, "Test Artist - test title")
|
||||
self.assertIn("pylast.Track('Test Artist', 'test title',", rep)
|
||||
self.assertEqual(title, "test title")
|
||||
self.assertEqual(title, name)
|
||||
self.assertIsInstance(playcount, int)
|
||||
self.assertGreater(playcount, 1)
|
||||
self.assertEqual(
|
||||
"https://www.last.fm/fr/music/test%2bartist/_/test%2btitle", url
|
||||
)
|
||||
assert stringed == "Test Artist - test title"
|
||||
assert "pylast.Track('Test Artist', 'test title'," in rep
|
||||
assert title == "test title"
|
||||
assert title == name
|
||||
assert isinstance(playcount, int)
|
||||
assert playcount > 1
|
||||
assert "https://www.last.fm/fr/music/test%2bartist/_/test%2btitle" == url
|
||||
|
||||
def test_country_top_artists(self):
|
||||
# Arrange
|
||||
|
@ -236,10 +235,10 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
tags2 = user.get_top_tags(limit=1, cacheable=True)
|
||||
|
||||
# Assert
|
||||
self.assertTrue(self.network.is_caching_enabled())
|
||||
self.assertEqual(tags1, tags2)
|
||||
assert self.network.is_caching_enabled()
|
||||
assert tags1 == tags2
|
||||
self.network.disable_caching()
|
||||
self.assertFalse(self.network.is_caching_enabled())
|
||||
assert not self.network.is_caching_enabled()
|
||||
|
||||
def test_album_mbid(self):
|
||||
# Arrange
|
||||
|
@ -250,9 +249,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
album_mbid = album.get_mbid()
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(album, pylast.Album)
|
||||
self.assertEqual(album.title.lower(), "test")
|
||||
self.assertEqual(album_mbid, mbid)
|
||||
assert isinstance(album, pylast.Album)
|
||||
assert album.title.lower() == "test"
|
||||
assert album_mbid == mbid
|
||||
|
||||
def test_artist_mbid(self):
|
||||
# Arrange
|
||||
|
@ -262,8 +261,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
artist = self.network.get_artist_by_mbid(mbid)
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(artist, pylast.Artist)
|
||||
self.assertEqual(artist.name, "MusicBrainz Test Artist")
|
||||
assert isinstance(artist, pylast.Artist)
|
||||
assert artist.name == "MusicBrainz Test Artist"
|
||||
|
||||
def test_track_mbid(self):
|
||||
# Arrange
|
||||
|
@ -274,9 +273,9 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
track_mbid = track.get_mbid()
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(track, pylast.Track)
|
||||
self.assertEqual(track.title, "first")
|
||||
self.assertEqual(track_mbid, mbid)
|
||||
assert isinstance(track, pylast.Track)
|
||||
assert track.title == "first"
|
||||
assert track_mbid == mbid
|
||||
|
||||
def test_init_with_token(self):
|
||||
# Arrange/Act
|
||||
|
@ -291,7 +290,7 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
msg = str(exc)
|
||||
|
||||
# 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):
|
||||
# Arrange
|
||||
|
@ -300,11 +299,11 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
|
||||
# Act / Assert
|
||||
self.network.enable_proxy(host, port)
|
||||
self.assertTrue(self.network.is_proxy_enabled())
|
||||
self.assertEqual(self.network._get_proxy(), ["https://example.com", 1234])
|
||||
assert self.network.is_proxy_enabled()
|
||||
assert self.network._get_proxy() == ["https://example.com", 1234]
|
||||
|
||||
self.network.disable_proxy()
|
||||
self.assertFalse(self.network.is_proxy_enabled())
|
||||
assert not self.network.is_proxy_enabled()
|
||||
|
||||
def test_album_search(self):
|
||||
# Arrange
|
||||
|
@ -315,8 +314,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
results = search.get_next_page()
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(results, list)
|
||||
self.assertIsInstance(results[0], pylast.Album)
|
||||
assert isinstance(results, list)
|
||||
assert isinstance(results[0], pylast.Album)
|
||||
|
||||
def test_album_search_images(self):
|
||||
# Arrange
|
||||
|
@ -328,15 +327,15 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
images = results[0].info["image"]
|
||||
|
||||
# Assert
|
||||
self.assertEqual(len(images), 4)
|
||||
assert len(images) == 4
|
||||
|
||||
self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
|
||||
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_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):
|
||||
# Arrange
|
||||
|
@ -347,8 +346,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
results = search.get_next_page()
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(results, list)
|
||||
self.assertIsInstance(results[0], pylast.Artist)
|
||||
assert isinstance(results, list)
|
||||
assert isinstance(results[0], pylast.Artist)
|
||||
|
||||
def test_artist_search_images(self):
|
||||
# Arrange
|
||||
|
@ -360,15 +359,15 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
images = results[0].info["image"]
|
||||
|
||||
# Assert
|
||||
self.assertEqual(len(images), 5)
|
||||
assert len(images) == 5
|
||||
|
||||
self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
|
||||
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_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):
|
||||
# Arrange
|
||||
|
@ -380,8 +379,8 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
results = search.get_next_page()
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(results, list)
|
||||
self.assertIsInstance(results[0], pylast.Track)
|
||||
assert isinstance(results, list)
|
||||
assert isinstance(results[0], pylast.Track)
|
||||
|
||||
def test_track_search_images(self):
|
||||
# Arrange
|
||||
|
@ -394,15 +393,15 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
images = results[0].info["image"]
|
||||
|
||||
# Assert
|
||||
self.assertEqual(len(images), 4)
|
||||
assert len(images) == 4
|
||||
|
||||
self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
|
||||
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_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):
|
||||
# Arrange
|
||||
|
@ -414,8 +413,4 @@ class TestPyLastNetwork(TestPyLastWithLastFm):
|
|||
total = search.get_total_result_count()
|
||||
|
||||
# Assert
|
||||
self.assertGreater(int(total), 10000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(failfast=True)
|
||||
assert int(total) > 10000
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue