Skip to content

Commit

Permalink
Merge pull request #390 from datayoga-io/389-relationalwrite-incorrec…
Browse files Browse the repository at this point in the history
…tly-handles-invalid-mapped-field

`relational.write` incorrectly handles invalid mapped field #389
  • Loading branch information
zalmane authored Jan 5, 2025
2 parents 25ef812 + fd5a998 commit cafa231
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
13 changes: 11 additions & 2 deletions core/src/datayoga_core/write_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ def map_record(

source_path = utils.split_field(source)
obj = record
found = True
for key in source_path:
key = utils.unescape_field(key)
if key in obj:

if obj is None:
found = False
break

if isinstance(obj, dict) and key in obj:
obj = obj[key]
else:
found = False
break

mapped_record[target] = obj if obj != record else None
mapped_record[target] = obj if found else None

return mapped_record
67 changes: 67 additions & 0 deletions core/tests/test_write_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from datayoga_core import write_utils
from datayoga_core.result import Status

Expand Down Expand Up @@ -32,3 +33,69 @@ def test_group_by_opcode_invalid_opcode():
groups = write_utils.group_records_by_opcode(change_records, opcode_field="opcode")
assert groups["r"] == change_records[0:1]
assert groups["x"] == change_records[1:2]


@pytest.mark.parametrize("record, source, expected", [
# Existing simple nested key
(
{"value": {"id": 123}},
"value.id",
{"target": 123}
),
# Existing deeply nested key
(
{"user": {"profile": {"email": "[email protected]"}}},
"user.profile.email",
{"target": "[email protected]"}
),
# Missing simple nested key
(
{"value": {"x": 2}},
"value.id",
{"target": None}
),
# Missing deeply nested key
(
{"user": {"profile": {"name": "John"}}},
"user.profile.email",
{"target": None}
),
# Partially nested key
(
{"user": {"profile": {"name": "John"}}},
"user.address.street",
{"target": None}
),
# Mixed case with existing and non-existing keys
(
{"data": {"info": {"id": 123, "active": True}}},
"data.info.status",
{"target": None}
),
# Empty nested structure
(
{"user": {}},
"user.profile",
{"target": None}
),
# Null value in nested structure
(
{"user": {"profile": None}},
"user.profile.name",
{"target": None}
),
# Boolean value
(
{"settings": {"active": True}},
"settings.active",
{"target": True}
)
])
def test_map_record_nested_key(record, source, expected):
"""Tests map_record function's handling of nested keys, including existing and missing scenarios."""
mapped = write_utils.map_record(
record,
keys=[{"target": source}]
)

assert mapped == expected, f"Failed for record {record} with source {source}"

0 comments on commit cafa231

Please sign in to comment.