Skip to content

Commit

Permalink
relations: added test for nested field dereferencing
Browse files Browse the repository at this point in the history
  • Loading branch information
0einstein0 committed Sep 26, 2024
1 parent 6121b68 commit 21a7ef1
Showing 1 changed file with 51 additions and 0 deletions.
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),
}
assert record["metadata"][0]["desc"]["languages"][1] == {
"id": str(fr_lang.id),
"iso": fr_lang["iso"],
"information": fr_lang["information"],
"@v": str(fr_lang.id) + "::" + str(fr_lang.revision_id),
}

0 comments on commit 21a7ef1

Please sign in to comment.