Add additional keys for database.Service struct
All checks were successful
Go Test / test (push) Successful in 30s

This commit is contained in:
Fred Boniface 2024-04-08 11:44:36 +01:00
parent d0c9250afa
commit c4d680116a
2 changed files with 170 additions and 6 deletions

View File

@ -21,8 +21,12 @@ func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database
PlanSpeed: parseSpeed(&input.ScheduleSegment.CifSpeed),
ScheduleStartDate: ParseCifDate(&input.ScheduleStartDate, "start"),
ScheduleEndDate: ParseCifDate(&input.ScheduleEndDate, "end"),
DaysRun: parseDaysRun(&input.ScheduleDaysRun),
Stops: parseStops(&input.ScheduleSegment.ScheduleLocation),
FirstClass: hasFirstClass(&input.ScheduleSegment.CifTrainClass),
Catering: hasCatering(&input.ScheduleSegment.CifCateringCode),
// Remove CateringType from type definitions in Go and TS
Sleeper: hasSleeper(&input.ScheduleSegment.CifSleepers),
DaysRun: parseDaysRun(&input.ScheduleDaysRun),
Stops: parseStops(&input.ScheduleSegment.ScheduleLocation),
}
// New struct tags are not yet parsed. Ensure all struct tags are present.
@ -32,8 +36,10 @@ func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database
// Converts CifSpeed input string to an int32, automatically corrects VSTP speeds which are not actual speed values
func parseSpeed(CIFSpeed *string) int32 {
log.Msg.Debug("CIFSpeed Input: '" + *CIFSpeed + "'")
if *CIFSpeed == "" || CIFSpeed == nil {
if CIFSpeed == nil {
return 0
}
if *CIFSpeed == "" {
log.Msg.Debug("Speed data not provided")
return int32(0)
}
@ -75,10 +81,34 @@ func parseStops(input *[]upstreamApi.CifScheduleLocation) []database.Stop {
return output
}
// Not tested
// Ascertains whether a given location is a public stop or not
func isPublic(input *upstreamApi.CifScheduleLocation) bool {
if input.PublicArrival == "" && input.PublicDeparture == "" {
return false
}
return true
}
// Ascertains whether the service offers first class
func hasFirstClass(input *string) bool {
if input == nil {
return false
}
return *input != "S"
}
// Ascertains whether the service offers catering
func hasCatering(input *string) bool {
if input == nil {
return false
}
return *input != ""
}
// Ascertains whether the service offers sleeping berths
func hasSleeper(input *string) bool {
if input == nil {
return false
}
return *input != ""
}

View File

@ -1,6 +1,10 @@
package cif
import "testing"
import (
"testing"
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
)
func TestParseSpeed(t *testing.T) {
testCases := []struct {
@ -24,4 +28,134 @@ func TestParseSpeed(t *testing.T) {
t.Errorf("For speed: %s, expected: %d, but got: %d", tc.input, tc.expected, result)
}
}
nilResult := parseSpeed(nil)
if nilResult != 0 {
t.Errorf("parseSpeed failed to handle nil pointer, expected %d, but got %d", 0, nilResult)
}
}
func TestIsPublic(t *testing.T) {
testCases := []struct {
input upstreamApi.CifScheduleLocation
expect bool
}{
{upstreamApi.CifScheduleLocation{
LocationType: "LO",
RecordIdentity: "",
TiplocCode: "BATHSPA",
TiplocInstance: "0",
Arrival: "0422H",
Departure: "0445",
PublicDeparture: "0445",
Pass: "",
Path: "UFR",
Platform: "1",
Line: "DFR",
EngineeringAllowance: "",
PathingAllowance: "",
PerformanceAllowance: "",
}, true},
{upstreamApi.CifScheduleLocation{
LocationType: "LO",
RecordIdentity: "",
TiplocCode: "BATHSPA",
TiplocInstance: "0",
Arrival: "0422H",
Departure: "0445",
PublicDeparture: "",
Pass: "",
Path: "UFR",
Platform: "1",
Line: "DFR",
EngineeringAllowance: "",
PathingAllowance: "",
PerformanceAllowance: "",
}, false},
}
for num, tc := range testCases {
result := isPublic(&tc.input)
if result != tc.expect {
t.Errorf("For testCase %d, expected %t, but got %t", num, tc.expect, result)
}
}
}
func TestHasFirstClass(t *testing.T) {
testCases := []struct {
input string
expect bool
}{
{"", true},
{"B", true},
{"S", false},
}
for _, tc := range testCases {
result := hasFirstClass(&tc.input)
if result != tc.expect {
t.Errorf("For %s, expected %t, but got %t", tc.input, tc.expect, result)
}
}
nilResult := hasFirstClass(nil)
if nilResult {
t.Errorf("hasFirstClass failed to handle nil pointer, expected %t, got %t", false, nilResult)
}
}
func TestHasCatering(t *testing.T) {
testCases := []struct {
input string
expect bool
}{
{"", false},
{"CF", true},
{"HT", true},
{"MR", true},
{"RP", true},
{"T", true},
{"F", true},
}
for _, tc := range testCases {
result := hasCatering(&tc.input)
if result != tc.expect {
t.Errorf("For %s, expected %t, but got %t", tc.input, tc.expect, result)
}
}
nilResult := hasCatering(nil)
if nilResult {
t.Errorf("hasCatering failed to handle nil pointer, expected %t, but got %t", false, nilResult)
}
}
func TestHasSleeper(t *testing.T) {
testCases := []struct {
input string
expect bool
}{
{"B", true},
{"F", true},
{"S", true},
{"", false},
}
for _, tc := range testCases {
result := hasSleeper(&tc.input)
if result != tc.expect {
t.Errorf("For %s, expected %t, but got %t", tc.input, tc.expect, result)
}
}
nilResult := hasSleeper(nil)
if nilResult {
t.Errorf("hasSleeper failed to handle nil pointer, expected %t, but got %t", false, nilResult)
}
}