diff options
author | Michaël Ball <michael.ball@gmail.com> | 2017-03-26 10:19:59 +0100 |
---|---|---|
committer | Michaël Ball <michael.ball@gmail.com> | 2017-06-04 07:37:53 +0100 |
commit | d06f96388d754ed41876f7fccb63f84241d44963 (patch) | |
tree | 640a4f3eaf7e1f2b76a246a1977c27775d0b59a1 /tests | |
parent | caa1c3ccdf94ee20140b3964aab0ad3058e03699 (diff) |
Works on python 2/pypy
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 8 | ||||
-rw-r--r-- | tests/mach2_test.py | 11 | ||||
-rw-r--r-- | tests/models/album_test.py | 16 | ||||
-rw-r--r-- | tests/models/artist_test.py | 20 | ||||
-rw-r--r-- | tests/models/track_test.py | 36 | ||||
-rw-r--r-- | tests/test.db | bin | 19456 -> 19456 bytes | |||
-rw-r--r-- | tests/testapp.db | bin | 4096 -> 2048 bytes | |||
-rw-r--r-- | tests/testnew.ogg | bin | 0 -> 3929 bytes | |||
-rw-r--r-- | tests/watcher_test.py | 96 |
9 files changed, 142 insertions, 45 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 0028475..90364ce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,8 +8,8 @@ from db.db_manager import DbManager @pytest.fixture(scope="module") def database(request): database = DbManager( - db=os.path.join(os.path.dirname(os.path.realpath(__file__)), - "test.db")) + os.path.join(os.path.dirname(os.path.realpath(__file__)), + "test.db")) def fin(): database.close() @@ -32,8 +32,8 @@ def app(request): db = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testapp.db") library_db = DbManager( - db=os.path.join(os.path.dirname(os.path.realpath(__file__)), - "test.db")) + os.path.join(os.path.dirname(os.path.realpath(__file__)), + "test.db")) def fin(): library_db.close() diff --git a/tests/mach2_test.py b/tests/mach2_test.py index c45ff2a..c33fd92 100644 --- a/tests/mach2_test.py +++ b/tests/mach2_test.py @@ -2,6 +2,7 @@ import json import unittest import pytest +import six from mach2 import create_app @@ -25,17 +26,17 @@ class Mach2TestCase(unittest.TestCase): def test_login(self): rv = self.login("admin", "testpass") - assert bytes("Log out", "utf-8") in rv.data + assert six.b("Log out") in rv.data self.logout() rv = self.login("wrong", "definitelywrong") - assert bytes("Log out", "utf-8") not in rv.data + assert six.b("Log out") not in rv.data self.logout() def test_album(self): self.login("admin", "testpass") rv = self.app.get("/albums/1") - assert bytes("Album 1", "utf-8") in rv.data + assert six.b("Album 1") in rv.data self.logout() @@ -43,8 +44,8 @@ class Mach2TestCase(unittest.TestCase): self.login("admin", "testpass") rv = self.app.get("/artists") - assert bytes("Artist 1", "utf-8") in rv.data - assert bytes("Artist 2", "utf-8") in rv.data + assert six.b("Artist 1") in rv.data + assert six.b("Artist 2") in rv.data artists = json.loads(rv.data.decode("utf-8")) assert artists diff --git a/tests/models/album_test.py b/tests/models/album_test.py index 680a5cb..5153626 100644 --- a/tests/models/album_test.py +++ b/tests/models/album_test.py @@ -2,20 +2,20 @@ from models.album import Album def test_instance(database): - album = Album(id=1, db=database) + album = Album(database, 1) assert album.id == 1 assert album.name == "Album 1" assert album.date == "1999-02-04" def test_artists(database): - album = Album(id=1, db=database) + album = Album(database, 1) assert len(album.artists) == 1 assert album.artists[0].name == "Artist 2" def test_tracks(database): - album = Album(id=1, db=database) + album = Album(database, 1) assert len(album.tracks) == 2 assert album.tracks[0].name == "Album track 1" assert album.tracks[0].tracknumber == 1 @@ -36,28 +36,28 @@ def test_delete(database): album_id = cursor.lastrowid cursor.close() - album = Album(album_id, db=database) + album = Album(database, album_id) assert album.delete() - test_album = Album(album_id, db=database) + test_album = Album(database, album_id) assert not hasattr(test_album, "name") def test_search(database): search_payload = {"name": {"data": "Album 1", "operator": "="}} - album_results = Album.search(db=database, **search_payload) + album_results = Album.search(database, **search_payload) assert len(album_results) > 0 invalid_search_payload = {"name": {"data": "This album does not exist", "operator": "="}} - no_album_results = Album.search(db=database, **invalid_search_payload) + no_album_results = Album.search(database, **invalid_search_payload) assert len(no_album_results) == 0 def test_all(database): - album_results = Album.all(db=database) + album_results = Album.all(database) assert len(album_results) > 0 diff --git a/tests/models/artist_test.py b/tests/models/artist_test.py index bb66813..b3504a1 100644 --- a/tests/models/artist_test.py +++ b/tests/models/artist_test.py @@ -2,27 +2,27 @@ from models.artist import Artist def test_instance(database): - album = Artist(id=1, db=database) + album = Artist(database, 1) assert album.id == 1 assert album.name == "Artist 1" def test_albums(database): - artist1 = Artist(id=1, db=database) + artist1 = Artist(database, 1) assert len(artist1.albums) == 0 - artist2 = Artist(id=2, db=database) + artist2 = Artist(database, 2) assert len(artist2.albums) == 1 assert artist2.albums[0].name == "Album 1" assert artist2.albums[0].date == "1999-02-04" def test_tracks(database): - artist1 = Artist(id=1, db=database) + artist1 = Artist(database, 1) assert len(artist1.tracks) == 1 assert artist1.tracks[0].name == "Non album track" assert artist1.tracks[0].tracknumber is None assert artist1.tracks[0].filename == "1.mp3" - artist2 = Artist(id=2, db=database) + artist2 = Artist(database, 2) assert artist2.tracks[0].name == "Album track 1" assert artist2.tracks[0].tracknumber == 1 assert artist2.tracks[0].filename == "album/1.mp3" @@ -40,28 +40,28 @@ def test_delete(database): artist_id = cursor.lastrowid - artist = Artist(artist_id, db=database) + artist = Artist(database, artist_id) assert artist.delete() - test_artist = Artist(artist_id, db=database) + test_artist = Artist(database, artist_id) assert not hasattr(test_artist, "name") def test_search(database): search_payload = {"name": {"data": "Artist 1", "operator": "="}} - artist_results = Artist.search(db=database, **search_payload) + artist_results = Artist.search(database, **search_payload) assert len(artist_results) > 0 invalid_search_payload = {"name": {"data": "This artist does not exist", "operator": "="}} - no_artist_results = Artist.search(db=database, **invalid_search_payload) + no_artist_results = Artist.search(database, **invalid_search_payload) assert len(no_artist_results) == 0 def test_all(database): - artist_results = Artist.all(db=database) + artist_results = Artist.all(database) assert len(artist_results) > 0 diff --git a/tests/models/track_test.py b/tests/models/track_test.py index 56685fd..2bb4df1 100644 --- a/tests/models/track_test.py +++ b/tests/models/track_test.py @@ -4,14 +4,14 @@ from models.track import Track def test_instance(database): - track = Track(id=1, db=database) + track = Track(database, 1) assert track.id == 1 assert track.name == "Non album track" assert track.filename == "1.mp3" - + assert track.artists def test_as_dict(database): - track = Track(id=1, db=database) + track = Track(database, 1) track_dict = track.as_dict() @@ -22,35 +22,35 @@ def test_as_dict(database): def test_album(database): - track1 = Track(id=1, db=database) + track1 = Track(database, 1) assert track1.album is None - track2 = Track(id=2, db=database) + track2 = Track(database, 2) assert track2.album.name == "Album 1" assert track2.album.date == "1999-02-04" def test_artists(database): - track = Track(id=1, db=database) - assert track.artists is not None - assert len(track.artists) > 0 + track = Track(database, 1) + assert track.artists assert track.artists[0].name == "Artist 1" def test_find_by_path(database): - track1 = Track.find_by_path("album/2.mp3", db=database) + track1 = Track.find_by_path("album/2.mp3", database) assert track1.filename == "album/2.mp3" assert track1.name == "Album track 2" assert track1.grouping == "swing" + assert track1.artists nonexistent_track = Track.find_by_path("path/does/not/exist.mp3", - db=database) + database) assert nonexistent_track is None def test_search(database): - tracks = Track.search(db=database, name={"data": "Album track %", - "operator": "LIKE"}) + tracks = Track.search(database, name={"data": "Album track %", + "operator": "LIKE"}) assert tracks is not None assert len(tracks) == 2 @@ -59,7 +59,7 @@ def test_search(database): def test_store(database, test_file): metadata = mutagen.File(test_file, easy=True) - test_track = Track.store(test_file, metadata, db=database) + test_track = Track.store(test_file, metadata, database) assert test_track.filename == test_file assert test_track.name == "Silence" @@ -76,7 +76,7 @@ def test_store(database, test_file): def test_update(database, test_file): metadata = {"artist": ["New artist"], "title": ["New title"]} - test_track = Track.find_by_path(test_file, db=database) + test_track = Track.find_by_path(test_file, database) test_track.update(metadata) assert test_track.artists @@ -86,21 +86,21 @@ def test_update(database, test_file): def test_save(database, test_file): - test_track = Track.find_by_path(test_file, db=database) + test_track = Track.find_by_path(test_file, database) test_track.name = "Totally new name" test_track.save() - new_track_to_test = Track.find_by_path(test_file, db=database) + new_track_to_test = Track.find_by_path(test_file, database) assert new_track_to_test.name == "Totally new name" def test_delete(database, test_file): - test_track = Track.find_by_path(test_file, db=database) + test_track = Track.find_by_path(test_file, database) test_track.delete() - should_not_exist = Track.find_by_path(test_file, db=database) + should_not_exist = Track.find_by_path(test_file, database) assert should_not_exist is None diff --git a/tests/test.db b/tests/test.db Binary files differindex ccb7825..1602d6f 100644 --- a/tests/test.db +++ b/tests/test.db diff --git a/tests/testapp.db b/tests/testapp.db Binary files differindex 2fc7b8c..3ccfa2f 100644 --- a/tests/testapp.db +++ b/tests/testapp.db diff --git a/tests/testnew.ogg b/tests/testnew.ogg Binary files differnew file mode 100644 index 0000000..64a9f49 --- /dev/null +++ b/tests/testnew.ogg diff --git a/tests/watcher_test.py b/tests/watcher_test.py new file mode 100644 index 0000000..5f92d36 --- /dev/null +++ b/tests/watcher_test.py @@ -0,0 +1,96 @@ +"""Tests for the watcher module.""" +from os import remove +from os.path import dirname, join, realpath +from shutil import copy, move, rmtree +from tempfile import mkdtemp +import unittest + +import mutagen +import six + +from db.db_manager import DbManager +from models.track import Track +from watcher import LibraryWatcher + + +if six.PY2: + def _u(string): + return unicode(string, encoding="utf_8") +else: + def _u(string): + return string + + +class WatcherTestCase(unittest.TestCase): + """Defines tests for the watcher module. + + Extends: + unittest.Testcase + + """ + + @classmethod + def setUpClass(cls): + """Set up fixtures for the tests.""" + cls._tempdir = mkdtemp() + cls._tempdbdir = mkdtemp() + cls._db_path = join(cls._tempdbdir, "test.db") + copy(join(dirname(realpath(__file__)), "test.db"), cls._db_path) + cls._db = DbManager(cls._db_path) + cls._watcher = LibraryWatcher(cls._tempdir, cls._db_path) + + @classmethod + def tearDownClass(cls): + """Remove test fixtures.""" + cls._watcher.stop() + rmtree(cls._tempdbdir) + rmtree(cls._tempdir) + + def test_watcher_actions(self): + """Test creating, moving, modifying and deleting a file.""" + new_file = join(dirname(realpath(__file__)), "testnew.ogg") + copy(new_file, self._tempdir) + + self._watcher.check_for_events() + + found_track = Track.find_by_path(join(self._tempdir, "testnew.ogg"), + self._db) + assert found_track + assert "testnew.ogg" in found_track.filename + assert found_track.artists + found_artist = False + for artist in found_track.artists: + if _u("Art Ist") == artist.name: + found_artist = True + break + + assert found_artist + + original_file = join(self._tempdir, "testnew.ogg") + moved_file = join(self._tempdir, "testmoved.ogg") + move(original_file, moved_file) + + self._watcher.check_for_events() + + moved_track = Track.find_by_path(moved_file, self._db) + + assert moved_track + assert "testmoved.ogg" in moved_track.filename + + original_metadata = mutagen.File(moved_file, easy=True) + + original_metadata["title"] = [_u("New title")] + original_metadata.save() + + self._watcher.check_for_events() + + modified_track = Track.find_by_path(moved_file, self._db) + + assert modified_track + assert modified_track.name == "New title" + + remove(moved_file) + + self._watcher.check_for_events() + + assert Track.find_by_path(moved_file, self._db) is None |