From 2fdb840644bcc602e5f76a2e465768dd6e5a4da2 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 4 Apr 2024 13:58:18 +0100 Subject: [PATCH] Add test to cif package --- src/cif/check.go | 15 +++++++++++++-- src/cif/constants.go | 8 ++++++++ src/cif/helpers.go | 3 ++- src/cif/helpers_test.go | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/cif/helpers_test.go diff --git a/src/cif/check.go b/src/cif/check.go index 8570877..be375f4 100644 --- a/src/cif/check.go +++ b/src/cif/check.go @@ -1,6 +1,8 @@ package cif import ( + "time" + "git.fjla.uk/owlboard/timetable-mgr/dbAccess" "git.fjla.uk/owlboard/timetable-mgr/helpers" "git.fjla.uk/owlboard/timetable-mgr/log" @@ -59,7 +61,10 @@ func parseMetadata(metadata *dbAccess.CifMetadata, cfg *helpers.Configuration) e // what days data needs updating, then calls an update function to handle the update. func CheckCif(cfg *helpers.Configuration) { // Check that it is after 0600, if not then skip update - // Not written yet + if time.Now().In(londonTimezone).Hour() <= dataAvailable { + log.Msg.Info("Too early to update CIF data, not published until 0600") + return + } log.Msg.Info("Checking age of CIF Data") @@ -80,7 +85,13 @@ func CheckCif(cfg *helpers.Configuration) { return } - // Check if last update was before today + // Check if last update was today + if isSameToday(metadata.LastUpdate) { + log.Msg.Info("CIF Data has already been updated today, skipping") + return + } + + // // Here I need to determine which days I need to update CIF data for, then pass to an appropriate function: // newMetadata, err := runCifUpdate(days []time.Time, cfg) diff --git a/src/cif/constants.go b/src/cif/constants.go index 262b901..286dd44 100644 --- a/src/cif/constants.go +++ b/src/cif/constants.go @@ -1,7 +1,15 @@ package cif +import "time" + // The URL required for a daily update of the CIF Data - The 'day string' must be appended const dailyUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_ALL_UPDATE_DAILY&day=toc-update-" // The URL required for a full fetch of the CIF Data const fullUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_ALL_FULL_DAILY&day=toc-full" + +// The time at which CIF Data is expected to be available for download (full hour) +const dataAvailable = 6 + +// An object representing the Europe/London timezone +var londonTimezone, err = time.LoadLocation("Europe/London") diff --git a/src/cif/helpers.go b/src/cif/helpers.go index e093b16..8ead8ae 100644 --- a/src/cif/helpers.go +++ b/src/cif/helpers.go @@ -37,8 +37,9 @@ func getUpdateUrl(updateType string) (string, error) { // Takes a time.Time as input and returns True if it is // the same day as now, or false if it is not the same day as now func isSameToday(t time.Time) bool { + test := t.In(time.UTC) today := time.Now().In(time.UTC) - return t.Year() == today.Year() && t.Month() == today.Month() && t.Day() == today.Day() + return test.Year() == today.Year() && test.Month() == today.Month() && test.Day() == today.Day() } // Returns how many days ago `t` was compared to today diff --git a/src/cif/helpers_test.go b/src/cif/helpers_test.go new file mode 100644 index 0000000..49cff57 --- /dev/null +++ b/src/cif/helpers_test.go @@ -0,0 +1,19 @@ +package cif + +import ( + "testing" + "time" +) + +func TestIsSameDay(t *testing.T) { + today := time.Now() + notToday := time.Date(2024, 01, 23, 23, 01, 3, 421, time.Local) + + if !isSameToday(today) { + t.Errorf("Error in isSameDay(today). Expected true, got false.") + } + + if isSameToday(notToday) { + t.Errorf("Error in isSameDay(notToday). Expected false, got true.") + } +}