summaryrefslogtreecommitdiff
path: root/internal/service/trash_test.go
blob: 8d88a81ef9d93266e8733168a48b7b4a81c28ff3 (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
package service

import (
	"context"
	"errors"
	"testing"

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

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

	tests := []struct {
		name     string
		media    []model.Media
		storeErr error
		wantErr  bool
		wantLen  int
	}{
		{
			name:    "ok empty",
			media:   []model.Media{},
			wantLen: 0,
		},
		{
			name:    "ok with items",
			media:   []model.Media{{ID: 1, FileName: "a.mp4"}},
			wantLen: 1,
		},
		{
			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{
					ListDeletedMediaFunc: func(ctx context.Context) ([]model.Media, error) {
						return tt.media, tt.storeErr
					},
				},
			}
			svc := NewTrashService(store)
			res, err := svc.ListTrash(ctx)
			if tt.wantErr {
				if err == nil {
					t.Fatal("expected error")
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if len(res) != tt.wantLen {
				t.Fatalf("expected %d items, got %d", tt.wantLen, len(res))
			}
		})
	}
}