From 18d5bbe307db062ae9329ebe1953e6f51e2708ba Mon Sep 17 00:00:00 2001 From: Landon Reed Date: Mon, 1 Apr 2019 11:01:14 -0400 Subject: [PATCH] test(JdbcTableWriter): check that update to pattern_stop shape_dist_traveled updates stop_time --- .../conveyal/gtfs/loader/JdbcTableWriter.java | 7 ++- .../gtfs/loader/JDBCTableWriterTest.java | 61 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java b/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java index b542baffd..bc5fbac84 100644 --- a/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java +++ b/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java @@ -261,7 +261,12 @@ public String update(Integer id, String json, boolean autoCommit) throws SQLExce */ public int normalizeStopTimesForPattern (int id, int beginWithSequence) throws SQLException { try { - JDBCTableReader patternStops = new JDBCTableReader(Table.PATTERN_STOP, dataSource, tablePrefix + ".", EntityPopulator.PATTERN_STOP); + JDBCTableReader patternStops = new JDBCTableReader( + Table.PATTERN_STOP, + dataSource, + tablePrefix + ".", + EntityPopulator.PATTERN_STOP + ); String patternId = getValueForId(id, "pattern_id", tablePrefix, Table.PATTERNS, connection); List patternStopsToNormalize = new ArrayList<>(); for (PatternStop patternStop : patternStops.getOrdered(patternId)) { diff --git a/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java b/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java index 21358928a..2a9077caf 100644 --- a/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java +++ b/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java @@ -25,6 +25,7 @@ import javax.sql.DataSource; import java.io.IOException; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.UUID; @@ -334,6 +335,60 @@ public void canCreateUpdateAndDeleteRoutes() throws IOException, SQLException, I )); } + /** + * This test verifies that stop_times#shape_dist_traveled (and other "linked fields") are updated when a pattern + * is updated. + */ + @Test + public void shouldUpdateStopTimeShapeDistTraveledOnPatternStopUpdate() throws IOException, SQLException, InvalidNamespaceException { + String routeId = newUUID(); + String patternId = newUUID(); + int startTime = 6 * 60 * 60; // 6 AM + PatternDTO pattern = createRouteAndPattern( + routeId, + patternId, + "pattern name", + null, + new ShapePointDTO[]{}, + new PatternStopDTO[]{ + new PatternStopDTO(patternId, firstStopId, 0), + new PatternStopDTO(patternId, lastStopId, 1) + }, + 0 + ); + // Make sure saved data matches expected data. + assertThat(pattern.route_id, equalTo(routeId)); + // Create trip so we can check that the stop_time values are updated after the patter update. + TripDTO tripInput = constructTimetableTrip(pattern.pattern_id, pattern.route_id, startTime, 60); + JdbcTableWriter createTripWriter = createTestTableWriter(Table.TRIPS); + String createdTripOutput = createTripWriter.create(mapper.writeValueAsString(tripInput), true); + TripDTO createdTrip = mapper.readValue(createdTripOutput, TripDTO.class); + // Check the stop_time's initial shape_dist_traveled value. TODO test that other linked fields are updated? + PreparedStatement statement = testDataSource.getConnection().prepareStatement( + String.format( + "select shape_dist_traveled from %s.stop_times where stop_sequence=1 and trip_id='%s'", + testNamespace, + createdTrip.trip_id + ) + ); + ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + // First stop_time shape_dist_traveled should be zero. + assertThat(resultSet.getInt(1), equalTo(0)); + } + // Update pattern_stop#shape_dist_traveled and check that the stop_time's shape_dist value is updated. + final double updatedShapeDistTraveled = 45.5; + pattern.pattern_stops[1].shape_dist_traveled = updatedShapeDistTraveled; + JdbcTableWriter patternUpdater = createTestTableWriter(Table.PATTERNS); + String updatedPatternOutput = patternUpdater.update(pattern.id, mapper.writeValueAsString(pattern), true); + LOG.info("Updated pattern: {}", updatedPatternOutput); + ResultSet resultSet2 = statement.executeQuery(); + while (resultSet2.next()) { + // First stop_time shape_dist_traveled should be updated. + assertThat(resultSet2.getDouble(1), equalTo(updatedShapeDistTraveled)); + } + } + @Test public void shouldDeleteReferencingTripsAndStopTimesOnPatternDelete() throws IOException, SQLException, InvalidNamespaceException { String routeId = "9834914"; @@ -514,7 +569,7 @@ public void canNormalizePatternStopTimes() throws IOException, SQLException, Inv new PatternStopDTO(patternId, lastStopId, 1) }; patternStops[1].default_travel_time = initialTravelTime; - PatternDTO pattern = createRouteAndPattern(UUID.randomUUID().toString(), + PatternDTO pattern = createRouteAndPattern(newUUID(), patternId, "Pattern A", null, @@ -581,6 +636,10 @@ public void canUpdateServiceId() throws InvalidNamespaceException, IOException, * End tests, begin helpers ****************************************************************************************************************/ + private static String newUUID() { + return UUID.randomUUID().toString(); + } + private void assertThatSqlQueryYieldsRowCount(String sql, int expectedRowCount) throws SQLException { LOG.info(sql); int recordCount = 0;