Each user can make playlists: a name (1 to 200 characters), an optional description and an ordered list of tracks, where a track may appear more than once (each place has its own item id). The API makes, lists, renames and deletes playlists, and adds tracks (at the end or at a position), lists them, moves one and removes one; positions count from 0 without gaps. A playlist is its owner's alone: somebody else's answers playlist_not_found (404), as one that does not exist. A track whose files were deleted or moved stays in the playlist and is listed with available false. Deleting a user deletes their playlists. Limits: 1,000 playlists per user, 10,000 tracks per playlist, 500 tracks per request. Changes of one playlist are serialised by locking its row, so two requests at once keep the positions unique on PostgreSQL too. Migration 0019, OpenAPI spec, contract test, README and the web API types follow. The web app comes separately. Closes #89
188 lines
6.1 KiB
Go
188 lines
6.1 KiB
Go
package playlists
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fakeStore records what the service asks and answers what the test sets.
|
|
type fakeStore struct {
|
|
created []string // name|description
|
|
updated [][2]*string
|
|
listLimit int
|
|
listOffset int
|
|
itemsLimit int
|
|
itemsOffset int
|
|
addIDs []int64
|
|
addPos *int
|
|
movedTo int
|
|
err error
|
|
}
|
|
|
|
func (f *fakeStore) Create(_ context.Context, _ int64, name, description string, _ time.Time) (Playlist, error) {
|
|
f.created = append(f.created, name+"|"+description)
|
|
return Playlist{ID: 1, Name: name, Description: description}, f.err
|
|
}
|
|
|
|
func (f *fakeStore) List(_ context.Context, _ int64, limit, offset int) ([]Playlist, int, error) {
|
|
f.listLimit, f.listOffset = limit, offset
|
|
return nil, 0, f.err
|
|
}
|
|
|
|
func (f *fakeStore) Get(context.Context, int64, int64) (Playlist, error) { return Playlist{}, f.err }
|
|
|
|
func (f *fakeStore) Update(_ context.Context, _, _ int64, name, description *string, _ time.Time) (Playlist, error) {
|
|
f.updated = append(f.updated, [2]*string{name, description})
|
|
return Playlist{}, f.err
|
|
}
|
|
|
|
func (f *fakeStore) Delete(context.Context, int64, int64) error { return f.err }
|
|
|
|
func (f *fakeStore) Items(_ context.Context, _, _ int64, limit, offset int) ([]Item, int, error) {
|
|
f.itemsLimit, f.itemsOffset = limit, offset
|
|
return nil, 0, f.err
|
|
}
|
|
|
|
func (f *fakeStore) AddTracks(_ context.Context, _, _ int64, ids []int64, pos *int, _ time.Time) (Playlist, error) {
|
|
f.addIDs, f.addPos = ids, pos
|
|
return Playlist{}, f.err
|
|
}
|
|
|
|
func (f *fakeStore) RemoveItem(context.Context, int64, int64, int64, time.Time) error { return f.err }
|
|
|
|
func (f *fakeStore) MoveItem(_ context.Context, _, _, _ int64, pos int, _ time.Time) error {
|
|
f.movedTo = pos
|
|
return f.err
|
|
}
|
|
|
|
func newSvc() (*Service, *fakeStore) {
|
|
f := &fakeStore{}
|
|
return NewService(f), f
|
|
}
|
|
|
|
func TestCreateTrimsTheNameAndChecksItsSize(t *testing.T) {
|
|
s, f := newSvc()
|
|
if _, err := s.Create(context.Background(), 1, " Road trip ", " for the car "); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(f.created) != 1 || f.created[0] != "Road trip|for the car" {
|
|
t.Errorf("created = %q", f.created)
|
|
}
|
|
for name, tt := range map[string]struct{ name, desc string }{
|
|
"empty": {"", ""},
|
|
"only spaces": {" ", ""},
|
|
"name too long": {strings.Repeat("a", MaxNameLen+1), ""},
|
|
"too long, accent": {strings.Repeat("é", MaxNameLen+1), ""},
|
|
"description long": {"ok", strings.Repeat("a", MaxDescriptionLen+1)},
|
|
} {
|
|
if _, err := s.Create(context.Background(), 1, tt.name, tt.desc); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("%s: err = %v, want ErrInvalidInput", name, err)
|
|
}
|
|
}
|
|
// 200 letters with an accent are 200 characters, not 400 bytes.
|
|
if _, err := s.Create(context.Background(), 1, strings.Repeat("é", MaxNameLen), ""); err != nil {
|
|
t.Errorf("a name of %d characters: %v", MaxNameLen, err)
|
|
}
|
|
}
|
|
|
|
func TestUpdateNeedsSomethingToChangeAndAValidValue(t *testing.T) {
|
|
s, f := newSvc()
|
|
ctx := context.Background()
|
|
name, empty := " New ", " "
|
|
if _, err := s.Update(ctx, 1, 2, nil, nil); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("nothing to change: err = %v", err)
|
|
}
|
|
if _, err := s.Update(ctx, 1, 2, &empty, nil); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("an empty name: err = %v", err)
|
|
}
|
|
if _, err := s.Update(ctx, 1, 2, &name, &empty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := f.updated[0]; *got[0] != "New" || *got[1] != "" {
|
|
t.Errorf("updated = %q %q: the name is trimmed and an empty description clears it", *got[0], *got[1])
|
|
}
|
|
}
|
|
|
|
func TestListsArePagedWithDefaultsAndACap(t *testing.T) {
|
|
s, f := newSvc()
|
|
ctx := context.Background()
|
|
if _, err := s.List(ctx, 1, Page{}); err != nil || f.listLimit != DefaultLimit {
|
|
t.Errorf("default: limit = %d err = %v", f.listLimit, err)
|
|
}
|
|
if _, err := s.List(ctx, 1, Page{Limit: 100000, Offset: 5}); err != nil || f.listLimit != MaxLimit || f.listOffset != 5 {
|
|
t.Errorf("cap: limit = %d offset = %d err = %v", f.listLimit, f.listOffset, err)
|
|
}
|
|
if _, err := s.Items(ctx, 1, 2, Page{Limit: 100000}); err != nil || f.itemsLimit != MaxLimit {
|
|
t.Errorf("items cap: limit = %d err = %v", f.itemsLimit, err)
|
|
}
|
|
for _, p := range []Page{{Limit: -1}, {Offset: -1}} {
|
|
if _, err := s.List(ctx, 1, p); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("%+v: err = %v", p, err)
|
|
}
|
|
if _, err := s.Items(ctx, 1, 2, p); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("items %+v: err = %v", p, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAddTracksChecksWhatIsAdded(t *testing.T) {
|
|
s, f := newSvc()
|
|
ctx := context.Background()
|
|
two := 2
|
|
if _, err := s.AddTracks(ctx, 1, 2, []int64{5, 5, 6}, &two); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(f.addIDs) != 3 || f.addPos == nil || *f.addPos != 2 {
|
|
t.Errorf("ids = %v pos = %v: repeated tracks are allowed", f.addIDs, f.addPos)
|
|
}
|
|
neg := -1
|
|
big := make([]int64, MaxAddAtOnce+1)
|
|
for i := range big {
|
|
big[i] = int64(i + 1)
|
|
}
|
|
for name, tt := range map[string]struct {
|
|
ids []int64
|
|
pos *int
|
|
}{
|
|
"nothing": {nil, nil},
|
|
"too many": {big, nil},
|
|
"a zero id": {[]int64{1, 0}, nil},
|
|
"a negative id": {[]int64{-3}, nil},
|
|
"a negative place": {[]int64{1}, &neg},
|
|
} {
|
|
if _, err := s.AddTracks(ctx, 1, 2, tt.ids, tt.pos); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("%s: err = %v, want ErrInvalidInput", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMoveNeedsAPlaceFromZero(t *testing.T) {
|
|
s, f := newSvc()
|
|
if err := s.MoveItem(context.Background(), 1, 2, 3, 4); err != nil || f.movedTo != 4 {
|
|
t.Errorf("moved to %d err = %v", f.movedTo, err)
|
|
}
|
|
if err := s.MoveItem(context.Background(), 1, 2, 3, -1); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestIdsThatCannotExistAreNotFoundWithoutAskingTheStore(t *testing.T) {
|
|
s, f := newSvc()
|
|
f.err = errors.New("the store must not be asked")
|
|
ctx := context.Background()
|
|
if _, err := s.Get(ctx, 1, 0); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("get: err = %v", err)
|
|
}
|
|
if err := s.Delete(ctx, 1, -1); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("delete: err = %v", err)
|
|
}
|
|
if err := s.RemoveItem(ctx, 1, 2, 0); !errors.Is(err, ErrItemNotFound) {
|
|
t.Errorf("remove: err = %v", err)
|
|
}
|
|
if err := s.MoveItem(ctx, 1, 2, -5, 0); !errors.Is(err, ErrItemNotFound) {
|
|
t.Errorf("move: err = %v", err)
|
|
}
|
|
}
|