MergeUtils: Fixing dictionary merge for null values

This commit is contained in:
Zoe Roux 2021-08-06 20:36:24 +02:00
parent 886983fa5a
commit 33b74cac37
3 changed files with 92 additions and 3 deletions

View File

@ -79,7 +79,16 @@ namespace Kyoo
if (second == null) if (second == null)
return first; return first;
foreach ((T key, T2 value) in second) foreach ((T key, T2 value) in second)
hasChanged |= first.TryAdd(key, value); {
bool success = first.TryAdd(key, value);
hasChanged |= success;
if (success || first[key]?.Equals(default) == false || value?.Equals(default) != false)
continue;
first[key] = value;
hasChanged = true;
}
return first; return first;
} }
@ -123,7 +132,7 @@ namespace Kyoo
hasChanged = false; hasChanged = false;
if (second == null) if (second == null)
return first; return first;
hasChanged = second.Any(x => !x.Value.Equals(first[x.Key])); hasChanged = second.Any(x => x.Value?.Equals(first[x.Key]) == false);
foreach ((T key, T2 value) in first) foreach ((T key, T2 value) in first)
second.TryAdd(key, value); second.TryAdd(key, value);
return second; return second;

View File

@ -157,7 +157,7 @@ namespace Kyoo.TheMovieDb
TMDbClient client = new(_apiKey.Value.ApiKey); TMDbClient client = new(_apiKey.Value.ApiKey);
return (await client.GetTvEpisodeAsync(id, episode.SeasonNumber.Value, episode.EpisodeNumber.Value)) return (await client.GetTvEpisodeAsync(id, episode.SeasonNumber.Value, episode.EpisodeNumber.Value))
.ToEpisode(id, Provider); ?.ToEpisode(id, Provider);
} }
/// <summary> /// <summary>

View File

@ -455,5 +455,85 @@ namespace Kyoo.Tests.Utility
Merger.Merge(first, second); Merger.Merge(first, second);
// This should no call the setter of first so the test should pass. // This should no call the setter of first so the test should pass.
} }
[Fact]
public void MergeDictionaryNullValue()
{
Dictionary<int, string> first = new()
{
[Images.Logo] = "logo",
[Images.Poster] = null
};
Dictionary<int, string> second = new()
{
[Images.Poster] = "new-poster",
[Images.Thumbnail] = "thumbnails"
};
IDictionary<int, string> ret = Merger.MergeDictionaries(first, second, out bool changed);
Assert.True(changed);
Assert.Equal(3, ret.Count);
Assert.Equal("new-poster", ret[Images.Poster]);
Assert.Equal("thumbnails", ret[Images.Thumbnail]);
Assert.Equal("logo", ret[Images.Logo]);
}
[Fact]
public void MergeDictionaryNullValueNoChange()
{
Dictionary<int, string> first = new()
{
[Images.Logo] = "logo",
[Images.Poster] = null
};
Dictionary<int, string> second = new()
{
[Images.Poster] = null,
};
IDictionary<int, string> ret = Merger.MergeDictionaries(first, second, out bool changed);
Assert.False(changed);
Assert.Equal(2, ret.Count);
Assert.Null(ret[Images.Poster]);
Assert.Equal("logo", ret[Images.Logo]);
}
[Fact]
public void CompleteDictionaryNullValue()
{
Dictionary<int, string> first = new()
{
[Images.Logo] = "logo",
[Images.Poster] = null
};
Dictionary<int, string> second = new()
{
[Images.Poster] = "new-poster",
[Images.Thumbnail] = "thumbnails"
};
IDictionary<int, string> ret = Merger.CompleteDictionaries(first, second, out bool changed);
Assert.True(changed);
Assert.Equal(3, ret.Count);
Assert.Equal("new-poster", ret[Images.Poster]);
Assert.Equal("thumbnails", ret[Images.Thumbnail]);
Assert.Equal("logo", ret[Images.Logo]);
}
[Fact]
public void CompleteDictionaryNullValueNoChange()
{
Dictionary<int, string> first = new()
{
[Images.Logo] = "logo",
[Images.Poster] = null
};
Dictionary<int, string> second = new()
{
[Images.Poster] = null,
};
IDictionary<int, string> ret = Merger.CompleteDictionaries(first, second, out bool changed);
Assert.False(changed);
Assert.Equal(2, ret.Count);
Assert.Null(ret[Images.Poster]);
Assert.Equal("logo", ret[Images.Logo]);
}
} }
} }