Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for SqliteItemRepository.(De)SerializeProviderIds #5954

Merged
merged 1 commit into from May 2, 2021
Merged
Changes from all commits
Commits
File filter
Filter file types
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -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>();
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.