Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 60a8eb7

Browse files
committed
add "model" and "status" fields
1 parent c982b1e commit 60a8eb7

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

data_diff/dbt.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TDiffVars(pydantic.BaseModel):
5252
where_filter: Optional[str] = None
5353
include_columns: List[str]
5454
exclude_columns: List[str]
55+
dbt_model: Optional[str] = None
5556

5657

5758
def dbt_diff(
@@ -166,6 +167,7 @@ def _get_diff_vars(
166167
datadiff_model_config = dbt_parser.get_datadiff_model_config(model.meta)
167168

168169
return TDiffVars(
170+
dbt_model=model.unique_id,
169171
dev_path=dev_qualified_list,
170172
prod_path=prod_qualified_list,
171173
primary_keys=primary_keys,
@@ -280,10 +282,14 @@ def _local_diff(diff_vars: TDiffVars, json_output: bool = False) -> None:
280282
# drain the iterator to get accumulated stats in diff.info_tree
281283
list(diff)
282284

283-
print(json.dumps(jsonify(diff, with_summary=True, with_columns={
284-
"added": columns_added,
285-
"removed": columns_removed,
286-
"changed": columns_type_changed,
285+
print(json.dumps(
286+
jsonify(
287+
diff,
288+
dbt_model=diff_vars.dbt_model,
289+
with_summary=True, with_columns={
290+
"added": columns_added,
291+
"removed": columns_removed,
292+
"changed": columns_type_changed,
287293
})))
288294
return
289295

data_diff/format.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from typing import Any, Optional, List, Dict, Tuple
33

44
from runtype import dataclass
5-
65
from data_diff.diff_tables import DiffResultWrapper
76

87

98
def jsonify(diff: DiffResultWrapper,
9+
dbt_model: str,
1010
with_summary: bool = False,
1111
with_columns: Optional[Dict[str, List[str]]] = None) -> 'JsonDiff':
1212
"""
@@ -57,7 +57,8 @@ def jsonify(diff: DiffResultWrapper,
5757
)
5858
)
5959
return JsonDiff(
60-
isDifferent=is_different,
60+
status="different" if is_different else "identical",
61+
model=dbt_model,
6162
table1=list(table1.table_path),
6263
table2=list(table2.table_path),
6364
rows=RowsDiff(
@@ -150,7 +151,8 @@ class RowsDiff:
150151

151152
@dataclass
152153
class JsonDiff:
153-
isDifferent: bool
154+
status: str # Literal ["identical", "different"]
155+
model: str
154156
table1: List[str]
155157
table2: List[str]
156158
rows: RowsDiff
@@ -231,23 +233,23 @@ def _jsonify_exclusive(row: Dict[str, Any], key_columns: List[str]) -> Dict[str,
231233

232234

233235
def _jsonify_diff_summary(stats_dict: dict) -> JsonDiffSummary:
234-
return {
235-
'rows': {
236-
'total': {
237-
'table1': stats_dict["rows_A"],
238-
'table2': stats_dict["rows_B"]
239-
},
240-
'exclusive': {
241-
'table1': stats_dict["exclusive_A"],
242-
'table2': stats_dict["exclusive_B"],
243-
},
244-
'updated': stats_dict["updated"],
245-
'unchanged': stats_dict["unchanged"]
246-
},
247-
'stats': {
248-
'diffCounts': stats_dict["stats"]['diff_counts']
249-
}
250-
}
236+
return JsonDiffSummary(
237+
rows=Rows(
238+
total=Total(
239+
table1=stats_dict["rows_A"],
240+
table2=stats_dict["rows_B"]
241+
),
242+
exclusive=ExclusiveRows(
243+
table1=stats_dict["exclusive_A"],
244+
table2=stats_dict["exclusive_B"],
245+
),
246+
updated=stats_dict["updated"],
247+
unchanged=stats_dict["unchanged"]
248+
),
249+
stats=Stats(
250+
diffCounts=stats_dict["stats"]['diff_counts']
251+
)
252+
)
251253

252254

253255
def _jsonify_columns_diff(columns_diff: Dict[str, List[str]]) -> JsonColumnsSummary:

tests/test_dbt.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ def test_get_diff_vars_meta_where(self, mock_prod_path_from_manifest, mock_prod_
11511151
mock_model.config.schema_ = None
11521152
mock_model.config.database = None
11531153
mock_model.alias = "a_model_name"
1154+
mock_model.unique_id = "unique_id"
11541155
mock_tdatadiffmodelconfig = Mock()
11551156
mock_tdatadiffmodelconfig.where_filter = "where"
11561157
mock_tdatadiffmodelconfig.include_columns = ["include"]
@@ -1187,6 +1188,7 @@ def test_get_diff_vars_meta_unrelated(self, mock_prod_path_from_manifest, mock_p
11871188
mock_model.config.schema_ = None
11881189
mock_model.config.database = None
11891190
mock_model.alias = "a_model_name"
1191+
mock_model.unique_id = "unique_id"
11901192
mock_tdatadiffmodelconfig = Mock()
11911193
mock_tdatadiffmodelconfig.where_filter = "where"
11921194
mock_tdatadiffmodelconfig.include_columns = ["include"]
@@ -1234,6 +1236,7 @@ def test_get_diff_vars_meta_none(self, mock_prod_path_from_manifest, mock_prod_p
12341236
mock_dbt_parser.get_pk_from_model.return_value = primary_keys
12351237
mock_dbt_parser.requires_upper = False
12361238
mock_model.meta = None
1239+
mock_model.unique_id = "unique_id"
12371240
mock_dbt_parser.prod_manifest_obj = None
12381241
mock_prod_path_from_config.return_value = ("prod_db", "prod_schema")
12391242

@@ -1271,6 +1274,7 @@ def test_get_diff_vars_custom_db(self, mock_prod_path_from_manifest, mock_prod_p
12711274
mock_dbt_parser.get_pk_from_model.return_value = primary_keys
12721275
mock_dbt_parser.requires_upper = False
12731276
mock_model.meta = None
1277+
mock_model.unique_id = "unique_id"
12741278
mock_dbt_parser.prod_manifest_obj = None
12751279
mock_prod_path_from_config.return_value = ("prod_db", "prod_schema")
12761280

@@ -1309,6 +1313,7 @@ def test_get_diff_vars_upper(self, mock_prod_path_from_manifest, mock_prod_path_
13091313
mock_dbt_parser.get_pk_from_model.return_value = primary_keys
13101314
mock_dbt_parser.requires_upper = True
13111315
mock_model.meta = None
1316+
mock_model.unique_id = "unique_id"
13121317
mock_dbt_parser.prod_manifest_obj = None
13131318
mock_prod_path_from_config.return_value = ("prod_db", "prod_schema")
13141319

@@ -1333,6 +1338,7 @@ def test_get_diff_vars_call_get_prod_path_from_manifest(
13331338
mock_model = Mock()
13341339
primary_keys = ["a_primary_key"]
13351340
mock_model.database = "a_dev_db"
1341+
mock_model.unique_id = "unique_id"
13361342
mock_model.schema_ = "a_schema"
13371343
mock_model.config.schema_ = None
13381344
mock_model.config.database = "custom_database"

tests/test_format.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def test_jsonify_diff(self):
2828
),
2929
diff=[], stats={}
3030
)
31-
json_diff = jsonify(diff)
31+
json_diff = jsonify(diff, dbt_model='my_model')
3232
self.assertEqual(json_diff, {
3333
'version': '1.0.0',
34-
'isDifferent': True,
34+
'status': 'different',
35+
'model': 'my_model',
3536
'table1': ['db', 'schema', 'table1'],
3637
'table2': ['db', 'schema', 'table2'],
3738
'rows': {
@@ -78,10 +79,11 @@ def test_jsonify_diff_no_difeference(self):
7879
),
7980
diff=[], stats={}
8081
)
81-
json_diff = jsonify(diff)
82+
json_diff = jsonify(diff, dbt_model='model')
8283
self.assertEqual(json_diff, {
8384
'version': '1.0.0',
84-
'isDifferent': False,
85+
'status': 'identical',
86+
'model': 'model',
8587
'table1': ['db', 'schema', 'table1'],
8688
'table2': ['db', 'schema', 'table2'],
8789
'rows': {

0 commit comments

Comments
 (0)