mirror of
https://github.com/jellyfin/jellyfin-plugin-bookshelf.git
synced 2024-11-26 23:20:27 +00:00
Add test for the GoogleBooksImageProvider
This commit is contained in:
parent
67a4e59a29
commit
5f779bc781
@ -0,0 +1,18 @@
|
||||
namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
{
|
||||
/// <summary>
|
||||
/// Constants for the Google Books provider.
|
||||
/// </summary>
|
||||
public static class GoogleBooksConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// Name of the Google Books provider.
|
||||
/// </summary>
|
||||
public const string ProviderName = "Google Books";
|
||||
|
||||
/// <summary>
|
||||
/// Id of the Google Books provider.
|
||||
/// </summary>
|
||||
public const string ProviderId = "GoogleBooks";
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
@ -31,7 +31,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => "Google Books";
|
||||
public string Name => GoogleBooksConstants.ProviderName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Supports(BaseItem item)
|
||||
@ -51,7 +51,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var list = new List<RemoteImageInfo>();
|
||||
|
||||
var googleBookId = item.GetProviderId("GoogleBooks");
|
||||
var googleBookId = item.GetProviderId(GoogleBooksConstants.ProviderId);
|
||||
|
||||
if (string.IsNullOrEmpty(googleBookId))
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@ -24,16 +24,6 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
/// </summary>
|
||||
public class GoogleBooksProvider : IRemoteMetadataProvider<Book, BookInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// Name of the provider.
|
||||
/// </summary>
|
||||
public const string ProviderName = "Google Books";
|
||||
|
||||
/// <summary>
|
||||
/// Id of the provider.
|
||||
/// </summary>
|
||||
public const string ProviderId = "GoogleBooks";
|
||||
|
||||
// convert these characters to whitespace for better matching
|
||||
// there are two dashes with different char codes
|
||||
private const string Spacers = "/,.:;\\(){}[]+-_=–*";
|
||||
@ -85,7 +75,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => ProviderName;
|
||||
public string Name => GoogleBooksConstants.ProviderName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BookInfo searchInfo, CancellationToken cancellationToken)
|
||||
@ -108,8 +98,8 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
|
||||
var remoteSearchResult = new RemoteSearchResult();
|
||||
|
||||
remoteSearchResult.SetProviderId(ProviderId, result.Id);
|
||||
remoteSearchResult.SearchProviderName = ProviderName;
|
||||
remoteSearchResult.SetProviderId(GoogleBooksConstants.ProviderId, result.Id);
|
||||
remoteSearchResult.SearchProviderName = GoogleBooksConstants.ProviderName;
|
||||
remoteSearchResult.Name = result.VolumeInfo.Title;
|
||||
remoteSearchResult.Overview = result.VolumeInfo.Description;
|
||||
remoteSearchResult.ProductionYear = GetYearFromPublishedDate(result.VolumeInfo.PublishedDate);
|
||||
@ -135,7 +125,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
QueriedById = true
|
||||
};
|
||||
|
||||
var googleBookId = info.GetProviderId(ProviderId);
|
||||
var googleBookId = info.GetProviderId(GoogleBooksConstants.ProviderId);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(googleBookId))
|
||||
{
|
||||
@ -324,7 +314,7 @@ namespace Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(bookResult.Id))
|
||||
{
|
||||
book.SetProviderId(ProviderId, bookResult.Id);
|
||||
book.SetProviderId(GoogleBooksConstants.ProviderId, bookResult.Id);
|
||||
}
|
||||
|
||||
return book;
|
||||
|
@ -0,0 +1,36 @@
|
||||
using System.Net;
|
||||
using Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks;
|
||||
using Jellyfin.Plugin.Bookshelf.Tests.Http;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using NSubstitute;
|
||||
|
||||
namespace Jellyfin.Plugin.Bookshelf.Tests
|
||||
{
|
||||
public class GoogleBooksImageProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task GetImages_WithAllLinks_PicksLargestAndThumbnail()
|
||||
{
|
||||
var mockedMessageHandler = new MockHttpMessageHandler(new List<(Func<Uri, bool> requestMatcher, MockHttpResponse response)>
|
||||
{
|
||||
((Uri uri) => uri.AbsoluteUri.Contains("volumes/G7utDwAAQBAJ"), new MockHttpResponse(HttpStatusCode.OK, TestHelpers.GetFixture("google-books-single-volume-fr.json")))
|
||||
});
|
||||
|
||||
var mockedHttpClientFactory = Substitute.For<IHttpClientFactory>();
|
||||
mockedHttpClientFactory.CreateClient(Arg.Any<string>()).Returns(new HttpClient(mockedMessageHandler));
|
||||
|
||||
IRemoteImageProvider provider = new GoogleBooksImageProvider(mockedHttpClientFactory);
|
||||
|
||||
var images = await provider.GetImages(new Book()
|
||||
{
|
||||
ProviderIds = { { GoogleBooksConstants.ProviderId, "G7utDwAAQBAJ" } }
|
||||
}, CancellationToken.None);
|
||||
|
||||
Assert.Collection(
|
||||
images,
|
||||
largest => Assert.Equal("http://books.google.com/books/publisher/content?id=G7utDwAAQBAJ&printsec=frontcover&img=1&zoom=6&edge=curl&imgtk=AFLRE70zvRYbN6L3AM1H-SFdT_b8RDDGh6SfKIC_erPvfkI3QnpI_sFSIyOjXKgLJqbxVttwKVw12OUkxkPGjlAekXU7tTbpS7OcUQ_XbxhKaIsoC6ekr32GtMzZ5WkHbGu6rRpdIYVQ&source=gbs_api", largest.Url),
|
||||
thumbnail => Assert.Equal("http://books.google.com/books/publisher/content?id=G7utDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&imgtk=AFLRE73iXAAA6Bipi-q6HwR1kz5-XegugreP1A2Mbu63gh2TQKdI1lOCoRg9EuW7sFt2RjQgDbAXaHQlBPe8TBY2mo0i2ngWotY1eAvIusIEaCLRD18wl0baMruHUs4b3QvBF56gznpu&source=gbs_api", thumbnail.Url));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using Jellyfin.Plugin.Bookshelf.Providers.GoogleBooks;
|
||||
using Jellyfin.Plugin.Bookshelf.Tests.Http;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
@ -10,35 +10,17 @@ namespace Jellyfin.Plugin.Bookshelf.Tests
|
||||
{
|
||||
public class GoogleBooksProviderTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the content of a fixture file.
|
||||
/// </summary>
|
||||
/// <param name="fileName">Name of the fixture file.</param>
|
||||
/// <returns>The file's content.</returns>
|
||||
/// <exception cref="FileNotFoundException">If the file does not exist.</exception>
|
||||
private string GetFixture(string fileName)
|
||||
{
|
||||
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fixtures", fileName);
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
throw new FileNotFoundException($"The fixture file '{filePath}' was not found.");
|
||||
}
|
||||
|
||||
return File.ReadAllText(filePath);
|
||||
}
|
||||
|
||||
// From the query 'https://www.googleapis.com/books/v1/volumes?q=children+of+time+2015'
|
||||
private string GetTestSearchResult() => GetFixture("google-books-volume-search.json");
|
||||
private string GetTestSearchResult() => TestHelpers.GetFixture("google-books-volume-search.json");
|
||||
|
||||
private string GetEnglishTestVolumeResult() => GetFixture("google-books-single-volume-en.json");
|
||||
private string GetFrenchTestVolumeResult() => GetFixture("google-books-single-volume-fr.json");
|
||||
private string GetEnglishTestVolumeResult() => TestHelpers.GetFixture("google-books-single-volume-en.json");
|
||||
private string GetFrenchTestVolumeResult() => TestHelpers.GetFixture("google-books-single-volume-fr.json");
|
||||
|
||||
private bool HasGoogleId(string id, Dictionary<string, string> providerIds)
|
||||
{
|
||||
return providerIds.Count == 1
|
||||
&& providerIds.ContainsKey(GoogleBooksProvider.ProviderId)
|
||||
&& providerIds[GoogleBooksProvider.ProviderId] == id;
|
||||
&& providerIds.ContainsKey(GoogleBooksConstants.ProviderId)
|
||||
&& providerIds[GoogleBooksConstants.ProviderId] == id;
|
||||
}
|
||||
|
||||
#region GetSearchResults
|
||||
@ -58,7 +40,7 @@ namespace Jellyfin.Plugin.Bookshelf.Tests
|
||||
|
||||
var results = await provider.GetSearchResults(new BookInfo() { Name = "Children of Time" }, CancellationToken.None);
|
||||
|
||||
Assert.True(results.All(result => result.SearchProviderName == GoogleBooksProvider.ProviderName));
|
||||
Assert.True(results.All(result => result.SearchProviderName == GoogleBooksConstants.ProviderName));
|
||||
|
||||
Assert.Collection(
|
||||
results,
|
||||
@ -157,7 +139,7 @@ namespace Jellyfin.Plugin.Bookshelf.Tests
|
||||
var metadataResult = await provider.GetMetadata(new BookInfo()
|
||||
{
|
||||
Name = "Children of Time",
|
||||
ProviderIds = { { GoogleBooksProvider.ProviderId, "G7utDwAAQBAJ" } }
|
||||
ProviderIds = { { GoogleBooksConstants.ProviderId, "G7utDwAAQBAJ" } }
|
||||
}, CancellationToken.None);
|
||||
|
||||
Assert.True(metadataResult.QueriedById);
|
||||
|
23
tests/Jellyfin.Plugin.Bookshelf.Tests/TestHelpers.cs
Normal file
23
tests/Jellyfin.Plugin.Bookshelf.Tests/TestHelpers.cs
Normal file
@ -0,0 +1,23 @@
|
||||
namespace Jellyfin.Plugin.Bookshelf.Tests
|
||||
{
|
||||
internal static class TestHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the content of a fixture file.
|
||||
/// </summary>
|
||||
/// <param name="fileName">Name of the fixture file.</param>
|
||||
/// <returns>The file's content.</returns>
|
||||
/// <exception cref="FileNotFoundException">If the file does not exist.</exception>
|
||||
public static string GetFixture(string fileName)
|
||||
{
|
||||
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fixtures", fileName);
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
throw new FileNotFoundException($"The fixture file '{filePath}' was not found.");
|
||||
}
|
||||
|
||||
return File.ReadAllText(filePath);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user