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

relation: modified lookup_data for nested fields & 📦 release: v2.4.0 #325

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 11 additions & 4 deletions invenio_records/systemfields/relations/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

"""Relations system field."""

from itertools import chain

from ...dictutils import dict_lookup, dict_set
from .errors import InvalidCheckValue, InvalidRelationValue

Expand Down Expand Up @@ -154,10 +156,15 @@ def _lookup_id(self, data):
def _lookup_data(self):
data = dict_lookup(self.record, self.key)
if self.relation_field:
filtered_data = [
e.get(self.relation_field) for e in data if self.relation_field in e
]
return filtered_data
fields = self.relation_field.split(".")
0einstein0 marked this conversation as resolved.
Show resolved Hide resolved
for field in fields:
if isinstance(data, list):
data = [d.get(field) for d in data if d.get(field)]
else:
data = data.get(field)
if not data:
return []

return data

def validate(self):
Expand Down
51 changes: 51 additions & 0 deletions tests/test_relations_systemfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,3 +1356,54 @@ class Record4(Record, SystemFieldsMixin):
assert res == oe_lang
with pytest.raises(StopIteration): # finished first iterator
next(res_inner_iter)


def test_nested_field_dereferencing(testapp, db, languages):
"""Test dereferencing with relation_field containing nested relation."""
Language, languages = languages
en_lang = languages["en"]
fr_lang = languages["fr"]

class Record1(Record, SystemFieldsMixin):
# Example for testing a nested field dereferencing
relations = RelationsField(
nested_array_of_objects=PKNestedListRelation(
key="metadata",
keys=["iso", "information", "native_speakers"],
record_cls=Language,
relation_field="desc.languages",
),
)

# Define the record with nested fields
record_data = {
"metadata": [
{
"desc": {
"id": "1",
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"languages": [{"id": str(en_lang.id)}, {"id": str(fr_lang.id)}],
}
},
]
}

# Create the record instance
record = Record1(record_data)

# Test that dereferencing works for the languages
record.relations.nested_array_of_objects.dereference()

# Check that the languages have been dereferenced correctly
assert record["metadata"][0]["desc"]["languages"][0] == {
"id": str(en_lang.id),
"iso": en_lang["iso"],
"information": en_lang["information"],
"@v": str(en_lang.id) + "::" + str(en_lang.revision_id),
zzacharo marked this conversation as resolved.
Show resolved Hide resolved
}
assert record["metadata"][0]["desc"]["languages"][1] == {
zzacharo marked this conversation as resolved.
Show resolved Hide resolved
"id": str(fr_lang.id),
"iso": fr_lang["iso"],
"information": fr_lang["information"],
"@v": str(fr_lang.id) + "::" + str(fr_lang.revision_id),
}
Loading