Skip to content

Commit 2a3b2cc

Browse files
Copilotdbnk0
andcommitted
Add test coverage for register_correlation_hook_with_master_record (#33)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Dávid Benko <34683450+dbnk0@users.noreply.github.com>
1 parent b0443cd commit 2a3b2cc

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

tests/modules/test_module.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ def modify_value(_: str, record: dict, attr: str, value):
1414
record[attr] = value
1515

1616

17+
def use_master_record(
18+
_: str, record: dict, master_record: dict, target_attr: str, source_attr: str
19+
):
20+
"""Hook that uses master record to copy a value from master to snapshot.
21+
22+
Only applies when source attribute in master record has value starting with "master_"
23+
to avoid interfering with other test cases.
24+
"""
25+
if source_attr in master_record:
26+
# Get the value from master record
27+
master_value = master_record[source_attr].get("v", None)
28+
if master_value is not None and str(master_value).startswith("master_"):
29+
# Append a suffix to demonstrate master record was used
30+
record[target_attr] = f"{master_value}_from_master"
31+
32+
1733
dummy_hook_abc = update_wrapper(partial(modify_value, attr="data2", value="abc"), modify_value)
1834
dummy_hook_def = update_wrapper(partial(modify_value, attr="data1", value="def"), modify_value)
1935

@@ -67,3 +83,15 @@ def __init__(
6783
depends_on=[],
6884
may_change=[["data1"]],
6985
)
86+
87+
# Testing register_correlation_hook_with_master_record
88+
# This hook should copy data1 from master record to data2 with a suffix
89+
registrar.register_correlation_hook_with_master_record(
90+
update_wrapper(
91+
partial(use_master_record, target_attr="data2", source_attr="data1"),
92+
use_master_record,
93+
),
94+
"A",
95+
depends_on=[],
96+
may_change=[["data2"]],
97+
)

tests/test_api/test_snapshots.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def make_dp(type, id, attr, v, time=False):
4242
make_dp("C", "c1", "ds", {"eid": "d1"}, time=True),
4343
make_dp("C", "c1", "data1", "inita"),
4444
make_dp("C", "c1", "data2", "inita"),
45+
# For test_master_record_hook (A-423)
46+
make_dp("A", 423, "data1", "master_test"),
47+
make_dp("A", 423, "data2", "placeholder"),
4548
]
4649
res = cls.push_datapoints(entity_datapoints)
4750
if res.status_code != 200:
@@ -79,3 +82,17 @@ def test_hook_dependency_value_forwarding(self):
7982
for snapshot in data.snapshots:
8083
self.assertEqual(snapshot["data1"], "modifd")
8184
self.assertEqual(snapshot["data2"], "modifc")
85+
86+
def test_master_record_hook(self):
87+
"""
88+
Test that hooks registered via register_correlation_hook_with_master_record
89+
correctly receive the master record parameter.
90+
"""
91+
# Entity A-423 has data1="master_test" in its master record
92+
# The master record hook should copy data1 from master and append "_from_master"
93+
data = self.get_entity_data("entity/A/423", EntityEidData)
94+
self.assertGreater(len(data.snapshots), 0)
95+
for snapshot in data.snapshots:
96+
self.assertEqual(snapshot["data1"], "master_test")
97+
# The hook should have set data2 to data1 from master record + "_from_master"
98+
self.assertEqual(snapshot["data2"], "master_test_from_master")

0 commit comments

Comments
 (0)