Add test to cif package

This commit is contained in:
Fred Boniface 2024-04-04 13:58:18 +01:00
parent c8e6b9d7c8
commit 2fdb840644
4 changed files with 42 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package cif package cif
import ( import (
"time"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess" "git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/helpers" "git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log" "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. // what days data needs updating, then calls an update function to handle the update.
func CheckCif(cfg *helpers.Configuration) { func CheckCif(cfg *helpers.Configuration) {
// Check that it is after 0600, if not then skip update // 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") log.Msg.Info("Checking age of CIF Data")
@ -80,7 +85,13 @@ func CheckCif(cfg *helpers.Configuration) {
return 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: // 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) // newMetadata, err := runCifUpdate(days []time.Time, cfg)

View File

@ -1,7 +1,15 @@
package cif package cif
import "time"
// The URL required for a daily update of the CIF Data - The 'day string' must be appended // 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-" 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 // 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" 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")

View File

@ -37,8 +37,9 @@ func getUpdateUrl(updateType string) (string, error) {
// Takes a time.Time as input and returns True if it is // 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 // the same day as now, or false if it is not the same day as now
func isSameToday(t time.Time) bool { func isSameToday(t time.Time) bool {
test := t.In(time.UTC)
today := time.Now().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 // Returns how many days ago `t` was compared to today

19
src/cif/helpers_test.go Normal file
View File

@ -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.")
}
}