mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-23 15:30:34 -04:00
MergeUtils: Fixing dictionary merge for null values
This commit is contained in:
parent
886983fa5a
commit
33b74cac37
@ -79,7 +79,16 @@ namespace Kyoo
|
||||
if (second == null)
|
||||
return first;
|
||||
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;
|
||||
}
|
||||
|
||||
@ -123,7 +132,7 @@ namespace Kyoo
|
||||
hasChanged = false;
|
||||
if (second == null)
|
||||
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)
|
||||
second.TryAdd(key, value);
|
||||
return second;
|
||||
|
@ -157,7 +157,7 @@ namespace Kyoo.TheMovieDb
|
||||
|
||||
TMDbClient client = new(_apiKey.Value.ApiKey);
|
||||
return (await client.GetTvEpisodeAsync(id, episode.SeasonNumber.Value, episode.EpisodeNumber.Value))
|
||||
.ToEpisode(id, Provider);
|
||||
?.ToEpisode(id, Provider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -455,5 +455,85 @@ namespace Kyoo.Tests.Utility
|
||||
Merger.Merge(first, second);
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user