|
| 1 | +"""Compare two database summaries and return a list of discrepancy messages. |
| 2 | +
|
| 3 | +Mirrors MATLAB's did.util.compareDatabaseSummary for cross-language symmetry testing. |
| 4 | +""" |
| 5 | + |
| 6 | + |
| 7 | +def compare_database_summary(summary_a, summary_b): |
| 8 | + """Compare two summary dicts and return a list of mismatch descriptions. |
| 9 | +
|
| 10 | + Returns an empty list when the summaries are equivalent. |
| 11 | + """ |
| 12 | + report = [] |
| 13 | + |
| 14 | + branches_a = summary_a.get("branchNames", []) |
| 15 | + branches_b = summary_b.get("branchNames", []) |
| 16 | + |
| 17 | + only_in_a = set(branches_a) - set(branches_b) |
| 18 | + only_in_b = set(branches_b) - set(branches_a) |
| 19 | + |
| 20 | + for name in sorted(only_in_a): |
| 21 | + report.append(f'Branch "{name}" exists only in summary A.') |
| 22 | + for name in sorted(only_in_b): |
| 23 | + report.append(f'Branch "{name}" exists only in summary B.') |
| 24 | + |
| 25 | + # Compare branch hierarchy |
| 26 | + hier_a = summary_a.get("branchHierarchy", {}) |
| 27 | + hier_b = summary_b.get("branchHierarchy", {}) |
| 28 | + common_branches = sorted(set(branches_a) & set(branches_b)) |
| 29 | + |
| 30 | + for branch_name in common_branches: |
| 31 | + if branch_name in hier_a and branch_name in hier_b: |
| 32 | + parent_a = hier_a[branch_name].get("parent", "") |
| 33 | + parent_b = hier_b[branch_name].get("parent", "") |
| 34 | + if parent_a != parent_b: |
| 35 | + report.append( |
| 36 | + f'Branch "{branch_name}": parent mismatch ' |
| 37 | + f'("{parent_a}" vs "{parent_b}").' |
| 38 | + ) |
| 39 | + |
| 40 | + # Compare per-branch documents |
| 41 | + br_a = summary_a.get("branches", {}) |
| 42 | + br_b = summary_b.get("branches", {}) |
| 43 | + |
| 44 | + for branch_name in common_branches: |
| 45 | + if branch_name not in br_a or branch_name not in br_b: |
| 46 | + continue |
| 47 | + |
| 48 | + branch_a = br_a[branch_name] |
| 49 | + branch_b = br_b[branch_name] |
| 50 | + |
| 51 | + if branch_a["docCount"] != branch_b["docCount"]: |
| 52 | + report.append( |
| 53 | + f'Branch "{branch_name}": doc count mismatch ' |
| 54 | + f'({branch_a["docCount"]} vs {branch_b["docCount"]}).' |
| 55 | + ) |
| 56 | + |
| 57 | + map_a = {d["id"]: d for d in branch_a.get("documents", [])} |
| 58 | + map_b = {d["id"]: d for d in branch_b.get("documents", [])} |
| 59 | + |
| 60 | + missing_in_a = sorted(set(map_b) - set(map_a)) |
| 61 | + missing_in_b = sorted(set(map_a) - set(map_b)) |
| 62 | + |
| 63 | + for doc_id in missing_in_a: |
| 64 | + report.append( |
| 65 | + f'Branch "{branch_name}": doc "{doc_id}" missing in summary A.' |
| 66 | + ) |
| 67 | + for doc_id in missing_in_b: |
| 68 | + report.append( |
| 69 | + f'Branch "{branch_name}": doc "{doc_id}" missing in summary B.' |
| 70 | + ) |
| 71 | + |
| 72 | + for doc_id in sorted(set(map_a) & set(map_b)): |
| 73 | + doc_a = map_a[doc_id] |
| 74 | + doc_b = map_b[doc_id] |
| 75 | + |
| 76 | + if doc_a.get("className", "") != doc_b.get("className", ""): |
| 77 | + report.append( |
| 78 | + f'Branch "{branch_name}", doc "{doc_id}": class name mismatch ' |
| 79 | + f'("{doc_a["className"]}" vs "{doc_b["className"]}").' |
| 80 | + ) |
| 81 | + |
| 82 | + props_a = doc_a.get("properties", {}) |
| 83 | + props_b = doc_b.get("properties", {}) |
| 84 | + |
| 85 | + for field in ("demoA", "demoB", "demoC"): |
| 86 | + has_a = field in props_a |
| 87 | + has_b = field in props_b |
| 88 | + if has_a and has_b: |
| 89 | + val_a = ( |
| 90 | + props_a[field].get("value") |
| 91 | + if isinstance(props_a[field], dict) |
| 92 | + else props_a[field] |
| 93 | + ) |
| 94 | + val_b = ( |
| 95 | + props_b[field].get("value") |
| 96 | + if isinstance(props_b[field], dict) |
| 97 | + else props_b[field] |
| 98 | + ) |
| 99 | + if val_a != val_b: |
| 100 | + report.append( |
| 101 | + f'Branch "{branch_name}", doc "{doc_id}": ' |
| 102 | + f"{field}.value mismatch ({val_a} vs {val_b})." |
| 103 | + ) |
| 104 | + elif has_a != has_b: |
| 105 | + report.append( |
| 106 | + f'Branch "{branch_name}", doc "{doc_id}": ' |
| 107 | + f'field "{field}" present in one summary but not the other.' |
| 108 | + ) |
| 109 | + |
| 110 | + # Compare depends_on |
| 111 | + deps_a = props_a.get("depends_on", []) |
| 112 | + deps_b = props_b.get("depends_on", []) |
| 113 | + if isinstance(deps_a, dict): |
| 114 | + deps_a = [deps_a] |
| 115 | + if isinstance(deps_b, dict): |
| 116 | + deps_b = [deps_b] |
| 117 | + norm_a = [ |
| 118 | + (d.get("name", ""), d.get("value", "")) |
| 119 | + for d in deps_a |
| 120 | + if isinstance(d, dict) |
| 121 | + ] |
| 122 | + norm_b = [ |
| 123 | + (d.get("name", ""), d.get("value", "")) |
| 124 | + for d in deps_b |
| 125 | + if isinstance(d, dict) |
| 126 | + ] |
| 127 | + if norm_a != norm_b: |
| 128 | + report.append( |
| 129 | + f'Branch "{branch_name}", doc "{doc_id}": depends_on mismatch.' |
| 130 | + ) |
| 131 | + |
| 132 | + return report |
0 commit comments