Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relations: Add functionality to sort json refs by relation_type #1202

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
1 change: 0 additions & 1 deletion invenio_app_ils/records_relations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ class IlsRecordWithRelations(IlsRecord):
def relations(self):
"""Get record relations."""
from .retriever import get_relations

return get_relations(self)

def clear(self):
Expand Down
8 changes: 8 additions & 0 deletions invenio_app_ils/records_relations/retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ def get(self):
r = self._build_relation_obj(sibling_pid, name)
relations.setdefault(name, [])
relations[name].append(r)

# Pre-requisite: fields being sorted should be of the same type.
# Sort by the keys available in the data in order
if relation_type.sort_by:
relations.get(name, []).sort(
key=lambda rec: [rec.get(key) for key in relation_type.sort_by]
)

return relations


Expand Down
8 changes: 7 additions & 1 deletion invenio_app_ils/relations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from invenio_app_ils.errors import RecordRelationsError

ILS_RELATION_TYPE = namedtuple(
"IlsRelationType", RelationType._fields + ("relation_class",)
"IlsRelationType", RelationType._fields + ("relation_class", "sort_by",)
)

LANGUAGE_RELATION = ILS_RELATION_TYPE(
Expand All @@ -27,6 +27,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.SiblingsRelation",
None,
)
EDITION_RELATION = ILS_RELATION_TYPE(
1,
Expand All @@ -35,6 +36,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.SiblingsRelation",
["edition"],
)
OTHER_RELATION = ILS_RELATION_TYPE(
2,
Expand All @@ -43,6 +45,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.SiblingsRelation",
None,
)
MULTIPART_MONOGRAPH_RELATION = ILS_RELATION_TYPE(
3,
Expand All @@ -51,6 +54,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.ParentChildRelation",
None,
)
SERIAL_RELATION = ILS_RELATION_TYPE(
4,
Expand All @@ -59,6 +63,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.ParentChildRelation",
None,
)
SEQUENCE_RELATION = ILS_RELATION_TYPE(
5,
Expand All @@ -67,6 +72,7 @@
"invenio_app_ils.relations.nodes:PIDNodeRelated",
"invenio_pidrelations.serializers.schemas.RelationSchema",
"invenio_app_ils.relations.api.SequenceRelation",
None,
)


Expand Down
123 changes: 109 additions & 14 deletions tests/api/ils/records_relations/test_records_relations_siblings.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _test_create():
)

def _test_delete():
"""Test relation creation."""
"""Test relation deletion."""
rec1, rec2 = recrel_choose_endpoints_and_do_request(
(client, json_headers, "DELETE"),
(
Expand Down Expand Up @@ -183,13 +183,22 @@ def _test_sibl_edition_relation(client, json_headers, testdata):
first_pid_type = "docid"
second_pid_value = "docid-1"
second_pid_type = "docid"
third_pid_value = "docid-8"
third_pid_type = "docid"
relation_type = "edition"

payload = {
"pid_value": second_pid_value,
"pid_type": second_pid_type,
"relation_type": relation_type,
}
payload = [
{
"pid_value": second_pid_value,
"pid_type": second_pid_type,
"relation_type": relation_type,
},
{
"pid_value": third_pid_value,
"pid_type": third_pid_type,
"relation_type": relation_type,
},
]

def _test_create():
"""Test relation creation."""
Expand All @@ -203,6 +212,9 @@ def _test_create():
),
payload,
)
rec3 = Document.get_record_by_pid(third_pid_value)
rec3 = rec3.replace_refs()

recrel_assert_record_relations(
rec1,
expected={
Expand All @@ -219,7 +231,50 @@ def _test_create():
"document_type": rec2["document_type"],
"publication_year": rec2["publication_year"],
},
}
},
{
"pid_value": third_pid_value,
"pid_type": third_pid_type,
"relation_type": "edition",
"record_metadata": {
"title": rec3["title"],
"edition": rec3["edition"],
"document_type": rec3["document_type"],
"publication_year": rec3["publication_year"],
},
},
]
}
},
)
recrel_assert_record_relations(
rec3,
expected={
"relations": {
"edition": [
{
"pid_value": second_pid_value,
"pid_type": second_pid_type,
"relation_type": "edition",
"record_metadata": {
"title": rec2["title"],
"edition": rec2["edition"],
"languages": rec2["languages"],
"document_type": rec2["document_type"],
"publication_year": rec2["publication_year"],
},
},
{
"pid_value": first_pid_value,
"pid_type": first_pid_type,
"relation_type": "edition",
"record_metadata": {
"title": rec1["title"],
"edition": rec1["edition"],
"document_type": rec1["document_type"],
"publication_year": rec1["publication_year"],
},
},
]
}
},
Expand All @@ -230,9 +285,20 @@ def _test_create():

recrel_assert_record_relations(
rec2,
expected={
{
"relations": {
"edition": [
{
"pid_value": third_pid_value,
"pid_type": third_pid_type,
"relation_type": "edition",
"record_metadata": {
"title": rec3["title"],
"edition": rec3["edition"],
"document_type": rec3["document_type"],
"publication_year": rec3["publication_year"],
},
},
{
"pid_value": first_pid_value,
"pid_type": first_pid_type,
Expand All @@ -243,7 +309,7 @@ def _test_create():
"document_type": rec1["document_type"],
"publication_year": rec1["publication_year"],
},
}
},
],
"language": [
{
Expand Down Expand Up @@ -274,7 +340,7 @@ def _test_create():
)

def _test_delete():
"""Test relation creation."""
"""Test relation deletion."""
rec1, rec2 = recrel_choose_endpoints_and_do_request(
(client, json_headers, "DELETE"),
(
Expand All @@ -285,11 +351,16 @@ def _test_delete():
),
payload,
)
rec3 = Document.get_record_by_pid(third_pid_value)
rec3 = rec3.replace_refs()

recrel_assert_record_relations(rec1, expected={"relations": {}})
recrel_assert_record_relations(rec3, expected={"relations": {}})

rec_docid_2 = get_test_record(testdata, "documents", "docid-2")
rec_docid_6 = get_test_record(testdata, "documents", "docid-6")

# docid-1 should still have the languages relation created in the previous test
recrel_assert_record_relations(
rec2,
expected={
Expand Down Expand Up @@ -383,6 +454,7 @@ def _test_create():

rec_docid_1 = get_test_record(testdata, "documents", "docid-1")
rec_docid_6 = get_test_record(testdata, "documents", "docid-6")
rec_docid_8 = get_test_record(testdata, "documents", "docid-8")

recrel_assert_record_relations(
rec1,
Expand Down Expand Up @@ -455,7 +527,18 @@ def _test_create():
"document_type": rec_docid_1["document_type"],
"publication_year": rec_docid_1["publication_year"],
},
}
},
{
"pid_value": rec_docid_8["pid"],
"pid_type": "docid",
"relation_type": "edition",
"record_metadata": {
"title": rec_docid_8["title"],
"edition": rec_docid_8["edition"],
"document_type": rec_docid_8["document_type"],
"publication_year": rec_docid_8["publication_year"],
},
},
],
"other": [
{
Expand All @@ -476,7 +559,7 @@ def _test_create():
)

def _test_delete():
"""Test relation creation."""
"""Test relation deletion."""
rec1, rec2 = recrel_choose_endpoints_and_do_request(
(client, json_headers, "DELETE"),
(
Expand All @@ -490,6 +573,7 @@ def _test_delete():

rec_docid_1 = get_test_record(testdata, "documents", "docid-1")
rec_docid_6 = get_test_record(testdata, "documents", "docid-6")
rec_docid_8 = get_test_record(testdata, "documents", "docid-8")

recrel_assert_record_relations(
rec1,
Expand Down Expand Up @@ -539,7 +623,18 @@ def _test_delete():
"document_type": rec_docid_1["document_type"],
"publication_year": rec_docid_1["publication_year"],
},
}
},
{
"pid_value": rec_docid_8["pid"],
"pid_type": "docid",
"relation_type": "edition",
"record_metadata": {
"title": rec_docid_8["title"],
"edition": rec_docid_8["edition"],
"document_type": rec_docid_8["document_type"],
"publication_year": rec_docid_8["publication_year"],
},
},
]
}
},
Expand Down Expand Up @@ -612,7 +707,7 @@ def test_siblings_relations(client, json_headers, testdata, users):
# docid-1 --language--> docid-2 and docid-6
_test_sibl_language_relation(client, json_headers)

# docid-3 --edition--> docid-1
# docid-3 --edition--> docid-1 and docid-8
_test_sibl_edition_relation(client, json_headers, testdata)

# docid-2 --other--> docid-3
Expand Down
1 change: 1 addition & 0 deletions tests/data/documents.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"title": "Less: A Novel",
"authors": [{ "full_name": "Andrew Sean Greer" }],
"abstract": "This is an abstract",
"edition": "2",
"document_type": "THESIS",
"publication_year": "1950"
},
Expand Down
Loading