From ccc44a8d51ee5038fc46ace964d3f48a6e07b1b0 Mon Sep 17 00:00:00 2001 From: Konrad du Plessis Date: Wed, 22 Apr 2026 22:35:30 +0200 Subject: [PATCH] Fix _current_outstanding_in_scope sort + tighten team-filter test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two tweaks from code review on 82594fa: 1. The sort `key=lambda r: -r['amount']` placed NEGATIVE amounts (rare but possible: a project with only a deductive adjustment) AHEAD of larger positive exposures. Swapped to `key=lambda r: r['amount'], reverse=True` — same runtime, clearer intent, correct for negatives. 2. test_team_filter_scopes_total only asserted the net total. A partial scoping regression where the adjustment leaked but netted to zero would have silently passed. Added two assertions that by_project has exactly the expected 2 entries and R 500 never appears in the amount list. Co-Authored-By: Claude Opus 4.7 (1M context) --- core/tests.py | 5 +++++ core/views.py | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/tests.py b/core/tests.py index d2006f0..4aeabc8 100644 --- a/core/tests.py +++ b/core/tests.py @@ -589,3 +589,8 @@ class CurrentOutstandingInScopeTests(TestCase): # With team filter, only self.w's logs appear — R 400 total result = _current_outstanding_in_scope(team_ids=[self.t1.id]) self.assertEqual(result['total'], Decimal('400.00')) + # The R500 bonus on other_worker must NOT appear in by_project because + # that worker isn't in t1 — the team scope excludes them entirely. + self.assertEqual(len(result['by_project']), 2) + amounts = [row['amount'] for row in result['by_project']] + self.assertNotIn(Decimal('500.00'), amounts) diff --git a/core/views.py b/core/views.py index 565c0d5..7fbc9e8 100644 --- a/core/views.py +++ b/core/views.py @@ -270,7 +270,8 @@ def _current_outstanding_in_scope(project_ids=None, team_ids=None): raw = _compute_outstanding(project_ids=project_ids, team_ids=team_ids) by_project_list = sorted( [{'name': name, 'amount': amt} for name, amt in raw['outstanding_by_project'].items()], - key=lambda r: -r['amount'], + key=lambda r: r['amount'], + reverse=True, ) return { 'total': raw['outstanding_payments'],