Skip to content

Commit 371761e

Browse files
committed
Couple more tests
1 parent 019282b commit 371761e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

moneyflow/ynab_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,24 @@ def _batch_reassign_transactions(
580580
Returns:
581581
Dictionary with results (same format as batch_update_merchant)
582582
"""
583+
# Guard against attempting to reassign payee to itself
584+
if old_payee_id == target_payee_id:
585+
logger.warning(
586+
f"Attempted to reassign payee {old_payee_id} to itself. "
587+
f"This suggests '{old_merchant_name}' and '{new_merchant_name}' "
588+
"are the same payee (possible duplicate)."
589+
)
590+
return {
591+
"success": False,
592+
"payee_id": old_payee_id,
593+
"transactions_affected": 0,
594+
"method": "same_payee_error",
595+
"message": (
596+
f"Cannot reassign: '{old_merchant_name}' and '{new_merchant_name}' "
597+
f"are the same payee (id={old_payee_id})"
598+
),
599+
}
600+
583601
try:
584602
transactions_api = ynab.TransactionsApi(self.api_client)
585603

tests/test_ynab_backend.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,40 @@ def test_batch_update_merchant_target_exists(self, backend, mock_ynab_api):
547547
)
548548
mock_transactions_api.update_transactions.assert_called_once()
549549

550+
def test_batch_update_merchant_same_payee_id(self, backend, mock_ynab_api):
551+
"""Test that reassigning to the same payee ID is rejected."""
552+
backend.client.budget_id = "test-budget-id"
553+
backend.client.api_client = MagicMock()
554+
555+
# Mock two payees with different names but same ID (edge case - shouldn't happen but guard against it)
556+
mock_old_payee = MagicMock()
557+
mock_old_payee.id = "payee-same"
558+
mock_old_payee.name = "Amazon.com"
559+
560+
mock_target_payee = MagicMock()
561+
mock_target_payee.id = "payee-same" # Same ID!
562+
mock_target_payee.name = "Amazon"
563+
564+
mock_payees_response = MagicMock()
565+
mock_payees_response.data.payees = [mock_old_payee, mock_target_payee]
566+
567+
mock_payees_api = MagicMock()
568+
mock_payees_api.get_payees.return_value = mock_payees_response
569+
570+
mock_ynab_api.PayeesApi.return_value = mock_payees_api
571+
572+
# Attempt to rename payee to itself (simulating a bug where names differ but ID is same)
573+
result = backend.batch_update_merchant("Amazon.com", "Amazon")
574+
575+
# Should fail with same_payee_error
576+
assert result["success"] is False
577+
assert result["method"] == "same_payee_error"
578+
assert "same payee" in result["message"]
579+
assert result["payee_id"] == "payee-same"
580+
581+
# Verify no API calls were made to update transactions
582+
mock_payees_api.update_payee.assert_not_called()
583+
550584
def test_batch_update_merchant_integration(self, backend, mock_ynab_api):
551585
"""
552586
Integration test: Verify that batch_update_merchant cascades to transactions.

0 commit comments

Comments
 (0)