Skip to content

Commit

Permalink
Merged checking of the assumed continuing attribute of Assertions int…
Browse files Browse the repository at this point in the history
…o the correctDBAssertion method in utils.
  • Loading branch information
michelleblom committed May 19, 2024
1 parent 353258d commit c63430d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra


import static au.org.democracydevelopers.raireservice.testUtils.correctDBAssertionData;
import static au.org.democracydevelopers.raireservice.testUtils.correctAssumedContinuing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -127,7 +126,7 @@ void NEBassertionRetrievalWorks() {
Assertion assertion = assertionRepository.findByContestName(oneNEBAssertionContest).getFirst();
assertInstanceOf(NEBAssertion.class, assertion);
assertTrue(correctDBAssertionData(320, 0.32, 1.1, "Alice",
"Bob", assertion));
"Bob", List.of(), assertion));
}


Expand All @@ -141,8 +140,7 @@ void NENassertionRetrievalWorks() {
Assertion assertion = assertionRepository.findByContestName(oneNENAssertionContest).getFirst();

assertTrue(correctDBAssertionData(20, 0.4, 2.5, "Alice",
"Bob", assertion));
assertTrue(correctAssumedContinuing(Arrays.stream(aliceChuanBob).toList(), assertion));
"Bob", Arrays.stream(aliceChuanBob).toList(), assertion));
assertInstanceOf(NENAssertion.class, assertion);
}

Expand Down Expand Up @@ -197,7 +195,7 @@ void testGuideToRaireExample1() throws RaireServiceException {
NEBAssertion nebAssertion = (NEBAssertion) nebMaybeAssertion.get();

assertTrue(correctDBAssertionData(8, 8 / 27.0, 27 / 8.0,
"Chuan", "Bob", nebAssertion));
"Chuan", "Bob", List.of(), nebAssertion));
}

/**
Expand Down Expand Up @@ -260,16 +258,15 @@ public void simpleContestSingleCounty() throws RaireServiceException {
assertTrue(nebMaybeAssertion.isPresent());
NEBAssertion nebAssertion = (NEBAssertion) nebMaybeAssertion.get();
assertTrue(correctDBAssertionData(1, 0.2, 5, "Alice",
"Chuan", nebAssertion));
"Chuan", List.of(), nebAssertion));

// There should be one NEN assertion: Alice > Bob if only {Alice,Bob} remain.
Optional<Assertion> nenMaybeAssertion = assertions.stream()
.filter(a -> a instanceof NENAssertion).findFirst();
assertTrue(nenMaybeAssertion.isPresent());
NENAssertion nenAssertion = (NENAssertion) nenMaybeAssertion.get();
assertTrue(correctDBAssertionData(1, 0.2, 5, "Alice",
"Bob", nenAssertion));
assertTrue(correctAssumedContinuing(List.of("Bob","Alice"), nenAssertion));
"Bob", List.of("Bob","Alice"), nenAssertion));
}

/**
Expand All @@ -284,7 +281,8 @@ public void simpleContestCrossCounty() throws RaireServiceException {
RaireResultOrError response = generateAssertionsService.generateAssertions(request);
assertNotNull(response.Ok);
assertNull(response.Err);
assertTrue(StringUtils.containsIgnoreCase(request.candidates.get(response.Ok.winner), "Alice"));
assertTrue(StringUtils.containsIgnoreCase(request.candidates.get(response.Ok.winner),
"Alice"));

// Test persistence of assertions generated by raire-java.
generateAssertionsService.persistAssertions(response.Ok, request);
Expand All @@ -298,15 +296,15 @@ public void simpleContestCrossCounty() throws RaireServiceException {
assertTrue(nebMaybeAssertion.isPresent());
NEBAssertion nebAssertion = (NEBAssertion) nebMaybeAssertion.get();
assertTrue(correctDBAssertionData(1, 0.2, 5, "Alice",
"Chuan", nebAssertion));
"Chuan", List.of(), nebAssertion));

// There should be one NEN assertion: Alice > Bob if only {Alice,Bob} remain.
Optional<Assertion> nenMaybeAssertion = assertions.stream().filter(a -> a instanceof NENAssertion).findFirst();
assertTrue(nenMaybeAssertion.isPresent());
Optional<Assertion> nenMaybeAssertion = assertions.stream().filter(
a -> a instanceof NENAssertion).findFirst(); assertTrue(nenMaybeAssertion.isPresent());
NENAssertion nenAssertion = (NENAssertion) nenMaybeAssertion.get();
assertTrue(correctDBAssertionData(1, 0.2, 5, "Alice",
"Bob", nenAssertion));
assertTrue(correctAssumedContinuing(List.of("Bob","Alice"), nenAssertion));
"Bob", List.of("Bob","Alice"), nenAssertion));


}

Expand All @@ -329,7 +327,8 @@ public void simpleContestSingleCountyDoubleBallots() throws RaireServiceExceptio
RaireResultOrError response = generateAssertionsService.generateAssertions(request);
assertNotNull(response.Ok);
assertNull(response.Err);
assertTrue(StringUtils.containsIgnoreCase(request.candidates.get(response.Ok.winner), "Alice"));
assertTrue(StringUtils.containsIgnoreCase(request.candidates.get(response.Ok.winner),
"Alice"));

// Test persistence of assertions generated by raire-java.
generateAssertionsService.persistAssertions(response.Ok, request);
Expand All @@ -342,16 +341,15 @@ public void simpleContestSingleCountyDoubleBallots() throws RaireServiceExceptio
assertTrue(nebMaybeAssertion.isPresent());
NEBAssertion nebAssertion = (NEBAssertion) nebMaybeAssertion.get();
assertTrue(correctDBAssertionData(1, 0.1, 10, "Alice",
"Chuan", nebAssertion));
"Chuan", List.of(), nebAssertion));

// There should be one NEN assertion: Alice > Bob if only {Alice,Bob} remain.
Optional<Assertion> nenMaybeAssertion = assertions.stream()
.filter(a -> a instanceof NENAssertion).findFirst();
assertTrue(nenMaybeAssertion.isPresent());
NENAssertion nenAssertion = (NENAssertion) nenMaybeAssertion.get();
assertTrue(correctDBAssertionData(1, 0.1, 10, "Alice",
"Bob", nenAssertion));
assertTrue(correctAssumedContinuing(List.of("Bob","Alice"), nenAssertion));
"Bob", List.of("Bob","Alice"), nenAssertion));
}

/**
Expand Down Expand Up @@ -714,7 +712,7 @@ void checkGuideToRaireExample2Assertions(List<Assertion> assertions){
assertTrue(nebMaybeAssertion.isPresent());
NEBAssertion nebAssertion = (NEBAssertion) nebMaybeAssertion.get();
assertTrue(correctDBAssertionData(10, 10 / 41.0, 4.1,
"Chuan","Alice", nebAssertion));
"Chuan","Alice", List.of(), nebAssertion));

// There should be one NEN assertion: Chuan > Bob if only {Chuan,Bob} remain.
// Margin is 9,000, but data is divided by 1000, so 9. Difficulty is 41/9 = 4.5555...,
Expand All @@ -725,8 +723,7 @@ void checkGuideToRaireExample2Assertions(List<Assertion> assertions){
assertTrue(nenMaybeAssertion.isPresent());
NENAssertion nenAssertion = (NENAssertion) nenMaybeAssertion.get();
assertTrue(correctDBAssertionData(9, 9/41.0, 41.0/9,
"Chuan", "Bob", nenAssertion));
assertTrue(correctAssumedContinuing(List.of("Chuan","Bob"), nenAssertion));
"Chuan", "Bob", List.of("Chuan","Bob"), nenAssertion));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,19 @@ public static boolean correctAPIAssertionData(String type, int margin, double di
* @param winner the expected winner
* @param loser the expected loser
* @param assertion the assertion to be checked (either as an Assertion or as json)
* @param assumedContinuing the list of candidate names expected to be in the
* 'assumed continuing' field.
* @return true if the assertion's data match all the expected values.
*/
public static boolean correctDBAssertionData(int margin, double dilutedMargin, double difficulty,
String winner, String loser, Assertion assertion) {
String winner, String loser, List<String> assumedContinuing, Assertion assertion) {

return margin == assertion.getMargin()
&& doubleComparator.compare(difficulty, assertion.getDifficulty()) == 0
&& doubleComparator.compare(dilutedMargin, assertion.getDilutedMargin()) == 0
&& loser.equals(assertion.getLoser())
&& winner.equals(assertion.getWinner());
&& winner.equals(assertion.getWinner())
&& setsNoDupesEqual(assertion.getAssumedContinuing(), assumedContinuing);
}

/**
Expand Down Expand Up @@ -231,7 +234,7 @@ public static boolean correctDBAssertionData(long id, int margin, double diluted
int otherCount, BigDecimal currentRisk, String contestName, Assertion assertion){

boolean test = correctDBAssertionData(margin, dilutedMargin, difficulty, winner,
loser, assertion);
loser, assumedContinuing, assertion);

return test && assertion.getEstimatedSamplesToAudit() == estimatedSamplesToAudit &&
assertion.getOptimisticSamplesToAudit() == optimisticSamplesToAudit &&
Expand All @@ -241,24 +244,11 @@ public static boolean correctDBAssertionData(long id, int margin, double diluted
assertion.getTwoVoteOverCount() == twoVoteOverCount &&
assertion.getOtherCount() == otherCount &&
assertion.getCurrentRisk().compareTo(currentRisk) == 0 &&
correctAssumedContinuing(assumedContinuing, assertion) &&
assertion.getCvrDiscrepancy().equals(cvrDiscrepancies) &&
assertion.getId() == id &&
assertion.getContestName().equals(contestName);
}


/**
* Utility to check that the expected assumedContinuing list matches the one in the assertion,
* ignoring order.
* @param expectedNames the list of candidate names expected to be in the 'assumed continuing' field.
* @param assertion the assertion to be checked.
* @return true if the NEN assertion's 'assumed continuing' list matches expectedNames, ignoring order.
*/
public static boolean correctAssumedContinuing(List<String> expectedNames, Assertion assertion) {
return setsNoDupesEqual(assertion.getAssumedContinuing(), expectedNames);
}

/**
* Check that the max difficulty of a list of assertions matches the expected difficulty.
* @param expectedDifficulty the expected difficulty, generated by raire-java and raire-rs directly.
Expand Down

0 comments on commit c63430d

Please sign in to comment.