Skip to content

Commit

Permalink
Replaced TimeProfile from/to date pointers with concrete values
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Jun 5, 2024
1 parent 4993b61 commit 4401feb
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 60 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [ ] simulator get-device ReleaseDate
- [ ] 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
8 changes: 4 additions & 4 deletions types/time_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
type TimeProfile struct {
ID uint8 `json:"id"`
LinkedProfileID uint8 `json:"linked-profile,omitempty"`
From *Date `json:"start-date,omitempty"`
To *Date `json:"end-date,omitempty"`
From Date `json:"start-date,omitempty"`
To Date `json:"end-date,omitempty"`
Weekdays Weekdays `json:"weekdays,omitempty"`
Segments Segments `json:"segments,omitempty"`
}
Expand Down Expand Up @@ -48,8 +48,8 @@ func (t *TimeProfile) UnmarshalJSON(bytes []byte) error {
profile := struct {
ID uint8 `json:"id"`
LinkedProfileID uint8 `json:"linked-profile"`
From *Date `json:"start-date"`
To *Date `json:"end-date"`
From Date `json:"start-date"`
To Date `json:"end-date"`
Weekdays Weekdays `json:"weekdays"`
Segments Segments `json:"segments"`
}{
Expand Down
71 changes: 57 additions & 14 deletions types/time_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (
func TestTimeProfileToString(t *testing.T) {
expected := "4 2021-04-01:2021-12-29 Mon,Tue,Thurs,Sat,Sun 08:30-09:45,11:35-13:15,14:01-17:59 19"

from := ToDate(2021, time.April, 1)
to := ToDate(2021, time.December, 29)
profile := TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: &from,
To: &to,
From: MustParseDate("2021-04-01"),
To: MustParseDate("2021-12-29"),
Weekdays: Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -64,14 +62,11 @@ func TestTimeProfileJSONMarshal(t *testing.T) {
]
}`

from := ToDate(2021, time.April, 1)
to := ToDate(2021, time.December, 29)

profile := TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: &from,
To: &to,
From: MustParseDate("2021-04-01"),
To: MustParseDate("2021-12-29"),
Weekdays: Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -99,14 +94,11 @@ func TestTimeProfileJSONMarshal(t *testing.T) {
}

func TestTimeProfileJSONUnmarshal(t *testing.T) {
from := ToDate(2021, time.April, 1)
to := ToDate(2021, time.December, 29)

expected := TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: &from,
To: &to,
From: MustParseDate("2021-04-01"),
To: MustParseDate("2021-12-29"),
Weekdays: Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -153,3 +145,54 @@ func TestTimeProfileJSONUnmarshal(t *testing.T) {
t.Errorf("TimeProfile incorrectly unmarshalled\n expected:%+v\n got: %+v", expected, profile)
}
}

func TestTimeProfileJSONUnmarshalWithMissingDates(t *testing.T) {
expected := TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: Date{},
To: Date{},
Weekdays: Weekdays{
time.Monday: true,
time.Tuesday: true,
time.Wednesday: false,
time.Thursday: true,
time.Friday: false,
time.Saturday: true,
time.Sunday: true,
},
Segments: Segments{
1: Segment{Start: hhmm("08:30"), End: hhmm("09:45")},
2: Segment{Start: hhmm("11:35")},
3: Segment{End: hhmm("17:59")},
},
}

bytes := []byte(`{
"id": 4,
"linked-profile": 19,
"weekdays": "Monday,Tuesday,Thursday,Saturday,Sunday",
"segments": [
{
"start": "08:30",
"end": "09:45"
},
{
"start": "11:35"
},
{
"end": "17:59"
}
]
}`)

var profile TimeProfile

if err := json.Unmarshal(bytes, &profile); err != nil {
t.Fatalf("Error unmarshalling TimeProfile (%v)", err)
}

if !reflect.DeepEqual(profile, expected) {
t.Errorf("TimeProfile incorrectly unmarshalled\n expected:%+v\n got: %+v", expected, profile)
}
}
8 changes: 4 additions & 4 deletions uhppote/get_time_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func (u *uhppote) GetTimeProfile(deviceID uint32, profileID uint8) (*types.TimeP
return nil, nil
}

// NTS: nil pointer replaced with zero value and zero value is (possibly) valid
// NTS: nil pointer replaced with zero value and zero value is theoretically possible (albeit invalid)
// if reply.From == nil {
// return nil, fmt.Errorf("invalid 'from' date in response")
// }

// NTS: nil pointer replaced with zero value and zero value is (possibly) valid
// NTS: nil pointer replaced with zero value and zero value is theoretically possible (albeit invalid)
// if reply.To == nil {
// return nil, fmt.Errorf("invalid 'to' date in response")
// }
Expand Down Expand Up @@ -69,8 +69,8 @@ func (u *uhppote) GetTimeProfile(deviceID uint32, profileID uint8) (*types.TimeP
profile := types.TimeProfile{
ID: reply.ProfileID,
LinkedProfileID: reply.LinkedProfileID,
From: &reply.From,
To: &reply.To,
From: reply.From,
To: reply.To,

Weekdays: types.Weekdays{
time.Monday: reply.Monday,
Expand Down
4 changes: 2 additions & 2 deletions uhppote/get_time_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestGetTimeProfile(t *testing.T) {
expected := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down
10 changes: 6 additions & 4 deletions uhppote/set_time_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ func (u *uhppote) SetTimeProfile(deviceID uint32, profile types.TimeProfile) (bo
return false, fmt.Errorf("invalid device ID (%v)", deviceID)
}

if profile.From == nil {
// NTS: zero value 'from' date may be allowed albeit invalid
if profile.From.IsZero() {
return false, fmt.Errorf("time profile requires a valid 'from' date")
}

if profile.To == nil {
// NTS: zero value 'to' date may be allowed albeit invalid
if profile.To.IsZero() {
return false, fmt.Errorf("time profile requires a valid 'to' date")
}

Expand All @@ -32,8 +34,8 @@ func (u *uhppote) SetTimeProfile(deviceID uint32, profile types.TimeProfile) (bo
request := messages.SetTimeProfileRequest{
SerialNumber: types.SerialNumber(deviceID),
ProfileID: profile.ID,
From: *profile.From,
To: *profile.To,
From: profile.From,
To: profile.To,
Monday: profile.Weekdays[time.Monday],
Tuesday: profile.Weekdays[time.Tuesday],
Wednesday: profile.Weekdays[time.Wednesday],
Expand Down
52 changes: 26 additions & 26 deletions uhppote/set_time_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestSetTimeProfile(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestSetTimeProfileWithInvalidFromDate(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
To: date("2021-12-29"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestSetTimeProfileWithInvalidToDate(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
From: types.MustParseDate("2021-04-01"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -158,8 +158,8 @@ func TestSetTimeProfileWithMissingSegment1(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -204,8 +204,8 @@ func TestSetTimeProfileWithMissingSegment2(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -250,8 +250,8 @@ func TestSetTimeProfileWithMissingSegment3(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -296,8 +296,8 @@ func TestSetTimeProfileWithMissingSegment1End(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -343,8 +343,8 @@ func TestSetTimeProfileWithInvalidSegment1End(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -390,8 +390,8 @@ func TestSetTimeProfileWithMissingSegment2End(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -437,8 +437,8 @@ func TestSetTimeProfileWithInvalidSegment2End(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -484,8 +484,8 @@ func TestSetTimeProfileWithInvalidSegment3Start(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -531,8 +531,8 @@ func TestSetTimeProfileWithInvalidSegment3End(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -578,8 +578,8 @@ func TestSetTimeProfileWithInvalidResponse(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down Expand Up @@ -614,8 +614,8 @@ func TestSetTimeProfileWithInvalidDeviceID(t *testing.T) {
profile := types.TimeProfile{
ID: 4,
LinkedProfileID: 19,
From: date("2021-04-01"),
To: date("2021-12-29"),
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Weekdays: types.Weekdays{
time.Monday: true,
time.Tuesday: true,
Expand Down
6 changes: 0 additions & 6 deletions uhppote/uhppote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ func (d *stub) Listen(signal chan interface{}, done chan interface{}, handler fu
return fmt.Errorf("NOT IMPLEMENTED")
}

var date = func(s string) *types.Date {
d, _ := time.ParseInLocation("2006-01-02", s, time.Local)
p := types.Date(d)
return &p
}

func TestBroadcastAddressRequest(t *testing.T) {
expected := messages.DeleteCardResponse{
MsgType: 0x52,
Expand Down

0 comments on commit 4401feb

Please sign in to comment.