Merge pull request #7078 from 1337joe/metadata-merge-data
commit
ef0708d876
@ -1,295 +0,0 @@
|
||||
#nullable disable
|
||||
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Diacritics.Extensions;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Audio;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace MediaBrowser.Providers.Manager
|
||||
{
|
||||
public static class ProviderUtils
|
||||
{
|
||||
public static void MergeBaseItemData<T>(
|
||||
MetadataResult<T> sourceResult,
|
||||
MetadataResult<T> targetResult,
|
||||
MetadataField[] lockedFields,
|
||||
bool replaceData,
|
||||
bool mergeMetadataSettings)
|
||||
where T : BaseItem
|
||||
{
|
||||
var source = sourceResult.Item;
|
||||
var target = targetResult.Item;
|
||||
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentException("Item cannot be null.", nameof(sourceResult));
|
||||
}
|
||||
|
||||
if (target == null)
|
||||
{
|
||||
throw new ArgumentException("Item cannot be null.", nameof(targetResult));
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Name))
|
||||
{
|
||||
if (replaceData || string.IsNullOrEmpty(target.Name))
|
||||
{
|
||||
// Safeguard against incoming data having an empty name
|
||||
if (!string.IsNullOrWhiteSpace(source.Name))
|
||||
{
|
||||
target.Name = source.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceData || string.IsNullOrEmpty(target.OriginalTitle))
|
||||
{
|
||||
// Safeguard against incoming data having an empty name
|
||||
if (!string.IsNullOrWhiteSpace(source.OriginalTitle))
|
||||
{
|
||||
target.OriginalTitle = source.OriginalTitle;
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceData || !target.CommunityRating.HasValue)
|
||||
{
|
||||
target.CommunityRating = source.CommunityRating;
|
||||
}
|
||||
|
||||
if (replaceData || !target.EndDate.HasValue)
|
||||
{
|
||||
target.EndDate = source.EndDate;
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Genres))
|
||||
{
|
||||
if (replaceData || target.Genres.Length == 0)
|
||||
{
|
||||
target.Genres = source.Genres;
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceData || !target.IndexNumber.HasValue)
|
||||
{
|
||||
target.IndexNumber = source.IndexNumber;
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.OfficialRating))
|
||||
{
|
||||
if (replaceData || string.IsNullOrEmpty(target.OfficialRating))
|
||||
{
|
||||
target.OfficialRating = source.OfficialRating;
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceData || string.IsNullOrEmpty(target.CustomRating))
|
||||
{
|
||||
target.CustomRating = source.CustomRating;
|
||||
}
|
||||
|
||||
if (replaceData || string.IsNullOrEmpty(target.Tagline))
|
||||
{
|
||||
target.Tagline = source.Tagline;
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Overview))
|
||||
{
|
||||
if (replaceData || string.IsNullOrEmpty(target.Overview))
|
||||
{
|
||||
target.Overview = source.Overview;
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceData || !target.ParentIndexNumber.HasValue)
|
||||
{
|
||||
target.ParentIndexNumber = source.ParentIndexNumber;
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Cast))
|
||||
{
|
||||
if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
|
||||
{
|
||||
targetResult.People = sourceResult.People;
|
||||
}
|
||||
else if (targetResult.People != null && sourceResult.People != null)
|
||||
{
|
||||
MergePeople(sourceResult.People, targetResult.People);
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceData || !target.PremiereDate.HasValue)
|
||||
{
|
||||
target.PremiereDate = source.PremiereDate;
|
||||
}
|
||||
|
||||
if (replaceData || !target.ProductionYear.HasValue)
|
||||
{
|
||||
target.ProductionYear = source.ProductionYear;
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Runtime))
|
||||
{
|
||||
if (replaceData || !target.RunTimeTicks.HasValue)
|
||||
{
|
||||
if (target is not Audio && target is not Video)
|
||||
{
|
||||
target.RunTimeTicks = source.RunTimeTicks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Studios))
|
||||
{
|
||||
if (replaceData || target.Studios.Length == 0)
|
||||
{
|
||||
target.Studios = source.Studios;
|
||||
}
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.Tags))
|
||||
{
|
||||
if (replaceData || target.Tags.Length == 0)
|
||||
{
|
||||
target.Tags = source.Tags;
|
||||
}
|
||||
}
|
||||
|
||||
if (!lockedFields.Contains(MetadataField.ProductionLocations))
|
||||
{
|
||||
if (replaceData || target.ProductionLocations.Length == 0)
|
||||
{
|
||||
target.ProductionLocations = source.ProductionLocations;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var id in source.ProviderIds)
|
||||
{
|
||||
var key = id.Key;
|
||||
|
||||
// Don't replace existing Id's.
|
||||
if (replaceData || !target.ProviderIds.ContainsKey(key))
|
||||
{
|
||||
target.ProviderIds[key] = id.Value;
|
||||
}
|
||||
}
|
||||
|
||||
MergeAlbumArtist(source, target, replaceData);
|
||||
MergeCriticRating(source, target, replaceData);
|
||||
MergeTrailers(source, target, replaceData);
|
||||
MergeVideoInfo(source, target, replaceData);
|
||||
MergeDisplayOrder(source, target, replaceData);
|
||||
|
||||
if (replaceData || string.IsNullOrEmpty(target.ForcedSortName))
|
||||
{
|
||||
var forcedSortName = source.ForcedSortName;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(forcedSortName))
|
||||
{
|
||||
target.ForcedSortName = forcedSortName;
|
||||
}
|
||||
}
|
||||
|
||||
if (mergeMetadataSettings)
|
||||
{
|
||||
target.LockedFields = source.LockedFields;
|
||||
target.IsLocked = source.IsLocked;
|
||||
|
||||
// Grab the value if it's there, but if not then don't overwrite the default
|
||||
if (source.DateCreated != default)
|
||||
{
|
||||
target.DateCreated = source.DateCreated;
|
||||
}
|
||||
|
||||
target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
|
||||
target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
|
||||
}
|
||||
}
|
||||
|
||||
private static void MergePeople(List<PersonInfo> source, List<PersonInfo> target)
|
||||
{
|
||||
foreach (var person in target)
|
||||
{
|
||||
var normalizedName = person.Name.RemoveDiacritics();
|
||||
var personInSource = source.FirstOrDefault(i => string.Equals(i.Name.RemoveDiacritics(), normalizedName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (personInSource != null)
|
||||
{
|
||||
foreach (var providerId in personInSource.ProviderIds)
|
||||
{
|
||||
if (!person.ProviderIds.ContainsKey(providerId.Key))
|
||||
{
|
||||
person.ProviderIds[providerId.Key] = providerId.Value;
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(person.ImageUrl))
|
||||
{
|
||||
person.ImageUrl = personInSource.ImageUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData)
|
||||
{
|
||||
if (source is IHasDisplayOrder sourceHasDisplayOrder
|
||||
&& target is IHasDisplayOrder targetHasDisplayOrder)
|
||||
{
|
||||
if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder))
|
||||
{
|
||||
var displayOrder = sourceHasDisplayOrder.DisplayOrder;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(displayOrder))
|
||||
{
|
||||
targetHasDisplayOrder.DisplayOrder = displayOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData)
|
||||
{
|
||||
if (source is IHasAlbumArtist sourceHasAlbumArtist
|
||||
&& target is IHasAlbumArtist targetHasAlbumArtist)
|
||||
{
|
||||
if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
|
||||
{
|
||||
targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void MergeCriticRating(BaseItem source, BaseItem target, bool replaceData)
|
||||
{
|
||||
if (replaceData || !target.CriticRating.HasValue)
|
||||
{
|
||||
target.CriticRating = source.CriticRating;
|
||||
}
|
||||
}
|
||||
|
||||
private static void MergeTrailers(BaseItem source, BaseItem target, bool replaceData)
|
||||
{
|
||||
if (replaceData || target.RemoteTrailers.Count == 0)
|
||||
{
|
||||
target.RemoteTrailers = source.RemoteTrailers;
|
||||
}
|
||||
}
|
||||
|
||||
private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData)
|
||||
{
|
||||
if (source is Video sourceCast && target is Video targetCast)
|
||||
{
|
||||
if (replaceData || targetCast.Video3DFormat == null)
|
||||
{
|
||||
targetCast.Video3DFormat = sourceCast.Video3DFormat;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,378 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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.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, false)]
|
||||
[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 != 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 != 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 _));
|
||||
}
|
||||
|
||||
private 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" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
object? result;
|
||||
List<PersonInfo> actual;
|
||||
|
||||
// overwrite provider id
|
||||
var overwriteNewValue = new List<PersonInfo>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "Name 2"
|
||||
}
|
||||
};
|
||||
Assert.False(TestMergeBaseItemDataPerson(GetOldValue(), overwriteNewValue, null, false, out result));
|
||||
// People not already in target are not merged into it from source
|
||||
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 == null ? Array.Empty<MetadataField>() : new[] { (MetadataField)lockField };
|
||||
MetadataService<Movie, MovieInfo>.MergeBaseItemData(source, target, lockedFields, replaceData, false);
|
||||
|
||||
actualValue = target.People;
|
||||
return newValue?.Equals(actualValue) ?? actualValue == 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 == 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 == null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue