Add report deduplication and single-report deletion

Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/37d68c2c-7cc8-45e4-bac3-e2e6f1611c6a

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-29 18:28:52 +00:00
parent c251a58340
commit 53d543f49c
4 changed files with 269 additions and 42 deletions
+64
View File
@@ -77,3 +77,67 @@ class TestReportStore:
def test_delete_nonexistent_domain(self):
store = ReportStore.get_instance()
assert store.delete_domain_with_cleanup("nope.com") is False
def test_has_report_returns_true_for_existing(self):
store = ReportStore.get_instance()
store.add_report(_sample_report("test.com"))
assert store.has_report("test.com", "rpt-001") is True
def test_has_report_returns_false_for_missing_report_id(self):
store = ReportStore.get_instance()
store.add_report(_sample_report("test.com"))
assert store.has_report("test.com", "rpt-999") is False
def test_has_report_returns_false_for_unknown_domain(self):
store = ReportStore.get_instance()
assert store.has_report("nobody.com", "rpt-001") is False
def test_delete_report_removes_report_and_updates_stats(self):
store = ReportStore.get_instance()
store.add_report(_sample_report("test.com"))
result = store.delete_report("test.com", "rpt-001")
assert result is True
# Domain should be gone entirely when no reports remain
assert "test.com" not in store.get_domains()
def test_delete_report_with_remaining_reports_recomputes_stats(self):
store = ReportStore.get_instance()
report_a = _sample_report("test.com")
report_a["report_id"] = "rpt-001"
report_b = _sample_report("test.com")
report_b["report_id"] = "rpt-002"
report_b["summary"] = {"total_count": 3, "passed_count": 1, "failed_count": 2}
store.add_report(report_a)
store.add_report(report_b)
assert store.get_domain_summary("test.com")["reports_processed"] == 2
result = store.delete_report("test.com", "rpt-001")
assert result is True
summary = store.get_domain_summary("test.com")
assert summary["reports_processed"] == 1
# Stats should now reflect only report_b
assert summary["total_count"] == 3
assert summary["passed_count"] == 1
def test_delete_report_nonexistent_returns_false(self):
store = ReportStore.get_instance()
assert store.delete_report("test.com", "rpt-999") is False
def test_delete_report_unknown_domain_returns_false(self):
store = ReportStore.get_instance()
assert store.delete_report("nobody.com", "rpt-001") is False
def test_recompute_stats_after_add(self):
"""_recompute_domain_stats is called on add; compliance_rate must be correct."""
store = ReportStore.get_instance()
report = _sample_report("test.com")
report["summary"] = {"total_count": 10, "passed_count": 8, "failed_count": 2}
store.add_report(report)
summary = store.get_domain_summary("test.com")
assert summary["compliance_rate"] == 80.0
+63
View File
@@ -69,3 +69,66 @@ def test_upload_and_get_domain_summary(client: TestClient):
assert data["domain"] == "example.com"
assert data["total_count"] == 2
assert data["reports_processed"] == 1
def test_duplicate_upload_returns_409(client: TestClient):
"""Uploading the same report twice returns 409 Conflict."""
zip_bytes = _make_zip(SAMPLE_XML)
first = client.post(
"/api/v1/reports/upload",
files={"file": ("report.zip", zip_bytes, "application/zip")},
)
assert first.status_code == 200
second = client.post(
"/api/v1/reports/upload",
files={"file": ("report.zip", zip_bytes, "application/zip")},
)
assert second.status_code == 409
assert "already been uploaded" in second.json()["detail"].lower()
def test_delete_report_success(client: TestClient):
"""Deleting an existing report returns 200 and removes it from the store."""
zip_bytes = _make_zip(SAMPLE_XML)
client.post(
"/api/v1/reports/upload",
files={"file": ("report.zip", zip_bytes, "application/zip")},
)
# Confirm the domain exists first
assert client.get("/api/v1/reports/domain/example.com/summary").status_code == 200
# Delete the report (report_id comes from SAMPLE_XML: "123456789")
response = client.delete("/api/v1/reports/domain/example.com/reports/123456789")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
# Domain should be gone now
assert client.get("/api/v1/reports/domain/example.com/summary").status_code == 404
def test_delete_nonexistent_report_returns_404(client: TestClient):
"""Deleting a report that does not exist returns 404."""
response = client.delete("/api/v1/reports/domain/example.com/reports/no-such-id")
assert response.status_code == 404
def test_upload_after_delete_succeeds(client: TestClient):
"""After deleting a report, the same report can be uploaded again."""
zip_bytes = _make_zip(SAMPLE_XML)
client.post(
"/api/v1/reports/upload",
files={"file": ("report.zip", zip_bytes, "application/zip")},
)
client.delete("/api/v1/reports/domain/example.com/reports/123456789")
response = client.post(
"/api/v1/reports/upload",
files={"file": ("report.zip", zip_bytes, "application/zip")},
)
assert response.status_code == 200
assert response.json()["success"] is True