From 43eaffe9a8563e3cddda4ccb97ab54dad79b28f4 Mon Sep 17 00:00:00 2001 From: WojtekMs <62173407+WojtekMs@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:18:07 +0100 Subject: [PATCH] Analyze only negative value transactions --- src/banker/analyzer/analyze.py | 3 +++ tests/banker/analyzer/test_analyze.py | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/banker/analyzer/analyze.py b/src/banker/analyzer/analyze.py index 36f5098..ce1828d 100644 --- a/src/banker/analyzer/analyze.py +++ b/src/banker/analyzer/analyze.py @@ -1,5 +1,7 @@ from dataclasses import dataclass +from moneyed import Money, PLN + from banker.data.category import Category from banker.data.transaction import Transaction from logging import getLogger @@ -16,6 +18,7 @@ class AnalyzeResult: def analyze_transactions(transactions: list[Transaction], supported_categories: list[Category]) -> AnalyzeResult: unmatched_transactions = [] matched_categories = {} + transactions = [transaction for transaction in transactions if transaction.value < Money(amount='0', currency=PLN)] for transaction in transactions: matching_categories = transaction.find_matching(supported_categories) matching_categories_count = len(matching_categories) diff --git a/tests/banker/analyzer/test_analyze.py b/tests/banker/analyzer/test_analyze.py index 5dc31c1..1bd0286 100644 --- a/tests/banker/analyzer/test_analyze.py +++ b/tests/banker/analyzer/test_analyze.py @@ -179,6 +179,26 @@ def make_category_with_value(category: Category, value: Money) -> Category: unmatched_transactions=[], matched_categories=[]) ), + ( + [ + Transaction(date="2023-01-01", value=Money(amount="-11.27", currency=PLN), description="New shoes", + type="Card"), + Transaction(date="2023-01-02", value=Money(amount="500.00", currency=PLN), description="Refund two", + type="Card"), + Transaction(date="2023-01-02", value=Money(amount="25.00", currency=PLN), description="Refund one", + type="Card") + ], + [ + Category(name="Shoes", payment_type=PaymentType.Optional, matching_regexes=[r"(?i)shoes"]), + Category(name="Refund one", payment_type=PaymentType.Optional, matching_regexes=["Refund one"]), + ], + + AnalyzeResult( + unmatched_transactions=[], + matched_categories=[make_category_with_value( + Category(name="Shoes", payment_type=PaymentType.Optional, matching_regexes=[r"(?i)shoes"]), + Money(amount="-11.27", currency=PLN))]) + ) ], ids=["given matching transaction to one category then return matched category with increased value", "given many matching transactions to one category then return matched category with increased value", @@ -189,7 +209,8 @@ def make_category_with_value(category: Category, value: Money) -> Category: "given one transaction that does not match then return unmatched transaction and no categories", "given no transactions then return empty list of both transactions and categories", "given transactions and empty list of categories then return all transactions as unmatched", - "given empty transactions and empty categories then return empty lists"] + "given empty transactions and empty categories then return empty lists", + "given transaction with positive value then ignore it"] ) def test_given_transactions_and_supported_categories_when_analyze_then_return_result( transactions, supported_categories, expected_result