Open-source personal finance manager. Self-hosted, privacy-first.
fix: preserve regex pattern case in rule engine matching (#464)
Tomaz72a94df0c89d
diff --git a/backend/app/services/rule_engine.py b/backend/app/services/rule_engine.pyindex 82932791..2cd56611 100644--- a/backend/app/services/rule_engine.py+++ b/backend/app/services/rule_engine.py@@ -10,11 +10,15 @@ if TYPE_CHECKING:from app.models.transaction import Transaction+def _strip_accents(text: str) -> str:+ """Remove diacritics (accents), preserving case."""+ nfkd = unicodedata.normalize("NFKD", text)+ return "".join(c for c in nfkd if not unicodedata.combining(c))++def _normalize(text: str) -> str:"""Normalize text: uppercase and remove diacritics (accents)."""- upper = text.upper()- nfkd = unicodedata.normalize("NFKD", upper)- return "".join(c for c in nfkd if not unicodedata.combining(c))+ return _strip_accents(text.upper())def _to_decimal(val) -> Decimal:@@ -63,8 +67,11 @@ def _match_condition(condition: dict, tx: "Transaction") -> bool:return tx_str != val_strif op == "regex":try:- # Normalize both sides so accents don't break matches- pattern = _normalize(str(value or ""))+ # Strip accents from the pattern so it lines up with the+ # normalized text, but keep its case: uppercasing a regex+ # inverts escape classes (\s -> \S, \b -> \B, \d -> \D) and+ # breaks inline flags. Case is already handled by IGNORECASE.+ pattern = _strip_accents(str(value or ""))return bool(re.search(pattern, tx_str, re.IGNORECASE))except re.error:return Falsediff --git a/backend/tests/test_rule_engine.py b/backend/tests/test_rule_engine.pyindex 116d23bc..2e30d1e9 100644--- a/backend/tests/test_rule_engine.py+++ b/backend/tests/test_rule_engine.py@@ -77,6 +77,56 @@ def test_regex():assert evaluate_conditions("and", conditions, tx) is True+def test_regex_whitespace_class():+ conditions = [{"field": "description", "op": "regex", "value": r"PIX\s+RECEBIDO"}]+ tx = make_tx(description="PIX RECEBIDO JOAO")+ assert evaluate_conditions("and", conditions, tx) is True+++def test_regex_digit_class():+ conditions = [{"field": "description", "op": "regex", "value": r"NOTA \d+"}]+ tx = make_tx(description="OPERACOES EM BOLSA NOTA 123884393")+ assert evaluate_conditions("and", conditions, tx) is True+++def test_regex_word_boundary():+ conditions = [{"field": "description", "op": "regex", "value": r"\bCDB\b"}]+ tx = make_tx(description="VENCIMENTO CDB BANCO MASTER")+ assert evaluate_conditions("and", conditions, tx) is True+++def test_regex_word_boundary_no_match():+ conditions = [{"field": "description", "op": "regex", "value": r"\bCDB\b"}]+ tx = make_tx(description="COMPRA CDB321LQ8I6 RESGATE")+ assert evaluate_conditions("and", conditions, tx) is False+++def test_regex_negative_lookahead_with_whitespace():+ conditions = [+ {"field": "description", "op": "regex", "value": r"RESGATE(?!\s+PONTOS)"}+ ]+ assert evaluate_conditions("and", conditions, make_tx(description="RESGATE RDB")) is True+ assert evaluate_conditions("and", conditions, make_tx(description="RESGATE PONTOS")) is False+++def test_regex_inline_flag_is_valid():+ conditions = [{"field": "description", "op": "regex", "value": "(?i)uber"}]+ tx = make_tx(description="UBER TRIP")+ assert evaluate_conditions("and", conditions, tx) is True+++def test_regex_lowercase_pattern_still_matches():+ conditions = [{"field": "description", "op": "regex", "value": "pix.*recebido"}]+ tx = make_tx(description="PIX RECEBIDO JOAO")+ assert evaluate_conditions("and", conditions, tx) is True+++def test_regex_accented_pattern_still_matches():+ conditions = [{"field": "description", "op": "regex", "value": "APLICAÇÃO"}]+ tx = make_tx(description="APLICACAO EM CDB")+ assert evaluate_conditions("and", conditions, tx) is True++def test_amount_lt():conditions = [{"field": "amount", "op": "lt", "value": 50}]tx = make_tx(amount=Decimal("25.50"))