From caa1c3ccdf94ee20140b3964aab0ad3058e03699 Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Sun, 7 Feb 2016 15:28:56 +0000 Subject: Create test framework --- tests/models/album_test.py | 63 ++++++++++++++++++++++++++ tests/models/artist_test.py | 67 ++++++++++++++++++++++++++++ tests/models/track_test.py | 106 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 236 insertions(+) create mode 100644 tests/models/album_test.py create mode 100644 tests/models/artist_test.py create mode 100644 tests/models/track_test.py (limited to 'tests/models') 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 -- cgit v1.2.3