Skip to content

Commit

Permalink
fix(tests): Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Apr 6, 2020
1 parent fd249da commit 7e97461
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum NewGTFSErrorType {
URL_FORMAT(Priority.MEDIUM, "URL format should be <scheme>://<authority><path>?<query>#<fragment>"),
LANGUAGE_FORMAT(Priority.LOW, "Language should be specified with a valid BCP47 tag."),
ILLEGAL_FIELD_VALUE(Priority.MEDIUM, "Fields may not contain tabs, carriage returns or new lines."),
// The error type below is an MTC-specific requirement.
FIELD_VALUE_TOO_LONG(Priority.MEDIUM, "Field value has too many characters."),
INTEGER_FORMAT(Priority.MEDIUM, "Incorrect integer format."),
FARE_TRANSFER_MISMATCH(Priority.MEDIUM, "A fare that does not permit transfers has a non-zero transfer duration."),
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/com/conveyal/gtfs/validator/MTCValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import com.conveyal.gtfs.loader.Feed;
import com.conveyal.gtfs.model.*;

import java.net.URL;

import static com.conveyal.gtfs.error.NewGTFSErrorType.FIELD_VALUE_TOO_LONG;

/**
* MTCValidator checks in a GTFS feed that the length of certain field values
* do not exceed the 511 MTC guidelines. (TODO: add guidelines URL.)
* To refer to specific limits, search the guidelines for the word 'character'.
* MTCValidator runs a set of custom validation checks for GTFS feeds managed by MTC in Data Tools.
* The checks consist of validating field lengths at this time per the 511 MTC guidelines at
* https://github.com/ibi-group/datatools-ui/files/4438625/511.Transit_Data.Guidelines_V2.0_3-27-2020.pdf.
* For specific field lengths, search the guidelines for the word 'character'.
*
* Note that other validations, e.g. on GTFS+ files, are discussed in
* https://github.com/ibi-group/datatools-ui/issues/544.
*/
public class MTCValidator extends FeedValidator {

Expand All @@ -33,24 +39,25 @@ public void validate() {
fieldLengthShouldNotExceed(trip, trip.trip_headsign, 120);
fieldLengthShouldNotExceed(trip, trip.trip_short_name, 50);
}

// TODO: Handle calendar_attributes.txt?
}

/**
* Checks that the length of a string (or Object.toString()) does not exceed a length.
* Checks that the length of a string does not exceed a certain length.
* Reports an error if the length is exceeded.
* @param entity The containing GTFS entity (for error reporting purposes).
* @param objValue The value to check.
* @param value The String value to check.
* @param maxLength The length to check, should be positive or zero.
* @return true if the length of objValue.toString() is maxLength or less or if objValue is null; false otherwise.
* @return true if value.length() is maxLength or less, or if value is null; false otherwise.
*/
public boolean fieldLengthShouldNotExceed(Entity entity, Object objValue, int maxLength) {
String value = objValue != null ? objValue.toString() : "";
public boolean fieldLengthShouldNotExceed(Entity entity, String value, int maxLength) {
if (value.length() > maxLength) {
if (errorStorage != null) registerError(entity, FIELD_VALUE_TOO_LONG, "[over " + maxLength + " characters] " + value);
return false;
}
return true;
}

public boolean fieldLengthShouldNotExceed(Entity entity, URL url, int maxLength) {
return fieldLengthShouldNotExceed(entity, url != null ? url.toString() : "", maxLength);
}
}
44 changes: 4 additions & 40 deletions src/test/java/com/conveyal/gtfs/GTFSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,24 +331,6 @@ public void canLoadFeedWithLongFieldValues () {
);
}

/**
* Shorthand for next method.
*/
private boolean runIntegrationTestOnFolder(
String folderName,
Matcher<Object> fatalExceptionExpectation,
PersistenceExpectation[] persistenceExpectations,
ErrorExpectation[] errorExpectations
) {
return runIntegrationTestOnFolder(
folderName,
fatalExceptionExpectation,
persistenceExpectations,
errorExpectations,
null
);
}

/**
* A helper method that will zip a specified folder in test/main/resources and call
* {@link #runIntegrationTestOnZipFile} on that file.
Expand All @@ -358,7 +340,7 @@ private boolean runIntegrationTestOnFolder(
Matcher<Object> fatalExceptionExpectation,
PersistenceExpectation[] persistenceExpectations,
ErrorExpectation[] errorExpectations,
FeedValidatorCreator customValidator
FeedValidatorCreator... customValidators
) {
LOG.info("Running integration test on folder {}", folderName);
// zip up test folder into temp zip file
Expand All @@ -374,25 +356,7 @@ private boolean runIntegrationTestOnFolder(
fatalExceptionExpectation,
persistenceExpectations,
errorExpectations,
customValidator
);
}

/**
* Shorthand for next method.
*/
private boolean runIntegrationTestOnZipFile(
String zipFileName,
Matcher<Object> fatalExceptionExpectation,
PersistenceExpectation[] persistenceExpectations,
ErrorExpectation[] errorExpectations
) {
return runIntegrationTestOnZipFile(
zipFileName,
fatalExceptionExpectation,
persistenceExpectations,
errorExpectations,
null
customValidators
);
}

Expand All @@ -410,7 +374,7 @@ private boolean runIntegrationTestOnZipFile(
Matcher<Object> fatalExceptionExpectation,
PersistenceExpectation[] persistenceExpectations,
ErrorExpectation[] errorExpectations,
FeedValidatorCreator customValidator
FeedValidatorCreator... customValidators
) {
String testDBName = TestUtils.generateNewDB();
String dbConnectionUrl = String.join("/", JDBC_URL, testDBName);
Expand All @@ -427,7 +391,7 @@ private boolean runIntegrationTestOnZipFile(
// load and validate feed
LOG.info("load and validate GTFS file {}", zipFileName);
FeedLoadResult loadResult = GTFS.load(zipFileName, dataSource);
ValidationResult validationResult = GTFS.validate(loadResult.uniqueIdentifier, dataSource, customValidator);
ValidationResult validationResult = GTFS.validate(loadResult.uniqueIdentifier, dataSource, customValidators);

assertThat(validationResult.fatalException, is(fatalExceptionExpectation));
namespace = loadResult.uniqueIdentifier;
Expand Down

0 comments on commit 7e97461

Please sign in to comment.