Skip to content

Commit 9232d60

Browse files
test(fp-history): assert query count stays flat with N affected findings
New test creates 5 pre-existing findings and asserts the batch still uses exactly 7 queries regardless — proving the old O(N) per-finding save loop is gone and a single bulk UPDATE covers all affected rows.
1 parent 0ea84ab commit 9232d60

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

unittests/test_false_positive_history_logic.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,42 @@ def test_fp_history_batch_retroactive_marks_existing_active_fp(self):
17191719
# The pre-existing active finding must now be retroactively marked FP.
17201720
self.assert_finding(find_pre, false_p=True)
17211721

1722+
def test_fp_history_batch_query_count_does_not_grow_with_affected_findings(self):
1723+
"""
1724+
Query count must stay flat (7) no matter how many findings are retroactively marked.
1725+
1726+
With the old per-finding approach this would have been 7 + N queries where N is the
1727+
number of pre-existing findings that get marked as FP. With the batch approach it is
1728+
always 7: System_Settings, 4 lazy-load chain, candidates SELECT, one bulk UPDATE.
1729+
"""
1730+
NUM_PRE_EXISTING = 5
1731+
1732+
# Create several pre-existing active findings with the same hash_code.
1733+
pre_existing = []
1734+
for _ in range(NUM_PRE_EXISTING):
1735+
find, _f = self.copy_and_reset_finding(find_id=2)
1736+
find.save()
1737+
pre_existing.append(find)
1738+
1739+
# Incoming batch finding already carries false_p=True — triggers retroactive marking.
1740+
find_incoming, _f = self.copy_and_reset_finding(find_id=2)
1741+
find_incoming.false_p = True
1742+
find_incoming.active = False
1743+
find_incoming.save()
1744+
1745+
batch = [Finding.objects.get(id=find_incoming.id)]
1746+
# 7 queries regardless of NUM_PRE_EXISTING:
1747+
# 1 System_Settings SELECT
1748+
# 4 lazy-load chain: findings[0].test / .engagement / .product / .test_type
1749+
# 1 candidates SELECT (with .only())
1750+
# 1 bulk UPDATE covering all retroactively marked findings
1751+
with self.assertNumQueries(7):
1752+
do_false_positive_history_batch(batch)
1753+
1754+
# All pre-existing findings must now be marked as FP.
1755+
for find in pre_existing:
1756+
self.assert_finding(find, false_p=True)
1757+
17221758
# -------------------------------------------------------------------- #
17231759
# Single-finding edit: retroactive reactivation (was dead code pre-fix) #
17241760
# -------------------------------------------------------------------- #

0 commit comments

Comments
 (0)