From 9312ff0763ba4d7685f78fcf0c0df3e9775c0d6a Mon Sep 17 00:00:00 2001 From: aj Date: Fri, 5 Feb 2021 21:44:59 +0000 Subject: [PATCH] unittesting, removed key_code from audio features, batch audio features for retrieval --- pyproject.toml | 6 +++- scripts.py | 17 +++++++++++ spotframework/model/track.py | 1 - spotframework/net/network.py | 5 +++- tests/test_decorators.py | 39 +++++++++++++++++++++++++ tests/test_uri.py | 56 ++++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 scripts.py create mode 100644 tests/test_decorators.py create mode 100644 tests/test_uri.py diff --git a/pyproject.toml b/pyproject.toml index e790a50..c773a60 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "spotframework" -version = "1.0.0" +version = "1.0.1" description = "Spotify HTTP wrapper library" authors = ["andy "] repository = "https://github.com/Sarsoo/spotframework" @@ -14,6 +14,10 @@ click = "^7.1.2" [tool.poetry.dev-dependencies] pylint = "2.5.3" +[tool.poetry.scripts] +test = 'scripts:test' +testv = 'scripts:testv' + [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" diff --git a/scripts.py b/scripts.py new file mode 100644 index 0000000..49ba7a4 --- /dev/null +++ b/scripts.py @@ -0,0 +1,17 @@ +import subprocess + +def test(): + """ + Run all unittests. + """ + subprocess.run( + ['python', '-u', '-m', 'unittest', 'discover', "-s", "tests"] + ) + +def testv(): + """ + Run all unittests with verbose. + """ + subprocess.run( + ['python', '-u', '-m', 'unittest', 'discover', "-v", "-s", "tests"] + ) \ No newline at end of file diff --git a/spotframework/model/track.py b/spotframework/model/track.py index 11374c2..97d7fb4 100644 --- a/spotframework/model/track.py +++ b/spotframework/model/track.py @@ -204,7 +204,6 @@ class AudioFeatures: 'uri': str(self.uri) if self.uri is not None else None, 'instrumentalness': self.instrumentalness, 'key': self.key, - 'key_code': self._key, 'liveness': self.liveness, 'loudness': self.loudness, 'mode': self.mode.value, diff --git a/spotframework/net/network.py b/spotframework/net/network.py index e17a49d..0175d67 100644 --- a/spotframework/net/network.py +++ b/spotframework/net/network.py @@ -774,7 +774,10 @@ class Network: elif isinstance(tracks, List): if all(isinstance(i, SimplifiedTrack) for i in tracks): - audio_features = self.track_audio_features(uris=[i.uri for i in tracks]) + + audio_features = list() + for chunk in self.chunk(tracks, 100): + audio_features += self.track_audio_features(uris=[i.uri for i in chunk]) if audio_features: if len(audio_features) != len(tracks): diff --git a/tests/test_decorators.py b/tests/test_decorators.py new file mode 100644 index 0000000..1eed48c --- /dev/null +++ b/tests/test_decorators.py @@ -0,0 +1,39 @@ +import unittest +from unittest.mock import Mock + +from spotframework.model.uri import Uri +from spotframework.util.decorators import inject_uri + +class TestInjectURI(unittest.TestCase): + + def test_one_uri(self): + func = Mock() + decorated = inject_uri(func, uris=False) + + result = decorated(uri="spotify:track:test") + + self.assertIsInstance(func.call_args.kwargs["uri"], Uri) + func.assert_called_once() + + def test_uris(self): + func = Mock() + decorated = inject_uri(func, uri=False) + + result = decorated(uris=["spotify:track:test", + "spotify:album:test"]) + + for arg in func.call_args.kwargs["uris"]: + self.assertIsInstance(arg, Uri) + func.assert_called_once() + + def test_uri_optional(self): + func = Mock() + decorated = inject_uri(func, uri_optional=True, uris=False) + + result = decorated() + + func.assert_called_once() + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_uri.py b/tests/test_uri.py new file mode 100644 index 0000000..9eab70a --- /dev/null +++ b/tests/test_uri.py @@ -0,0 +1,56 @@ +import unittest + +from spotframework.model.uri import Uri + +class TestURI(unittest.TestCase): + + def test_input_not_string(self): + with self.assertRaises(AttributeError): + Uri(7) + + def test_start_spotify(self): + with self.assertRaises(ValueError): + # all uris start with spotify + # spotify:track:3EHhS6B2qJWup1nqUVQy1H + Uri("_potify:track:3EHhS6B2qJWup1nqUVQy1H") + + # test 5 component scenario or remove from source + + def test_6_components_is_local_uri(self): + with self.assertRaises(ValueError): + # all uris start with spotify + # spotify:local:{artist}:{album_title}:{track_title}:{duration_in_seconds} + Uri("spotify:_ocal:{artist}:{album_title}:{track_title}:{duration_in_seconds}") + + def test_too_many_components(self): + with self.assertRaises(ValueError): + # all uris start with spotifys + Uri("spotify:test:test:test:test:test:test") + + def test_equals(self): + uri_one = Uri("spotify:track:test") + uri_two = Uri("spotify:track:test") + + self.assertEqual(uri_one, uri_two) + + def test_equal_different_type(self): + uri_one = Uri("spotify:track:test") + uri_two = 7 + + self.assertNotEqual(uri_one, uri_two) + + def test_equal_object_type(self): + uri_one = Uri("spotify:album:test") + uri_two = Uri("spotify:track:test") + + self.assertNotEqual(uri_one, uri_two) + + def test_equal_object_id(self): + uri_one = Uri("spotify:track:test") + uri_two = Uri("spotify:track:tester") + + self.assertNotEqual(uri_one, uri_two) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file