-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract testable functionality to unit tests
- Loading branch information
1 parent
a55e37e
commit 7521cc7
Showing
5 changed files
with
113 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/com/conveyal/datatools/manager/jobs/validation/SharedStopsValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.conveyal.datatools.manager.jobs.validation; | ||
|
||
import com.conveyal.datatools.UnitTest; | ||
import com.csvreader.CsvReader; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static com.zenika.snapshotmatcher.SnapshotMatcher.matchesSnapshot; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class SharedStopsValidatorTest extends UnitTest { | ||
private void attemptToParseInvalidCSV(String csv, String expectedError) { | ||
CsvReader configReader = CsvReader.parse(csv); | ||
Exception exception = assertThrows(RuntimeException.class, () -> { | ||
SharedStopsValidator.getHeaderIncedes(configReader); | ||
}); | ||
|
||
String error = exception.getMessage(); | ||
assertEquals(expectedError, error); | ||
} | ||
|
||
private Map parseValidCSV(String csv) { | ||
CsvReader configReader = CsvReader.parse(csv); | ||
return SharedStopsValidator.getHeaderIncedes(configReader); | ||
} | ||
|
||
@Test | ||
void canHandleIncorrectCsv() { | ||
String invalid = "shared_stops.csv contained invalid headers!"; | ||
String missing = "shared_stops.csv is missing headers!"; | ||
|
||
attemptToParseInvalidCSV("this is a great string, but it's not a shared_stops csv!", invalid); | ||
attemptToParseInvalidCSV("", invalid); | ||
attemptToParseInvalidCSV("is_primary,stop_id", missing); | ||
attemptToParseInvalidCSV("is_primary,feed_id,,stop_group_id", invalid); | ||
attemptToParseInvalidCSV("is_primary,feed_id,stop_group_id", missing); | ||
attemptToParseInvalidCSV("feed_id, is_primary, stop_group_id,stop_id", invalid); | ||
} | ||
|
||
@Test | ||
void canHandleVariousCorrectCSV() { | ||
assertThat(parseValidCSV("is_primary,feed_id,stop_id,stop_group_id"), matchesSnapshot()); | ||
assertThat(parseValidCSV("feed_id,stop_id,stop_group_id,is_primary"), matchesSnapshot()); | ||
|
||
// Only last is handled | ||
assertThat(parseValidCSV("feed_id,is_primary,stop_group_id,stop_id,feed_id"), matchesSnapshot()); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...atools/manager/jobs/validation/SharedStopsValidatorTest/canHandleVariousCorrectCSV-0.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"STOP_GROUP_ID_INDEX" : 3, | ||
"STOP_ID_INDEX" : 2, | ||
"IS_PRIMARY_INDEX" : 0, | ||
"FEED_ID_INDEX" : 1 | ||
} |
6 changes: 6 additions & 0 deletions
6
...atools/manager/jobs/validation/SharedStopsValidatorTest/canHandleVariousCorrectCSV-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"STOP_GROUP_ID_INDEX" : 2, | ||
"STOP_ID_INDEX" : 1, | ||
"IS_PRIMARY_INDEX" : 3, | ||
"FEED_ID_INDEX" : 0 | ||
} |
6 changes: 6 additions & 0 deletions
6
...atools/manager/jobs/validation/SharedStopsValidatorTest/canHandleVariousCorrectCSV-2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"STOP_GROUP_ID_INDEX" : 2, | ||
"STOP_ID_INDEX" : 3, | ||
"IS_PRIMARY_INDEX" : 1, | ||
"FEED_ID_INDEX" : 4 | ||
} |