Skip to content

Commit

Permalink
fixed impl
Browse files Browse the repository at this point in the history
  • Loading branch information
asikowitz committed Sep 17, 2024
1 parent f00741d commit 6a823b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
14 changes: 7 additions & 7 deletions metadata-ingestion/src/datahub/testing/compare_metadata_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ def assert_metadata_files_equal(

diff = diff_metadata_json(output, golden, ignore_paths, ignore_order=ignore_order)
if diff and update_golden:
if isinstance(diff, MCPDiff):
if diff.apply_delta(golden):
write_metadata_file(pathlib.Path(golden_path), golden)
return

# Fallback: just overwrite the golden file
shutil.copyfile(str(output_path), str(golden_path))
if isinstance(diff, MCPDiff) and diff.is_delta_valid:
diff.apply_delta(golden)
write_metadata_file(pathlib.Path(golden_path), golden)
else:
# Fallback: just overwrite the golden file
logger.info("Overwriting golden file!!!")
shutil.copyfile(str(output_path), str(golden_path))
return

if diff:
Expand Down
17 changes: 10 additions & 7 deletions metadata-ingestion/src/datahub/testing/mcp_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class MCPDiff:
aspect_changes: Dict[str, Dict[str, MCPAspectDiff]] # urn -> aspect -> diff
urns_added: Set[str]
urns_removed: Set[str]
is_delta_valid: bool

def __bool__(self) -> bool:
return bool(self.aspect_changes)
Expand All @@ -162,25 +163,31 @@ def create(
) -> "MCPDiff":
ignore_paths = [cls.convert_path(path) for path in ignore_paths]

is_delta_valid = True
aspect_changes: Dict[str, Dict[str, MCPAspectDiff]] = defaultdict(dict)
for urn in golden.keys() | output.keys():
golden_map = golden.get(urn, {})
output_map = output.get(urn, {})
for aspect_name in golden_map.keys() | output_map.keys():
t1 = golden_map.get(aspect_name, [])
t2 = output_map.get(aspect_name, [])
diff = DeepDiff(
t1=golden_map.get(aspect_name, []),
t2=output_map.get(aspect_name, []),
t1=t1,
t2=t2,
exclude_regex_paths=ignore_paths,
ignore_order=True,
custom_operators=[DeltaInfoOperator()],
)
if diff:
aspect_changes[urn][aspect_name] = MCPAspectDiff.create(diff)
if len(t1) > 1 and len(t2) > 1:
is_delta_valid = False

return cls(
urns_added=output.keys() - golden.keys(),
urns_removed=golden.keys() - output.keys(),
aspect_changes=aspect_changes,
is_delta_valid=is_delta_valid,
)

@staticmethod
Expand All @@ -192,17 +199,13 @@ def convert_path(path: str) -> str:
path,
)

def apply_delta(self, golden: List[Dict[str, Any]]) -> bool:
def apply_delta(self, golden: List[Dict[str, Any]]) -> None:
"""Update a golden file to match an output file based on the diff.
:param golden: Golden file represented as a list of MCPs, altered in-place.
:return: Whether the delta was successfully applied. If false, fallback to copying file when updating golden.
"""
aspect_diffs = [v for d in self.aspect_changes.values() for v in d.values()]
for aspect_diff in aspect_diffs:
if len(aspect_diff.aspects_changed) > 1:
return False

for (_, old, new) in aspect_diff.aspects_changed.keys():
golden[old.delta_info.idx] = new.delta_info.original

Expand Down

0 comments on commit 6a823b6

Please sign in to comment.