diff --git a/TODO.md b/TODO.md index 42033f9..7df4875 100644 --- a/TODO.md +++ b/TODO.md @@ -7,11 +7,11 @@ - [x] GetTimeProfile - [x] types.TimeProfile - [x] // FIXME: replace From in types.TimeProfile - - [ ] lib::encoding/tsv + - [x] Replace test usage of types.ToDate with types.MustParseDate - [ ] simulator get-device ReleaseDate + - [ ] lib::encoding/tsv - [ ] uhppoted-app-wild-apricot types.Date - [ ] uhppoted-rest acl.go - - (?) Remove types.ToDate (test only AFAIK) - [ ] Rework any remaining DateTime pointers to rather use IsZero/IsValid - [ ] Rework any remaining Time pointers to rather use IsZero/IsValid - (?) Replace (* UHPPOTE) in API functions with non-pointer version diff --git a/messages/get_card_test.go b/messages/get_card_test.go index 16a53bf..9131e1d 100644 --- a/messages/get_card_test.go +++ b/messages/get_card_test.go @@ -5,7 +5,6 @@ import ( "github.com/uhppoted/uhppote-core/types" "reflect" "testing" - "time" ) func TestMarshalGetCardByIndexRequest(t *testing.T) { @@ -74,12 +73,12 @@ func TestUnmarshalGetCardByIndexResponse(t *testing.T) { t.Errorf("Incorrect 'card number' - expected:%v, got:%v", 6154412, reply.CardNumber) } - from := types.ToDate(2019, time.February, 3) + from := types.MustParseDate("2019-02-03") if reply.From != from { t.Errorf("Incorrect 'from date' - expected:%v, got:%v", from, reply.From) } - to := types.ToDate(2019, time.December, 29) + to := types.MustParseDate("2019-12-29") if reply.To != types.Date(to) { t.Errorf("Incorrect 'to date' - expected:%v, got:%v", to, reply.To) } @@ -131,12 +130,12 @@ func TestUnmarshalGetCardWithPINByIndexResponse(t *testing.T) { t.Errorf("Incorrect 'card number' - expected:%v, got:%v", 6154412, reply.CardNumber) } - from := types.ToDate(2019, time.February, 3) + from := types.MustParseDate("2019-02-03") if reply.From != from { t.Errorf("Incorrect 'from date' - expected:%v, got:%v", from, reply.From) } - to := types.ToDate(2019, time.December, 29) + to := types.MustParseDate("2019-12-29") if reply.To != to { t.Errorf("Incorrect 'to date' - expected:%v, got:%v", to, reply.To) } @@ -239,12 +238,12 @@ func TestUnmarshalGetCardByIdResponse(t *testing.T) { t.Errorf("Incorrect 'card number' - expected:%v, got:%v", 6154412, reply.CardNumber) } - from := types.ToDate(2019, time.February, 03) + from := types.MustParseDate("2019-02-03") if reply.From != from { t.Errorf("Incorrect 'from date' - expected:%v, got:%v", from, reply.From) } - to := types.ToDate(2019, time.December, 29) + to := types.MustParseDate("2019-12-29") if reply.To != to { t.Errorf("Incorrect 'to date' - expected:%v, got:%v", to, reply.To) } @@ -296,12 +295,12 @@ func TestUnmarshalGetCardWithPINByIdResponse(t *testing.T) { t.Errorf("Incorrect 'card number' - expected:%v, got:%v", 6154412, reply.CardNumber) } - from := types.ToDate(2019, time.February, 3) + from := types.MustParseDate("2019-02-03") if reply.From != from { t.Errorf("Incorrect 'from date' - expected:%v, got:%v", from, reply.From) } - to := types.ToDate(2019, time.December, 29) + to := types.MustParseDate("2019-12-29") if reply.To != to { t.Errorf("Incorrect 'to date' - expected:%v, got:%v", to, reply.To) } diff --git a/types/card_test.go b/types/card_test.go index b458565..01d1423 100644 --- a/types/card_test.go +++ b/types/card_test.go @@ -5,7 +5,6 @@ import ( "fmt" "reflect" "testing" - "time" ) func TestCardToString(t *testing.T) { @@ -13,8 +12,8 @@ func TestCardToString(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -35,8 +34,8 @@ func TestCardWithPINToString(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -58,8 +57,8 @@ func TestCardWithInvalidPINToString(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -81,7 +80,7 @@ func TestCardToStringWithMissingFromDate(t *testing.T) { card := Card{ CardNumber: 12345, - To: ToDate(2020, time.December, 31), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -100,7 +99,7 @@ func TestCardToStringWithMissingFromDate(t *testing.T) { func TestCardToStringWithMissingToDate(t *testing.T) { expected := "12345 2020-01-01 - Y N N Y" - from := ToDate(2020, time.January, 1) + from := MustParseDate("2020-01-01") card := Card{ CardNumber: 12345, From: from, @@ -124,8 +123,8 @@ func TestCardToStringWithMissingDoors(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -144,8 +143,8 @@ func TestCardToStringWithExtraDoors(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -167,8 +166,8 @@ func TestCardMarshalJSON(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -203,8 +202,8 @@ func TestCardMarshalJSON(t *testing.T) { func TestCardWithPINMarshalJSON(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -241,8 +240,8 @@ func TestCardWithPINMarshalJSON(t *testing.T) { func TestCardWithInvalidPINMarshalJSON(t *testing.T) { card := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -278,8 +277,8 @@ func TestCardWithInvalidPINMarshalJSON(t *testing.T) { func TestUnmarshalCard(t *testing.T) { expected := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -316,8 +315,8 @@ func TestUnmarshalCard(t *testing.T) { func TestUnmarshalCardWithPIN(t *testing.T) { expected := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -355,8 +354,8 @@ func TestUnmarshalCardWithPIN(t *testing.T) { func TestUnmarshalCardWithMissingCardNumber(t *testing.T) { expected := Card{ CardNumber: 0, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -429,8 +428,8 @@ func TestUnmarshalCardWithMissingEndDate(t *testing.T) { func TestUnmarshalCardWithMissingDoors(t *testing.T) { expected := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 0, 2: 1, @@ -463,8 +462,8 @@ func TestUnmarshalCardWithMissingDoors(t *testing.T) { func TestUnmarshalCardWithInvalidDoors(t *testing.T) { expected := Card{ CardNumber: 12345, - From: ToDate(2020, time.January, 1), - To: ToDate(2020, time.December, 31), + From: MustParseDate("2020-01-01"), + To: MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, diff --git a/types/date.go b/types/date.go index ff18b59..3bd30cd 100644 --- a/types/date.go +++ b/types/date.go @@ -10,17 +10,9 @@ import ( type Date time.Time -func ToDate(year int, month time.Month, day int) Date { - date := time.Date(year, month, day, 0, 0, 0, 0, time.Local) - - return Date(date) -} - -/* - * MustParseDate invokes ParseDate and panics on error. - * - * It is intended for use in tests with hard-coded strings. - */ +// MustParseDate invokes ParseDate and panics on error. +// +// It is intended for use in tests with hard-coded strings. func MustParseDate(s string) Date { if date, err := ParseDate(s); err != nil { panic(err) @@ -29,11 +21,8 @@ func MustParseDate(s string) Date { } } -/* - * Parses a date string, returning a zero value Date{} and an - * error if the string is blank or not a valid date. - * - */ +// Parses a date string, returning a zero value Date{} and an +// error if the string is blank or not a valid date. func ParseDate(s string) (Date, error) { if s == "" { return Date{}, fmt.Errorf("blank date string") @@ -44,6 +33,13 @@ func ParseDate(s string) (Date, error) { } } +// Utility function to explicitly construct a Date from year, month and day. +func ToDate(year int, month time.Month, day int) Date { + date := time.Date(year, month, day, 0, 0, 0, 0, time.Local) + + return Date(date) +} + func (d Date) IsZero() bool { return time.Time(d).IsZero() } diff --git a/types/date_test.go b/types/date_test.go index 641a60b..7d5f58a 100644 --- a/types/date_test.go +++ b/types/date_test.go @@ -31,7 +31,25 @@ func TestDateString(t *testing.T) { } } -func TestDateFromString(t *testing.T) { +func TestToDate(t *testing.T) { + expected := Date(time.Date(2021, time.February, 28, 0, 0, 0, 0, time.Local)) + date := ToDate(2021, time.February, 28) + + if !reflect.DeepEqual(date, expected) { + t.Errorf("Date incorrectly unmarshaled - expected:%v, got:%v", time.Time(expected), time.Time(date)) + } +} + +func TestMustParseDate(t *testing.T) { + expected := Date(time.Date(2021, time.February, 28, 0, 0, 0, 0, time.Local)) + date := MustParseDate("2021-02-28") + + if !reflect.DeepEqual(date, expected) { + t.Errorf("Date incorrectly unmarshaled - expected:%v, got:%v", time.Time(expected), time.Time(date)) + } +} + +func TestParseDate(t *testing.T) { expected := Date(time.Date(2021, time.February, 28, 0, 0, 0, 0, time.Local)) date, err := ParseDate("2021-02-28") @@ -104,17 +122,17 @@ func TestDateBefore(t *testing.T) { q Date expected bool }{ - {ToDate(2020, time.May, 5), ToDate(2021, time.May, 5), true}, - {ToDate(2021, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2022, time.May, 5), ToDate(2021, time.May, 5), false}, + {MustParseDate("2020-05-05"), MustParseDate("2021-05-05"), true}, + {MustParseDate("2021-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2022-05-05"), MustParseDate("2021-05-05"), false}, - {ToDate(2021, time.April, 5), ToDate(2021, time.May, 5), true}, - {ToDate(2021, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.June, 5), ToDate(2021, time.May, 5), false}, + {MustParseDate("2021-04-05"), MustParseDate("2021-05-05"), true}, + {MustParseDate("2021-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-06-05"), MustParseDate("2021-05-05"), false}, - {ToDate(2021, time.May, 4), ToDate(2021, time.May, 5), true}, - {ToDate(2021, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.May, 6), ToDate(2021, time.May, 5), false}, + {MustParseDate("2021-05-04"), MustParseDate("2021-05-05"), true}, + {MustParseDate("2021-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-05-06"), MustParseDate("2021-05-05"), false}, } for _, v := range tests { @@ -143,17 +161,17 @@ func TestDateAfter(t *testing.T) { q Date expected bool }{ - {ToDate(2020, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2022, time.May, 5), ToDate(2021, time.May, 5), true}, + {MustParseDate("2020-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2022-05-05"), MustParseDate("2021-05-05"), true}, - {ToDate(2021, time.April, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.June, 5), ToDate(2021, time.May, 5), true}, + {MustParseDate("2021-04-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-06-05"), MustParseDate("2021-05-05"), true}, - {ToDate(2021, time.May, 4), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.May, 5), ToDate(2021, time.May, 5), false}, - {ToDate(2021, time.May, 6), ToDate(2021, time.May, 5), true}, + {MustParseDate("2021-05-04"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-05-05"), MustParseDate("2021-05-05"), false}, + {MustParseDate("2021-05-06"), MustParseDate("2021-05-05"), true}, } for _, v := range tests { @@ -243,7 +261,7 @@ func TestDateUnmarshalUT0311L0x(t *testing.T) { expected Date isZero bool }{ - {[]byte{0x20, 0x21, 0x02, 0x28}, ToDate(2021, time.February, 28), false}, + {[]byte{0x20, 0x21, 0x02, 0x28}, MustParseDate("2021-02-28"), false}, {[]byte{0x00, 0x00, 0x00, 0x00}, Date{}, true}, {[]byte{0x00, 0x01, 0x01, 0x01}, Date{}, true}, } @@ -281,7 +299,7 @@ func TestDateUnmarshalUT0311L0xWithInvalidDate(t *testing.T) { } func TestDatePtrUnmarshalUT0311L0x(t *testing.T) { - d20210228 := ToDate(2021, time.February, 28) + d20210228 := MustParseDate("2021-02-28") tests := []struct { bytes []byte diff --git a/types/task_test.go b/types/task_test.go index c5226a4..d7176d4 100644 --- a/types/task_test.go +++ b/types/task_test.go @@ -14,8 +14,8 @@ func TestTaskToString(t *testing.T) { task := Task{ Task: EnableTimeProfile, Door: 3, - From: ToDate(2021, time.April, 1), - To: ToDate(2021, time.December, 29), + From: MustParseDate("2021-04-01"), + To: MustParseDate("2021-12-29"), Weekdays: Weekdays{ time.Monday: true, time.Tuesday: true, @@ -42,8 +42,8 @@ func TestTaskToStringWithEnableMoreCards(t *testing.T) { task := Task{ Task: EnableMoreCards, Door: 3, - From: ToDate(2021, time.April, 1), - To: ToDate(2021, time.December, 29), + From: MustParseDate("2021-04-01"), + To: MustParseDate("2021-12-29"), Weekdays: Weekdays{ time.Monday: true, time.Tuesday: true, @@ -78,8 +78,8 @@ func TestTaskJSONMarshal(t *testing.T) { task := Task{ Task: EnableTimeProfile, Door: 3, - From: ToDate(2021, time.April, 1), - To: ToDate(2021, time.December, 29), + From: MustParseDate("2021-04-01"), + To: MustParseDate("2021-12-29"), Weekdays: Weekdays{ time.Monday: true, time.Tuesday: true, @@ -105,8 +105,8 @@ func TestTaskJSONMarshal(t *testing.T) { func TestTaskJSONUnmarshal(t *testing.T) { expected := Task{ - From: ToDate(2021, time.April, 1), - To: ToDate(2021, time.December, 29), + From: MustParseDate("2021-04-01"), + To: MustParseDate("2021-12-29"), Weekdays: Weekdays{ time.Monday: true, time.Tuesday: true, diff --git a/uhppote/get_card_test.go b/uhppote/get_card_test.go index 4fc908c..626d919 100644 --- a/uhppote/get_card_test.go +++ b/uhppote/get_card_test.go @@ -4,7 +4,6 @@ import ( "net" "reflect" "testing" - "time" "github.com/uhppoted/uhppote-core/types" ) @@ -19,8 +18,8 @@ func TestGetCardByID(t *testing.T) { expected := types.Card{ CardNumber: 6129876, - From: types.ToDate(2020, time.January, 1), - To: types.ToDate(2020, time.December, 31), + From: types.MustParseDate("2020-01-01"), + To: types.MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -63,7 +62,7 @@ func TestGetCardByIDWithInvalidFromDate(t *testing.T) { expected := types.Card{ CardNumber: 6129876, From: types.Date{}, - To: types.ToDate(2023, time.December, 31), + To: types.MustParseDate("2023-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -105,7 +104,7 @@ func TestGetCardByIDWithInvalidToDate(t *testing.T) { expected := types.Card{ CardNumber: 6129876, - From: types.ToDate(2023, time.January, 1), + From: types.MustParseDate("2023-01-01"), To: types.Date{}, Doors: map[uint8]uint8{ 1: 1, @@ -157,8 +156,8 @@ func TestGetCardByIndex(t *testing.T) { expected := types.Card{ CardNumber: 6129876, - From: types.ToDate(2020, time.January, 1), - To: types.ToDate(2020, time.December, 31), + From: types.MustParseDate("2020-01-01"), + To: types.MustParseDate("2020-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -201,7 +200,7 @@ func TestGetCardByIndexWithInvalidFromDate(t *testing.T) { expected := types.Card{ CardNumber: 6129876, From: types.Date{}, - To: types.ToDate(2023, time.December, 31), + To: types.MustParseDate("2023-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -243,7 +242,7 @@ func TestGetCardByIndexWithInvalidToDate(t *testing.T) { expected := types.Card{ CardNumber: 6129876, - From: types.ToDate(2023, time.January, 1), + From: types.MustParseDate("2023-01-01"), To: types.Date{}, Doors: map[uint8]uint8{ 1: 1, diff --git a/uhppote/put_card_test.go b/uhppote/put_card_test.go index c88a849..06d173d 100644 --- a/uhppote/put_card_test.go +++ b/uhppote/put_card_test.go @@ -6,7 +6,6 @@ import ( "fmt" "net" "testing" - "time" "github.com/uhppoted/uhppote-core/types" ) @@ -32,8 +31,8 @@ func TestPutCard(t *testing.T) { }, card: types.Card{ CardNumber: 6129876, - From: types.ToDate(2023, time.January, 1), - To: types.ToDate(2023, time.December, 31), + From: types.MustParseDate("2023-01-01"), + To: types.MustParseDate("2023-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -58,8 +57,8 @@ func TestPutCard(t *testing.T) { card := types.Card{ CardNumber: 6129876, - From: types.ToDate(2023, time.January, 1), - To: types.ToDate(2023, time.December, 31), + From: types.MustParseDate("2023-01-01"), + To: types.MustParseDate("2023-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0, @@ -97,8 +96,8 @@ func TestPutCardWithInvalidCardNumber(t *testing.T) { for _, v := range cards { card := types.Card{ CardNumber: v, - From: types.ToDate(2023, time.January, 1), - To: types.ToDate(2023, time.December, 31), + From: types.MustParseDate("2023-01-01"), + To: types.MustParseDate("2023-12-31"), Doors: map[uint8]uint8{ 1: 1, 2: 0,