Skip to content

Commit

Permalink
use diff class
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed Jun 4, 2024
1 parent 07f7041 commit 8b1b0d6
Showing 1 changed file with 76 additions and 21 deletions.
97 changes: 76 additions & 21 deletions dbt_common/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,63 @@ def from_dict(cls, dct: Mapping) -> "Record":
class Diff:
"""Marker class for diffs?"""

pass
def __init__(self, current_recording_path: str, previous_recording_path: str) -> None:
# self.diff = diff
self.current_recording_path = current_recording_path
self.previous_recording_path = previous_recording_path

def diff_query_records(self, current: List, previous: List) -> Dict[str, Any]:
# some of the table results are returned as a stringified list of dicts that don't diff because order isn't consistent
for i in range(len(current)):
if current[i].get("result").get("table") is not None:
current[i] = json.loads(current[i]["result"]["table"])
if previous[i].get("result").get("table") is not None:
previous[i] = json.loads(previous[i]["result"]["table"])

return DeepDiff(current, previous, ignore_order=True, verbose_level=2)

def diff_get_env_records(self, current: List, previous: List) -> Dict[str, Any]:
# testing out excluding all the env records from the diff
exclude_regex_paths = [
r"root\[0\]\['result'\]\['env'\]\['[^']+'\]" # ignore env vars for diff results
]

return DeepDiff(
current,
previous,
ignore_order=True,
verbose_level=2,
exclude_regex_paths=exclude_regex_paths,
)

def diff_default(self, current: List, previous: List) -> Dict[str, Any]:
return DeepDiff(current, previous, ignore_order=True, verbose_level=2)

def calculate_diff(self) -> Dict[str, Any]:
# TODO: should i convert the files to Records and diff the records?

with open(self.current_recording_path) as current_recording:
current_dct = json.load(current_recording)

with open(self.previous_recording_path) as previous_recording:
previous_dct = json.load(previous_recording)

diff = {}
for record_type in current_dct:
if record_type == "GetEnvRecord":
diff[record_type] = self.diff_get_env_records(
current_dct[record_type], previous_dct[record_type]
)
elif record_type == "QueryRecord":
diff[record_type] = self.diff_query_records(
current_dct[record_type], previous_dct[record_type]
)
else:
diff[record_type] = self.diff_default(
current_dct[record_type], previous_dct[record_type]
)

return diff


class RecorderMode(Enum):
Expand All @@ -73,18 +129,30 @@ def __init__(
self,
mode: RecorderMode,
types: Optional[List],
current_recording_path: str = "recording.json",
previous_recording_path: Optional[str] = None,
) -> None:
self.mode = mode
self.types = types
self._records_by_type: Dict[str, List[Record]] = {}
self._replay_diffs: List["Diff"] = []
self.diff: Diff
self.previous_recording_path = previous_recording_path
self.current_recording_path = current_recording_path

# TODO: better way to do this?
if previous_recording_path is not None and self.mode == RecorderMode.REPLAY:
self._records_by_type = self.load(previous_recording_path)
if self.previous_recording_path is not None and self.mode == RecorderMode.REPLAY:
self._records_by_type = self.load(self.previous_recording_path)
self.diff = Diff(
current_recording_path=self.current_recording_path,
previous_recording_path=self.previous_recording_path,
)

self.previous_recording_path = previous_recording_path
if self.previous_recording_path is not None and self.mode == RecorderMode.DIFF:
self.diff = Diff(
current_recording_path=self.current_recording_path,
previous_recording_path=self.previous_recording_path,
)

@classmethod
def register_record_type(cls, rec_type) -> Any:
Expand All @@ -110,8 +178,8 @@ def pop_matching_record(self, params: Any) -> Optional[Record]:

return match

def write(self, file_name: str) -> None:
with open(file_name, "w") as file:
def write(self) -> None:
with open(self.current_recording_path, "w") as file:
json.dump(self._to_dict(), file)

def _to_dict(self) -> Dict:
Expand Down Expand Up @@ -150,27 +218,14 @@ def expect_record(self, params: Any) -> Any:
result_tuple = dataclasses.astuple(record.result)
return result_tuple[0] if len(result_tuple) == 1 else result_tuple

def diff_with_previous(self, diff_file_name: str) -> None:
# TODO: should we error here?
if self.previous_recording_path is None:
raise Exception("No previous recording path provided")

with open(self.previous_recording_path) as previous_recording:
previous_dct = json.load(previous_recording)

diff = DeepDiff(previous_dct, self._to_dict())

with open(diff_file_name, "w") as file:
json.dump(diff.to_dict(), file)

def write_diffs(self, diff_file_name) -> None:
json.dump(
self._replay_diffs,
self.diff.calculate_diff(),
open(diff_file_name, "w"),
)

def print_diffs(self) -> None:
print(repr(self._replay_diffs))
print(repr(self.diff.calculate_diff()))


def get_record_mode_from_env() -> Optional[RecorderMode]:
Expand Down

0 comments on commit 8b1b0d6

Please sign in to comment.