Skip to content

Commit

Permalink
feat: added "Shapes" component + unit tests (#1463)
Browse files Browse the repository at this point in the history
* feat: added "Shapes" component + unit tests

* feat: formatting components

* fix: changed asserting method
  • Loading branch information
cka-y authored Jun 5, 2023
1 parent ead1346 commit 287faf6
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ private <E extends GtfsEntity> int loadUniqueCount(
}

private void loadSpecFeatures(GtfsFeedContainer feedContainer) {
var pathwaysTable = feedContainer.getTableForFilename(GtfsPathway.FILENAME);
boolean pathways = pathwaysTable.isPresent() && pathwaysTable.get().entityCount() > 0;
specFeatures.put("Pathways", pathways ? "Yes" : "No");
specFeatures.put(
"Pathways", hasOneRecordOfComponent(feedContainer, GtfsPathway.FILENAME) ? "Yes" : "No");
var fareAttributesTable = feedContainer.getTableForFilename(GtfsFareAttribute.FILENAME);
boolean faresV1 =
fareAttributesTable.isPresent() && fareAttributesTable.get().entityCount() > 0;
Expand All @@ -96,9 +95,10 @@ private void loadSpecFeatures(GtfsFeedContainer feedContainer) {
boolean faresV2 = fareProductsTable.isPresent() && fareProductsTable.get().entityCount() > 0;
specFeatures.put("Fares V2", faresV2 ? "Yes" : "No");
specFeatures.put("Route Names", hasRouteNamesComponent(feedContainer) ? "Yes" : "No");
specFeatures.put(
"Shapes", hasOneRecordOfComponent(feedContainer, GtfsShape.FILENAME) ? "Yes" : "No");
var stopTimesTable = feedContainer.getTableForFilename(GtfsStopTime.FILENAME);
// TODO: figure out Flex V1 & V2 checks

}

private void loadAgencyData(GtfsTableContainer<GtfsAgency> agencyTable) {
Expand Down Expand Up @@ -135,6 +135,12 @@ private boolean hasRouteNamesComponent(GtfsFeedContainer feedContainer) {
return false;
}

private boolean hasOneRecordOfComponent(
GtfsFeedContainer feedContainer, String componentFilename) {
var table = feedContainer.getTableForFilename(componentFilename);
return table.isPresent() && table.get().entityCount() > 0;
}

public ArrayList<String> foundFiles() {
var foundFiles = new ArrayList<String>();
for (var table : tableMetaData.values()) {
Expand Down
1 change: 1 addition & 0 deletions main/src/main/resources/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
.spec-feature {
background-color: #d4d4d4;
padding: 2px 5px;
margin-right: 2px;
}

.tooltip {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package org.mobilitydata.gtfsvalidator.report.model;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.GtfsInput;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.table.*;
import org.mobilitydata.gtfsvalidator.validator.*;

public class FeedMetadataTest {

@Rule public final TemporaryFolder tmpDir = new TemporaryFolder();
GtfsFeedLoader feedLoaderMock;
ValidationContext validationContext =
ValidationContext.builder()
.setCountryCode(CountryCode.forStringOrUnknown("CA"))
.setCurrentDateTime(new CurrentDateTime(ZonedDateTime.now(ZoneId.systemDefault())))
.build();
ValidatorLoader validatorLoader;
File rootDir;

private void createDataFile(String filename, String content) throws IOException {
File dataFile = tmpDir.newFile("data/" + filename);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(dataFile))) {
writer.write(content);
}
}

@Before
public void setup() throws IOException, ValidatorLoaderException {
rootDir = tmpDir.newFolder("data");
String agencyContent =
"agency_id, agency_name, agency_url, agency_timezone\n"
+ "1, name, https://dummy.dummy, CA\n"
+ "2, name, https://dummy.dummy, CA\n";
createDataFile("agency.txt", agencyContent);
validatorLoader =
ValidatorLoader.createForClasses(ClassGraphDiscovery.discoverValidatorsInDefaultPackage());
}

private void validateSpecFeature(
String specFeature,
String expectedValue,
ImmutableList<Class<? extends GtfsTableDescriptor<?>>> tableDescriptors)
throws IOException, InterruptedException {
feedLoaderMock = new GtfsFeedLoader(tableDescriptors);
try (GtfsInput gtfsInput = GtfsInput.createFromPath(rootDir.toPath())) {
GtfsFeedContainer feedContainer =
feedLoaderMock.loadAndValidate(
gtfsInput,
new DefaultValidatorProvider(validationContext, validatorLoader),
new NoticeContainer());
FeedMetadata feedMetadata = FeedMetadata.from(feedContainer, gtfsInput.getFilenames());
assertThat(feedMetadata.specFeatures.get(specFeature)).isEqualTo(expectedValue);
}
}

@Test
public void containsRouteNamesComponentTest() throws IOException, InterruptedException {
String routesContent =
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
+ "1,1,Short Name,Long Name,1\n"
+ "2,1,,,1\n";
createDataFile("routes.txt", routesContent);
validateSpecFeature(
"Route Names",
"Yes",
ImmutableList.of(GtfsRouteTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void omitsRouteNamesComponentTest1() throws IOException, InterruptedException {
String routesContent =
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
+ "1,1,,,1\n"
+ "2,1,,,1\n";
createDataFile("routes.txt", routesContent);
validateSpecFeature(
"Route Names",
"No",
ImmutableList.of(GtfsRouteTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void omitsRouteNamesComponentTest2() throws IOException, InterruptedException {
String routesContent =
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
+ "1,1,Short Name,,1\n"
+ "2,1,,,1\n";
createDataFile("routes.txt", routesContent);
validateSpecFeature(
"Route Names",
"No",
ImmutableList.of(GtfsRouteTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void omitsRouteNamesComponentTest3() throws IOException, InterruptedException {
String routesContent =
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
+ "1,1,,Long Name,1\n"
+ "2,1,,,1\n";
createDataFile("routes.txt", routesContent);
validateSpecFeature(
"Route Names",
"No",
ImmutableList.of(GtfsRouteTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void containsPathwaysComponentTest() throws IOException, InterruptedException {
String pathwayContent =
"pathway_id,from_stop_id,to_stop_id,pathway_mode,is_bidirectional\n"
+ "pathway1,stop1,stop2,1,1\n"
+ "pathway2,stop2,stop3,2,0\n";
createDataFile("pathways.txt", pathwayContent);
validateSpecFeature(
"Pathways",
"Yes",
ImmutableList.of(GtfsPathwayTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void omitsPathwaysComponentTest() throws IOException, InterruptedException {
String pathwayContent = "pathway_id,from_stop_id,to_stop_id,pathway_mode,is_bidirectional\n";
createDataFile("pathways.txt", pathwayContent);
validateSpecFeature(
"Pathways",
"No",
ImmutableList.of(GtfsPathwayTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void omitsComponents() throws IOException, InterruptedException {
validateSpecFeature(
"Pathways",
"No",
ImmutableList.of(GtfsPathwayTableDescriptor.class, GtfsAgencyTableDescriptor.class));
validateSpecFeature(
"Route Names",
"No",
ImmutableList.of(GtfsPathwayTableDescriptor.class, GtfsAgencyTableDescriptor.class));
validateSpecFeature(
"Shapes",
"No",
ImmutableList.of(GtfsPathwayTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}

@Test
public void containsShapesComponentTest() throws IOException, InterruptedException {
String shapesContent =
"shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence\n" + "A_shp,37.61956,-122.48161,0\n";
createDataFile("shapes.txt", shapesContent);
validateSpecFeature(
"Shapes",
"Yes",
ImmutableList.of(GtfsShapeTableDescriptor.class, GtfsAgencyTableDescriptor.class));
}
}

0 comments on commit 287faf6

Please sign in to comment.