timetable-mgr/cif/metadata_test.go

102 lines
2.9 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)
}
}
}
func TestIsTimestampWithinLastFiveDays(t *testing.T) {
testCases := []struct {
name string
ts int64
expect bool
}{
{"WithinLastFiveDays", time.Now().AddDate(0, 0, -3).Unix(), true},
{"ExactlyFiveDaysAgo", time.Now().AddDate(0, 0, -5).Unix(), false}, //False due to elapsed time during test
{"MoreThanFiveDaysAgo", time.Now().AddDate(0, 0, -7).Unix(), false},
}
for _, tc := range testCases {
result := isTimestampWithinLastFiveDays(tc.ts)
if result != tc.expect {
t.Errorf("For %s (%d), expected %t, but got %t", tc.name, tc.ts, tc.expect, result)
}
}
}