Skip to content
Permalink
Browse files
Merge pull request #5954 from Bond-009/tests5
Add tests for SqliteItemRepository.(De)SerializeProviderIds
  • Loading branch information
Bond-009 committed May 2, 2021
2 parents 7eb9f57 + 874f92e commit f6d967ded67e60030ad591750c2e08654cfa69e7
@@ -897,7 +897,7 @@ private void SaveItem(BaseItem item, BaseItem topParent, string userDataKey, ISt
saveItemStatement.TryBind("@ExternalSeriesId", item.ExternalSeriesId);
saveItemStatement.TryBind("@Tagline", item.Tagline);

saveItemStatement.TryBind("@ProviderIds", SerializeProviderIds(item));
saveItemStatement.TryBind("@ProviderIds", SerializeProviderIds(item.ProviderIds));
saveItemStatement.TryBind("@Images", SerializeImages(item.ImageInfos));

if (item.ProductionLocations.Length > 0)
@@ -968,10 +968,10 @@ private void SaveItem(BaseItem item, BaseItem topParent, string userDataKey, ISt
saveItemStatement.MoveNext();
}

private static string SerializeProviderIds(BaseItem item)
internal static string SerializeProviderIds(Dictionary<string, string> providerIds)
{
StringBuilder str = new StringBuilder();
foreach (var i in item.ProviderIds)
foreach (var i in providerIds)
{
// Ideally we shouldn't need this IsNullOrWhiteSpace check,
// but we're seeing some cases of bad data slip through
@@ -995,18 +995,13 @@ private static string SerializeProviderIds(BaseItem item)
return str.ToString();
}

private static void DeserializeProviderIds(string value, BaseItem item)
internal static void DeserializeProviderIds(string value, IHasProviderIds item)
{
if (string.IsNullOrWhiteSpace(value))
{
return;
}

if (item.ProviderIds.Count > 0)
{
return;
}

var parts = value.Split('|', StringSplitOptions.RemoveEmptyEntries);

foreach (var part in parts)
@@ -1787,16 +1782,16 @@ IEnumerable<TrailerType> GetTrailerTypes(string s)
index++;
}

if (!reader.IsDBNull(index))
if (item.ProviderIds.Count == 0 && !reader.IsDBNull(index))
{
DeserializeProviderIds(reader.GetString(index), item);
}

index++;

if (query.DtoOptions.EnableImages && item.ImageInfos.Length == 0)
if (query.DtoOptions.EnableImages)
{
if (!reader.IsDBNull(index))
if (item.ImageInfos.Length == 0 && !reader.IsDBNull(index))
{
item.ImageInfos = DeserializeImages(reader.GetString(index));
}
@@ -168,5 +168,62 @@ public void SerializeImages_Valid_Success(string expected, ItemImageInfo[] value
{
Assert.Equal(expected, _sqliteItemRepository.SerializeImages(value));
}

public static IEnumerable<object[]> DeserializeProviderIds_Valid_TestData()
{
yield return new object[]
{
"Imdb=tt0119567",
new Dictionary<string, string>()
{
{ "Imdb", "tt0119567" },
}
};

yield return new object[]
{
"Imdb=tt0119567|Tmdb=330|TmdbCollection=328",
new Dictionary<string, string>()
{
{ "Imdb", "tt0119567" },
{ "Tmdb", "330" },
{ "TmdbCollection", "328" },
}
};

yield return new object[]
{
"MusicBrainzAlbum=9d363e43-f24f-4b39-bc5a-7ef305c677c7|MusicBrainzReleaseGroup=63eba062-847c-3b73-8b0f-6baf27bba6fa|AudioDbArtist=111352|AudioDbAlbum=2116560|MusicBrainzAlbumArtist=20244d07-534f-4eff-b4d4-930878889970",
new Dictionary<string, string>()
{
{ "MusicBrainzAlbum", "9d363e43-f24f-4b39-bc5a-7ef305c677c7" },
{ "MusicBrainzReleaseGroup", "63eba062-847c-3b73-8b0f-6baf27bba6fa" },
{ "AudioDbArtist", "111352" },
{ "AudioDbAlbum", "2116560" },
{ "MusicBrainzAlbumArtist", "20244d07-534f-4eff-b4d4-930878889970" },
}
};
}

[Theory]
[MemberData(nameof(DeserializeProviderIds_Valid_TestData))]
public void DeserializeProviderIds_Valid_Success(string value, Dictionary<string, string> expected)
{
var result = new ProviderIdsExtensionsTestsObject();
SqliteItemRepository.DeserializeProviderIds(value, result);
Assert.Equal(expected, result.ProviderIds);
}

[Theory]
[MemberData(nameof(DeserializeProviderIds_Valid_TestData))]
public void SerializeProviderIds_Valid_Success(string expected, Dictionary<string, string> values)
{
Assert.Equal(expected, SqliteItemRepository.SerializeProviderIds(values));
}

private class ProviderIdsExtensionsTestsObject : IHasProviderIds
{
public Dictionary<string, string> ProviderIds { get; set; } = new Dictionary<string, string>();
}
}
}

0 comments on commit f6d967d

Please sign in to comment.