summaryrefslogtreecommitdiff
path: root/player-server/internal/service/fav_test.go
blob: 9a764edce0d2a749447999d7c13862ce2517fb63 (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
package service

import (
	"context"
	"errors"
	"testing"

	"codeberg.org/snonux/player/internal/model"
	"codeberg.org/snonux/player/internal/repository"
)

func TestFavService_ToggleFavorite(t *testing.T) {
	ctx := context.Background()

	tests := []struct {
		name     string
		storeErr error
		wantErr  bool
	}{
		{
			name: "ok",
		},
		{
			name:     "store error",
			storeErr: errors.New("boom"),
			wantErr:  true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			store := &repository.MockStore{
				MediaRepo: repository.MockMediaRepo{
					GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
						return &model.Media{ID: 1, SetID: 1}, nil
					},
				},
				UserRepo: repository.MockUserRepo{
					GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
						return &model.User{ID: id, IsAdmin: true}, nil
					},
				},
				SetRepo: repository.MockSetRepo{
					GetSetByIDFunc: func(ctx context.Context, id int64) (*model.Set, error) {
						return &model.Set{ID: id}, nil
					},
				},
				FavoriteRepo: repository.MockFavoriteRepo{
					ToggleFavoriteFunc: func(ctx context.Context, userID, mediaID int64) (bool, error) {
						return true, tt.storeErr
					},
				},
			}
			svc := NewFavService(store, &accessHelper{store: store})
			_, err := svc.ToggleFavorite(ctx, 1, 1)
			if tt.wantErr {
				if err == nil {
					t.Fatal("expected error")
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
		})
	}
}