83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package cif
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
|
|
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
|
)
|
|
|
|
func TestGenerateMetadata(t *testing.T) {
|
|
header := &upstreamApi.JsonTimetableV1{
|
|
Classification: "public",
|
|
Timestamp: 1711227636,
|
|
Owner: "Network Rail",
|
|
Sender: upstreamApi.TimetableSender{
|
|
Organisation: "Rockshore",
|
|
Application: "NTROD",
|
|
Component: "SCHEDULE",
|
|
},
|
|
Metadata: upstreamApi.TimetableMetadata{
|
|
Type: "update",
|
|
Sequence: 4307,
|
|
},
|
|
}
|
|
|
|
expected := &dbAccess.CifMetadata{
|
|
Doctype: dbAccess.Doctype,
|
|
LastTimestamp: header.Timestamp,
|
|
LastSequence: header.Metadata.Sequence,
|
|
LastUpdate: time.Now().In(londonTimezone),
|
|
}
|
|
|
|
result := generateMetadata(header)
|
|
|
|
if result == nil {
|
|
t.Errorf("generateMetadata returned nil pointer")
|
|
return // Static type checking likes this return to be here, even if it is redundant in reality.
|
|
}
|
|
|
|
if result.Doctype != expected.Doctype {
|
|
t.Errorf("Doctype: expected %s, got %s", expected.Doctype, result.Doctype)
|
|
}
|
|
|
|
if result.LastTimestamp != expected.LastTimestamp {
|
|
t.Errorf("LastTimestamp: expected %d, got %d", expected.LastTimestamp, result.LastTimestamp)
|
|
}
|
|
|
|
if result.LastSequence != expected.LastSequence {
|
|
t.Errorf("LastSequence: expected %d, got %d", expected.LastSequence, result.LastSequence)
|
|
}
|
|
|
|
tolerance := time.Second
|
|
if !result.LastUpdate.Before(expected.LastUpdate.Add(tolerance)) ||
|
|
!result.LastUpdate.After(expected.LastUpdate.Add(-tolerance)) {
|
|
t.Errorf("LastUpdate: expected %s, got %s", expected.LastUpdate, result.LastUpdate)
|
|
}
|
|
}
|
|
|
|
func TestIsAfterYesterdayAt0600(t *testing.T) {
|
|
yesterday0600 := time.Now().In(londonTimezone).AddDate(0, 0, -1).Truncate(24 * time.Hour).Add(6 * time.Hour)
|
|
|
|
testCases := []struct {
|
|
input time.Time
|
|
expect bool
|
|
}{
|
|
{yesterday0600.Add(-1 * time.Hour), false},
|
|
{yesterday0600.Add(-12 * time.Hour), false},
|
|
{yesterday0600.Add(-24 * time.Hour), false},
|
|
{yesterday0600.Add(1 * time.Microsecond), true},
|
|
{yesterday0600.Add(1 * time.Hour), true},
|
|
{yesterday0600.Add(12 * time.Hour), true},
|
|
{yesterday0600.Add(24 * time.Hour), true},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
result := isAfterYesterdayAt0600(tc.input)
|
|
if result != tc.expect {
|
|
t.Errorf("For input %v, expected %t, but got %t", tc.input, tc.expect, result)
|
|
}
|
|
}
|
|
}
|