Skip to content

Commit

Permalink
Refactored diff logic, added comments, renamed a few symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
eprbell committed Feb 2, 2025
1 parent ce7af17 commit 3a99d03
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions tests/transfer_analysis_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def _run_test(self, test: _Test, transfer_semantics: AbstractAccountingMethod) -
print(f"\nDescription: {test.description:}\nTransfer: {transfer_semantics}\nWant error: {'yes' if test.want_error else 'no'}")
configuration = Configuration("./config/test_data.ini", US())

# Prepare the test input
# Prepare test input
universal_input_data = self._create_universal_input_data_from_transaction_descriptors(configuration, test.input)

# If the test expects an error, run the test and check for error.
# If the test expects an error, run transfer analysis and check for error.
if test.want_error:
if test.want:
raise ValueError(f"Test data error: both want and want_error are set: {test}")
Expand All @@ -62,37 +62,25 @@ def _run_test(self, test: _Test, transfer_semantics: AbstractAccountingMethod) -

# Create transfer analyzer, call analyze() on universal InputData and receive per-wallet InputData.
transfer_analyzer = TransferAnalyzer(configuration, transfer_semantics, universal_input_data)
wallet_2_per_wallet_input_data = transfer_analyzer.analyze()
got_wallet_2_per_wallet_input_data = transfer_analyzer.analyze()

# Create expected per-wallet InputData, based on the want field of the test.
want_wallet_2_per_wallet_input_data = self._create_per_wallet_input_data_from_transaction_descriptors(configuration, test.want)

# Diff got and want results.
# Diff got and want per-wallet input data.
got: List[str] = []
for wallet, per_wallet_input_data in wallet_2_per_wallet_input_data.items():
got.append(f"{wallet}:")
for transaction in per_wallet_input_data.unfiltered_in_transaction_set:
got.extend(f"{transaction}".splitlines())
for transaction in per_wallet_input_data.unfiltered_out_transaction_set:
got.extend(f"{transaction}".splitlines())
for transaction in per_wallet_input_data.unfiltered_intra_transaction_set:
got.extend(f"{transaction}".splitlines())

want: List[str] = []
for wallet, per_wallet_input_data in got_wallet_2_per_wallet_input_data.items():
got.append(f"{wallet}:")
self._serialize_input_data_as_string_list(per_wallet_input_data, got)
for wallet, per_wallet_input_data in want_wallet_2_per_wallet_input_data.items():
want.append(f"{wallet}:")
for transaction in per_wallet_input_data.unfiltered_in_transaction_set:
want.extend(f"{transaction}".splitlines())
for transaction in per_wallet_input_data.unfiltered_out_transaction_set:
want.extend(f"{transaction}".splitlines())
for transaction in per_wallet_input_data.unfiltered_intra_transaction_set:
want.extend(f"{transaction}".splitlines())

self._serialize_input_data_as_string_list(per_wallet_input_data, want)
self.assertEqual("\n".join(unified_diff(got, want, lineterm="", n=10)), "")

# Create got_actual_amounts and compare them with want actual amounts.
# Diff got and want actual amounts.
got_actual_amounts: Dict[Account, Dict[str, int]] = {
wallet: {transaction.unique_id: int(actual_amount) for transaction, actual_amount in per_wallet_input_data.in_transaction_2_actual_amount.items()}
for wallet, per_wallet_input_data in wallet_2_per_wallet_input_data.items()
for wallet, per_wallet_input_data in got_wallet_2_per_wallet_input_data.items()
}
self.assertEqual(got_actual_amounts, test.want_amounts)

0 comments on commit 3a99d03

Please sign in to comment.