Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 6, 2024
1 parent 8a96f3a commit 9571773
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Predicate<Itinerary> shouldBeFlaggedForRemoval() {
// end up time-shifted right up to the arrive by time.
// further reading: https://github.com/opentripplanner/OpenTripPlanner/issues/6046
if (it.isOnStreetAndFlexOnly() && searchDirection.isArriveBy()) {
return startTime.isBefore(latestDepartureTime);
return startTime.isBefore(earliestDepartureTime);
} else {
return (
startTime.isBefore(earliestDepartureTime) || !startTime.isBefore(latestDepartureTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,13 @@ public TestItineraryBuilder flex(int start, int end, Place to) {
int legCost = 0;
StopTime fromStopTime = new StopTime();
fromStopTime.setStop(lastPlace.stop);
fromStopTime.setFlexWindowStart(start);
fromStopTime.setFlexWindowStart(end);

StopTime toStopTime = new StopTime();
toStopTime.setStop(to.stop);
toStopTime.setFlexWindowStart(start);
toStopTime.setFlexWindowStart(end);

Trip trip = trip("1", route("flex").build());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.opentripplanner.routing.algorithm.filterchain.filters.system;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.framework.time.TimeUtils.time;
import static org.opentripplanner.model.plan.TestItineraryBuilder.newItinerary;

import java.time.Duration;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opentripplanner.framework.time.TimeUtils;
import org.opentripplanner.model.SystemNotice;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.model.plan.PlanTestConstants;
Expand All @@ -21,11 +23,13 @@
class OutsideSearchWindowFilterTest implements PlanTestConstants {

private static final Duration SEARCH_WINDOW_10m = Duration.ofMinutes(10);
private final int startTime = TimeUtils.time("09:30");
private final int endTime = TimeUtils.time("09:40");
private static final int START_TIME = time("09:30");
private static final int END_TIME = time("09:40");

private final Itinerary itinerary = newItinerary(A).bus(32, startTime, endTime, E).build();
private final List<Itinerary> input = List.of(itinerary);
private static final Itinerary BUS_ITINERARY = newItinerary(A)
.bus(32, START_TIME, END_TIME, E)
.build();
private static final List<Itinerary> INPUT = List.of(BUS_ITINERARY);

static List<Arguments> filterOnSearchWindowTestCases() {
return List.of(
Expand All @@ -39,13 +43,13 @@ static List<Arguments> filterOnSearchWindowTestCases() {
@ParameterizedTest(name = "{0}, edt: {1}, sw: 10m, expects flagged for removal: {2}")
@MethodSource("filterOnSearchWindowTestCases")
void filterOnSearchWindow(String description, String edt, boolean flaggedForRemoval) {
List<Itinerary> expected = flaggedForRemoval ? input : List.of();
List<Itinerary> expected = flaggedForRemoval ? INPUT : List.of();
var subject = new OutsideSearchWindowFilter(
TestItineraryBuilder.newTime(TimeUtils.time(edt)).toInstant(),
TestItineraryBuilder.newTime(time(edt)).toInstant(),
SEARCH_WINDOW_10m,
SearchDirection.DEPART_AT
);
var result = subject.flagForRemoval(input);
var result = subject.flagForRemoval(INPUT);
assertEquals(expected, result, description);
}

Expand All @@ -57,4 +61,31 @@ public void testTaggedBy() {
it.flagForDeletion(new SystemNotice(OutsideSearchWindowFilter.TAG, "Text"));
assertTrue(OutsideSearchWindowFilter.taggedBy(it));
}

private static Stream<Itinerary> onStreetTestCases() {
int t9_28 = time("9:28");
int t9_38 = time("9:38");
return Stream
.of(
newItinerary(A, t9_28).walk(D2m, B),
newItinerary(A, t9_38).walk(D12m, B),
newItinerary(A, t9_28).bicycle(t9_28, t9_38, B),
newItinerary(A, t9_28).flex(t9_28, t9_38, B),
newItinerary(A, t9_28).flex(t9_38, time("9:48"), B),
newItinerary(A, time("9:20")).flex(time("9:20"), t9_28, B).walk(D12m, C)
)
.map(TestItineraryBuilder::build);
}

@ParameterizedTest
@MethodSource("onStreetTestCases")
void onStreetArriveByShouldNotBeRemoved(Itinerary itin) {
var edt = "9:20";
var subject = new OutsideSearchWindowFilter(
TestItineraryBuilder.newTime(time(edt)).toInstant(),
SEARCH_WINDOW_10m,
SearchDirection.ARRIVE_BY
);
assertThat(subject.flagForRemoval(List.of(itin))).isEmpty();
}
}

0 comments on commit 9571773

Please sign in to comment.