Merge pull request #233 from pylast/develop

Merge develop into master
This commit is contained in:
Hugo 2017-10-29 00:20:21 +03:00 committed by GitHub
commit 6d3ef3c7bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 1697 additions and 4001 deletions

View file

@ -1,5 +1,7 @@
language: python
cache: pip
env:
global:
- secure: ivg6II471E9HV8xyqnawLIuP/sZ0J63Y+BC0BQcRVKtLn/K3zmD1ozM3TFL9S549Nxd0FqDKHXJvXsgaTGIDpK8sxE2AMKV5IojyM0iAVuN7YjPK9vwSlRw1u0EysPMFqxOZVQnoDyHrSGIUrP/VMdnhBu6dbUX0FyEkvZshXhY=
@ -14,17 +16,17 @@ env:
matrix:
include:
- python: 2.7
env: TOXENV=lint
env: TOXENV=py2lint
- python: 2.7
env: TOXENV=py27
- python: 3.6
env: TOXENV=py3lint
- python: 3.6
env: TOXENV=py36
- python: 3.5
env: TOXENV=py35
- python: 3.4
env: TOXENV=py34
- python: 3.3
env: TOXENV=py33
- python: pypy3
env: TOXENV=pypy3
- python: pypy
@ -37,7 +39,7 @@ matrix:
sudo: false
install:
- travis_retry pip install tox==2.1.1
- travis_retry pip install tox
- travis_retry pip install coverage
script: tox

View file

@ -1,6 +1,6 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
https://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

View file

@ -1,6 +1,6 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
https://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
@ -192,7 +192,7 @@
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,

View file

@ -3,15 +3,15 @@ pyLast
[![Build status](https://travis-ci.org/pylast/pylast.svg?branch=develop)](https://travis-ci.org/pylast/pylast)
[![PyPI version](https://img.shields.io/pypi/v/pylast.svg)](https://pypi.python.org/pypi/pylast/)
<!--[![PyPI downloads](https://img.shields.io/pypi/dm/pylast.svg)](https://pypi.python.org/pypi/pylast/)-->
[![Supported Python versions](https://img.shields.io/pypi/pyversions/pylast.svg)](https://pypi.python.org/pypi/pylast/)
[![Coverage (Codecov)](https://codecov.io/gh/pylast/pylast/branch/develop/graph/badge.svg)](https://codecov.io/gh/pylast/pylast)
[![Coverage (Coveralls)](https://coveralls.io/repos/github/pylast/pylast/badge.svg?branch=develop)](https://coveralls.io/github/pylast/pylast?branch=develop)
[![Code health](https://landscape.io/github/pylast/pylast/develop/landscape.svg)](https://landscape.io/github/hugovk/pylast/develop)
A Python interface to [Last.fm](http://www.last.fm/) and other API-compatible websites such as [Libre.fm](http://libre.fm/).
A Python interface to [Last.fm](https://www.last.fm/) and other API-compatible websites such as [Libre.fm](https://libre.fm/).
Try using the pydoc utility for help on usage or see [test_pylast.py](tests/test_pylast.py) for examples.
Use the pydoc utility for help on usage or see [tests/](tests/) for examples.
Installation
------------
@ -20,6 +20,13 @@ Install via pip:
pip install pylast
Note:
* pyLast >= 2.0.0 supports Python 2.7.10+ and 3.4, 3.5, 3.6.
* pyLast >= 1.7.0 < 2.0.0 supports Python 2.7, 3.3, 3.4, 3.5, 3.6.
* pyLast >= 1.0.0 < 1.7.0 supports Python 2.7, 3.3, 3.4.
* pyLast >= 0.5 < 1.0.0 supports Python 2, 3.
* pyLast < 0.5 supports Python 2.
Features
--------
@ -43,7 +50,7 @@ Here's some simple code example to get you started. In order to create any objec
import pylast
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from http://www.last.fm/api/account/create for Last.fm
# Obtain yours from https://www.last.fm/api/account/create for Last.fm
API_KEY = "b25b959554ed76058ac220b7b2e0a026" # this is a sample key
API_SECRET = "425b55975eed76058ac220b7b4e8a054"
@ -67,12 +74,12 @@ track.add_tags(("awesome", "favorite"))
# to get more help about anything and see examples of how it works
```
More examples in <a href="https://github.com/hugovk/lastfm-tools">hugovk/lastfm-tools</a> and [test_pylast.py](test_pylast.py).
More examples in <a href="https://github.com/hugovk/lastfm-tools">hugovk/lastfm-tools</a> and [tests/](tests/).
Testing
-------
[tests/test_pylast.py](tests/test_pylast.py) contains integration tests with Last.fm, and plenty of code examples. Unit tests are also in the [tests/](tests/) directory.
The [tests/](tests/) directory contains integration and unit tests with Last.fm, and plenty of code examples.
For integration tests you need a test account at Last.fm that will become cluttered with test data, and an API key and secret. Either copy [example_test_pylast.yaml](example_test_pylast.yaml) to test_pylast.yaml and fill out the credentials, or set them as environment variables like:
@ -86,17 +93,17 @@ export PYLAST_API_SECRET=TODO_ENTER_YOURS_HERE
To run all unit and integration tests:
```sh
pip install pytest flaky mock
py.test
pytest
```
Or run just one test case:
```sh
py.test -k test_scrobble
pytest -k test_scrobble
```
To run with coverage:
```sh
py.test -v --cov pylast --cov-report term-missing
pytest -v --cov pylast --cov-report term-missing
coverage report # for command-line report
coverage html # for HTML report
open htmlcov/index.html

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
from setuptools import setup, find_packages
from setuptools import find_packages, setup
setup(
@ -7,14 +7,9 @@ setup(
version="1.9.0",
author="Amr Hassan <amr.hassan@gmail.com>",
install_requires=['six'],
# FIXME This can be removed after 2017-09 when 3.3 is no longer supported
# and pypy3 uses 3.4 or later, see
# https://en.wikipedia.org/wiki/CPython#Version_history
extras_require={
':python_version=="3.3"': ["certifi"],
},
tests_require=['mock', 'pytest', 'coverage', 'pep8', 'pyyaml', 'pyflakes'],
description=("A Python interface to Last.fm and Libre.fm"),
tests_require=['mock', 'pytest', 'coverage', 'pycodestyle', 'pyyaml',
'pyflakes', 'flaky'],
description="A Python interface to Last.fm and Libre.fm",
author_email="amr.hassan@gmail.com",
url="https://github.com/pylast/pylast",
classifiers=[
@ -26,10 +21,11 @@ setup(
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
],
keywords=["Last.fm", "music", "scrobble", "scrobbling"],
packages=find_packages(exclude=('tests*',)),

File diff suppressed because it is too large Load diff

115
tests/test_pylast_album.py Executable file
View file

@ -0,0 +1,115 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastAlbum(PyLastTestCase):
def test_album_tags_are_topitems(self):
# Arrange
albums = self.network.get_user('RJ').get_top_albums()
# Act
tags = albums[0].item.get_top_tags(limit=1)
# Assert
self.assertGreater(len(tags), 0)
self.assertIsInstance(tags[0], pylast.TopItem)
def test_album_is_hashable(self):
# Arrange
album = self.network.get_album("Test Artist", "Test Album")
# Act/Assert
self.helper_is_thing_hashable(album)
def test_album_in_recent_tracks(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
# limit=2 to ignore now-playing:
track = lastfm_user.get_recent_tracks(limit=2)[0]
# Assert
self.assertTrue(hasattr(track, 'album'))
def test_album_in_artist_tracks(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
track = lastfm_user.get_artist_tracks(artist="Test Artist")[0]
# Assert
self.assertTrue(hasattr(track, 'album'))
def test_album_wiki_content(self):
# Arrange
album = pylast.Album("Test Artist", "Test Album", self.network)
# Act
wiki = album.get_wiki_content()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_album_wiki_published_date(self):
# Arrange
album = pylast.Album("Test Artist", "Test Album", self.network)
# Act
wiki = album.get_wiki_published_date()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_album_wiki_summary(self):
# Arrange
album = pylast.Album("Test Artist", "Test Album", self.network)
# Act
wiki = album.get_wiki_summary()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_album_eq_none_is_false(self):
# Arrange
album1 = None
album2 = pylast.Album("Test Artist", "Test Album", self.network)
# Act / Assert
self.assertFalse(album1 == album2)
def test_album_ne_none_is_true(self):
# Arrange
album1 = None
album2 = pylast.Album("Test Artist", "Test Album", self.network)
# Act / Assert
self.assertTrue(album1 != album2)
def test_get_cover_image(self):
# Arrange
album = self.network.get_album("Test Artist", "Test Album")
# Act
image = album.get_cover_image()
# Assert
self.assertTrue(image.startswith("https://"))
self.assertTrue(image.endswith(".png"))
if __name__ == '__main__':
unittest.main(failfast=True)

263
tests/test_pylast_artist.py Executable file
View file

@ -0,0 +1,263 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastArtist(PyLastTestCase):
def test_repr(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
representation = repr(artist)
# Assert
self.assertTrue(
representation.startswith("pylast.Artist('Test Artist',"))
def test_artist_is_hashable(self):
# Arrange
test_artist = self.network.get_artist("Test Artist")
artist = test_artist.get_similar(limit=2)[0].item
self.assertIsInstance(artist, pylast.Artist)
# Act/Assert
self.helper_is_thing_hashable(artist)
def test_bio_published_date(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
bio = artist.get_bio_published_date()
# Assert
self.assertIsNotNone(bio)
self.assertGreaterEqual(len(bio), 1)
def test_bio_content(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
bio = artist.get_bio_content(language="en")
# Assert
self.assertIsNotNone(bio)
self.assertGreaterEqual(len(bio), 1)
def test_bio_summary(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
# Act
bio = artist.get_bio_summary(language="en")
# Assert
self.assertIsNotNone(bio)
self.assertGreaterEqual(len(bio), 1)
def test_artist_top_tracks(self):
# Arrange
# Pick an artist with plenty of plays
artist = self.network.get_top_artists(limit=1)[0].item
# Act
things = artist.get_top_tracks(limit=2)
# Assert
self.helper_two_different_things_in_top_list(things, pylast.Track)
def test_artist_top_albums(self):
# Arrange
# Pick an artist with plenty of plays
artist = self.network.get_top_artists(limit=1)[0].item
# Act
things = artist.get_top_albums(limit=2)
# Assert
self.helper_two_different_things_in_top_list(things, pylast.Album)
def test_artist_listener_count(self):
# Arrange
artist = self.network.get_artist("Test Artist")
# Act
count = artist.get_listener_count()
# Assert
self.assertIsInstance(count, int)
self.assertGreater(count, 0)
def test_tag_artist(self):
# Arrange
artist = self.network.get_artist("Test Artist")
# artist.clear_tags()
# Act
artist.add_tag("testing")
# Assert
tags = artist.get_tags()
self.assertGreater(len(tags), 0)
found = False
for tag in tags:
if tag.name == "testing":
found = True
break
self.assertTrue(found)
def test_remove_tag_of_type_text(self):
# Arrange
tag = "testing" # text
artist = self.network.get_artist("Test Artist")
artist.add_tag(tag)
# Act
artist.remove_tag(tag)
# Assert
tags = artist.get_tags()
found = False
for tag in tags:
if tag.name == "testing":
found = True
break
self.assertFalse(found)
def test_remove_tag_of_type_tag(self):
# Arrange
tag = pylast.Tag("testing", self.network) # Tag
artist = self.network.get_artist("Test Artist")
artist.add_tag(tag)
# Act
artist.remove_tag(tag)
# Assert
tags = artist.get_tags()
found = False
for tag in tags:
if tag.name == "testing":
found = True
break
self.assertFalse(found)
def test_remove_tags(self):
# Arrange
tags = ["removetag1", "removetag2"]
artist = self.network.get_artist("Test Artist")
artist.add_tags(tags)
artist.add_tags("1more")
tags_before = artist.get_tags()
# Act
artist.remove_tags(tags)
# Assert
tags_after = artist.get_tags()
self.assertEqual(len(tags_after), len(tags_before) - 2)
found1, found2 = False, False
for tag in tags_after:
if tag.name == "removetag1":
found1 = True
elif tag.name == "removetag2":
found2 = True
self.assertFalse(found1)
self.assertFalse(found2)
def test_set_tags(self):
# Arrange
tags = ["sometag1", "sometag2"]
artist = self.network.get_artist("Test Artist 2")
artist.add_tags(tags)
tags_before = artist.get_tags()
new_tags = ["settag1", "settag2"]
# Act
artist.set_tags(new_tags)
# Assert
tags_after = artist.get_tags()
self.assertNotEqual(tags_before, tags_after)
self.assertEqual(len(tags_after), 2)
found1, found2 = False, False
for tag in tags_after:
if tag.name == "settag1":
found1 = True
elif tag.name == "settag2":
found2 = True
self.assertTrue(found1)
self.assertTrue(found2)
def test_artists(self):
# Arrange
artist1 = self.network.get_artist("Radiohead")
artist2 = self.network.get_artist("Portishead")
# Act
url = artist1.get_url()
mbid = artist1.get_mbid()
image = artist1.get_cover_image()
playcount = artist1.get_playcount()
streamable = artist1.is_streamable()
name = artist1.get_name(properly_capitalized=False)
name_cap = artist1.get_name(properly_capitalized=True)
# Assert
self.assertIn("https", image)
self.assertGreater(playcount, 1)
self.assertTrue(artist1 != artist2)
self.assertEqual(name.lower(), name_cap.lower())
self.assertEqual(url, "https://www.last.fm/music/radiohead")
self.assertEqual(mbid, "a74b1b7f-71a5-4011-9441-d0b5e4122711")
self.assertIsInstance(streamable, bool)
def test_artist_eq_none_is_false(self):
# Arrange
artist1 = None
artist2 = pylast.Artist("Test Artist", self.network)
# Act / Assert
self.assertFalse(artist1 == artist2)
def test_artist_ne_none_is_true(self):
# Arrange
artist1 = None
artist2 = pylast.Artist("Test Artist", self.network)
# Act / Assert
self.assertTrue(artist1 != artist2)
def test_artist_get_correction(self):
# Arrange
artist = pylast.Artist("guns and roses", self.network)
# Act
corrected_artist_name = artist.get_correction()
# Assert
self.assertEqual(corrected_artist_name, "Guns N' Roses")
def test_get_userplaycount(self):
# Arrange
artist = pylast.Artist("John Lennon", self.network,
username=self.username)
# Act
playcount = artist.get_userplaycount()
# Assert
self.assertGreaterEqual(playcount, 0)
if __name__ == '__main__':
unittest.main(failfast=True)

41
tests/test_pylast_country.py Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastCountry(PyLastTestCase):
def test_country_is_hashable(self):
# Arrange
country = self.network.get_country("Italy")
# Act/Assert
self.helper_is_thing_hashable(country)
def test_countries(self):
# Arrange
country1 = pylast.Country("Italy", self.network)
country2 = pylast.Country("Finland", self.network)
# Act
text = str(country1)
rep = repr(country1)
url = country1.get_url()
# Assert
self.assertIn("Italy", rep)
self.assertIn("pylast.Country", rep)
self.assertEqual(text, "Italy")
self.assertTrue(country1 == country1)
self.assertTrue(country1 != country2)
self.assertEqual(url, "https://www.last.fm/place/italy")
if __name__ == '__main__':
unittest.main(failfast=True)

61
tests/test_pylast_library.py Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastLibrary(PyLastTestCase):
def test_repr(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Act
representation = repr(library)
# Assert
self.assertTrue(representation.startswith("pylast.Library("))
def test_str(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Act
string = str(library)
# Assert
self.assertTrue(string.endswith("'s Library"))
def test_library_is_hashable(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
# Act/Assert
self.helper_is_thing_hashable(library)
def test_cacheable_library(self):
# Arrange
library = pylast.Library(self.username, self.network)
# Act/Assert
self.helper_validate_cacheable(library, "get_artists")
def test_get_user(self):
# Arrange
library = pylast.Library(user=self.username, network=self.network)
user_to_get = self.network.get_user(self.username)
# Act
library_user = library.get_user()
# Assert
self.assertEqual(library_user, user_to_get)
if __name__ == '__main__':
unittest.main(failfast=True)

49
tests/test_pylast_librefm.py Executable file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
from flaky import flaky
import pylast
from .test_pylast import load_secrets
@flaky(max_runs=5, min_passes=1)
class TestPyLastWithLibreFm(unittest.TestCase):
"""Own class for Libre.fm because we don't need the Last.fm setUp"""
def test_libre_fm(self):
# Arrange
secrets = load_secrets()
username = secrets["username"]
password_hash = secrets["password_hash"]
# Act
network = pylast.LibreFMNetwork(
password_hash=password_hash, username=username)
artist = network.get_artist("Radiohead")
name = artist.get_name()
# Assert
self.assertEqual(name, "Radiohead")
def test_repr(self):
# Arrange
secrets = load_secrets()
username = secrets["username"]
password_hash = secrets["password_hash"]
network = pylast.LibreFMNetwork(
password_hash=password_hash, username=username)
# Act
representation = repr(network)
# Assert
self.assertTrue(representation.startswith("pylast.LibreFMNetwork("))
if __name__ == '__main__':
unittest.main(failfast=True)

357
tests/test_pylast_network.py Executable file
View file

@ -0,0 +1,357 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import time
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastNetwork(PyLastTestCase):
def test_scrobble(self):
# Arrange
artist = "Test Artist"
title = "test title"
timestamp = self.unix_timestamp()
lastfm_user = self.network.get_user(self.username)
# Act
self.network.scrobble(artist=artist, title=title, timestamp=timestamp)
# Assert
# limit=2 to ignore now-playing:
last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0]
self.assertEqual(str(last_scrobble.track.artist), str(artist))
self.assertEqual(str(last_scrobble.track.title), str(title))
self.assertEqual(str(last_scrobble.timestamp), str(timestamp))
def test_update_now_playing(self):
# Arrange
artist = "Test Artist"
title = "test title"
album = "Test Album"
track_number = 1
lastfm_user = self.network.get_user(self.username)
# Act
self.network.update_now_playing(
artist=artist, title=title, album=album, track_number=track_number)
# Assert
current_track = lastfm_user.get_now_playing()
self.assertIsNotNone(current_track)
self.assertEqual(str(current_track.title), "test title")
self.assertEqual(str(current_track.artist), "Test Artist")
def test_enable_rate_limiting(self):
# Arrange
self.assertFalse(self.network.is_rate_limited())
# Act
self.network.enable_rate_limit()
then = time.time()
# Make some network call, limit not applied first time
self.network.get_user(self.username)
# Make a second network call, limiting should be applied
self.network.get_top_artists()
now = time.time()
# Assert
self.assertTrue(self.network.is_rate_limited())
self.assertGreaterEqual(now - then, 0.2)
def test_disable_rate_limiting(self):
# Arrange
self.network.enable_rate_limit()
self.assertTrue(self.network.is_rate_limited())
# Act
self.network.disable_rate_limit()
# Make some network call, limit not applied first time
self.network.get_user(self.username)
# Make a second network call, limiting should be applied
self.network.get_top_artists()
# Assert
self.assertFalse(self.network.is_rate_limited())
def test_lastfm_network_name(self):
# Act
name = str(self.network)
# Assert
self.assertEqual(name, "Last.fm Network")
def test_geo_get_top_artists(self):
# Arrange
# Act
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)
def test_geo_get_top_tracks(self):
# Arrange
# Act
tracks = self.network.get_geo_top_tracks(
country="United Kingdom", location="Manchester", limit=1)
# Assert
self.assertEqual(len(tracks), 1)
self.assertIsInstance(tracks[0], pylast.TopItem)
self.assertIsInstance(tracks[0].item, pylast.Track)
def test_network_get_top_artists_with_limit(self):
# Arrange
# Act
artists = self.network.get_top_artists(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(artists, pylast.Artist)
def test_network_get_top_tags_with_limit(self):
# Arrange
# Act
tags = self.network.get_top_tags(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(tags, pylast.Tag)
def test_network_get_top_tags_with_no_limit(self):
# Arrange
# Act
tags = self.network.get_top_tags()
# Assert
self.helper_at_least_one_thing_in_top_list(tags, pylast.Tag)
def test_network_get_top_tracks_with_limit(self):
# Arrange
# Act
tracks = self.network.get_top_tracks(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(tracks, pylast.Track)
def test_country_top_tracks(self):
# Arrange
country = self.network.get_country("Croatia")
# Act
things = country.get_top_tracks(limit=2)
# Assert
self.helper_two_different_things_in_top_list(things, pylast.Track)
def test_country_network_top_tracks(self):
# Arrange
# Act
things = self.network.get_geo_top_tracks("Croatia", limit=2)
# Assert
self.helper_two_different_things_in_top_list(things, pylast.Track)
def test_tag_top_tracks(self):
# Arrange
tag = self.network.get_tag("blues")
# Act
things = tag.get_top_tracks(limit=2)
# Assert
self.helper_two_different_things_in_top_list(things, pylast.Track)
def test_album_data(self):
# Arrange
thing = self.network.get_album("Test Artist", "Test Album")
# Act
stringed = str(thing)
rep = thing.__repr__()
title = thing.get_title()
name = thing.get_name()
playcount = thing.get_playcount()
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)
def test_track_data(self):
# Arrange
thing = self.network.get_track("Test Artist", "test title")
# Act
stringed = str(thing)
rep = thing.__repr__()
title = thing.get_title()
name = thing.get_name()
playcount = thing.get_playcount()
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)
def test_country_top_artists(self):
# Arrange
country = self.network.get_country("Ukraine")
# Act
artists = country.get_top_artists(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(artists, pylast.Artist)
def test_caching(self):
# Arrange
user = self.network.get_user("RJ")
# Act
self.network.enable_caching()
tags1 = user.get_top_tags(limit=1, cacheable=True)
tags2 = user.get_top_tags(limit=1, cacheable=True)
# Assert
self.assertTrue(self.network.is_caching_enabled())
self.assertEqual(tags1, tags2)
self.network.disable_caching()
self.assertFalse(self.network.is_caching_enabled())
def test_album_mbid(self):
# Arrange
mbid = "a6a265bf-9f81-4055-8224-f7ac0aa6b937"
# Act
album = self.network.get_album_by_mbid(mbid)
album_mbid = album.get_mbid()
# Assert
self.assertIsInstance(album, pylast.Album)
self.assertEqual(album.title.lower(), "test")
self.assertEqual(album_mbid, mbid)
def test_artist_mbid(self):
# Arrange
mbid = "7e84f845-ac16-41fe-9ff8-df12eb32af55"
# Act
artist = self.network.get_artist_by_mbid(mbid)
# Assert
self.assertIsInstance(artist, pylast.Artist)
self.assertEqual(artist.name, "MusicBrainz Test Artist")
def test_track_mbid(self):
# Arrange
mbid = "ebc037b1-cc9c-44f2-a21f-83c219f0e1e0"
# Act
track = self.network.get_track_by_mbid(mbid)
track_mbid = track.get_mbid()
# Assert
self.assertIsInstance(track, pylast.Track)
self.assertEqual(track.title, "first")
self.assertEqual(track_mbid, mbid)
def test_init_with_token(self):
# Arrange/Act
msg = None
try:
pylast.LastFMNetwork(
api_key=self.__class__.secrets["api_key"],
api_secret=self.__class__.secrets["api_secret"],
token="invalid",
)
except pylast.WSError as exc:
msg = str(exc)
# Assert
self.assertEqual(msg,
"Unauthorized Token - This token has not been issued")
def test_proxy(self):
# Arrange
host = "https://example.com"
port = 1234
# 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])
self.network.disable_proxy()
self.assertFalse(self.network.is_proxy_enabled())
def test_album_search(self):
# Arrange
album = "Nevermind"
# Act
search = self.network.search_for_album(album)
results = search.get_next_page()
# Assert
self.assertIsInstance(results, list)
self.assertIsInstance(results[0], pylast.Album)
def test_artist_search(self):
# Arrange
artist = "Nirvana"
# Act
search = self.network.search_for_artist(artist)
results = search.get_next_page()
# Assert
self.assertIsInstance(results, list)
self.assertIsInstance(results[0], pylast.Artist)
def test_track_search(self):
# Arrange
artist = "Nirvana"
track = "Smells Like Teen Spirit"
# Act
search = self.network.search_for_track(artist, track)
results = search.get_next_page()
# Assert
self.assertIsInstance(results, list)
self.assertIsInstance(results[0], pylast.Track)
def test_search_get_total_result_count(self):
# Arrange
artist = "Nirvana"
track = "Smells Like Teen Spirit"
search = self.network.search_for_track(artist, track)
# Act
total = search.get_total_result_count()
# Assert
self.assertGreater(int(total), 10000)
if __name__ == '__main__':
unittest.main(failfast=True)

63
tests/test_pylast_tag.py Executable file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastTag(PyLastTestCase):
def test_tag_is_hashable(self):
# Arrange
tag = self.network.get_top_tags(limit=1)[0]
# Act/Assert
self.helper_is_thing_hashable(tag)
def test_tag_top_artists(self):
# Arrange
tag = self.network.get_tag("blues")
# Act
artists = tag.get_top_artists(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(artists, pylast.Artist)
def test_tag_top_albums(self):
# Arrange
tag = self.network.get_tag("blues")
# Act
albums = tag.get_top_albums(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(albums, pylast.Album)
def test_tags(self):
# Arrange
tag1 = self.network.get_tag("blues")
tag2 = self.network.get_tag("rock")
# Act
tag_repr = repr(tag1)
tag_str = str(tag1)
name = tag1.get_name(properly_capitalized=True)
url = tag1.get_url()
# Assert
self.assertEqual("blues", tag_str)
self.assertIn("pylast.Tag", tag_repr)
self.assertIn("blues", tag_repr)
self.assertEqual("blues", name)
self.assertTrue(tag1 == tag1)
self.assertTrue(tag1 != tag2)
self.assertEqual(url, "https://www.last.fm/tag/blues")
if __name__ == '__main__':
unittest.main(failfast=True)

165
tests/test_pylast_track.py Executable file
View file

@ -0,0 +1,165 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastTrack(PyLastTestCase):
def test_love(self):
# Arrange
artist = "Test Artist"
title = "test title"
track = self.network.get_track(artist, title)
lastfm_user = self.network.get_user(self.username)
# Act
track.love()
# Assert
loved = lastfm_user.get_loved_tracks(limit=1)
self.assertEqual(str(loved[0].track.artist), "Test Artist")
self.assertEqual(str(loved[0].track.title), "test title")
def test_unlove(self):
# Arrange
artist = pylast.Artist("Test Artist", self.network)
title = "test title"
track = pylast.Track(artist, title, self.network)
lastfm_user = self.network.get_user(self.username)
track.love()
# Act
track.unlove()
# Assert
loved = lastfm_user.get_loved_tracks(limit=1)
if len(loved): # OK to be empty but if not:
self.assertNotEqual(str(loved.track.artist), "Test Artist")
self.assertNotEqual(str(loved.track.title), "test title")
def test_user_play_count_in_track_info(self):
# Arrange
artist = "Test Artist"
title = "test title"
track = pylast.Track(
artist=artist, title=title,
network=self.network, username=self.username)
# Act
count = track.get_userplaycount()
# Assert
self.assertGreaterEqual(count, 0)
def test_user_loved_in_track_info(self):
# Arrange
artist = "Test Artist"
title = "test title"
track = pylast.Track(
artist=artist, title=title,
network=self.network, username=self.username)
# Act
loved = track.get_userloved()
# Assert
self.assertIsNotNone(loved)
self.assertIsInstance(loved, bool)
self.assertNotIsInstance(loved, str)
def test_track_is_hashable(self):
# Arrange
artist = self.network.get_artist("Test Artist")
track = artist.get_top_tracks()[0].item
self.assertIsInstance(track, pylast.Track)
# Act/Assert
self.helper_is_thing_hashable(track)
def test_track_wiki_content(self):
# Arrange
track = pylast.Track("Test Artist", "test title", self.network)
# Act
wiki = track.get_wiki_content()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_track_wiki_summary(self):
# Arrange
track = pylast.Track("Test Artist", "test title", self.network)
# Act
wiki = track.get_wiki_summary()
# Assert
self.assertIsNotNone(wiki)
self.assertGreaterEqual(len(wiki), 1)
def test_track_get_duration(self):
# Arrange
track = pylast.Track("Nirvana", "Lithium", self.network)
# Act
duration = track.get_duration()
# Assert
self.assertGreaterEqual(duration, 200000)
def test_track_is_streamable(self):
# Arrange
track = pylast.Track("Nirvana", "Lithium", self.network)
# Act
streamable = track.is_streamable()
# Assert
self.assertFalse(streamable)
def test_track_is_fulltrack_available(self):
# Arrange
track = pylast.Track("Nirvana", "Lithium", self.network)
# Act
fulltrack_available = track.is_fulltrack_available()
# Assert
self.assertFalse(fulltrack_available)
def test_track_get_album(self):
# Arrange
track = pylast.Track("Nirvana", "Lithium", self.network)
# Act
album = track.get_album()
print(album)
# Assert
self.assertEqual(str(album), "Nirvana - Nevermind")
def test_track_get_similar(self):
# Arrange
track = pylast.Track("Cher", "Believe", self.network)
# Act
similar = track.get_similar()
# Assert
found = False
for track in similar:
if str(track.item) == "Madonna - Vogue":
found = True
break
self.assertTrue(found)
if __name__ == '__main__':
unittest.main(failfast=True)

495
tests/test_pylast_user.py Executable file
View file

@ -0,0 +1,495 @@
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import os
import unittest
import pylast
from .test_pylast import PyLastTestCase
class TestPyLastUser(PyLastTestCase):
def test_repr(self):
# Arrange
user = self.network.get_user("RJ")
# Act
representation = repr(user)
# Assert
self.assertTrue(representation.startswith("pylast.User('RJ',"))
def test_str(self):
# Arrange
user = self.network.get_user("RJ")
# Act
string = str(user)
# Assert
self.assertEqual(string, "RJ")
def test_equality(self):
# Arrange
user_1a = self.network.get_user("RJ")
user_1b = self.network.get_user("RJ")
user_2 = self.network.get_user("Test User")
not_a_user = self.network
# Act / Assert
self.assertEqual(user_1a, user_1b)
self.assertTrue(user_1a == user_1b)
self.assertFalse(user_1a != user_1b)
self.assertNotEqual(user_1a, user_2)
self.assertTrue(user_1a != user_2)
self.assertFalse(user_1a == user_2)
self.assertNotEqual(user_1a, not_a_user)
self.assertTrue(user_1a != not_a_user)
self.assertFalse(user_1a == not_a_user)
def test_get_name(self):
# Arrange
user = self.network.get_user("RJ")
# Act
name = user.get_name(properly_capitalized=True)
# Assert
self.assertEqual(name, "RJ")
def test_get_user_registration(self):
# Arrange
user = self.network.get_user("RJ")
# Act
registered = user.get_registered()
# Assert
if int(registered):
# Last.fm API broken? Used to be yyyy-mm-dd not Unix timestamp
self.assertEqual(registered, "1037793040")
else:
# Old way
# Just check date because of timezones
self.assertIn(u"2002-11-20 ", registered)
def test_get_user_unixtime_registration(self):
# Arrange
user = self.network.get_user("RJ")
# Act
unixtime_registered = user.get_unixtime_registered()
# Assert
# Just check date because of timezones
self.assertEqual(unixtime_registered, u"1037793040")
def test_get_countryless_user(self):
# Arrange
# Currently test_user has no country set:
lastfm_user = self.network.get_user("test_user")
# Act
country = lastfm_user.get_country()
# Assert
self.assertIsNone(country)
def test_user_get_country(self):
# Arrange
lastfm_user = self.network.get_user("RJ")
# Act
country = lastfm_user.get_country()
# Assert
self.assertEqual(str(country), "United Kingdom")
def test_user_equals_none(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
value = (lastfm_user is None)
# Assert
self.assertFalse(value)
def test_user_not_equal_to_none(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
value = (lastfm_user is not None)
# Assert
self.assertTrue(value)
def test_now_playing_user_with_no_scrobbles(self):
# Arrange
# Currently test-account has no scrobbles:
user = self.network.get_user('test-account')
# Act
current_track = user.get_now_playing()
# Assert
self.assertIsNone(current_track)
def test_love_limits(self):
# Arrange
# Currently test-account has at least 23 loved tracks:
user = self.network.get_user("test-user")
# Act/Assert
self.assertEqual(len(user.get_loved_tracks(limit=20)), 20)
self.assertLessEqual(len(user.get_loved_tracks(limit=100)), 100)
self.assertGreaterEqual(len(user.get_loved_tracks(limit=None)), 23)
self.assertGreaterEqual(len(user.get_loved_tracks(limit=0)), 23)
def test_user_is_hashable(self):
# Arrange
user = self.network.get_user(self.username)
# Act/Assert
self.helper_is_thing_hashable(user)
# Commented out because (a) it'll take a long time and (b) it strangely
# fails due Last.fm's complaining of hitting the rate limit, even when
# limited to one call per second. The ToS allows 5 calls per second.
# def test_get_all_scrobbles(self):
# # Arrange
# lastfm_user = self.network.get_user("RJ")
# self.network.enable_rate_limit() # this is going to be slow...
# # Act
# tracks = lastfm_user.get_recent_tracks(limit=None)
# # Assert
# self.assertGreaterEqual(len(tracks), 0)
def test_pickle(self):
# Arrange
import pickle
lastfm_user = self.network.get_user(self.username)
filename = str(self.unix_timestamp()) + ".pkl"
# Act
with open(filename, "wb") as f:
pickle.dump(lastfm_user, f)
with open(filename, "rb") as f:
loaded_user = pickle.load(f)
os.remove(filename)
# Assert
self.assertEqual(lastfm_user, loaded_user)
def test_cacheable_user_artist_tracks(self):
# Arrange
lastfm_user = self.network.get_authenticated_user()
# Act
result1 = lastfm_user.get_artist_tracks("Test Artist", cacheable=False)
result2 = lastfm_user.get_artist_tracks("Test Artist", cacheable=True)
result3 = lastfm_user.get_artist_tracks("Test Artist")
# Assert
self.helper_validate_results(result1, result2, result3)
def test_cacheable_user(self):
# Arrange
lastfm_user = self.network.get_authenticated_user()
# Act/Assert
self.helper_validate_cacheable(lastfm_user, "get_friends")
self.helper_validate_cacheable(lastfm_user, "get_loved_tracks")
self.helper_validate_cacheable(lastfm_user, "get_recent_tracks")
def test_user_get_top_tags_with_limit(self):
# Arrange
user = self.network.get_user("RJ")
# Act
tags = user.get_top_tags(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(tags, pylast.Tag)
def test_user_top_tracks(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
things = lastfm_user.get_top_tracks(limit=2)
# Assert
self.helper_two_different_things_in_top_list(things, pylast.Track)
def helper_assert_chart(self, chart, expected_type):
# Assert
self.assertIsNotNone(chart)
self.assertGreater(len(chart), 0)
self.assertIsInstance(chart[0], pylast.TopItem)
self.assertIsInstance(chart[0].item, expected_type)
def helper_get_assert_charts(self, thing, date):
# Arrange
album_chart, track_chart = None, None
(from_date, to_date) = date
# Act
artist_chart = thing.get_weekly_artist_charts(from_date, to_date)
if type(thing) is not pylast.Tag:
album_chart = thing.get_weekly_album_charts(from_date, to_date)
track_chart = thing.get_weekly_track_charts(from_date, to_date)
# Assert
self.helper_assert_chart(artist_chart, pylast.Artist)
if type(thing) is not pylast.Tag:
self.helper_assert_chart(album_chart, pylast.Album)
self.helper_assert_chart(track_chart, pylast.Track)
def helper_dates_valid(self, dates):
# Assert
self.assertGreaterEqual(len(dates), 1)
self.assertIsInstance(dates[0], tuple)
(start, end) = dates[0]
self.assertLess(start, end)
def test_user_charts(self):
# Arrange
lastfm_user = self.network.get_user("RJ")
dates = lastfm_user.get_weekly_chart_dates()
self.helper_dates_valid(dates)
# Act/Assert
self.helper_get_assert_charts(lastfm_user, dates[0])
def test_user_top_artists(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
# Act
artists = lastfm_user.get_top_artists(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(artists, pylast.Artist)
def test_user_top_albums(self):
# Arrange
user = self.network.get_user("RJ")
# Act
albums = user.get_top_albums(limit=1)
# Assert
self.helper_only_one_thing_in_top_list(albums, pylast.Album)
def test_user_tagged_artists(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
tags = ["artisttagola"]
artist = self.network.get_artist("Test Artist")
artist.add_tags(tags)
# Act
artists = lastfm_user.get_tagged_artists('artisttagola', limit=1)
# Assert
self.helper_only_one_thing_in_list(artists, pylast.Artist)
def test_user_tagged_albums(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
tags = ["albumtagola"]
album = self.network.get_album("Test Artist", "Test Album")
album.add_tags(tags)
# Act
albums = lastfm_user.get_tagged_albums('albumtagola', limit=1)
# Assert
self.helper_only_one_thing_in_list(albums, pylast.Album)
def test_user_tagged_tracks(self):
# Arrange
lastfm_user = self.network.get_user(self.username)
tags = ["tracktagola"]
track = self.network.get_track("Test Artist", "test title")
track.add_tags(tags)
# Act
tracks = lastfm_user.get_tagged_tracks('tracktagola', limit=1)
# Assert
self.helper_only_one_thing_in_list(tracks, pylast.Track)
def test_user_subscriber(self):
# Arrange
subscriber = self.network.get_user("RJ")
non_subscriber = self.network.get_user("Test User")
# Act
subscriber_is_subscriber = subscriber.is_subscriber()
non_subscriber_is_subscriber = non_subscriber.is_subscriber()
# Assert
self.assertTrue(subscriber_is_subscriber)
self.assertFalse(non_subscriber_is_subscriber)
def test_user_get_image(self):
# Arrange
user = self.network.get_user("RJ")
# Act
url = user.get_image()
# Assert
self.assertTrue(url.startswith("https://"))
def test_user_get_library(self):
# Arrange
user = self.network.get_user(self.username)
# Act
library = user.get_library()
# Assert
self.assertIsInstance(library, pylast.Library)
def test_get_recent_tracks_from_to(self):
# Arrange
lastfm_user = self.network.get_user("RJ")
from datetime import datetime
start = datetime(2011, 7, 21, 15, 10)
end = datetime(2011, 7, 21, 15, 15)
import calendar
utc_start = calendar.timegm(start.utctimetuple())
utc_end = calendar.timegm(end.utctimetuple())
# Act
tracks = lastfm_user.get_recent_tracks(time_from=utc_start,
time_to=utc_end)
# Assert
self.assertEqual(len(tracks), 1)
self.assertEqual(str(tracks[0].track.artist), "Johnny Cash")
self.assertEqual(str(tracks[0].track.title), "Ring of Fire")
def test_tracks_notequal(self):
# Arrange
track1 = pylast.Track("Test Artist", "test title", self.network)
track2 = pylast.Track("Test Artist", "Test Track", self.network)
# Act
# Assert
self.assertNotEqual(track1, track2)
def test_track_title_prop_caps(self):
# Arrange
track = pylast.Track("test artist", "test title", self.network)
# Act
title = track.get_title(properly_capitalized=True)
# Assert
self.assertEqual(title, "test title")
def test_track_listener_count(self):
# Arrange
track = pylast.Track("test artist", "test title", self.network)
# Act
count = track.get_listener_count()
# Assert
self.assertGreater(count, 21)
def test_album_tracks(self):
# Arrange
album = pylast.Album("Test Artist", "Test Release", self.network)
# Act
tracks = album.get_tracks()
url = tracks[0].get_url()
# Assert
self.assertIsInstance(tracks, list)
self.assertIsInstance(tracks[0], pylast.Track)
self.assertEqual(len(tracks), 4)
self.assertTrue(url.startswith("https://www.last.fm/music/test"))
def test_track_eq_none_is_false(self):
# Arrange
track1 = None
track2 = pylast.Track("Test Artist", "test title", self.network)
# Act / Assert
self.assertFalse(track1 == track2)
def test_track_ne_none_is_true(self):
# Arrange
track1 = None
track2 = pylast.Track("Test Artist", "test title", self.network)
# Act / Assert
self.assertTrue(track1 != track2)
def test_track_get_correction(self):
# Arrange
track = pylast.Track("Guns N' Roses", "mrbrownstone", self.network)
# Act
corrected_track_name = track.get_correction()
# Assert
self.assertEqual(corrected_track_name, "Mr. Brownstone")
def test_track_with_no_mbid(self):
# Arrange
track = pylast.Track("Static-X", "Set It Off", self.network)
# Act
mbid = track.get_mbid()
# Assert
self.assertEqual(mbid, None)
def test_get_playcount(self):
# Arrange
user = self.network.get_user("RJ")
# Act
playcount = user.get_playcount()
# Assert
self.assertGreaterEqual(playcount, 128387)
def test_get_image(self):
# Arrange
user = self.network.get_user("RJ")
# Act / Assert
image = user.get_image()
self.assertTrue(image.startswith("https://"))
self.assertTrue(image.endswith(".png"))
def test_get_url(self):
# Arrange
user = self.network.get_user("RJ")
# Act / Assert
url = user.get_url()
self.assertEqual(url, "https://www.last.fm/user/rj")
if __name__ == '__main__':
unittest.main(failfast=True)

22
tox.ini
View file

@ -15,22 +15,30 @@ deps =
ipdb
pytest-cov
flaky
commands = py.test -v --cov pylast --cov-report term-missing {posargs}
commands = pytest -v -s -W all --cov pylast --cov-report term-missing {posargs}
[testenv:venv]
deps = ipdb
commands = {posargs}
[testenv:lint]
[testenv:py2lint]
deps =
coverage
pep8
pyyaml
pycodestyle
pyflakes
clonedigger
commands =
pyflakes pylast
pyflakes tests
pep8 pylast
pep8 tests
pycodestyle pylast
pycodestyle tests
./clonedigger.sh
[testenv:py3lint]
deps =
pycodestyle
pyflakes
commands =
pyflakes pylast
pyflakes tests
pycodestyle pylast
pycodestyle tests