-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
SpotifyMappingFixture.cs
346 lines (298 loc) · 11.6 KB
/
SpotifyMappingFixture.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.Net;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Cloud;
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.ImportLists.Spotify;
using NzbDrone.Core.MetadataSource;
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ImportListTests
{
[TestFixture]
// the base import list class is abstract so use the followed artists one
public class SpotifyMappingFixture : CoreTest<SpotifyFollowedArtists>
{
[SetUp]
public void Setup()
{
Mocker.SetConstant<ILidarrCloudRequestBuilder>(new LidarrCloudRequestBuilder());
Mocker.SetConstant<IMetadataRequestBuilder>(Mocker.Resolve<MetadataRequestBuilder>());
}
[Test]
public void map_artist_should_return_name_if_id_null()
{
var data = new SpotifyImportListItemInfo
{
Artist = "Adele"
};
Subject.MapArtistItem(data);
data.Artist.Should().Be("Adele");
data.ArtistMusicBrainzId.Should().BeNull();
data.Album.Should().BeNull();
data.AlbumMusicBrainzId.Should().BeNull();
}
[Test]
public void map_artist_should_set_id_0_if_no_match()
{
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Get<ArtistResource>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>((x) => new HttpResponse<ArtistResource>(new HttpResponse(x, new HttpHeader(), Array.Empty<byte>(), HttpStatusCode.NotFound)));
var data = new SpotifyImportListItemInfo
{
Artist = "Adele",
ArtistSpotifyId = "id"
};
Subject.MapArtistItem(data);
data.ArtistMusicBrainzId.Should().Be("0");
}
[Test]
public void map_artist_should_not_update_id_if_http_throws()
{
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Get<ArtistResource>(It.IsAny<HttpRequest>()))
.Throws(new Exception("Dummy exception"));
var data = new SpotifyImportListItemInfo
{
Artist = "Adele",
ArtistSpotifyId = "id"
};
Subject.MapArtistItem(data);
data.ArtistMusicBrainzId.Should().BeNull();
ExceptionVerification.ExpectedErrors(1);
}
[Test]
public void map_artist_should_work()
{
UseRealHttp();
var data = new SpotifyImportListItemInfo
{
Artist = "Adele",
ArtistSpotifyId = "4dpARuHxo51G3z768sgnrY"
};
Subject.MapArtistItem(data);
data.Should().NotBeNull();
data.Artist.Should().Be("Adele");
data.ArtistMusicBrainzId.Should().Be("cc2c9c3c-b7bc-4b8b-84d8-4fbd8779e493");
data.Album.Should().BeNull();
data.AlbumMusicBrainzId.Should().BeNull();
}
[Test]
public void map_album_should_return_name_if_uri_null()
{
var data = new SpotifyImportListItemInfo
{
Album = "30",
Artist = "Adele"
};
Subject.MapAlbumItem(data);
data.Should().NotBeNull();
data.Artist.Should().Be("Adele");
data.ArtistMusicBrainzId.Should().BeNull();
data.Album.Should().Be("30");
data.AlbumMusicBrainzId.Should().BeNull();
}
[Test]
public void map_album_should_set_id_0_if_no_match()
{
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Get<AlbumResource>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>((x) => new HttpResponse<AlbumResource>(new HttpResponse(x, new HttpHeader(), Array.Empty<byte>(), HttpStatusCode.NotFound)));
var data = new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "id",
Artist = "Adele"
};
Subject.MapAlbumItem(data);
data.AlbumMusicBrainzId.Should().Be("0");
}
[Test]
public void map_album_should_not_update_id_if_http_throws()
{
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Get<AlbumResource>(It.IsAny<HttpRequest>()))
.Throws(new Exception("Dummy exception"));
var data = new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "id",
Artist = "Adele"
};
Subject.MapAlbumItem(data);
data.Should().NotBeNull();
data.Artist.Should().Be("Adele");
data.ArtistMusicBrainzId.Should().BeNull();
data.Album.Should().Be("30");
data.AlbumMusicBrainzId.Should().BeNull();
ExceptionVerification.ExpectedErrors(1);
}
[Test]
[Ignore("Pending mapping fixes", Until = "2024-02-20 00:00:00Z")]
public void map_album_should_work()
{
UseRealHttp();
var data = new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "21jF5jlMtzo94wbxmJ18aa",
Artist = "Adele"
};
Subject.MapAlbumItem(data);
data.Should().NotBeNull();
data.Artist.Should().Be("Adele");
data.Album.Should().Be("30");
data.AlbumMusicBrainzId.Should().Be("d8a99910-33c2-481d-9b96-085ece5545f6");
}
[Test]
public void map_spotify_releases_should_only_map_album_id_for_album()
{
var data = new List<SpotifyImportListItemInfo>
{
new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "21jF5jlMtzo94wbxmJ18aa",
Artist = "Adele",
ArtistSpotifyId = "4dpARuHxo51G3z768sgnrY"
}
};
var map = new List<SpotifyMap>
{
new SpotifyMap
{
SpotifyId = "21jF5jlMtzo94wbxmJ18aa",
MusicbrainzId = "d8a99910-33c2-481d-9b96-085ece5545f6"
},
new SpotifyMap
{
SpotifyId = "4dpARuHxo51G3z768sgnrY",
MusicbrainzId = "cc2c9c3c-b7bc-4b8b-84d8-4fbd8779e493"
}
};
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Post<List<SpotifyMap>>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse<List<SpotifyMap>>(new HttpResponse(r, new HttpHeader(), map.ToJson())));
var result = Subject.MapSpotifyReleases(data);
result[0].AlbumMusicBrainzId.Should().Be("d8a99910-33c2-481d-9b96-085ece5545f6");
result[0].ArtistMusicBrainzId.Should().BeNull();
}
[Test]
public void map_spotify_releases_should_map_artist_id_for_artist()
{
var data = new List<SpotifyImportListItemInfo>
{
new SpotifyImportListItemInfo
{
Artist = "Adele",
ArtistSpotifyId = "4dpARuHxo51G3z768sgnrY"
}
};
var map = new List<SpotifyMap>
{
new SpotifyMap
{
SpotifyId = "21jF5jlMtzo94wbxmJ18aa",
MusicbrainzId = "d8a99910-33c2-481d-9b96-085ece5545f6"
},
new SpotifyMap
{
SpotifyId = "4dpARuHxo51G3z768sgnrY",
MusicbrainzId = "cc2c9c3c-b7bc-4b8b-84d8-4fbd8779e493"
}
};
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Post<List<SpotifyMap>>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse<List<SpotifyMap>>(new HttpResponse(r, new HttpHeader(), map.ToJson())));
var result = Subject.MapSpotifyReleases(data);
result[0].ArtistMusicBrainzId.Should().Be("cc2c9c3c-b7bc-4b8b-84d8-4fbd8779e493");
}
[Test]
public void map_spotify_releases_should_drop_not_found()
{
var data = new List<SpotifyImportListItemInfo>
{
new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "21jF5jlMtzo94wbxmJ18aa",
Artist = "Adele"
}
};
var map = new List<SpotifyMap>
{
new SpotifyMap
{
SpotifyId = "21jF5jlMtzo94wbxmJ18aa",
MusicbrainzId = "0"
}
};
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Post<List<SpotifyMap>>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse<List<SpotifyMap>>(new HttpResponse(r, new HttpHeader(), map.ToJson())));
var result = Subject.MapSpotifyReleases(data);
result.Should().BeEmpty();
}
[Test]
public void map_spotify_releases_should_catch_exception_from_api()
{
var data = new List<SpotifyImportListItemInfo>
{
new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "21jF5jlMtzo94wbxmJ18aa",
Artist = "Adele"
}
};
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Post<List<SpotifyMap>>(It.IsAny<HttpRequest>()))
.Throws(new Exception("Dummy exception"));
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Get<AlbumResource>(It.IsAny<HttpRequest>()))
.Throws(new Exception("Dummy exception"));
var result = Subject.MapSpotifyReleases(data);
result.Should().NotBeNull();
ExceptionVerification.ExpectedErrors(2);
}
[Test]
public void map_spotify_releases_should_cope_with_duplicate_spotify_ids()
{
var data = new List<SpotifyImportListItemInfo>
{
new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "21jF5jlMtzo94wbxmJ18aa",
Artist = "Adele"
},
new SpotifyImportListItemInfo
{
Album = "30",
AlbumSpotifyId = "21jF5jlMtzo94wbxmJ18aa",
Artist = "Adele"
}
};
var map = new List<SpotifyMap>
{
new SpotifyMap
{
SpotifyId = "21jF5jlMtzo94wbxmJ18aa",
MusicbrainzId = "d8a99910-33c2-481d-9b96-085ece5545f6"
}
};
Mocker.GetMock<IHttpClient>()
.Setup(x => x.Post<List<SpotifyMap>>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse<List<SpotifyMap>>(new HttpResponse(r, new HttpHeader(), map.ToJson())));
var result = Subject.MapSpotifyReleases(data);
result.Should().HaveCount(2);
result[0].AlbumMusicBrainzId.Should().Be("d8a99910-33c2-481d-9b96-085ece5545f6");
result[1].AlbumMusicBrainzId.Should().Be("d8a99910-33c2-481d-9b96-085ece5545f6");
}
}
}