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 9, 2025
1 parent 7e94694 commit f17bfc1
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 @@ -57,6 +57,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 @@ -210,7 +210,6 @@ def test_serialize_publication_conferenceproceeding(

serializer = BibtexSerializer()
serialized_record = serializer.serialize_object(updated_minimal_record)

expected_data = "\n".join(
[
"@proceedings{brown_2023_abcde-fghij,",
Expand All @@ -221,6 +220,61 @@ def test_serialize_publication_conferenceproceeding(
"}",
]
)
assert serialized_record == expected_data


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},",
" pages = {1-5},",
" month = mar,",
"}",
]
)

assert serialized_record == expected_data

# 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},",
" pages = {1-5},",
" year = 2023,",
" publisher = {Acme Inc},",
" month = mar,",
"}",
]
)

assert serialized_record == expected_data

Expand Down

0 comments on commit f17bfc1

Please sign in to comment.