Skip to content

Commit

Permalink
serializers: add publication-section rdm type (book section) to bibte…
Browse files Browse the repository at this point in the history
…x serialization
  • Loading branch information
tmorrell committed Jan 8, 2025
1 parent de2a0e4 commit bd6fed1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
4 changes: 4 additions & 0 deletions invenio_rdm_records/resources/serializers/bibtex/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class BibTexSchema(BaseSerializerSchema, CommonFieldsMixin):
BibTexFormatter.book,
BibTexFormatter.booklet,
],
"publication-section": [
BibTexFormatter.in_collection,
BibTexFormatter.in_book,
],
"publication-article": [BibTexFormatter.article],
"publication-preprint": [BibTexFormatter.unpublished],
"publication-thesis": [BibTexFormatter.thesis],
Expand Down
35 changes: 35 additions & 0 deletions invenio_rdm_records/resources/serializers/bibtex/schema_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@ class BibTexFormatter:
}
"""A single-volume conference proceedings."""

in_collection = {
"name": "incollection",
"req_fields": ["author", "title", "booktitle", "year", "publisher"],
"opt_fields": [
"pages",
"address",
"month",
"editor",
"volume",
"number",
"series",
"doi",
"url",
],
}
"""An article in a book."""

in_book = {
"name": "inbook",
"req_fields": ["author", "title", "pages", "year", "publisher"],
"opt_fields": [
"address",
"month",
"editor",
"edition",
"volume",
"number",
"series",
"note",
"doi",
"url",
],
}
"""A part of a book that doesn't have a title."""

article = {
"name": "article",
"req_fields": ["author", "title", "journal", "year"],
Expand Down
56 changes: 55 additions & 1 deletion tests/resources/serializers/test_bibtex_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def test_bibtex_serializer_full_record(running_app, updated_full_record):
[
("publication"),
("publication-annotationcollection"),
("publication-section"),
("publication-conferenceproceeding"),
("publication-datamanagementplan"),
("publication-journal"),
Expand Down Expand Up @@ -196,6 +195,61 @@ def test_serialize_publication_conferencepaper(running_app, updated_minimal_reco
)


def test_serialize_publication_booksection(running_app, updated_minimal_record):
"""Test bibtex formatter for a section of a book.
It serializes into `incollection` based on
- incollection (Book title is present)
- inbook (Book title is not present, pages are present)
"""
updated_minimal_record["metadata"]["resource_type"]["id"] = "publication-section"

# Force serialization into 'incollection'
updated_minimal_record.update(
{"custom_fields": {"imprint:imprint": {"title": "book title", "pages": "1-5"}}}
)
serializer = BibtexSerializer()
serialized_record = serializer.serialize_object(updated_minimal_record)

expected_data = "\n".join(
[
"@incollection{brown_2023_abcde-fghij,",
" author = {Name and",
" Troy Inc.},",
" title = {A Romans story},",
" booktitle = {book title},",
" year = 2023,",
" publisher = {Acme Inc},",
" month = mar,",
" pages = {1-5},",
"}",
]
)

assert serialized_record == expected_data

Check failure on line 230 in tests/resources/serializers/test_bibtex_serializer.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.9, postgresql14, opensearch2)

test_serialize_publication_booksection AssertionError: assert '@incollectio... = mar,\n}' == '@incollectio... = {1-5},\n}' Skipping 205 identical leading characters in diff, use -v to show e Inc}, + pages = {1-5}, month = mar, - pages = {1-5}, }

Check failure on line 230 in tests/resources/serializers/test_bibtex_serializer.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.12, postgresql14, opensearch2)

test_serialize_publication_booksection AssertionError: assert '@incollectio... = mar,\n}' == '@incollectio... = {1-5},\n}' Skipping 205 identical leading characters in diff, use -v to show e Inc}, + pages = {1-5}, month = mar, - pages = {1-5}, }

# Force serialization into 'inbook'
del updated_minimal_record["custom_fields"]["imprint:imprint"]["title"]
serialized_record = serializer.serialize_object(updated_minimal_record)

expected_data = "\n".join(
[
"@inbook{brown_2023_abcde-fghij,",
" author = {Name and",
" Troy Inc.},",
" title = {A Romans story},",
" year = 2023,",
" publisher = {Acme Inc},",
" pages = {1-5},",
" month = mar,",
"}",
]
)

assert serialized_record == expected_data


def test_serialize_publication_book(running_app, updated_minimal_record):
"""Test bibtex formatter for books.
Expand Down

0 comments on commit bd6fed1

Please sign in to comment.