Skip to content

Commit e6b6900

Browse files
committed
Add unit tests for rating tracks, albums and artists
1 parent 8f9338f commit e6b6900

File tree

2 files changed

+158
-4
lines changed

2 files changed

+158
-4
lines changed

src/library/local_favourites_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// favourites works.
1414
func TestFavouritesTracks(t *testing.T) {
1515
ctx := context.Background()
16-
lib := setUpLibForFavoutites(t)
16+
lib := setUpLibForFavRatingsTesting(t)
1717
defer func() { _ = lib.Truncate() }()
1818

1919
classicalSearch := SearchArgs{Query: "Classical Bugs", Count: 10}
@@ -93,7 +93,7 @@ func TestFavouritesTracks(t *testing.T) {
9393
// TestFavouritesAlbums checks that adding albums to favourites work.
9494
func TestFavouritesAlbums(t *testing.T) {
9595
ctx := context.Background()
96-
lib := setUpLibForFavoutites(t)
96+
lib := setUpLibForFavRatingsTesting(t)
9797
defer func() { _ = lib.Truncate() }()
9898

9999
classicalSearch := SearchArgs{Query: "Classical Bugs", Count: 10}
@@ -170,7 +170,7 @@ func TestFavouritesAlbums(t *testing.T) {
170170
// TestFavouritesArtists checks that adding artists to the favourites works.
171171
func TestFavouritesArtists(t *testing.T) {
172172
ctx := context.Background()
173-
lib := setUpLibForFavoutites(t)
173+
lib := setUpLibForFavRatingsTesting(t)
174174
defer func() { _ = lib.Truncate() }()
175175

176176
stackSearchArgs := SearchArgs{Query: "Stack Overflow", Count: 10}
@@ -241,7 +241,9 @@ func TestFavouritesArtists(t *testing.T) {
241241
}
242242
}
243243

244-
func setUpLibForFavoutites(t *testing.T) *LocalLibrary {
244+
// setUpLibForFavRatingsTesting returns a library which already has some media files
245+
// included. It is suitable for testing of the favourites and ratings.
246+
func setUpLibForFavRatingsTesting(t *testing.T) *LocalLibrary {
245247
ctx := context.Background()
246248
lib, err := NewLocalLibrary(ctx, SQLiteMemoryFile, getTestMigrationFiles())
247249
if err != nil {

src/library/local_ratings_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package library
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/ironsmile/euterpe/src/assert"
8+
)
9+
10+
// TestRatingOutOfBounds checks that setting ratings outside of the [0-5] interval causes
11+
// an error.
12+
func TestRatingOutOfBounds(t *testing.T) {
13+
ctx := context.Background()
14+
lib := setUpLibForFavRatingsTesting(t)
15+
defer func() { _ = lib.Truncate() }()
16+
17+
tracks := lib.Search(ctx, SearchArgs{
18+
Count: 1,
19+
})
20+
if len(tracks) < 1 {
21+
t.Errorf("expected at least one track to be returned")
22+
}
23+
24+
err := lib.SetTrackRating(ctx, tracks[0].ID, 12)
25+
if err == nil {
26+
t.Errorf("expected error when setting out of bounds track rating")
27+
}
28+
29+
err = lib.SetAlbumRating(ctx, tracks[0].AlbumID, 12)
30+
if err == nil {
31+
t.Errorf("expected error when setting out of bounds album rating")
32+
}
33+
34+
err = lib.SetArtistRating(ctx, tracks[0].ArtistID, 12)
35+
if err == nil {
36+
t.Errorf("expected error when setting out of bounds artist rating")
37+
}
38+
}
39+
40+
// TestRatingTracks checks that setting and removing ratings for tracks does work.
41+
func TestRatingTracks(t *testing.T) {
42+
ctx := context.Background()
43+
lib := setUpLibForFavRatingsTesting(t)
44+
defer func() { _ = lib.Truncate() }()
45+
46+
tracks := lib.Search(ctx, SearchArgs{
47+
Count: 1,
48+
})
49+
if len(tracks) < 1 {
50+
t.Errorf("expected at least one track to be returned")
51+
}
52+
53+
track := tracks[0]
54+
if err := lib.SetTrackRating(ctx, track.ID, 3); err != nil {
55+
t.Errorf("unexpected error: %s", err)
56+
}
57+
58+
found, err := lib.GetTrack(ctx, track.ID)
59+
if err != nil {
60+
t.Errorf("unexpected error: %s", err)
61+
}
62+
63+
assert.Equal(t, track.ID, found.ID)
64+
assert.Equal(t, 3, found.Rating, "wrong track rating")
65+
66+
if err := lib.SetTrackRating(ctx, track.ID, 0); err != nil {
67+
t.Errorf("error removing track rating: %s", err)
68+
}
69+
70+
found, err = lib.GetTrack(ctx, track.ID)
71+
if err != nil {
72+
t.Errorf("unexpected error: %s", err)
73+
}
74+
assert.Equal(t, track.ID, found.ID)
75+
assert.Equal(t, 0, found.Rating, "wrong track rating after removal")
76+
}
77+
78+
// TestRatingsAlbums checks that setting and removing ratings from albums work.
79+
func TestRatingsAlbums(t *testing.T) {
80+
ctx := context.Background()
81+
lib := setUpLibForFavRatingsTesting(t)
82+
defer func() { _ = lib.Truncate() }()
83+
84+
albums := lib.SearchAlbums(ctx, SearchArgs{
85+
Count: 1,
86+
})
87+
if len(albums) < 1 {
88+
t.Errorf("expected at least one album to be returned")
89+
}
90+
91+
album := albums[0]
92+
if err := lib.SetAlbumRating(ctx, album.ID, 4); err != nil {
93+
t.Errorf("unexpected error: %s", err)
94+
}
95+
96+
found, err := lib.GetAlbum(ctx, album.ID)
97+
if err != nil {
98+
t.Errorf("unexpected error: %s", err)
99+
}
100+
101+
assert.Equal(t, album.ID, found.ID)
102+
assert.Equal(t, 4, found.Rating, "wrong album rating")
103+
104+
if err := lib.SetAlbumRating(ctx, album.ID, 0); err != nil {
105+
t.Errorf("error removing album rating: %s", err)
106+
}
107+
108+
found, err = lib.GetAlbum(ctx, album.ID)
109+
if err != nil {
110+
t.Errorf("unexpected error: %s", err)
111+
}
112+
assert.Equal(t, album.ID, found.ID)
113+
assert.Equal(t, 0, found.Rating, "wrong album rating after removal")
114+
}
115+
116+
// TestRatingsArtists checks that setting and removing ratings for artists work.
117+
func TestRatingsArtists(t *testing.T) {
118+
ctx := context.Background()
119+
lib := setUpLibForFavRatingsTesting(t)
120+
defer func() { _ = lib.Truncate() }()
121+
122+
artists := lib.SearchArtists(ctx, SearchArgs{
123+
Count: 1,
124+
})
125+
if len(artists) < 1 {
126+
t.Errorf("expected at least one artist to be returned")
127+
}
128+
129+
artist := artists[0]
130+
if err := lib.SetArtistRating(ctx, artist.ID, 4); err != nil {
131+
t.Errorf("unexpected error: %s", err)
132+
}
133+
134+
found, err := lib.GetArtist(ctx, artist.ID)
135+
if err != nil {
136+
t.Errorf("unexpected error: %s", err)
137+
}
138+
139+
assert.Equal(t, artist.ID, found.ID)
140+
assert.Equal(t, 4, found.Rating, "wrong artist rating")
141+
142+
if err := lib.SetArtistRating(ctx, artist.ID, 0); err != nil {
143+
t.Errorf("error removing artist rating: %s", err)
144+
}
145+
146+
found, err = lib.GetArtist(ctx, artist.ID)
147+
if err != nil {
148+
t.Errorf("unexpected error: %s", err)
149+
}
150+
assert.Equal(t, artist.ID, found.ID)
151+
assert.Equal(t, 0, found.Rating, "wrong artist rating after removal")
152+
}

0 commit comments

Comments
 (0)