aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Providers.Tests/Manager/MetadataServiceTests.cs
blob: b32ecf6ec4c7719500fe6756ff58e6ec3e62869c (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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Providers.Manager;
using Xunit;

namespace Jellyfin.Providers.Tests.Manager
{
    public class MetadataServiceTests
    {
        [Theory]
        [InlineData(false, false)]
        [InlineData(true, false)]
        [InlineData(true, true)]
        public void MergeBaseItemData_MergeMetadataSettings_MergesWhenSet(bool mergeMetadataSettings, bool defaultDate)
        {
            var newLocked = new[] { MetadataField.Genres, MetadataField.Cast };
            var newString = "new";
            var newDate = DateTime.Now;

            var oldLocked = new[] { MetadataField.Genres };
            var oldString = "old";
            var oldDate = DateTime.UnixEpoch;

            var source = new MetadataResult<Movie>
            {
                Item = new Movie
                {
                    LockedFields = newLocked,
                    IsLocked = true,
                    PreferredMetadataCountryCode = newString,
                    PreferredMetadataLanguage = newString,
                    DateCreated = newDate
                }
            };
            if (defaultDate)
            {
                source.Item.DateCreated = default;
            }

            var target = new MetadataResult<Movie>
            {
                Item = new Movie
                {
                    LockedFields = oldLocked,
                    IsLocked = false,
                    PreferredMetadataCountryCode = oldString,
                    PreferredMetadataLanguage = oldString,
                    DateCreated = oldDate
                }
            };

            MetadataService<Movie, MovieInfo>.MergeBaseItemData(source, target, Array.Empty<MetadataField>(), true, mergeMetadataSettings);

            if (mergeMetadataSettings)
            {
                Assert.Equal(newLocked, target.Item.LockedFields);
                Assert.True(target.Item.IsLocked);
                Assert.Equal(newString, target.Item.PreferredMetadataCountryCode);
                Assert.Equal(newString, target.Item.PreferredMetadataLanguage);
                Assert.Equal(defaultDate ? oldDate : newDate, target.Item.DateCreated);
            }
            else
            {
                Assert.Equal(oldLocked, target.Item.LockedFields);
                Assert.False(target.Item.IsLocked);
                Assert.Equal(oldString, target.Item.PreferredMetadataCountryCode);
                Assert.Equal(oldString, target.Item.PreferredMetadataLanguage);
                Assert.Equal(oldDate, target.Item.DateCreated);
            }
        }

        [Theory]
        [InlineData("Name", MetadataField.Name, false)]
        [InlineData("OriginalTitle", null)]
        [InlineData("OfficialRating", MetadataField.OfficialRating)]
        [InlineData("CustomRating")]
        [InlineData("Tagline")]
        [InlineData("Overview", MetadataField.Overview)]
        [InlineData("DisplayOrder", null, false)]
        [InlineData("ForcedSortName", null, false)]
        public void MergeBaseItemData_StringField_ReplacesAppropriately(string propName, MetadataField? lockField = null, bool replacesWithEmpty = true)
        {
            var oldValue = "Old";
            var newValue = "New";

            // Use type Series to hit DisplayOrder
            Assert.False(TestMergeBaseItemData<Series, SeriesInfo>(propName, oldValue, newValue, null, false, out _));
            if (lockField is not null)
            {
                Assert.False(TestMergeBaseItemData<Series, SeriesInfo>(propName, oldValue, newValue, lockField, true, out _));
                Assert.False(TestMergeBaseItemData<Series, SeriesInfo>(propName, null, newValue, lockField, false, out _));
                Assert.False(TestMergeBaseItemData<Series, SeriesInfo>(propName, string.Empty, newValue, lockField, false, out _));
            }

            Assert.True(TestMergeBaseItemData<Series, SeriesInfo>(propName, oldValue, newValue, null, true, out _));
            Assert.True(TestMergeBaseItemData<Series, SeriesInfo>(propName, null, newValue, null, false, out _));
            Assert.True(TestMergeBaseItemData<Series, SeriesInfo>(propName, string.Empty, newValue, null, false, out _));

            var replacedWithEmpty = TestMergeBaseItemData<Series, SeriesInfo>(propName, oldValue, string.Empty, null, true, out _);
            Assert.Equal(replacesWithEmpty, replacedWithEmpty);
        }

        [Theory]
        [InlineData("Genres", MetadataField.Genres)]
        [InlineData("Studios", MetadataField.Studios)]
        [InlineData("Tags", MetadataField.Tags)]
        [InlineData("ProductionLocations", MetadataField.ProductionLocations)]
        [InlineData("AlbumArtists")]
        public void MergeBaseItemData_StringArrayField_ReplacesAppropriately(string propName, MetadataField? lockField = null)
        {
            // Note that arrays are replaced, not merged
            var oldValue = new[] { "Old" };
            var newValue = new[] { "New" };

            // Use type Audio to hit AlbumArtists
            Assert.False(TestMergeBaseItemData<Audio, SongInfo>(propName, oldValue, newValue, null, false, out _));
            if (lockField is not null)
            {
                Assert.False(TestMergeBaseItemData<Audio, SongInfo>(propName, oldValue, newValue, lockField, true, out _));
                Assert.False(TestMergeBaseItemData<Audio, SongInfo>(propName, Array.Empty<string>(), newValue, lockField, false, out _));
            }

            Assert.True(TestMergeBaseItemData<Audio, SongInfo>(propName, oldValue, newValue, null, true, out _));
            Assert.True(TestMergeBaseItemData<Audio, SongInfo>(propName, Array.Empty<string>(), newValue, null, false, out _));

            Assert.True(TestMergeBaseItemData<Audio, SongInfo>(propName, oldValue, Array.Empty<string>(), null, true, out _));
        }

        public static TheoryData<string, object, object> MergeBaseItemData_SimpleField_ReplacesAppropriately_TestData()
            => new()
            {
                { "IndexNumber", 1, 2 },
                { "ParentIndexNumber", 1, 2 },
                { "ProductionYear", 1, 2 },
                { "CommunityRating", 1.0f, 2.0f },
                { "CriticRating", 1.0f, 2.0f },
                { "EndDate", DateTime.UnixEpoch, DateTime.Now },
                { "PremiereDate", DateTime.UnixEpoch, DateTime.Now },
                { "Video3DFormat", Video3DFormat.HalfSideBySide, Video3DFormat.FullSideBySide }
            };

        [Theory]
        [MemberData(nameof(MergeBaseItemData_SimpleField_ReplacesAppropriately_TestData))]
        public void MergeBaseItemData_SimpleField_ReplacesAppropriately(string propName, object oldValue, object newValue)
        {
            // Use type Movie to allow testing of Video3DFormat
            Assert.False(TestMergeBaseItemData<Movie, MovieInfo>(propName, oldValue, newValue, null, false, out _));

            Assert.True(TestMergeBaseItemData<Movie, MovieInfo>(propName, oldValue, newValue, null, true, out _));
            Assert.True(TestMergeBaseItemData<Movie, MovieInfo>(propName, null, newValue, null, false, out _));

            Assert.True(TestMergeBaseItemData<Movie, MovieInfo>(propName, oldValue, null, null, true, out _));
        }

        [Fact]
        public void MergeBaseItemData_MergeTrailers_ReplacesAppropriately()
        {
            string propName = "RemoteTrailers";
            var oldValue = new[]
            {
                new MediaUrl
                {
                    Name = "Name 1",
                    Url = "URL 1"
                }
            };
            var newValue = new[]
            {
                new MediaUrl
                {
                    Name = "Name 2",
                    Url = "URL 2"
                }
            };

            Assert.False(TestMergeBaseItemData<Movie, MovieInfo>(propName, oldValue, newValue, null, false, out _));

            Assert.True(TestMergeBaseItemData<Movie, MovieInfo>(propName, oldValue, newValue, null, true, out _));
            Assert.True(TestMergeBaseItemData<Movie, MovieInfo>(propName, Array.Empty<MediaUrl>(), newValue, null, false, out _));

            Assert.True(TestMergeBaseItemData<Movie, MovieInfo>(propName, oldValue, Array.Empty<MediaUrl>(), null, true, out _));
        }

        [Fact]
        public void MergeBaseItemData_ProviderIds_MergesAppropriately()
        {
            var propName = "ProviderIds";
            var oldValue = new Dictionary<string, string>
            {
                { "provider 1", "id 1" }
            };

            // overwrite provider id
            var overwriteNewValue = new Dictionary<string, string>
            {
                { "provider 1", "id 2" }
            };
            Assert.False(TestMergeBaseItemData<Movie, MovieInfo>(propName, new Dictionary<string, string>(oldValue), overwriteNewValue, null, false, out _));
            TestMergeBaseItemData<Movie, MovieInfo>(propName, new Dictionary<string, string>(oldValue), overwriteNewValue, null, true, out var overwritten);
            Assert.Equal(overwriteNewValue, overwritten);

            // merge without overwriting
            var mergeNewValue = new Dictionary<string, string>
            {
                { "provider 1", "id 2" },
                { "provider 2", "id 3" }
            };
            TestMergeBaseItemData<Movie, MovieInfo>(propName, new Dictionary<string, string>(oldValue), mergeNewValue, null, false, out var merged);
            var actual = (Dictionary<string, string>)merged!;
            Assert.Equal("id 1", actual["provider 1"]);
            Assert.Equal("id 3", actual["provider 2"]);

            // empty source results in no change
            TestMergeBaseItemData<Movie, MovieInfo>(propName, new Dictionary<string, string>(oldValue), new Dictionary<string, string>(), null, true, out var notOverwritten);
            Assert.Equal(oldValue, notOverwritten);
        }

        [Fact]
        public void MergeBaseItemData_MergePeople_MergesAppropriately()
        {
            // PersonInfo in list is changed by merge, create new for every call
            List<PersonInfo> GetOldValue()
                => new()
                {
                    new PersonInfo
                    {
                        Name = "Name 1",
                        ProviderIds = new Dictionary<string, string>
                        {
                            { "Provider 1", "1234" }
                        }
                    }
                };

            // overwrite provider id
            var overwriteNewValue = new List<PersonInfo>
            {
                new()
                {
                    Name = "Name 2"
                }
            };
            Assert.False(TestMergeBaseItemDataPerson(GetOldValue(), overwriteNewValue, null, false, out var result));
            // People not already in target are not merged into it from source
            List<PersonInfo> actual = (List<PersonInfo>)result!;
            Assert.Single(actual);
            Assert.Equal("Name 1", actual[0].Name);

            Assert.True(TestMergeBaseItemDataPerson(GetOldValue(), overwriteNewValue, null, true, out _));
            Assert.True(TestMergeBaseItemDataPerson(new List<PersonInfo>(), overwriteNewValue, null, false, out _));
            Assert.True(TestMergeBaseItemDataPerson(null, overwriteNewValue, null, false, out _));

            Assert.False(TestMergeBaseItemDataPerson(GetOldValue(), overwriteNewValue, MetadataField.Cast, true, out _));

            // providers merge but don't overwrite existing keys
            var mergeNewValue = new List<PersonInfo>
            {
                new()
                {
                    Name = "Name 1",
                    ProviderIds = new Dictionary<string, string>
                    {
                        { "Provider 1", "5678" },
                        { "Provider 2", "5678" }
                    }
                }
            };
            TestMergeBaseItemDataPerson(GetOldValue(), mergeNewValue, null, false, out result);
            actual = (List<PersonInfo>)result!;
            Assert.Single(actual);
            Assert.Equal("Name 1", actual[0].Name);
            Assert.Equal(2, actual[0].ProviderIds.Count);
            Assert.Equal("1234", actual[0].ProviderIds["Provider 1"]);
            Assert.Equal("5678", actual[0].ProviderIds["Provider 2"]);

            // picture adds if missing but won't overwrite (forcing overwrites entire list, not entries in merged PersonInfo)
            var mergePicture1 = new List<PersonInfo>
            {
                new()
                {
                    Name = "Name 1",
                    ImageUrl = "URL 1"
                }
            };
            TestMergeBaseItemDataPerson(GetOldValue(), mergePicture1, null, false, out result);
            actual = (List<PersonInfo>)result!;
            Assert.Single(actual);
            Assert.Equal("Name 1", actual[0].Name);
            Assert.Equal("URL 1", actual[0].ImageUrl);
            var mergePicture2 = new List<PersonInfo>
            {
                new()
                {
                    Name = "Name 1",
                    ImageUrl = "URL 2"
                }
            };
            TestMergeBaseItemDataPerson(mergePicture1, mergePicture2, null, false, out result);
            actual = (List<PersonInfo>)result!;
            Assert.Single(actual);
            Assert.Equal("Name 1", actual[0].Name);
            Assert.Equal("URL 1", actual[0].ImageUrl);

            // empty source can be forced to overwrite a target with data
            Assert.True(TestMergeBaseItemDataPerson(GetOldValue(), new List<PersonInfo>(), null, true, out _));
        }

        private static bool TestMergeBaseItemDataPerson(List<PersonInfo>? oldValue, List<PersonInfo>? newValue, MetadataField? lockField, bool replaceData, out object? actualValue)
        {
            var source = new MetadataResult<Movie>
            {
                Item = new Movie(),
                People = newValue
            };

            var target = new MetadataResult<Movie>
            {
                Item = new Movie(),
                People = oldValue
            };

            var lockedFields = lockField is null ? Array.Empty<MetadataField>() : new[] { (MetadataField)lockField };
            MetadataService<Movie, MovieInfo>.MergeBaseItemData(source, target, lockedFields, replaceData, false);

            actualValue = target.People;
            return newValue?.SequenceEqual((IEnumerable<PersonInfo>)actualValue!) ?? actualValue is null;
        }

        /// <summary>
        /// Makes a call to <see cref="MetadataService{TItemType,TIdType}.MergeBaseItemData"/> with the provided parameters and returns whether the target changed or not.
        ///
        /// Reflection is used to allow testing of all fields using the same logic, rather than relying on copy/pasting test code for each field.
        /// </summary>
        /// <param name="propName">The property to test.</param>
        /// <param name="oldValue">The initial value in the target object.</param>
        /// <param name="newValue">The initial value in the source object.</param>
        /// <param name="lockField">The metadata field that locks this property if the field should be locked, or <c>null</c> to leave unlocked.</param>
        /// <param name="replaceData">Passed through to <see cref="MetadataService{TItemType,TIdType}.MergeBaseItemData"/>.</param>
        /// <param name="actualValue">The resulting value set to the target.</param>
        /// <typeparam name="TItemType">The <see cref="BaseItem"/> type to test on.</typeparam>
        /// <typeparam name="TIdType">The <see cref="BaseItem"/> info type.</typeparam>
        /// <returns><c>true</c> if the property on the target updates to match the source value when<see cref="MetadataService{TItemType,TIdType}.MergeBaseItemData"/> is called.</returns>
        private static bool TestMergeBaseItemData<TItemType, TIdType>(string propName, object? oldValue, object? newValue, MetadataField? lockField, bool replaceData, out object? actualValue)
            where TItemType : BaseItem, IHasLookupInfo<TIdType>, new()
            where TIdType : ItemLookupInfo, new()
        {
            var property = typeof(TItemType).GetProperty(propName)!;

            var source = new MetadataResult<TItemType>
            {
                Item = new TItemType()
            };
            property.SetValue(source.Item, newValue);

            var target = new MetadataResult<TItemType>
            {
                Item = new TItemType()
            };
            property.SetValue(target.Item, oldValue);

            var lockedFields = lockField is null ? Array.Empty<MetadataField>() : new[] { (MetadataField)lockField };
            // generic type doesn't actually matter to call the static method, just has to be filled in
            MetadataService<TItemType, TIdType>.MergeBaseItemData(source, target, lockedFields, replaceData, false);

            actualValue = property.GetValue(target.Item);
            return newValue?.Equals(actualValue) ?? actualValue is null;
        }
    }
}