-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdiscogs.go
211 lines (175 loc) · 5.87 KB
/
testdiscogs.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package discogs
import (
"context"
"log"
pb "github.com/brotherlogic/discogs/proto"
"golang.org/x/exp/maps"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type TestDiscogsClient struct {
collectionRecords []*pb.Release
nonCollectionRecords []*pb.Release
UserId int32
Fields []*pb.Field
Folders []*pb.Folder
Sales []*pb.SaleItem
Rating map[int64]int32
Wants map[int64]*pb.Want
Masters map[int64]*pb.Release
}
func GetTestClient() *TestDiscogsClient {
return &TestDiscogsClient{
Rating: make(map[int64]int32),
Wants: make(map[int64]*pb.Want)}
}
func (t *TestDiscogsClient) Throttle() {
}
func (t *TestDiscogsClient) UpdateSaleState(ctx context.Context, saleId int64, releaseId int64, condition string, saleState pb.SaleStatus) error {
for _, sale := range t.Sales {
if sale.GetSaleId() == saleId {
sale.Status = saleState
}
}
return nil
}
func (t *TestDiscogsClient) GetMasterReleases(ctx context.Context, masterId int64, page int32, sort pb.MasterSort) ([]*pb.MasterRelease, error) {
var mr []*pb.MasterRelease
for _, r := range t.collectionRecords {
if r.GetMasterId() == masterId {
mr = append(mr, &pb.MasterRelease{Id: r.GetId()})
}
}
return mr, nil
}
func (t *TestDiscogsClient) SetRating(ctx context.Context, releaseId int64, newScore int32) error {
t.Rating[releaseId] = newScore
return nil
}
func (t *TestDiscogsClient) GetUserId() int32 {
return t.UserId
}
func (t *TestDiscogsClient) CreateSale(ctx context.Context, params SaleParams) (int64, error) {
return 1234, nil
}
func (t *TestDiscogsClient) CreateFolder(ctx context.Context, folderName string) (*pb.Folder, error) {
folder := &pb.Folder{Name: folderName, Id: 123}
t.Folders = append(t.Folders, folder)
log.Printf("Added folder: %v", t.Folders)
return folder, nil
}
func (t *TestDiscogsClient) DeleteFolder(ctx context.Context, folderId int32) error {
return nil
}
func (t *TestDiscogsClient) GetOrder(ctx context.Context, orderId string) (*pb.Order, error) {
return &pb.Order{}, nil
}
func (t *TestDiscogsClient) AddWant(ctx context.Context, releaseId int64) (*pb.Want, error) {
if t.Wants == nil {
t.Wants = make(map[int64]*pb.Want)
}
t.Wants[releaseId] = &pb.Want{Id: releaseId}
return &pb.Want{}, nil
}
func (t *TestDiscogsClient) DeleteWant(ctx context.Context, wantId int64) error {
if t.Wants == nil {
t.Wants = make(map[int64]*pb.Want)
}
delete(t.Wants, wantId)
return nil
}
func (t *TestDiscogsClient) GetSale(ctx context.Context, saleId int64) (*pb.SaleItem, error) {
for _, sale := range t.Sales {
if sale.GetSaleId() == saleId {
return sale, nil
}
}
return &pb.SaleItem{}, nil
}
func (t *TestDiscogsClient) SetDownloader(_ Downloader) {
// Do nothing
}
func (t *TestDiscogsClient) UpdateSale(ctx context.Context, saleId int64, releaseId int64, condition string, newPrice int32) error {
for _, sale := range t.Sales {
if sale.GetSaleId() == saleId {
sale.Price.Value = newPrice
}
}
return nil
}
func (t *TestDiscogsClient) GetWants(ctx context.Context, page int32) ([]*pb.Want, *pb.Pagination, error) {
if t.Wants == nil {
t.Wants = make(map[int64]*pb.Want)
}
return maps.Values(t.Wants), &pb.Pagination{}, nil
}
func (t *TestDiscogsClient) ListSales(ctx context.Context, page int32) ([]*pb.SaleItem, *pb.Pagination, error) {
return t.Sales, &pb.Pagination{}, nil
}
func (t *TestDiscogsClient) GetRelease(ctx context.Context, releaseId int64) (*pb.Release, error) {
for _, r := range t.collectionRecords {
if r.GetId() == releaseId {
return r, nil
}
}
for _, r := range t.nonCollectionRecords {
if r.GetId() == releaseId {
return r, nil
}
}
return nil, status.Errorf(codes.NotFound, "Unable to locate %v", releaseId)
}
func (t *TestDiscogsClient) SetFolder(ctx context.Context, instanceId, releaseId, folderId, newFolderId int64) error {
return nil
}
func (t *TestDiscogsClient) GetReleaseStats(ctx context.Context, releaseId int64) (*pb.ReleaseStats, error) {
return &pb.ReleaseStats{}, nil
}
func (t *TestDiscogsClient) ForUser(user *pb.User) Discogs {
t.UserId = user.GetDiscogsUserId()
return t
}
func (t *TestDiscogsClient) GetUserFolders(_ context.Context) ([]*pb.Folder, error) {
log.Printf("Getting folders: %v", t.Folders)
return t.Folders, nil
}
func (t *TestDiscogsClient) GetFields(_ context.Context) ([]*pb.Field, error) {
return t.Fields, nil
}
func (t *TestDiscogsClient) SetField(ctx context.Context, r *pb.Release, fnum int, value string) error {
t.Fields = append(t.Fields, &pb.Field{Name: value, Id: int32(fnum)})
return nil
}
func (t *TestDiscogsClient) AddCollectionRelease(r *pb.Release) {
if t.collectionRecords == nil {
t.collectionRecords = make([]*pb.Release, 0)
}
t.collectionRecords = append(t.collectionRecords, r)
}
func (t *TestDiscogsClient) GetCollectionRelease(ctx context.Context, id int64, page int32) ([]*pb.Release, *pb.Pagination, error) {
var ret []*pb.Release
for _, r := range t.collectionRecords {
if r.Id == id {
ret = append(ret, r)
}
}
return ret, &pb.Pagination{}, nil
}
func (t *TestDiscogsClient) AddCNonollectionRelease(r *pb.Release) {
if t.collectionRecords == nil {
t.nonCollectionRecords = make([]*pb.Release, 0)
}
t.nonCollectionRecords = append(t.collectionRecords, r)
}
func (t *TestDiscogsClient) GetLoginURL() (string, string, string, error) {
return "", "madeuptoken", "madeupysecret", nil
}
func (t *TestDiscogsClient) HandleDiscogsResponse(ctx context.Context, secret, token, verifier string) (string, string, error) {
return "", "", nil
}
func (t *TestDiscogsClient) GetDiscogsUser(ctx context.Context) (*pb.User, error) {
return &pb.User{Username: "madeupuser"}, nil
}
func (t *TestDiscogsClient) GetCollection(ctx context.Context, page int32) ([]*pb.Release, *pb.Pagination, error) {
return append(t.collectionRecords, t.nonCollectionRecords...), &pb.Pagination{}, nil
}