Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyze only negative value transactions #8

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading