Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.1.1
6 changes: 5 additions & 1 deletion document/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ def delete_documents_by_raw_id(cls, collection, raw_id):

@classmethod
def metadata(cls, collection=None):
queryset = cls.objects.select_related("collection", "source").only(
queryset = cls.objects.select_related(
"collection",
"source",
"parent_document",
).only(
"collection__acron3",
"default_lang",
"default_media_format",
Expand Down
75 changes: 69 additions & 6 deletions document/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,72 @@ def test_metadata_includes_source_context_and_legacy_identifiers(self):

metadata = list(Document.metadata(collection=collection))

self.assertEqual(len(metadata), 1)
self.assertEqual(metadata[0]["document_type"], Document.DOCUMENT_TYPE_ARTICLE)
self.assertEqual(metadata[0]["document_id"], "S123456782024000100001")
self.assertEqual(metadata[0]["source_type"], Source.SOURCE_TYPE_JOURNAL)
self.assertEqual(metadata[0]["source_id"], "1234-5678")
self.assertEqual(metadata[0]["scielo_issn"], "1234-5678")
self.assertEqual(
metadata,
[
{
"collection": "scl",
"default_lang": "en",
"default_media_format": None,
"document_id": "S123456782024000100001",
"document_type": Document.DOCUMENT_TYPE_ARTICLE,
"extra_data": {},
"files": {"pt": {"path": "/pdf/test.pdf"}},
"identifiers": {"doi": "10.1590/example"},
"parent_document_id": None,
"pid_generic": None,
"pid_v2": "S123456782024000100001",
"pid_v3": "abc123",
"processing_date": None,
"publication_date": "2024-01-15",
"publication_year": "2024",
"scielo_issn": "1234-5678",
"source_id": "1234-5678",
"source_type": Source.SOURCE_TYPE_JOURNAL,
"text_langs": ["en", "pt"],
"title": "Test Article",
}
],
)

def test_metadata_loads_parent_documents_in_the_initial_query(self):
collection = Collection.objects.create(acron3="books", acron2="bk")
source = Source.objects.create(
collection=collection,
source_type=Source.SOURCE_TYPE_BOOK,
source_id="book-1",
title="Test Book",
)
book = Document.objects.create(
collection=collection,
source=source,
document_type=Document.DOCUMENT_TYPE_BOOK,
document_id="book:book-1",
)
Document.objects.create(
collection=collection,
source=source,
parent_document=book,
document_type=Document.DOCUMENT_TYPE_CHAPTER,
document_id="book:book-1/chapter:1",
)
Document.objects.create(
collection=collection,
document_type=Document.DOCUMENT_TYPE_OTHER,
document_id="document-without-source",
)

with self.assertNumQueries(1):
metadata = list(Document.metadata(collection=collection))

metadata_by_id = {item["document_id"]: item for item in metadata}
self.assertIsNone(metadata_by_id["book:book-1"]["parent_document_id"])
self.assertEqual(
metadata_by_id["book:book-1/chapter:1"]["parent_document_id"],
"book:book-1",
)
self.assertIsNone(
metadata_by_id["document-without-source"]["parent_document_id"]
)
self.assertIsNone(metadata_by_id["document-without-source"]["source_id"])
self.assertIsNone(metadata_by_id["document-without-source"]["source_type"])
Loading