Files
go-gtfs/cmd/planner/main_test.go
2025-11-14 20:21:53 +01:00

111 lines
2.4 KiB
Go

package main
import (
"os"
"testing"
"time"
)
func TestFindRoute(t *testing.T) {
os.Chdir("../..")
tp := NewTripPlanner()
err := tp.LoadData("data")
if err != nil {
t.Fatalf("Failed to load data: %v", err)
}
err = tp.Preprocess()
if err != nil {
t.Fatalf("Failed to preprocess: %v", err)
}
route := tp.FindRoute("740000030", "740000001", time.Now())
if route == nil {
t.Fatal("No route found from Falun Centralstation to Stockholm Centralstation")
}
if len(route.Legs) < 1 {
t.Fatal("Route has no legs")
}
stops := []string{}
if len(route.Legs) > 0 {
stops = append(stops, route.Legs[0].From)
for _, leg := range route.Legs {
stops = append(stops, leg.To)
}
}
if len(stops) < 2 {
t.Fatal("Route path is too short")
}
if stops[0] != "740000030" {
t.Errorf("Route does not start at Falun Centralstation (740000030), starts at %s", stops[0])
}
if stops[len(stops)-1] != "740000001" {
t.Errorf("Route does not end at Stockholm Centralstation (740000001), ends at %s", stops[len(stops)-1])
}
// Additional check: ensure all stops in path exist
for _, stopID := range stops {
if s, exists := tp.stops[stopID]; !exists {
t.Errorf("Stop %s in path does not exist", stopID)
} else {
t.Logf("stop: %s", s.StopName)
}
}
}
func TestFindRouteToMalmo(t *testing.T) {
tp := NewTripPlanner()
err := tp.LoadData("data")
if err != nil {
t.Fatalf("Failed to load data: %v", err)
}
err = tp.Preprocess()
if err != nil {
t.Fatalf("Failed to preprocess: %v", err)
}
route := tp.FindRoute("740000030", "740000003", time.Now())
if route == nil {
t.Fatal("No route found from Falun Centralstation to Malmö Centralstation")
}
if len(route.Legs) < 1 {
t.Fatal("Route has no legs")
}
stops := []string{}
if len(route.Legs) > 0 {
stops = append(stops, route.Legs[0].From)
for _, leg := range route.Legs {
stops = append(stops, leg.To)
}
}
if len(stops) < 2 {
t.Fatal("Route path is too short")
}
if stops[0] != "740000030" {
t.Errorf("Route does not start at Falun Centralstation (740000030), starts at %s", stops[0])
}
if stops[len(stops)-1] != "740000003" {
t.Errorf("Route does not end at Malmö Centralstation (740000003), ends at %s", stops[len(stops)-1])
}
// Additional check: ensure all stops in path exist
for _, stopID := range stops {
if _, exists := tp.stops[stopID]; !exists {
t.Errorf("Stop %s in path does not exist", stopID)
}
}
}