Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 5, 2024
1 parent eae78db commit 913239d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.opentripplanner.ext.vectortiles.layers.stops;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDate;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.apis.gtfs.PatternTestModel;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;

class LayerFiltersTest {

private static final RegularStop STOP = TransitModelForTest.of().stop("1").build();
private static final LocalDate DATE = LocalDate.of(2024, 9, 5);
private static final TripPattern PATTERN = PatternTestModel.pattern();

@Test
void includeStopWithinServiceWeek() {
var predicate = LayerFilters.currentServiceWeek(
s -> List.of(PATTERN),
trip -> List.of(DATE),
() -> DATE
);

assertTrue(predicate.test(STOP));
}

@Test
void excludeOutsideServiceWeek() {
var inThreeWeeks = DATE.plusDays(21);
var predicate = LayerFilters.currentServiceWeek(
s -> List.of(PATTERN),
trip -> List.of(inThreeWeeks),
() -> DATE
);

assertFalse(predicate.test(STOP));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.opentripplanner.apis.gtfs.PatternByServiceDatesFilter;
import org.opentripplanner.apis.gtfs.model.LocalDateRange;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.service.TransitService;

/**
Expand All @@ -24,17 +30,24 @@ public class LayerFilters {
* Returns a predicate which only includes stop which are visited by a pattern that is in the current
* TriMet service week, namely from Sunday to Sunday.
*/
public static Predicate<RegularStop> currentServiceWeek(TransitService transitService) {
var serviceDate = LocalDate.now(transitService.getTimeZone());
public static Predicate<RegularStop> currentServiceWeek(
Function<RegularStop, Collection<TripPattern>> getPatternsForStop,
Function<Trip, Collection<LocalDate>> getServiceDatesForTrip,
Supplier<LocalDate> nowSupplier
) {
var serviceDate = nowSupplier.get();
var lastSunday = serviceDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
var nextSunday = serviceDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).plusDays(1);

var filter = new PatternByServiceDatesFilter(
new LocalDateRange(lastSunday, nextSunday),
transitService
// not used
route -> List.of(),
getServiceDatesForTrip
);

return regularStop -> {
var patterns = transitService.getPatternsForStop(regularStop);
var patterns = getPatternsForStop.apply(regularStop);
var patternsInCurrentWeek = filter.filterPatterns(patterns);
return !patternsInCurrentWeek.isEmpty();
};
Expand All @@ -43,7 +56,12 @@ public static Predicate<RegularStop> currentServiceWeek(TransitService transitSe
public static Predicate<RegularStop> forType(FilterType type, TransitService transitService) {
return switch (type) {
case NONE -> NO_FILTER;
case CURRENT_TRIMET_SERVICE_WEEK -> currentServiceWeek(transitService);
case CURRENT_TRIMET_SERVICE_WEEK -> currentServiceWeek(
transitService::getPatternsForStop,
trip ->
transitService.getCalendarService().getServiceDatesForServiceId(trip.getServiceId()),
() -> LocalDate.now(transitService.getTimeZone())
);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PatternByServiceDatesFilter {
* This method is not private to enable unit testing.
* <p>
*/
PatternByServiceDatesFilter(
public PatternByServiceDatesFilter(
LocalDateRange range,
Function<Route, Collection<TripPattern>> getPatternsForRoute,
Function<Trip, Collection<LocalDate>> getServiceDatesForTrip
Expand All @@ -56,14 +56,6 @@ public PatternByServiceDatesFilter(
);
}

public PatternByServiceDatesFilter(LocalDateRange range, TransitService transitService) {
this(
range,
transitService::getPatternsForRoute,
trip -> transitService.getCalendarService().getServiceDatesForServiceId(trip.getServiceId())
);
}

/**
* Filter the patterns by the service dates that it operates on.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,25 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.opentripplanner.apis.gtfs.PatternByServiceDatesFilterTest.FilterExpectation.NOT_REMOVED;
import static org.opentripplanner.apis.gtfs.PatternByServiceDatesFilterTest.FilterExpectation.REMOVED;
import static org.opentripplanner.transit.model._data.TransitModelForTest.id;
import static org.opentripplanner.apis.gtfs.PatternTestModel.ROUTE_1;

import java.time.LocalDate;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opentripplanner.apis.gtfs.model.LocalDateRange;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.timetable.ScheduledTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.service.StopModel;

class PatternByServiceDatesFilterTest {

private static final Route ROUTE_1 = TransitModelForTest.route("1").build();
private static final FeedScopedId SERVICE_ID = id("service");
private static final Trip TRIP = TransitModelForTest
.trip("t1")
.withRoute(ROUTE_1)
.withServiceId(SERVICE_ID)
.build();
private static final TransitModelForTest MODEL = new TransitModelForTest(StopModel.of());
private static final RegularStop STOP_1 = MODEL.stop("1").build();
private static final StopPattern STOP_PATTERN = TransitModelForTest.stopPattern(STOP_1, STOP_1);
private static final TripPattern PATTERN_1 = pattern();
private static final TripPattern PATTERN_1 = PatternTestModel.pattern();

enum FilterExpectation {
REMOVED,
NOT_REMOVED,
}

private static TripPattern pattern() {
var pattern = TransitModelForTest
.tripPattern("1", ROUTE_1)
.withStopPattern(STOP_PATTERN)
.build();

var tt = ScheduledTripTimes
.of()
.withTrip(TRIP)
.withArrivalTimes("10:00 10:05")
.withDepartureTimes("10:00 10:05")
.build();
pattern.add(tt);
return pattern;
}

static List<Arguments> invalidRangeCases() {
return List.of(
Arguments.of(null, null),
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/opentripplanner/apis/gtfs/PatternTestModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.opentripplanner.apis.gtfs;

import static org.opentripplanner.transit.model._data.TransitModelForTest.id;

import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.timetable.ScheduledTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.service.StopModel;

public class PatternTestModel {

public static final Route ROUTE_1 = TransitModelForTest.route("1").build();

private static final FeedScopedId SERVICE_ID = id("service");
private static final Trip TRIP = TransitModelForTest
.trip("t1")
.withRoute(ROUTE_1)
.withServiceId(SERVICE_ID)
.build();
private static final TransitModelForTest MODEL = new TransitModelForTest(StopModel.of());
private static final RegularStop STOP_1 = MODEL.stop("1").build();
private static final StopPattern STOP_PATTERN = TransitModelForTest.stopPattern(STOP_1, STOP_1);

public static TripPattern pattern() {
var pattern = TransitModelForTest
.tripPattern("1", ROUTE_1)
.withStopPattern(STOP_PATTERN)
.build();

var tt = ScheduledTripTimes
.of()
.withTrip(TRIP)
.withArrivalTimes("10:00 10:05")
.withDepartureTimes("10:00 10:05")
.build();
pattern.add(tt);
return pattern;
}
}

0 comments on commit 913239d

Please sign in to comment.