Skip to content

Commit

Permalink
Refactored tests with data providers/parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
vteague committed Jun 22, 2024
1 parent 8fb7ead commit 84c7217
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 2,894 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import au.org.democracydevelopers.corla.model.ContestType;
import au.org.democracydevelopers.corla.model.IRVComparisonAudit;
import au.org.democracydevelopers.corla.model.vote.IRVParsingException;
import au.org.democracydevelopers.corla.util.TestClassWithDatabase;
import au.org.democracydevelopers.corla.util.testUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.jdbc.JdbcDatabaseDelegate;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -61,7 +64,7 @@
* - an all-plurality contest is made into a (plain, plurality) ComparisonAudit,
* - a mixed-type contest, or a contest that is neither plurality nor IRV, throws an error.
*/
public class ComparisonAuditControllerTests {
public class ComparisonAuditControllerTests extends TestClassWithDatabase {

/**
* Class-wide logger
Expand All @@ -71,14 +74,7 @@ public class ComparisonAuditControllerTests {
/**
* Container for the mock-up database.
*/
static PostgreSQLContainer<?> postgres
= new PostgreSQLContainer<>("postgres:15-alpine")
// None of these actually have to be the same as the real database (except its name), but this
// makes it easy to match the setup scripts.
.withDatabaseName("corla")
.withUsername("corlaadmin")
.withPassword("corlasecret")
.withInitScript("SQL/corla-three-candidates-ten-votes-inconsistent-types.sql");
static PostgreSQLContainer<?> postgres = createTestContainer();

/**
* Blank properties for submitting to the DominionCVRExportParser instance.
Expand All @@ -103,14 +99,13 @@ public class ComparisonAuditControllerTests {
@BeforeClass
public static void beforeAll() {
postgres.start();
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.driver", "org.postgresql.Driver");
hibernateProperties.setProperty("hibernate.url", postgres.getJdbcUrl());
hibernateProperties.setProperty("hibernate.user", postgres.getUsername());
hibernateProperties.setProperty("hibernate.pass", postgres.getPassword());
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
Persistence.setProperties(hibernateProperties);
Persistence.beginTransaction();
Persistence.setProperties(createHibernateProperties(postgres));

var containerDelegate = new JdbcDatabaseDelegate(postgres, "");
ScriptUtils.runInitScript(containerDelegate,
"SQL/co-counties.sql");
ScriptUtils.runInitScript(containerDelegate,
"SQL/corla-three-candidates-ten-votes-inconsistent-types.sql");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
package au.org.democracydevelopers.corla.csv;

import au.org.democracydevelopers.corla.model.ContestType;
import au.org.democracydevelopers.corla.util.TestClassWithDatabase;
import au.org.democracydevelopers.corla.util.testUtils;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.jdbc.JdbcDatabaseDelegate;
import us.freeandfair.corla.csv.DominionCVRExportParser;
import us.freeandfair.corla.model.*;
import us.freeandfair.corla.persistence.Persistence;
Expand All @@ -44,6 +47,8 @@

import org.testng.annotations.*;

import javax.transaction.Transactional;

import static au.org.democracydevelopers.corla.util.testUtils.*;
import static org.testng.Assert.*;

Expand All @@ -62,7 +67,7 @@
* plus a bad plurality "Vote For= " with a non-integer.
* - an examples to test the broader class of Write In strings.
*/
public class DominionCVRExportParserTests {
public class DominionCVRExportParserTests extends TestClassWithDatabase {

/**
* Class-wide logger
Expand All @@ -72,14 +77,7 @@ public class DominionCVRExportParserTests {
/**
* Container for the mock-up database.
*/
static PostgreSQLContainer<?> postgres
= new PostgreSQLContainer<>("postgres:15-alpine")
// None of these actually have to be the same as the real database (except its name), but this
// makes it easy to match the setup scripts.
.withDatabaseName("corla")
.withUsername("corlaadmin")
.withPassword("corlasecret")
.withInitScript("SQL/corlaInit.sql");
static PostgreSQLContainer<?> postgres = createTestContainer();

/**
* Error message to match.
Expand All @@ -94,14 +92,10 @@ public class DominionCVRExportParserTests {
@BeforeClass
public static void beforeAll() {
postgres.start();
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.driver", "org.postgresql.Driver");
hibernateProperties.setProperty("hibernate.url", postgres.getJdbcUrl());
hibernateProperties.setProperty("hibernate.user", postgres.getUsername());
hibernateProperties.setProperty("hibernate.pass", postgres.getPassword());
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
Persistence.setProperties(hibernateProperties);
Persistence.beginTransaction();
Persistence.setProperties(createHibernateProperties(postgres));

var containerDelegate = new JdbcDatabaseDelegate(postgres, "");
ScriptUtils.runInitScript(containerDelegate, "SQL/co-counties.sql");

}

Expand All @@ -116,6 +110,7 @@ public static void afterAll() {
* @throws IOException never.
*/
@Test
@Transactional
public void parseThreeCandidatesTenVotesSucceeds() throws IOException {
testUtils.log(LOGGER, "parseThreeCandidatesTenVotesSucceeds");
Path path = Paths.get(TINY_CSV_PATH + "ThreeCandidatesTenVotes.csv");
Expand Down Expand Up @@ -169,6 +164,7 @@ public void parseThreeCandidatesTenVotesSucceeds() throws IOException {
* @throws IOException never.
*/
@Test
@Transactional
public void parseGuideToRaireExample3() throws IOException {
testUtils.log(LOGGER, "parseGuideToRaireExample3");
Path path = Paths.get(TINY_CSV_PATH + "GuideToRAIREExample3.csv");
Expand Down Expand Up @@ -219,6 +215,7 @@ public void parseGuideToRaireExample3() throws IOException {
* @throws IOException if there are file I/O issues.
*/
@Test
@Transactional
public void parseThreeCandidatesTenInvalidVotesSucceeds() throws IOException {
testUtils.log(LOGGER, "parseThreeCandidatesTenInvalidVotesSucceeds");
Path path = Paths.get(TINY_CSV_PATH + "ThreeCandidatesTenInvalidVotes.csv");
Expand Down Expand Up @@ -276,6 +273,7 @@ public void parseThreeCandidatesTenInvalidVotesSucceeds() throws IOException {
* Check correct parsing of the first vote.
*/
@Test
@Transactional
public void parseBoulder23Succeeds() throws IOException {
testUtils.log(LOGGER, "parseBoulder23Succeeds");
Path path = Paths.get(BOULDER_CSV_PATH + "Boulder-2023-Coordinated-CVR-Redactions-removed.csv");
Expand Down Expand Up @@ -539,6 +537,7 @@ public void parseBoulder23Succeeds() throws IOException {
*/
@Test(expectedExceptions = RuntimeException.class,
expectedExceptionsMessageRegExp = badNumsRegexp)
@Transactional
public void parseBadVoteForPluralityError() throws IOException {
testUtils.log(LOGGER, "parseBadVoteForPluralityError");
Path path = Paths.get(BAD_CSV_PATH + "badVoteForPlurality.csv");
Expand All @@ -556,6 +555,7 @@ public void parseBadVoteForPluralityError() throws IOException {
* by -, _, space or no space, followed by any capitalization of "in", followed by any whitespace.
*/
@Test
@Transactional
public void parseWriteIns() throws IOException {
testUtils.log(LOGGER, "parseWriteIns");
Path path = Paths.get(WRITEIN_CSV_PATH + "WriteIns.csv");
Expand Down
Loading

0 comments on commit 84c7217

Please sign in to comment.