Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

79 complete api tests for all 3 endpoints #93

Merged
merged 22 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
af856db
Move tied winners into wicked tests.
vteague May 19, 2024
f2fe6e4
Wicked test cases with: tied winners, time out checking winners, time…
vteague May 19, 2024
f6712bf
Wicked test cases with: tied winners, time out checking winners, time…
vteague May 19, 2024
e804119
Added API test for WRONG_CANDIDATES.
vteague May 19, 2024
5b5cffb
API bad request tests for get-assertions-csv.
vteague May 19, 2024
5a9d31e
Tests for wrong candidates for all relevant endpoints, including get-…
vteague May 20, 2024
68770ce
Merge remote-tracking branch 'origin/main' into 79-complete-api-tests…
vteague May 21, 2024
5aa1861
Merge remote-tracking branch 'origin/main' into 79-complete-api-tests…
vteague May 21, 2024
8585848
Merge main.
vteague May 21, 2024
8551ff7
Get Assertions tests complete and passing, for both JSON and CSV.
vteague May 21, 2024
37ac6b4
Get Assertions tests complete and passing, for both JSON and CSV.
vteague May 21, 2024
90829fe
All NSW contests tested for winner and difficulty via API: GenerateAs…
vteague May 22, 2024
45b87e6
Clean up NSW tests.
vteague May 22, 2024
1781f95
Add known examples, API versions.
vteague May 22, 2024
62861a8
Remove GSON.
vteague May 23, 2024
d7f34cb
Guide to Raire Part 2 Example 2 in known tests.
vteague May 23, 2024
ab8d670
Notes about assertion regeneration (and the incompleteness of tests f…
vteague May 23, 2024
99418f4
Removing unused dependencies and config file.
vteague May 23, 2024
e1ed203
Removing unused dependencies and config file.
vteague May 23, 2024
8489ec9
BigDecimals sorted out; all tests passing.
vteague May 24, 2024
7cedc91
Wicked tests for GenerateAssertionsAPI: tied winners, timeout checkin…
vteague May 24, 2024
3543895
Wicked tests for GenerateAssertionsAPI: tied winners, timeout checkin…
vteague May 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,16 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import static au.org.democracydevelopers.raireservice.util.CSVUtils.escapeThenJoin;

import au.org.democracydevelopers.raire.assertions.AssertionAndDifficulty;
import au.org.democracydevelopers.raireservice.service.RaireServiceException;
import au.org.democracydevelopers.raireservice.service.RaireServiceException.RaireErrorCode;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -190,7 +192,7 @@ public abstract class Assertion {
*/
@Column(name = "current_risk", updatable = false, nullable = false)
@ReadOnlyProperty
protected BigDecimal currentRisk = new BigDecimal("1.00");
protected BigDecimal currentRisk = BigDecimal.valueOf(1);

/**
* Default no-args constructor (required for persistence).
Expand Down Expand Up @@ -387,7 +389,7 @@ public BigDecimal getCurrentRisk() {
* data stored in the assertion.
*/
public abstract AssertionAndDifficulty convert(List<String> candidates)
throws IllegalArgumentException;
throws RaireServiceException;

/**
* Return as a list of strings intended for a CSV row, in the same order as the csvHeaders in
Expand All @@ -397,26 +399,38 @@ public abstract AssertionAndDifficulty convert(List<String> candidates)
* Floating-point numbers are formatted to 4 d.p, except the (BigDecimal) current risk, which is
* given to its full precision.
* @return The assertion data, as a list of csv-escaped strings.
*/
public List<String> asCSVRow() {
var fm = new DecimalFormat("0.0###");
return List.of(
getAssertionType(),
winner,
loser,
escapeThenJoin(assumedContinuing),
fm.format(difficulty),
margin+"",
fm.format(dilutedMargin),
currentRisk.toString(),
estimatedSamplesToAudit+"",
optimisticSamplesToAudit+"",
twoVoteOverCount+"",
oneVoteOverCount+"",
otherCount+"",
oneVoteUnderCount+"",
twoVoteUnderCount+""
);
* @throws RaireServiceException with error code WRONG_CANDIDATE_NAMES if the winner, loser or any of
* the assumed_continuing candidates are not in the input candidate list.
*/
public List<String> asCSVRow(List<String> candidates) throws RaireServiceException {
final String prefix = "[asCSVRow]";
final DecimalFormat fm = new DecimalFormat("0.0###");

if(candidates.contains(winner) && candidates.contains(loser)
&& candidates.containsAll(assumedContinuing) ) {
return List.of(
getAssertionType(),
winner,
loser,
escapeThenJoin(assumedContinuing),
fm.format(difficulty),
margin + "",
fm.format(dilutedMargin),
currentRisk.toString(),
estimatedSamplesToAudit + "",
optimisticSamplesToAudit + "",
twoVoteOverCount + "",
oneVoteOverCount + "",
otherCount + "",
oneVoteUnderCount + "",
twoVoteUnderCount + ""
);
} else {
final String msg = String.format("%s Candidate list provided as parameter is inconsistent " +
"with assertion (winner or loser or some continuing candidate not present).", prefix);
logger.error(msg);
throw new RaireServiceException(msg, RaireErrorCode.WRONG_CANDIDATE_NAMES);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import au.org.democracydevelopers.raire.assertions.AssertionAndDifficulty;
import au.org.democracydevelopers.raire.assertions.NotEliminatedBefore;
import au.org.democracydevelopers.raireservice.service.Metadata;
import au.org.democracydevelopers.raireservice.service.RaireServiceException;
import au.org.democracydevelopers.raireservice.service.RaireServiceException.RaireErrorCode;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import java.util.ArrayList;
Expand Down Expand Up @@ -85,7 +87,7 @@ public NEBAssertion(String contestName, long universeSize, int margin, double di
/**
* {@inheritDoc}
*/
public AssertionAndDifficulty convert(List<String> candidates) throws IllegalArgumentException {
public AssertionAndDifficulty convert(List<String> candidates) throws RaireServiceException {

final String prefix = "[convert]";
logger.debug(String.format("%s Constructing a raire-java AssertionAndDifficulty for the " +
Expand All @@ -107,7 +109,7 @@ public AssertionAndDifficulty convert(List<String> candidates) throws IllegalArg
final String msg = String.format("%s Candidate list provided as parameter is inconsistent " +
"with assertion (winner or loser not present).", prefix);
logger.error(msg);
throw new IllegalArgumentException(msg);
throw new RaireServiceException(msg, RaireErrorCode.WRONG_CANDIDATE_NAMES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import au.org.democracydevelopers.raire.assertions.AssertionAndDifficulty;
import au.org.democracydevelopers.raire.assertions.NotEliminatedNext;
import au.org.democracydevelopers.raireservice.service.Metadata;
import au.org.democracydevelopers.raireservice.service.RaireServiceException;
import au.org.democracydevelopers.raireservice.service.RaireServiceException.RaireErrorCode;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;

Expand Down Expand Up @@ -93,7 +95,7 @@ public NENAssertion(String contestName, long universeSize, int margin, double di
/**
* {@inheritDoc}
*/
public AssertionAndDifficulty convert(List<String> candidates) throws IllegalArgumentException {
public AssertionAndDifficulty convert(List<String> candidates) throws RaireServiceException {

final String prefix = "[convert]";
logger.debug(String.format("%s Constructing a raire-java AssertionAndDifficulty for the " +
Expand All @@ -119,7 +121,8 @@ public AssertionAndDifficulty convert(List<String> candidates) throws IllegalArg
else{
final String msg = String.format("%s Candidate list provided as parameter is inconsistent " +
"with assertion (winner or loser or some continuing candidate not present).", prefix);
throw new IllegalArgumentException(msg);
logger.error(msg);
throw new RaireServiceException(msg, RaireErrorCode.WRONG_CANDIDATE_NAMES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class GenerateAssertionsRequest extends ContestRequest {
/**
* The elapsed time allowed to raire to generate the assertions, in seconds.
*/
public final float timeLimitSeconds;
public final double timeLimitSeconds;

/**
* All args constructor.
Expand All @@ -64,7 +64,7 @@ public class GenerateAssertionsRequest extends ContestRequest {
*/
@ConstructorProperties({"contestName", "totalAuditableBallots", "timeLimitSeconds","candidates"})
public GenerateAssertionsRequest(String contestName, int totalAuditableBallots,
float timeLimitSeconds, List<String> candidates) {
double timeLimitSeconds, List<String> candidates) {
super(contestName, candidates);
this.totalAuditableBallots = totalAuditableBallots;
this.timeLimitSeconds = timeLimitSeconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String generateCSV(GetAssertionsRequest request) throws RaireServiceExcep
String preface = makePreface(request);
String extrema = findExtrema(sortedAssertions);
String headers = escapeThenJoin(csvHeaders);
String contents = makeContents(sortedAssertions);
String contents = makeContents(sortedAssertions, request.candidates);

logger.debug(String.format("%s %d assertions translated to csv.", prefix, assertions.size()));
return preface + extrema + "\n\n" + headers + "\n" + contents;
Expand Down Expand Up @@ -266,14 +266,17 @@ private String makePreface(GetAssertionsRequest request) {
* number (not related to the database's index) that begins at 1 and increments by 1 with each row.
* @param assertions a list of assertions
* @return their concatenated csv rows.
* @throws RaireServiceException if the candidate names in any of the assertions are inconsistent
* with the request's candidate list.
*/
private String makeContents(List<Assertion> assertions) {
private String makeContents(List<Assertion> assertions, List<String> candidates)
throws RaireServiceException {

int index = 1;
List<String> rows = new ArrayList<>();

for (Assertion assertion : assertions) {
rows.add(index++ + "," + escapeThenJoin(assertion.asCSVRow()));
rows.add(index++ + "," + escapeThenJoin(assertion.asCSVRow(candidates)));
}

return String.join("\n", rows) + "\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import au.org.democracydevelopers.raireservice.persistence.repository.AssertionRepository;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import au.org.democracydevelopers.raireservice.service.RaireServiceException.RaireErrorCode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -91,8 +92,10 @@ public RaireSolution getRaireSolution(GetAssertionsRequest request)
// keeping track of the maximum difficulty and minimum margin.
logger.debug(String.format("%s Converting %d assertions into raire-java format.", prefix,
assertions.size()));
List<AssertionAndDifficulty> translated = assertions.stream().map(
a -> a.convert(request.candidates)).toList();
List<AssertionAndDifficulty> translated = new ArrayList<>();
for(Assertion a : assertions) {
translated.add(a.convert(request.candidates));
}

logger.debug(String.format("%s %d assertions translated to json.", prefix,
assertions.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
*/
public class RaireServiceException extends Exception {

/**
* The string "error_code" - used for retrieving it from json etc.
*/
public static String ERROR_CODE_KEY = "error_code";

/**
* The error code - an enum used to describe what went wrong. Returned in the http response for
* colorado-rla to interpret for the user.
Expand Down
Loading
Loading