Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main' into 167-add-…
Browse files Browse the repository at this point in the history
…test-class-DD-version-of-assertionqueries

# Conflicts:
#	server/eclipse-project/src/test/resources/SQL/simple-assertions.sql
  • Loading branch information
vteague committed Aug 5, 2024
2 parents 4ced2ee + 9f45843 commit 0dcb4ca
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public class IRVComparisonAuditTests extends AssertionTests {
@Mock
private ContestResult simpleContest3;

/**
* Mock of a ContestResult for the contest 'Simple Contest 4'.
*/
@Mock
private ContestResult simpleContest4;

/**
* Mock of a CVRAuditInfo object, matched to a CVR with id 1.
*/
Expand Down Expand Up @@ -171,6 +177,9 @@ public void initContestResultMocks() {
when(simpleContest3.getContestName()).thenReturn("Simple Contest 3");
when(simpleContest3.getDilutedMargin()).thenReturn(BigDecimal.valueOf(0.02));

when(simpleContest4.getContestName()).thenReturn("Simple Contest 4");
when(simpleContest4.getDilutedMargin()).thenReturn(BigDecimal.valueOf(0.07));

when(auditInfo.id()).thenReturn(1L);
when(auditInfo.cvr()).thenReturn(cvr);
when(auditInfo.acvr()).thenReturn(auditedCvr);
Expand Down Expand Up @@ -637,11 +646,16 @@ public void testComputeRecordRemoveDiscrepancyCVR_ABCD_ACVR_BACD(final RecordTyp
ca.addContestCVRIds(List.of(1L));
ca.recordDiscrepancy(auditInfo, 2);

// Note that only the maximum discrepancy across assertions is recorded in the base level
// classes discrepancy counts (for reporting purposes). Each assertion's discrepancies are
// taken into account for risk measurement.
checkDiscrepancies(ca, 0, 1, 0, 0, 0);

riskMeasurementCheck(ca, List.of(Pair.make(1000, 0.214),
Pair.make(1500, 0.019)));

// Note that all discrepancies associated with this CVR/ballot are removed, across all
// assertions in the audit (i.e., not just where it represented a discrepancy of 2).
ca.removeDiscrepancy(auditInfo, 2);
checkDiscrepancies(ca, 0, 0, 0, 0, 0);

Expand Down Expand Up @@ -735,11 +749,16 @@ public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD(final RecordTyp
ca.addContestCVRIds(List.of(1L));
ca.recordDiscrepancy(auditInfo, 1);

// Note that only the maximum discrepancy across assertions is recorded in the base level
// classes discrepancy counts (for reporting purposes). Each assertion's discrepancies are
// taken into account for risk measurement.
checkDiscrepancies(ca, 1, 0, 0, 0, 0);

riskMeasurementCheck(ca, List.of(Pair.make(10, 1.0),
Pair.make(100, 0.894), Pair.make(200, 0.415)));

// Note that all discrepancies associated with this CVR/ballot are removed, across all
// assertions in the audit (i.e., not just where it represented a discrepancy of 1).
ca.removeDiscrepancy(auditInfo, 1);
checkDiscrepancies(ca, 0, 0, 0, 0, 0);

Expand All @@ -753,8 +772,8 @@ public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD(final RecordTyp
* Discrepancy computation and recording for 'Mixed Contest 2' with CVR "B","A","C","D" and
* audited ballot "A","B","C","D". The maximum discrepancy is -1. Also checks risk measurement
* before and after the removal of the recorded discrepancies against the ballot/CVR pair. We
* use Equation 9 in Stark's Super Simple Simultaneous Single-Ballot Risk Limiting Audits to compute the expected
* risk values.
* use Equation 9 in Stark's Super Simple Simultaneous Single-Ballot Risk Limiting Audits to
* compute the expected risk values.
*/
@Test(dataProvider = "AuditedRecordTypes", dataProviderClass = AssertionTests.class)
public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD_mixed2(final RecordType auditedType){
Expand All @@ -773,11 +792,16 @@ public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD_mixed2(final Re
ca.addContestCVRIds(List.of(1L));
ca.recordDiscrepancy(auditInfo, -1);

// Note that only the maximum discrepancy across assertions is recorded in the base level
// classes discrepancy counts (for reporting purposes). Each assertion's discrepancies are
// taken into account for risk measurement.
checkDiscrepancies(ca, 0, 0, 1, 0, 0);

riskMeasurementCheck(ca, List.of(Pair.make(10, 0.412),
Pair.make(50, 0.151), Pair.make(100, 0.045)));

// Note that all discrepancies associated with this CVR/ballot are removed, across all
// assertions in the audit (i.e., not just where it represented a discrepancy of -1).
ca.removeDiscrepancy(auditInfo, -1);
checkDiscrepancies(ca, 0, 0, 0, 0, 0);

Expand All @@ -791,8 +815,8 @@ public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD_mixed2(final Re
* Discrepancy computation and recording for 'Simple Contest 3' with CVR "B","A","C","D" and
* audited ballot "A","B","C","D". The maximum discrepancy is -2. Also checks risk measurement
* before and after the removal of the recorded discrepancies against the ballot/CVR pair. We
* use Equation 9 in Stark's Super Simple Simultaneous Single-Ballot Risk Limiting Audits to compute the expected
* risk values.
* use Equation 9 in Stark's Super Simple Simultaneous Single-Ballot Risk Limiting Audits to
* compute the expected risk values. Simple Contest 3 contains one NEB assertion.
*/
@Test(dataProvider = "AuditedRecordTypes", dataProviderClass = AssertionTests.class)
public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD_simple3(final RecordType auditedType){
Expand Down Expand Up @@ -825,6 +849,44 @@ public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD_simple3(final R
ca.riskMeasurement().doubleValue()), 0);
}

/**
* Discrepancy computation and recording for 'Simple Contest 4' with blank CVR vote and
* audited ballot "A","B","C","D". The maximum discrepancy is 0. Also checks risk measurement
* before and after the removal of the recorded discrepancy against the ballot/CVR pair. We
* use Equation 9 in Stark's Super Simple Simultaneous Single-Ballot Risk Limiting Audits to
* compute the expected risk values. Simple Contest 4 contains one NEN assertion.
*/
@Test(dataProvider = "AuditedRecordTypes", dataProviderClass = AssertionTests.class)
public void testComputeRecordRemoveDiscrepancyCVR_BACD_ACVR_ABCD_simple4(RecordType auditedType){
log(LOGGER, String.format("testComputeRecordDiscrepancyCVR_BACD_ACVR_ABCD_simple4[%s]", auditedType));
resetMocks(blank, ABCD, RecordType.UPLOADED, ConsensusValue.YES, auditedType, "Simple Contest 4");
IRVComparisonAudit ca = createIRVComparisonAuditSimple4();

final OptionalInt d = ca.computeDiscrepancy(cvr, auditedCvr);
assert(d.isPresent());
assertEquals(0, d.getAsInt());

// Note that computeDiscrepancy() does not update internal discrepancy counts, only
// recordDiscrepancy() and removeDiscrepancy() do.
checkDiscrepancies(ca, 0, 0, 0, 0, 0);

ca.addContestCVRIds(List.of(1L));
ca.recordDiscrepancy(auditInfo, 0);

checkDiscrepancies(ca, 0, 0, 0, 0, 1);

riskMeasurementCheck(ca, List.of(Pair.make(10, 0.710),
Pair.make(50, 0.180), Pair.make(100, 0.033)));

ca.removeDiscrepancy(auditInfo, 0);
checkDiscrepancies(ca, 0, 0, 0, 0, 0);

// ca.riskMeasurement() at this point, where the audited sample count is 100, should yield
// a risk of 0.033.
Assert.assertEquals(testUtils.doubleComparator.compare(0.033,
ca.riskMeasurement().doubleValue()), 0);
}

/**
* Discrepancy computation for 'Mixed Contest' with CVR "A" and audited ballot "B". The maximum
* discrepancy is 2.
Expand Down Expand Up @@ -905,6 +967,9 @@ public void testComputeRecordRemoveDiscrepancyCVR_blank_ACVR_ABCD(final RecordTy
ca.addContestCVRIds(List.of(1L));
ca.recordDiscrepancy(auditInfo, 0);

// Note that only the maximum discrepancy across assertions is recorded in the base level
// classes discrepancy counts (for reporting purposes). Each assertion's discrepancies are
// taken into account for risk measurement.
checkDiscrepancies(ca, 0, 0, 0, 0, 1);

riskMeasurementCheck(ca, List.of(Pair.make(10, 0.926),
Expand Down Expand Up @@ -1028,6 +1093,23 @@ private IRVComparisonAudit createIRVComparisonAuditSimple3(){
return ca;
}

/**
* Create and return an IRVComparisonAudit for the contest 'Simple Contest 4'.
* @return IRVComparisonAudit for the contest 'Simple Contest $'.
*/
private IRVComparisonAudit createIRVComparisonAuditSimple4(){
IRVComparisonAudit ca = new IRVComparisonAudit(simpleContest4,
AssertionTests.riskLimit5, AuditReason.COUNTY_WIDE_CONTEST);

checkIRVComparisonAudit(ca, AssertionTests.riskLimit5, AuditReason.COUNTY_WIDE_CONTEST,
AuditStatus.NOT_STARTED, 0.07);

final List<Assertion> assertions = ca.getAssertions();
assertEquals(1, assertions.size());

return ca;
}

/**
* A method to check that the given IRVComparisonAudit has been initialised properly.
* @param ca IRVComparisonAudit to check.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ INSERT INTO assertion_assumed_continuing values (30, 'C');
INSERT INTO contest (county_id, id, version, description, name, sequence_number, votes_allowed, winners_allowed) VALUES (4,11,0,'IRV','Simple Contest 3',11,4,1);
INSERT INTO assertion (assertion_type, contest_name, difficulty, diluted_margin, loser, margin, current_risk, estimated_samples_to_audit, one_vote_over_count, one_vote_under_count, optimistic_samples_to_audit, other_count, two_vote_over_count, two_vote_under_count, version, winner) values ('NEB', 'Simple Contest 3', 100, 0.02, 'B', 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'A');


INSERT INTO contest (county_id, id, version, description, name, sequence_number, votes_allowed, winners_allowed) VALUES (4,12,0,'IRV','Simple Contest 4',12,4,1);
INSERT INTO assertion (assertion_type, contest_name, difficulty, diluted_margin, loser, margin, current_risk, estimated_samples_to_audit, one_vote_over_count, one_vote_under_count, optimistic_samples_to_audit, other_count, two_vote_over_count, two_vote_under_count, version, winner) values ('NEN', 'Simple Contest 4', 200, 0.07, 'B', 200, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'F');
INSERT INTO assertion_assumed_continuing values (32, 'A');
INSERT INTO assertion_assumed_continuing values (32, 'B');
INSERT INTO assertion_assumed_continuing values (32, 'F');

INSERT INTO contest (county_id, id, version, description, name, sequence_number, votes_allowed, winners_allowed) VALUES (4,12,0,'IRV','Mixed Assertions With Discrepancies',12,4,1);
INSERT INTO assertion (assertion_type, contest_name, difficulty, diluted_margin, loser, margin, current_risk, estimated_samples_to_audit, one_vote_over_count, one_vote_under_count, optimistic_samples_to_audit, other_count, two_vote_over_count, two_vote_under_count, version, winner) values ('NEB', 'Mixed Assertions With Discrepancies', 159, 0.0159, 'C', 159, 1, 0, 0, 0, 0, 0, 0, 2, 0, 'B');
INSERT INTO assertion_discrepancies (id, discrepancy, cvr_id) values (32, -2, 1);
Expand Down

0 comments on commit 0dcb4ca

Please sign in to comment.