Spaces:
Build error
fix(tests): test_phase34_forensics.py fixture mismatches across all 5 test classes
Browse filesRoot cause: five tests were written against a plausible-sounding but
incorrect assumption about a function's parameter names, output keys,
or a minimum-sample threshold, and had never been run against the real
implementation until now. Each was verified individually by reading the
production code first (not guessed), then checking whether any other
call site in the repo depends on the incorrect name -- zero hits in
every case, confirming these are test-authoring drift, not production
bugs:
TestBenfordsAnalyzer:
- test_uniform_data_low_chi2 / test_all_same_digit_high_anomaly used
17 and 14 values; analyze() requires len(values) >= 20 (a named
minimum_required constant). Expanded both fixtures to 22 and 20
values respectively, verified the resulting chi2_statistic values
land on the correct side of the 15.507 significance threshold
before writing them into the test (2.44 for the uniform case, 417
for the all-9s case).
TestGhostCompanyDetector:
- Fixtures were keyed employee_count/registered_capital_crore/website;
score_company() reads reg_date/first_contract/cag_mentions/
sebi_mentions/directors/capital/total_value. Rebuilt both fixtures
against the real field names and verified the ghost fixture scores
100 (flagged) and the real-company fixture scores 0 (not flagged)
before writing them in.
- test_score_has_required_keys checked for "name"/"id";
score_company() returns "company_name"/"company_id".
TestShadowDirectorDetector:
- test_detect_address_reuse_groups used "registered_address" and no
"name" key; detect_address_reuse() reads "address" (the alias its
own real Neo4j fetcher produces) and accesses c["name"] directly
(not .get()), which would KeyError once the address key was fixed
without also adding name. Fixed both.
TestCircularOwnershipDetector:
- Fixtures used "from"/"to"; detect_cycles() reads "src"/"tgt".
TestCaseStore:
- Fixture called monkeypatch.setattr(cs_module, "CASE_FILE", ...) --
the real constant is CASE_STORE_FILE. This is why all 4 tests in
this class ERRORED rather than failed: monkeypatch.setattr raises
AttributeError by default when the target attribute doesn't exist.
- test_save_and_retrieve_case called save_case(risk_score=18);
save_case()'s real signature is (entity_id, entity_name, findings,
outcome, reasoning_path) -- outcome and reasoning_path are both
required, no risk_score parameter exists at all.
- test_find_similar_returns_list called find_similar(top_k=5);
real keyword is limit.
Note on what this is, precisely: none of these were production bugs --
every one of the five underlying functions is called correctly from its
real production call site elsewhere in the codebase (checked
individually: zero other references to CASE_FILE, risk_score=, top_k=,
registered_address, or bare from/to keys anywhere else in the repo).
Two negative-case tests in these same classes
(test_below_threshold_not_flagged, test_no_cycle_returns_empty) were
already passing before this fix -- for the wrong reason: with every
field silently mismatched, every input produced an empty result, so
'expect empty' tests passed by accident regardless of what they were
testing. Both are re-verified here to now pass because the underlying
logic correctly returns empty for these specific inputs, not because
the fixture is silently inert.
Verified: py_compile passes. All 19 tests in this file pass
individually (ran in isolation via pytest -v before this commit) and as
part of the full suite. Every fixture value was independently confirmed
against the real implementation via direct Python execution before
being written into the test file, not derived from the test's own
(previously incorrect) expectations.
- tests/test_phase34_forensics.py +81 -31
|
@@ -9,17 +9,32 @@ class TestBenfordsAnalyzer:
|
|
| 9 |
return BenfordsAnalyzer()
|
| 10 |
|
| 11 |
def test_uniform_data_low_chi2(self, ba):
|
| 12 |
-
"""Data with correct Benford distribution should have low chi2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
values = [1.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9,
|
| 14 |
-
1.01, 2.02, 3.03, 1.5, 2.5, 3.5, 1.9, 2.9
|
|
|
|
| 15 |
result = ba.analyze(values)
|
| 16 |
assert "chi2_statistic" in result
|
| 17 |
assert isinstance(result["chi2_statistic"], (int, float))
|
|
|
|
| 18 |
|
| 19 |
def test_all_same_digit_high_anomaly(self, ba):
|
| 20 |
-
"""All values starting with 9 is maximally non-Benford.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
values = [9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9,
|
| 22 |
-
90, 91, 92, 93, 94]
|
| 23 |
result = ba.analyze(values)
|
| 24 |
assert result["chi2_statistic"] > 15.5
|
| 25 |
|
|
@@ -47,21 +62,34 @@ class TestGhostCompanyDetector:
|
|
| 47 |
from ai.ghost_company import GhostCompanyDetector
|
| 48 |
return GhostCompanyDetector(driver=None)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
GHOST_COMPANY = {
|
| 51 |
-
"id":
|
| 52 |
-
"name":
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
REAL_COMPANY = {
|
| 59 |
-
"id":
|
| 60 |
-
"name":
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
def test_ghost_company_high_score(self, gcd):
|
|
@@ -73,9 +101,11 @@ class TestGhostCompanyDetector:
|
|
| 73 |
assert result.get("ghost_score", 100) < 40
|
| 74 |
|
| 75 |
def test_score_has_required_keys(self, gcd):
|
|
|
|
|
|
|
| 76 |
result = gcd.score_company(self.GHOST_COMPANY)
|
| 77 |
assert "ghost_score" in result
|
| 78 |
-
assert "
|
| 79 |
|
| 80 |
def test_empty_company_does_not_crash(self, gcd):
|
| 81 |
result = gcd.score_company({})
|
|
@@ -90,11 +120,16 @@ class TestShadowDirectorDetector:
|
|
| 90 |
return ShadowDirectorDetector(driver=None)
|
| 91 |
|
| 92 |
def test_detect_address_reuse_groups(self, sdd):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
companies = [
|
| 94 |
-
{"id": "c1", "
|
| 95 |
-
{"id": "c2", "
|
| 96 |
-
{"id": "c3", "
|
| 97 |
-
{"id": "c4", "
|
| 98 |
]
|
| 99 |
clusters = sdd.detect_address_reuse(companies)
|
| 100 |
assert len(clusters) >= 1
|
|
@@ -103,8 +138,8 @@ class TestShadowDirectorDetector:
|
|
| 103 |
|
| 104 |
def test_below_threshold_not_flagged(self, sdd):
|
| 105 |
companies = [
|
| 106 |
-
{"id": "c1", "
|
| 107 |
-
{"id": "c2", "
|
| 108 |
]
|
| 109 |
clusters = sdd.detect_address_reuse(companies)
|
| 110 |
assert len(clusters) == 0
|
|
@@ -122,18 +157,21 @@ class TestCircularOwnershipDetector:
|
|
| 122 |
return CircularOwnershipDetector(driver=None)
|
| 123 |
|
| 124 |
def test_detect_simple_cycle(self, cod):
|
|
|
|
|
|
|
|
|
|
| 125 |
edges = [
|
| 126 |
-
{"
|
| 127 |
-
{"
|
| 128 |
-
{"
|
| 129 |
]
|
| 130 |
cycles = cod.detect_cycles(ownership_edges=edges)
|
| 131 |
assert len(cycles) >= 1
|
| 132 |
|
| 133 |
def test_no_cycle_returns_empty(self, cod):
|
| 134 |
edges = [
|
| 135 |
-
{"
|
| 136 |
-
{"
|
| 137 |
]
|
| 138 |
cycles = cod.detect_cycles(ownership_edges=edges)
|
| 139 |
assert cycles == []
|
|
@@ -149,7 +187,12 @@ class TestCaseStore:
|
|
| 149 |
@pytest.fixture
|
| 150 |
def cs(self, tmp_path, monkeypatch):
|
| 151 |
from ai.case_memory import case_store as cs_module
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
str(tmp_path / "cases.json"))
|
| 154 |
from ai.case_memory.case_store import CaseStore
|
| 155 |
return CaseStore()
|
|
@@ -158,17 +201,24 @@ class TestCaseStore:
|
|
| 158 |
assert cs.get_case_count() == 0
|
| 159 |
|
| 160 |
def test_save_and_retrieve_case(self, cs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
cs.save_case(
|
| 162 |
entity_id="test_001",
|
| 163 |
entity_name="Test Entity",
|
| 164 |
findings=[{"type": "benfords_law_anomaly", "score": 18}],
|
| 165 |
-
|
|
|
|
| 166 |
)
|
| 167 |
assert cs.get_case_count() == 1
|
| 168 |
|
| 169 |
def test_find_similar_returns_list(self, cs):
|
|
|
|
| 170 |
findings = [{"type": "benfords_law_anomaly"}]
|
| 171 |
-
result = cs.find_similar(findings,
|
| 172 |
assert isinstance(result, list)
|
| 173 |
|
| 174 |
def test_record_false_positive_no_crash(self, cs):
|
|
|
|
| 9 |
return BenfordsAnalyzer()
|
| 10 |
|
| 11 |
def test_uniform_data_low_chi2(self, ba):
|
| 12 |
+
"""Data with correct Benford distribution should have low chi2.
|
| 13 |
+
|
| 14 |
+
analyze() requires len(values) >= 20 (a named, documented
|
| 15 |
+
minimum_required constant, not an incidental threshold) or it
|
| 16 |
+
returns status="insufficient_data" with no chi2_statistic key
|
| 17 |
+
at all. Fixture expanded from 17 to 22 values so this test
|
| 18 |
+
actually exercises the real analysis path instead of the
|
| 19 |
+
early-return path.
|
| 20 |
+
"""
|
| 21 |
values = [1.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9,
|
| 22 |
+
1.01, 2.02, 3.03, 1.5, 2.5, 3.5, 1.9, 2.9,
|
| 23 |
+
1.15, 1.25, 4.2, 12.3, 100.5]
|
| 24 |
result = ba.analyze(values)
|
| 25 |
assert "chi2_statistic" in result
|
| 26 |
assert isinstance(result["chi2_statistic"], (int, float))
|
| 27 |
+
assert result["chi2_statistic"] < 15.507
|
| 28 |
|
| 29 |
def test_all_same_digit_high_anomaly(self, ba):
|
| 30 |
+
"""All values starting with 9 is maximally non-Benford.
|
| 31 |
+
|
| 32 |
+
Fixture expanded from 14 to 20 values for the same reason as
|
| 33 |
+
test_uniform_data_low_chi2 above -- 14 values never reached
|
| 34 |
+
analyze()'s real analysis path at all.
|
| 35 |
+
"""
|
| 36 |
values = [9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9,
|
| 37 |
+
90, 91, 92, 93, 94, 95, 96, 97, 98, 900, 901]
|
| 38 |
result = ba.analyze(values)
|
| 39 |
assert result["chi2_statistic"] > 15.5
|
| 40 |
|
|
|
|
| 62 |
from ai.ghost_company import GhostCompanyDetector
|
| 63 |
return GhostCompanyDetector(driver=None)
|
| 64 |
|
| 65 |
+
# GhostCompanyDetector.score_company() reads reg_date, first_contract,
|
| 66 |
+
# cag_mentions, sebi_mentions, directors, capital, total_value --
|
| 67 |
+
# verified directly against ai/ghost_company.py, not the previous
|
| 68 |
+
# fixture's employee_count/registered_capital_crore/website, which
|
| 69 |
+
# score_company() never reads at all.
|
| 70 |
GHOST_COMPANY = {
|
| 71 |
+
"id": "co_ghost_001",
|
| 72 |
+
"name": "Phantom Services Pvt Ltd",
|
| 73 |
+
"reg_date": "2024-06-01",
|
| 74 |
+
"first_contract": "2024-07-01", # 30 days after registration
|
| 75 |
+
"capital": 2.0,
|
| 76 |
+
"directors": 1,
|
| 77 |
+
"cag_mentions": 0,
|
| 78 |
+
"sebi_mentions": 0,
|
| 79 |
+
"contract_count": 1,
|
| 80 |
+
"total_value": 50.0,
|
| 81 |
}
|
| 82 |
REAL_COMPANY = {
|
| 83 |
+
"id": "co_real_001",
|
| 84 |
+
"name": "Tata Consultancy Services",
|
| 85 |
+
"reg_date": "1968-01-01",
|
| 86 |
+
"first_contract": "1990-01-01",
|
| 87 |
+
"capital": 1000.0,
|
| 88 |
+
"directors": 12,
|
| 89 |
+
"cag_mentions": 3,
|
| 90 |
+
"sebi_mentions": 5,
|
| 91 |
+
"contract_count": 200,
|
| 92 |
+
"total_value": 5000.0,
|
| 93 |
}
|
| 94 |
|
| 95 |
def test_ghost_company_high_score(self, gcd):
|
|
|
|
| 101 |
assert result.get("ghost_score", 100) < 40
|
| 102 |
|
| 103 |
def test_score_has_required_keys(self, gcd):
|
| 104 |
+
"""score_company() returns company_id/company_name, not id/name --
|
| 105 |
+
verified directly against ai/ghost_company.py's return dict."""
|
| 106 |
result = gcd.score_company(self.GHOST_COMPANY)
|
| 107 |
assert "ghost_score" in result
|
| 108 |
+
assert "company_name" in result or "company_id" in result
|
| 109 |
|
| 110 |
def test_empty_company_does_not_crash(self, gcd):
|
| 111 |
result = gcd.score_company({})
|
|
|
|
| 120 |
return ShadowDirectorDetector(driver=None)
|
| 121 |
|
| 122 |
def test_detect_address_reuse_groups(self, sdd):
|
| 123 |
+
"""detect_address_reuse() reads co.get("address") (the alias its
|
| 124 |
+
own real Neo4j fetcher, _fetch_company_metadata(), produces) --
|
| 125 |
+
not "registered_address" -- and requires a "name" key to build
|
| 126 |
+
its output (accessed via c["name"], not c.get("name")), verified
|
| 127 |
+
directly against ai/shadow_director.py."""
|
| 128 |
companies = [
|
| 129 |
+
{"id": "c1", "name": "Shell One", "address": "123 Shell St, Mumbai"},
|
| 130 |
+
{"id": "c2", "name": "Shell Two", "address": "123 Shell St, Mumbai"},
|
| 131 |
+
{"id": "c3", "name": "Shell Three", "address": "123 Shell St, Mumbai"},
|
| 132 |
+
{"id": "c4", "name": "Real Co", "address": "456 Real Ave, Delhi"},
|
| 133 |
]
|
| 134 |
clusters = sdd.detect_address_reuse(companies)
|
| 135 |
assert len(clusters) >= 1
|
|
|
|
| 138 |
|
| 139 |
def test_below_threshold_not_flagged(self, sdd):
|
| 140 |
companies = [
|
| 141 |
+
{"id": "c1", "name": "A", "address": "Unique Addr 1 Long Enough"},
|
| 142 |
+
{"id": "c2", "name": "B", "address": "Unique Addr 2 Long Enough"},
|
| 143 |
]
|
| 144 |
clusters = sdd.detect_address_reuse(companies)
|
| 145 |
assert len(clusters) == 0
|
|
|
|
| 157 |
return CircularOwnershipDetector(driver=None)
|
| 158 |
|
| 159 |
def test_detect_simple_cycle(self, cod):
|
| 160 |
+
"""detect_cycles() reads edge["src"]/edge["tgt"] (with a fallback
|
| 161 |
+
to "source"/"target"), never "from"/"to" -- verified directly
|
| 162 |
+
against ai/circular_ownership.py."""
|
| 163 |
edges = [
|
| 164 |
+
{"src": "A", "tgt": "B"},
|
| 165 |
+
{"src": "B", "tgt": "C"},
|
| 166 |
+
{"src": "C", "tgt": "A"},
|
| 167 |
]
|
| 168 |
cycles = cod.detect_cycles(ownership_edges=edges)
|
| 169 |
assert len(cycles) >= 1
|
| 170 |
|
| 171 |
def test_no_cycle_returns_empty(self, cod):
|
| 172 |
edges = [
|
| 173 |
+
{"src": "A", "tgt": "B"},
|
| 174 |
+
{"src": "B", "tgt": "C"},
|
| 175 |
]
|
| 176 |
cycles = cod.detect_cycles(ownership_edges=edges)
|
| 177 |
assert cycles == []
|
|
|
|
| 187 |
@pytest.fixture
|
| 188 |
def cs(self, tmp_path, monkeypatch):
|
| 189 |
from ai.case_memory import case_store as cs_module
|
| 190 |
+
# The real module-level constant is CASE_STORE_FILE, not CASE_FILE
|
| 191 |
+
# -- monkeypatching a nonexistent attribute name raises
|
| 192 |
+
# AttributeError under pytest's default raising=True, which is
|
| 193 |
+
# exactly why all 4 tests in this class errored rather than
|
| 194 |
+
# failed. Verified directly against ai/case_memory/case_store.py.
|
| 195 |
+
monkeypatch.setattr(cs_module, "CASE_STORE_FILE",
|
| 196 |
str(tmp_path / "cases.json"))
|
| 197 |
from ai.case_memory.case_store import CaseStore
|
| 198 |
return CaseStore()
|
|
|
|
| 201 |
assert cs.get_case_count() == 0
|
| 202 |
|
| 203 |
def test_save_and_retrieve_case(self, cs):
|
| 204 |
+
"""save_case()'s real signature is (entity_id, entity_name,
|
| 205 |
+
findings, outcome, reasoning_path) -- outcome and reasoning_path
|
| 206 |
+
are both required, with no defaults, and there is no risk_score
|
| 207 |
+
parameter at all. Verified via inspect.signature() against
|
| 208 |
+
ai/case_memory/case_store.py."""
|
| 209 |
cs.save_case(
|
| 210 |
entity_id="test_001",
|
| 211 |
entity_name="Test Entity",
|
| 212 |
findings=[{"type": "benfords_law_anomaly", "score": 18}],
|
| 213 |
+
outcome="confirmed",
|
| 214 |
+
reasoning_path=["benfords_law_anomaly -> confirmed"],
|
| 215 |
)
|
| 216 |
assert cs.get_case_count() == 1
|
| 217 |
|
| 218 |
def test_find_similar_returns_list(self, cs):
|
| 219 |
+
"""find_similar()'s real keyword is limit, not top_k."""
|
| 220 |
findings = [{"type": "benfords_law_anomaly"}]
|
| 221 |
+
result = cs.find_similar(findings, limit=5)
|
| 222 |
assert isinstance(result, list)
|
| 223 |
|
| 224 |
def test_record_false_positive_no_crash(self, cs):
|