Skip to content

Commit

Permalink
Replace test usage of types.ToDate with types.MustParseDate
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Jun 6, 2024
1 parent cd6c0e9 commit a18316a
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 101 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions messages/get_card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/uhppoted/uhppote-core/types"
"reflect"
"testing"
"time"
)

func TestMarshalGetCardByIndexRequest(t *testing.T) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
57 changes: 28 additions & 29 deletions types/card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"fmt"
"reflect"
"testing"
"time"
)

func TestCardToString(t *testing.T) {
expected := "12345 2020-01-01 2020-12-31 Y N 29 Y"

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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 12 additions & 16 deletions types/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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()
}
Expand Down
60 changes: 39 additions & 21 deletions types/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
}
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit a18316a

Please sign in to comment.