Skip to content

Commit 8f5ce3e

Browse files
wesmclaude
andcommitted
refactor: Remove unnecessary make_edit helper, consolidate imports
Cleaned up test suite by removing unnecessary indirection: **1. Removed make_edit helper from conftest.py** - Was just a thin wrapper around TransactionEdit() - Tests now use TransactionEdit directly (clearer) - Eliminated unnecessary factory function layer **2. Consolidated imports in test_data_manager.py** - Moved TransactionEdit and datetime imports to top of file - Removed redundant imports from within test method bodies - Cleaner, more standard Python test file structure **3. Fixed indentation issues** - Corrected indentation after automated replacements - All test methods properly indented **Benefits**: - Less indirection = easier to understand - Standard import pattern (all at top) - Cleaner test code for humans to read - No change in functionality All 465 tests passing, pyright clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent fce417b commit 8f5ce3e

File tree

3 files changed

+18
-68
lines changed

3 files changed

+18
-68
lines changed

moneyflow/state.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,18 @@ class TimeFrame(Enum):
5454

5555
@dataclass
5656
class TransactionEdit:
57-
"""Represents a pending transaction edit."""
57+
"""
58+
Represents a pending transaction edit.
59+
60+
Tracks a single change to a transaction (merchant, category, or hide flag)
61+
before it's committed to the backend API.
62+
"""
5863

5964
transaction_id: str
6065
field: str # 'merchant', 'category', 'hide_from_reports'
6166
old_value: Any
6267
new_value: Any
63-
timestamp: datetime = dataclass_field(default_factory=datetime.now)
68+
timestamp: datetime = dataclass_field(default_factory=datetime.now) # When edit was queued
6469

6570

6671
@dataclass

tests/conftest.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,10 @@
1515

1616

1717
# ============================================================================
18-
# TEST HELPER FUNCTIONS - Eliminate duplication across test suite
18+
# TEST HELPER FUNCTIONS
1919
# ============================================================================
2020

2121

22-
def make_edit(
23-
transaction_id: str,
24-
field: str,
25-
old_value: any,
26-
new_value: any,
27-
timestamp: datetime = None
28-
) -> TransactionEdit:
29-
"""
30-
Create a TransactionEdit for testing.
31-
32-
Helper to eliminate repeated TransactionEdit(..., datetime.now()) patterns.
33-
34-
Args:
35-
transaction_id: Transaction ID
36-
field: Field being edited ('merchant', 'category', 'hide_from_reports')
37-
old_value: Original value
38-
new_value: New value
39-
timestamp: Optional timestamp (defaults to now)
40-
41-
Returns:
42-
TransactionEdit instance
43-
44-
Example:
45-
>>> edit = make_edit("txn1", "merchant", "Old", "New")
46-
>>> edit.transaction_id
47-
'txn1'
48-
"""
49-
return TransactionEdit(
50-
transaction_id=transaction_id,
51-
field=field,
52-
old_value=old_value,
53-
new_value=new_value,
54-
timestamp=timestamp or datetime.now()
55-
)
56-
57-
5822
def save_test_credentials(
5923
credential_manager,
6024
email: str = "test@example.com",

tests/test_data_manager.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import pytest
66
import polars as pl
7+
from datetime import datetime
8+
79
from moneyflow.data_manager import DataManager
10+
from moneyflow.state import TransactionEdit
811

912

1013
class TestDataFetching:
@@ -147,9 +150,7 @@ class TestCommitEdits:
147150

148151
async def test_commit_single_edit(self, data_manager, mock_mm):
149152
"""Test committing a single edit."""
150-
from tests.conftest import make_edit
151-
152-
edits = [make_edit("txn_1", "merchant", "Old Name", "New Name")]
153+
edits = [TransactionEdit("txn_1", "merchant", "Old Name", "New Name", datetime.now())]
153154

154155
success, failure = await data_manager.commit_pending_edits(edits)
155156

@@ -164,12 +165,10 @@ async def test_commit_single_edit(self, data_manager, mock_mm):
164165

165166
async def test_commit_multiple_edits(self, data_manager, mock_mm):
166167
"""Test committing multiple edits."""
167-
from tests.conftest import make_edit
168-
169168
edits = [
170-
make_edit("txn_1", "merchant", "A", "B"),
171-
make_edit("txn_2", "category", "cat_old", "cat_new"),
172-
make_edit("txn_3", "hide_from_reports", False, True),
169+
TransactionEdit("txn_1", "merchant", "A", "B", datetime.now()),
170+
TransactionEdit("txn_2", "category", "cat_old", "cat_new", datetime.now()),
171+
TransactionEdit("txn_3", "hide_from_reports", False, True, datetime.now()),
173172
]
174173

175174
success, failure = await data_manager.commit_pending_edits(edits)
@@ -188,9 +187,7 @@ async def test_commit_empty_edits(self, data_manager, mock_mm):
188187

189188
async def test_commit_merchant_rename(self, data_manager, mock_mm):
190189
"""Test committing a merchant rename."""
191-
from tests.conftest import make_edit
192-
193-
edits = [make_edit("txn_1", "merchant", "Amazon.com", "Amazon")]
190+
edits = [TransactionEdit("txn_1", "merchant", "Amazon.com", "Amazon", datetime.now())]
194191

195192
await data_manager.commit_pending_edits(edits)
196193

@@ -201,9 +198,7 @@ async def test_commit_merchant_rename(self, data_manager, mock_mm):
201198

202199
async def test_commit_category_change(self, data_manager, mock_mm):
203200
"""Test committing a category change."""
204-
from tests.conftest import make_edit
205-
206-
edits = [make_edit("txn_1", "category", "cat_groceries", "cat_shopping")]
201+
edits = [TransactionEdit("txn_1", "category", "cat_groceries", "cat_shopping", datetime.now())]
207202

208203
await data_manager.commit_pending_edits(edits)
209204

@@ -214,9 +209,7 @@ async def test_commit_category_change(self, data_manager, mock_mm):
214209

215210
async def test_commit_hide_toggle(self, data_manager, mock_mm):
216211
"""Test committing hide from reports toggle."""
217-
from tests.conftest import make_edit
218-
219-
edits = [make_edit("txn_1", "hide_from_reports", False, True)]
212+
edits = [TransactionEdit("txn_1", "hide_from_reports", False, True, datetime.now())]
220213

221214
await data_manager.commit_pending_edits(edits)
222215

@@ -508,9 +501,6 @@ async def test_get_stats_with_data(self, loaded_data_manager):
508501

509502
async def test_get_stats_with_pending_edits(self, loaded_data_manager):
510503
"""Test get_stats with pending edits."""
511-
from moneyflow.state import TransactionEdit
512-
from datetime import datetime
513-
514504
dm, df, _, _ = loaded_data_manager
515505
dm.df = df
516506
dm.pending_edits = [
@@ -557,9 +547,6 @@ class TestCommitEditsAdvanced:
557547

558548
async def test_commit_multiple_edits_same_transaction(self, data_manager, mock_mm):
559549
"""Test committing multiple edits to same transaction."""
560-
from moneyflow.state import TransactionEdit
561-
from datetime import datetime
562-
563550
# Multiple edits to the same transaction should be grouped
564551
edits = [
565552
TransactionEdit("txn_1", "merchant", "A", "B", datetime.now()),
@@ -582,9 +569,6 @@ async def test_commit_multiple_edits_same_transaction(self, data_manager, mock_m
582569

583570
async def test_commit_with_api_failure(self, data_manager, mock_mm):
584571
"""Test commit_pending_edits handles API failures gracefully."""
585-
from moneyflow.state import TransactionEdit
586-
from datetime import datetime
587-
588572
# Create a mock that raises an exception
589573
original_update = mock_mm.update_transaction
590574

@@ -609,9 +593,6 @@ async def failing_update(*args, **kwargs):
609593

610594
async def test_commit_mixed_edit_types(self, data_manager, mock_mm):
611595
"""Test committing different types of edits together."""
612-
from moneyflow.state import TransactionEdit
613-
from datetime import datetime
614-
615596
edits = [
616597
TransactionEdit("txn_1", "merchant", "Old", "New", datetime.now()),
617598
TransactionEdit("txn_2", "category", "cat_1", "cat_2", datetime.now()),

0 commit comments

Comments
 (0)