Maps OpenLibrary MARC publish_country to ReleaseCountry

Adds publish_country to the Open Library edition response, translates MARC 21 country codes into the ReleaseCountry enumeration through a private table, and drops the city based publish_places mapping.

Closes #222
This commit is contained in:
BinaryAlley committed 2026-11-08 10:00:00 +02:00
1 parent 3350f19c1d
commit c6469e70cf
7 files changed
+365 -27

No files matched your search

@@ -25,7 +25,7 @@ internal class OpenLibraryEditionResponseFixture
/// <param name="notes">Optional. The notes of the edition.</param>
/// <param name="publishDate">Optional. The publication date of the edition.</param>
/// <param name="publishers">Optional. The publishers of the edition.</param>
/// <param name="publishPlaces">Optional. The publication places of the edition.</param>
/// <param name="publishCountry">Optional. The MARC 21 country code of the publication place of the edition.</param>
/// <param name="numberOfPages">Optional. The number of pages of the edition.</param>
/// <param name="physicalFormat">Optional. The physical format of the edition.</param>
/// <param name="editionName">Optional. The name of the edition.</param>
@@ -49,7 +49,7 @@ internal class OpenLibraryEditionResponseFixture
JsonElement? notes = null,
string? publishDate = null,
List<string>? publishers = null,
List<string>? publishPlaces = null,
string? publishCountry = null,
int? numberOfPages = null,
string? physicalFormat = null,
string? editionName = null,
@@ -74,7 +74,7 @@ internal class OpenLibraryEditionResponseFixture
Notes = notes ?? default,
PublishDate = publishDate,
Publishers = publishers ?? [],
PublishPlaces = publishPlaces ?? [],
PublishCountry = publishCountry,
NumberOfPages = numberOfPages,
PhysicalFormat = physicalFormat,
EditionName = editionName,
@@ -28,7 +28,6 @@ internal class OpenLibrarySearchDocumentResponseFixture
/// <param name="languages">Optional. The languages of the search document.</param>
/// <param name="publishers">Optional. The publishers of the search document.</param>
/// <param name="subjects">Optional. The subjects of the search document.</param>
/// <param name="publishPlaces">Optional. The publication places of the search document.</param>
/// <param name="numberOfPagesMedian">Optional. The median number of pages of the search document.</param>
/// <param name="ratingsAverage">Optional. The average rating of the search document.</param>
/// <param name="ratingsCount">Optional. The number of ratings of the search document.</param>
@@ -50,7 +49,6 @@ internal class OpenLibrarySearchDocumentResponseFixture
List<string>? languages = null,
List<string>? publishers = null,
List<string>? subjects = null,
List<string>? publishPlaces = null,
int? numberOfPagesMedian = null,
decimal? ratingsAverage = null,
int? ratingsCount = null,
@@ -73,7 +71,6 @@ internal class OpenLibrarySearchDocumentResponseFixture
Languages = languages ?? [],
Publishers = publishers ?? [],
Subjects = subjects ?? [],
PublishPlaces = publishPlaces ?? [],
NumberOfPagesMedian = numberOfPagesMedian,
RatingsAverage = ratingsAverage,
RatingsCount = ratingsCount,
@@ -160,7 +160,6 @@ public class OpenLibraryMapperTests
authorNames: ["Search Author"],
firstPublishYear: 2003,
publishers: ["Search Publisher"],
publishPlaces: ["London"],
subjects: ["Science fiction", "Space"],
isbns: ["978-0-306-40615-7"],
numberOfPagesMedian: 250,
@@ -205,7 +204,7 @@ public class OpenLibraryMapperTests
title: "Edition Title",
publishDate: "2010-05-01",
publishers: ["Publisher Co"],
publishPlaces: ["New York"],
publishCountry: "nyu",
numberOfPages: 320,
physicalFormat: "Hardcover",
editionName: "First Edition",
@@ -243,7 +242,7 @@ public class OpenLibraryMapperTests
Assert.Equal(1987, result.ReleaseInfo.OriginalReleaseYear);
Assert.Equal(new DateOnly(2010, 5, 1), result.ReleaseInfo.ReReleaseDate);
Assert.Equal(2010, result.ReleaseInfo.ReReleaseYear);
Assert.Null(result.ReleaseInfo.ReleaseCountry);
Assert.Equal(ReleaseCountry.US, result.ReleaseInfo.ReleaseCountry);
Assert.Equal("First Edition", result.ReleaseInfo.ReleaseVersion);
GenreDto genre = Assert.Single(result.Genres!);
Assert.Equal("Fantasy", genre.Name);
@@ -282,6 +281,33 @@ public class OpenLibraryMapperTests
Assert.Equal(100, mappedRating.VoteCount);
}
[Theory]
[InlineData("nyu", ReleaseCountry.US)] // A MARC 21 state code that stands for the United States.
[InlineData("xxu", ReleaseCountry.US)] // The MARC 21 country code of the United States.
[InlineData("enk", ReleaseCountry.GB)] // The MARC 21 country code of England.
[InlineData("ENK", ReleaseCountry.GB)] // The code differs only in casing.
[InlineData("gw ", ReleaseCountry.DE)] // A two letter MARC 21 code, padded with a trailing space.
[InlineData("fr", ReleaseCountry.FR)] // A two letter MARC 21 code without padding.
[InlineData("abc", ReleaseCountry.CA)] // A MARC 21 province code that stands for Canada.
[InlineData("aca", ReleaseCountry.AU)] // A MARC 21 territory code that stands for Australia.
[InlineData("uk", ReleaseCountry.GB)] // A discontinued MARC 21 code that still appears in catalogued records.
[InlineData("cs", ReleaseCountry.CZ)] // A discontinued MARC 21 code, mapped to the country that succeeded it.
[InlineData("vp", null)] // A MARC 21 code that stands for various places, with no equivalent release country.
[InlineData("zzz", null)] // An unknown MARC 21 code.
[InlineData("", null)] // An empty MARC 21 code.
public void MapDetailed_WhenEditionHasPublishCountry_ShouldMapTheMarcCountryCode(string? publishCountry, ReleaseCountry? expected)
{
// Arrange
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create();
OpenLibraryEditionResponse edition = _openLibraryEditionResponseFixture.Create(publishCountry: publishCountry);
// Act
BookMetadataDto result = OpenLibraryMapper.MapDetailed(lookup, edition, null, [], null);
// Assert
Assert.Equal(expected, result.ReleaseInfo!.ReleaseCountry);
}
[Fact]
public void MapDetailed_WhenWorkHasNoExplicitGenres_ShouldDeriveGenresFromTheGenreLikeSubjects()
{