Add tests for ProviderIdsExtensions

pull/5270/head
Bond_009 4 years ago
parent b2700ecf44
commit 941d3f6217

@ -9,28 +9,6 @@ namespace MediaBrowser.Model.Entities
/// </summary>
public static class ProviderIdsExtensions
{
/// <summary>
/// Determines whether [has provider identifier] [the specified instance].
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <returns><c>true</c> if [has provider identifier] [the specified instance]; otherwise, <c>false</c>.</returns>
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString()));
}
/// <summary>
/// Gets a provider id.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <returns>System.String.</returns>
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return instance.GetProviderId(provider.ToString());
}
/// <summary>
/// Gets a provider id.
/// </summary>
@ -47,7 +25,7 @@ namespace MediaBrowser.Model.Entities
if (instance.ProviderIds == null)
{
id = string.Empty;
id = null;
return false;
}
@ -78,6 +56,17 @@ namespace MediaBrowser.Model.Entities
return id;
}
/// <summary>
/// Gets a provider id.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <returns>System.String.</returns>
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return instance.GetProviderId(provider.ToString());
}
/// <summary>
/// Sets a provider id.
/// </summary>
@ -94,13 +83,7 @@ namespace MediaBrowser.Model.Entities
// If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value))
{
if (instance.ProviderIds != null)
{
if (instance.ProviderIds.ContainsKey(name))
{
instance.ProviderIds.Remove(name);
}
}
instance.ProviderIds?.Remove(name);
}
else
{

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using MediaBrowser.Model.Entities;
using Xunit;
namespace Jellyfin.Model.Tests.Entities
{
public class ProviderIdsExtensionsTests
{
private const string ExampleImdbId = "tt0113375";
[Fact]
public void GetProviderId_NullInstance_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ProviderIdsExtensions.GetProviderId(null!, MetadataProvider.Imdb));
}
[Fact]
public void GetProviderId_NullName_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ProviderIdsExtensionsTestsObject.Empty.GetProviderId(null!));
}
[Fact]
public void GetProviderId_NotFoundName_Null()
{
Assert.Null(ProviderIdsExtensionsTestsObject.Empty.GetProviderId(MetadataProvider.Imdb));
}
[Fact]
public void GetProviderId_NullProvider_Null()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
Assert.Null(nullProvider.GetProviderId(MetadataProvider.Imdb));
}
[Fact]
public void TryGetProviderId_NotFoundName_False()
{
Assert.False(ProviderIdsExtensionsTestsObject.Empty.TryGetProviderId(MetadataProvider.Imdb, out _));
}
[Fact]
public void TryGetProviderId_NullProvider_False()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
Assert.False(nullProvider.TryGetProviderId(MetadataProvider.Imdb, out _));
}
[Fact]
public void GetProviderId_FoundName_Id()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
Assert.Equal(ExampleImdbId, provider.GetProviderId(MetadataProvider.Imdb));
}
[Fact]
public void TryGetProviderId_FoundName_True()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
Assert.True(provider.TryGetProviderId(MetadataProvider.Imdb, out var id));
Assert.Equal(ExampleImdbId, id);
}
[Fact]
public void SetProviderId_NullInstance_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ProviderIdsExtensions.SetProviderId(null!, MetadataProvider.Imdb, ExampleImdbId));
}
[Fact]
public void SetProviderId_Null_Remove()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.SetProviderId(MetadataProvider.Imdb, null!);
Assert.Empty(provider.ProviderIds);
}
[Fact]
public void SetProviderId_EmptyName_Remove()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
provider.SetProviderId(MetadataProvider.Imdb, string.Empty);
Assert.Empty(provider.ProviderIds);
}
[Fact]
public void SetProviderId_NonEmptyId_Success()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.SetProviderId(MetadataProvider.Imdb, ExampleImdbId);
Assert.Single(provider.ProviderIds);
}
[Fact]
public void SetProviderId_NullProvider_Success()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
nullProvider.SetProviderId(MetadataProvider.Imdb, ExampleImdbId);
Assert.Single(nullProvider.ProviderIds);
}
private class ProviderIdsExtensionsTestsObject : IHasProviderIds
{
public static readonly ProviderIdsExtensionsTestsObject Empty = new ProviderIdsExtensionsTestsObject();
public Dictionary<string, string> ProviderIds { get; set; } = new Dictionary<string, string>();
}
}
}
Loading…
Cancel
Save