summaryrefslogtreecommitdiff
path: root/tests/models/track_test.py
blob: 2bb4df1a627cc56f86f461a61db7557b84e89b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import mutagen

from models.track import Track


def test_instance(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(database, 1)

    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(database, 1)
    assert track1.album is None
    track2 = Track(database, 2)
    assert track2.album.name == "Album 1"
    assert track2.album.date == "1999-02-04"


def test_artists(database):
    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", 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",
                                           database)
    assert nonexistent_track is None


def test_search(database):
    tracks = Track.search(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, 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, 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, database)

    test_track.name = "Totally new name"
    test_track.save()

    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, database)

    test_track.delete()

    should_not_exist = Track.find_by_path(test_file, database)

    assert should_not_exist is None