diff options
author | Michaël Ball <michael.ball@gmail.com> | 2016-02-07 15:28:56 +0000 |
---|---|---|
committer | Michaël Ball <michael.ball@gmail.com> | 2016-07-15 07:15:13 +0100 |
commit | caa1c3ccdf94ee20140b3964aab0ad3058e03699 (patch) | |
tree | 12de8657e4fe4533a62c8693cb8cdaa90a74e27f /tests | |
parent | ea4391ba43fab82b8f1fbf2f9ab939e60d5e0bc2 (diff) |
Create test framework
Diffstat (limited to 'tests')
-rw-r--r-- | tests/common/utils_test.py | 14 | ||||
-rw-r--r-- | tests/conftest.py | 44 | ||||
-rw-r--r-- | tests/db/db_manager_test.py | 53 | ||||
-rw-r--r-- | tests/mach2_test.py | 52 | ||||
-rw-r--r-- | tests/models/album_test.py | 63 | ||||
-rw-r--r-- | tests/models/artist_test.py | 67 | ||||
-rw-r--r-- | tests/models/track_test.py | 106 | ||||
-rw-r--r-- | tests/test.db | bin | 0 -> 19456 bytes | |||
-rw-r--r-- | tests/test.ogg | bin | 0 -> 3929 bytes | |||
-rw-r--r-- | tests/testapp.db | bin | 0 -> 4096 bytes |
10 files changed, 399 insertions, 0 deletions
diff --git a/tests/common/utils_test.py b/tests/common/utils_test.py new file mode 100644 index 0000000..9cf1c55 --- /dev/null +++ b/tests/common/utils_test.py @@ -0,0 +1,14 @@ +from common import utils + + +def test_make_where_clause(): + params_with_between = {"column": "BETWEEN"} + + assert utils.make_where_clause(params_with_between) == "WHERE column "\ + "BETWEEN :column1 AND :column2" + + +def test_update_clause_from_dict(): + test_data = {"name": "Flaf"} + + assert utils.update_clause_from_dict(test_data) == "SET name = :name" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0028475 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,44 @@ +import os + +import pytest + +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")) + + def fin(): + database.close() + + request.addfinalizer(fin) + + return database + + +@pytest.fixture(scope="module") +def test_file(): + test_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), + "test.ogg") + + return test_file + + +@pytest.fixture(scope="class") +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")) + + def fin(): + library_db.close() + + request.addfinalizer(fin) + + request.cls.db = db + request.cls.library_db = library_db diff --git a/tests/db/db_manager_test.py b/tests/db/db_manager_test.py new file mode 100644 index 0000000..911c62d --- /dev/null +++ b/tests/db/db_manager_test.py @@ -0,0 +1,53 @@ +from db.db_manager import DbManager + + +class TestDbManager: + + def test_create_tables(self): + assert DbManager.create_album_table == "CREATE TABLE IF NOT EXISTS "\ + "album (id INTEGER PRIMARY KEY, name TEXT, date TEXT, "\ + "musicbrainz_albumid TEXT)" + + assert DbManager.create_album_artist_table == "CREATE TABLE IF NOT "\ + "EXISTS album_artist (album_id INTEGER, artist_id INTEGER, "\ + "CONSTRAINT ALBUM_ARTIST_PK PRIMARY KEY (album_id, artist_id), "\ + "CONSTRAINT ALBUM_ARTIST_FK_ALBUM FOREIGN KEY (album_id) "\ + "REFERENCES album(id), CONSTRAINT ALBUM_ARTIST_FK_ARTIST "\ + "FOREIGN KEY(artist_id) REFERENCES artist(id))" + + assert DbManager.create_album_track_table == "CREATE TABLE IF NOT "\ + "EXISTS album_track (album_id INTEGER, track_id INTEGER, "\ + "CONSTRAINT ALBUM_TRACK_PK PRIMARY KEY (album_id, track_id), "\ + "FOREIGN KEY(album_id) REFERENCES album(id), FOREIGN "\ + "KEY(track_id) REFERENCES track(id))" + + assert DbManager.create_artist_table == "CREATE TABLE IF NOT EXISTS "\ + "artist (id INTEGER PRIMARY KEY, name TEXT(2000000000), sortname "\ + "TEXT(2000000000), musicbrainz_artistid TEXT(2000000000))" + + assert DbManager.create_artist_track_table == "CREATE TABLE IF NOT "\ + "EXISTS artist_track (artist_id INTEGER, track_id INTEGER, "\ + "CONSTRAINT ARTIST_TRACK_PK PRIMARY KEY (artist_id, track_id), "\ + "FOREIGN KEY(artist_id) REFERENCES artist(id), FOREIGN "\ + "KEY(track_id) REFERENCES track(id))" + + assert DbManager.create_track_table == "CREATE TABLE IF NOT EXISTS "\ + "track (id INTEGER, tracknumber INTEGER, name TEXT(2000000000), "\ + "grouping TEXT(2000000000), filename TEXT(2000000000), "\ + "CONSTRAINT TRACK_PK PRIMARY KEY (id))" + + assert DbManager.create_musicbrainz_artist_index == "CREATE UNIQUE "\ + "INDEX IF NOT EXISTS artist_musicbrainz_artistid ON "\ + "artist(musicbrainz_artistid ASC)" + + assert DbManager.create_track_filename_index == "CREATE INDEX IF NOT "\ + "EXISTS track_filename_IDX ON track(filename)" + + assert DbManager.create_track_grouping_index == "CREATE INDEX IF NOT "\ + "EXISTS track_grouping_IDX ON track(grouping)" + + assert DbManager.create_track_name_index == "CREATE INDEX IF NOT "\ + "EXISTS track_name_IDX ON track(name)" + + assert DbManager.create_track_number_index == "CREATE INDEX IF NOT "\ + "EXISTS track_tracknumber_IDX ON track(tracknumber)" diff --git a/tests/mach2_test.py b/tests/mach2_test.py new file mode 100644 index 0000000..c45ff2a --- /dev/null +++ b/tests/mach2_test.py @@ -0,0 +1,52 @@ +import json +import unittest + +import pytest + +from mach2 import create_app + + +@pytest.mark.usefixtures("app") +class Mach2TestCase(unittest.TestCase): + + def setUp(self): + app = create_app(database=self.db, library=self.library_db) + app.config['TESTING'] = True + self.app = app.test_client() + + def login(self, username, password): + return self.app.post('/login', data=dict( + username=username, + password=password + ), follow_redirects=True) + + def logout(self): + return self.app.get('/logout', follow_redirects=True) + + def test_login(self): + rv = self.login("admin", "testpass") + assert bytes("Log out", "utf-8") in rv.data + self.logout() + rv = self.login("wrong", "definitelywrong") + assert bytes("Log out", "utf-8") 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 + + self.logout() + + def test_artists(self): + 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 + + artists = json.loads(rv.data.decode("utf-8")) + assert artists + + self.logout() diff --git a/tests/models/album_test.py b/tests/models/album_test.py new file mode 100644 index 0000000..680a5cb --- /dev/null +++ b/tests/models/album_test.py @@ -0,0 +1,63 @@ +from models.album import Album + + +def test_instance(database): + album = Album(id=1, db=database) + 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) + assert len(album.artists) == 1 + assert album.artists[0].name == "Artist 2" + + +def test_tracks(database): + album = Album(id=1, db=database) + assert len(album.tracks) == 2 + assert album.tracks[0].name == "Album track 1" + assert album.tracks[0].tracknumber == 1 + assert album.tracks[0].filename == "album/1.mp3" + assert album.tracks[1].name == "Album track 2" + assert album.tracks[1].tracknumber == 2 + assert album.tracks[1].grouping == "swing" + assert album.tracks[1].filename == "album/2.mp3" + + +def test_delete(database): + with database.conn: + cursor = database.cursor() + + cursor.execute("INSERT INTO album (name, date) VALUES(?,?)", + ("Test album", "2016-02-05")) + + album_id = cursor.lastrowid + cursor.close() + + album = Album(album_id, db=database) + + assert album.delete() + + test_album = Album(album_id, db=database) + 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) + + 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) + + assert len(no_album_results) == 0 + + +def test_all(database): + album_results = Album.all(db=database) + + assert len(album_results) > 0 diff --git a/tests/models/artist_test.py b/tests/models/artist_test.py new file mode 100644 index 0000000..bb66813 --- /dev/null +++ b/tests/models/artist_test.py @@ -0,0 +1,67 @@ +from models.artist import Artist + + +def test_instance(database): + album = Artist(id=1, db=database) + assert album.id == 1 + assert album.name == "Artist 1" + + +def test_albums(database): + artist1 = Artist(id=1, db=database) + assert len(artist1.albums) == 0 + artist2 = Artist(id=2, db=database) + 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) + 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) + assert artist2.tracks[0].name == "Album track 1" + assert artist2.tracks[0].tracknumber == 1 + assert artist2.tracks[0].filename == "album/1.mp3" + assert artist2.tracks[1].name == "Album track 2" + assert artist2.tracks[1].tracknumber == 2 + assert artist2.tracks[1].grouping == "swing" + assert artist2.tracks[1].filename == "album/2.mp3" + + +def test_delete(database): + with database.conn: + cursor = database.cursor() + + cursor.execute("INSERT INTO artist (name) VALUES(?)", ("Test artist",)) + + artist_id = cursor.lastrowid + + artist = Artist(artist_id, db=database) + + assert artist.delete() + + test_artist = Artist(artist_id, db=database) + 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) + + 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) + + assert len(no_artist_results) == 0 + + +def test_all(database): + artist_results = Artist.all(db=database) + + assert len(artist_results) > 0 diff --git a/tests/models/track_test.py b/tests/models/track_test.py new file mode 100644 index 0000000..56685fd --- /dev/null +++ b/tests/models/track_test.py @@ -0,0 +1,106 @@ +import mutagen + +from models.track import Track + + +def test_instance(database): + track = Track(id=1, db=database) + assert track.id == 1 + assert track.name == "Non album track" + assert track.filename == "1.mp3" + + +def test_as_dict(database): + track = Track(id=1, db=database) + + track_dict = track.as_dict() + + assert "_db" not in track_dict.keys() + assert track_dict["id"] == 1 + assert track_dict["name"] == "Non album track" + assert track_dict["filename"] == "1.mp3" + + +def test_album(database): + track1 = Track(id=1, db=database) + assert track1.album is None + track2 = Track(id=2, db=database) + 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 + assert track.artists[0].name == "Artist 1" + + +def test_find_by_path(database): + track1 = Track.find_by_path("album/2.mp3", db=database) + + assert track1.filename == "album/2.mp3" + assert track1.name == "Album track 2" + assert track1.grouping == "swing" + + nonexistent_track = Track.find_by_path("path/does/not/exist.mp3", + db=database) + assert nonexistent_track is None + + +def test_search(database): + tracks = Track.search(db=database, name={"data": "Album track %", + "operator": "LIKE"}) + + assert tracks is not None + assert len(tracks) == 2 + + +def test_store(database, test_file): + metadata = mutagen.File(test_file, easy=True) + + test_track = Track.store(test_file, metadata, db=database) + + assert test_track.filename == test_file + assert test_track.name == "Silence" + assert test_track.grouping == "Jazz" + assert test_track.tracknumber == 3 + + assert test_track.album.name == "Dummy album" + assert test_track.album.date == "2003" + + assert test_track.artists + assert test_track.artists[0].name == "Test Artist Flaf" + + +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.update(metadata) + + assert test_track.artists + assert len(test_track.artists) == 1 + assert test_track.artists[0].name == "New artist" + assert test_track.name == "New title" + + +def test_save(database, test_file): + test_track = Track.find_by_path(test_file, db=database) + + test_track.name = "Totally new name" + test_track.save() + + new_track_to_test = Track.find_by_path(test_file, db=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.delete() + + should_not_exist = Track.find_by_path(test_file, db=database) + + assert should_not_exist is None diff --git a/tests/test.db b/tests/test.db Binary files differnew file mode 100644 index 0000000..ccb7825 --- /dev/null +++ b/tests/test.db diff --git a/tests/test.ogg b/tests/test.ogg Binary files differnew file mode 100644 index 0000000..4ece5bc --- /dev/null +++ b/tests/test.ogg diff --git a/tests/testapp.db b/tests/testapp.db Binary files differnew file mode 100644 index 0000000..2fc7b8c --- /dev/null +++ b/tests/testapp.db |