Skip to content

Commit

Permalink
Analyze only negative value transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekMs committed Dec 4, 2023
1 parent 2ed50d9 commit 43eaffe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/banker/analyzer/analyze.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion tests/banker/analyzer/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit 43eaffe

Please sign in to comment.