Redesign test framework: fix conftest, rewrite all tests, fix model index conflicts, fix black formatting

Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/9d256101-34b6-4861-a8cf-7f86f32b54d5

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-29 10:19:43 +00:00
parent f5368927ef
commit 5fa315c8e3
9 changed files with 431 additions and 551 deletions
+21 -61
View File
@@ -4,14 +4,15 @@ from sqlalchemy.orm import Session
class TestDomainModel:
"""Tests for the Domain model"""
"""Tests for the Domain ORM model."""
def test_create_domain(self, db_session: Session):
"""Test creating a domain in the database"""
domain = Domain(
name="example.com", description="Test domain", active=True, dmarc_policy="quarantine"
name="example.com",
description="Test domain",
active=True,
dmarc_policy="quarantine",
)
db_session.add(domain)
db_session.commit()
db_session.refresh(domain)
@@ -23,66 +24,44 @@ class TestDomainModel:
assert domain.dmarc_policy == "quarantine"
def test_domain_reports_relationship(self, db_session: Session):
"""Test the relationship between domains and DMARC reports"""
# Create a domain
domain = Domain(name="example.com", active=True)
db_session.add(domain)
db_session.commit()
# Create reports for the domain
report1 = DMARCReport(
report = DMARCReport(
domain_id=domain.id,
report_id="report1",
org_name="Google",
begin_date=1597449600,
end_date=1597535999,
source_email="noreply-dmarc-support@google.com",
source_email="noreply@google.com",
)
report2 = DMARCReport(
domain_id=domain.id,
report_id="report2",
org_name="Microsoft",
begin_date=1597536000,
end_date=1597622399,
source_email="dmarc@microsoft.com",
)
db_session.add_all([report1, report2])
db_session.add(report)
db_session.commit()
# Query the domain and check its reports
domain = db_session.query(Domain).filter_by(name="example.com").first()
assert domain is not None
assert len(domain.reports) == 2
assert domain.reports[0].report_id in ["report1", "report2"]
assert domain.reports[1].report_id in ["report1", "report2"]
fetched = db_session.query(Domain).filter_by(name="example.com").first()
assert fetched is not None
assert len(fetched.reports) == 1
assert fetched.reports[0].report_id == "report1"
class TestDMARCReportModel:
"""Tests for the DMARCReport model"""
"""Tests for the DMARCReport ORM model."""
def test_create_report(self, db_session: Session):
"""Test creating a DMARC report in the database"""
# Create a domain first
domain = Domain(name="example.com", active=True)
db_session.add(domain)
db_session.commit()
# Create a report
report = DMARCReport(
domain_id=domain.id,
report_id="123456789",
org_name="Google",
begin_date=1597449600,
end_date=1597535999,
source_email="noreply-dmarc-support@google.com",
source_email="noreply@google.com",
policy="none",
adkim="r",
aspf="r",
percentage=100,
)
db_session.add(report)
db_session.commit()
db_session.refresh(report)
@@ -91,12 +70,9 @@ class TestDMARCReportModel:
assert report.domain_id == domain.id
assert report.report_id == "123456789"
assert report.org_name == "Google"
assert report.begin_date == 1597449600
assert report.policy == "none"
def test_report_records_relationship(self, db_session: Session):
"""Test the relationship between reports and records"""
# Create domain and report
domain = Domain(name="example.com", active=True)
db_session.add(domain)
db_session.commit()
@@ -107,13 +83,12 @@ class TestDMARCReportModel:
org_name="Google",
begin_date=1597449600,
end_date=1597535999,
source_email="noreply-dmarc-support@google.com",
source_email="noreply@google.com",
)
db_session.add(report)
db_session.commit()
# Create records for the report
record1 = ReportRecord(
record = ReportRecord(
report_id=report.id,
source_ip="203.0.113.1",
count=2,
@@ -121,26 +96,11 @@ class TestDMARCReportModel:
dkim="pass",
spf="fail",
header_from="example.com",
envelope_from=None,
)
record2 = ReportRecord(
report_id=report.id,
source_ip="203.0.113.2",
count=5,
disposition="none",
dkim="pass",
spf="pass",
header_from="example.com",
envelope_from=None,
)
db_session.add_all([record1, record2])
db_session.add(record)
db_session.commit()
# Query the report and check its records
report = db_session.query(DMARCReport).filter_by(report_id="123456789").first()
assert report is not None
assert len(report.records) == 2
assert report.records[0].source_ip in ["203.0.113.1", "203.0.113.2"]
assert report.records[1].source_ip in ["203.0.113.1", "203.0.113.2"]
fetched = db_session.query(DMARCReport).filter_by(report_id="123456789").first()
assert fetched is not None
assert len(fetched.records) == 1
assert fetched.records[0].source_ip == "203.0.113.1"