Skip to content

Commit

Permalink
handle empty client blockes service schedule in equals check (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed May 3, 2024
1 parent 9a32a1d commit 4afccfa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/client/model/model-functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,40 @@ func (cl *Client) Sort() {
}
}

// PrepareDiff timezone BlockedServicesSchedule might differ if all other fields are empty,
// so we skip it in diff
func (cl *Client) PrepareDiff() *string {
var tz *string
bss := cl.BlockedServicesSchedule
if bss != nil && bss.Mon == nil && bss.Tue == nil && bss.Wed == nil &&
bss.Thu == nil && bss.Fri == nil && bss.Sat == nil && bss.Sun == nil {

tz = cl.BlockedServicesSchedule.TimeZone
cl.BlockedServicesSchedule.TimeZone = nil
}
return tz
}

// AfterDiff reset after diff
func (cl *Client) AfterDiff(tz *string) {
if cl.BlockedServicesSchedule != nil {
cl.BlockedServicesSchedule.TimeZone = tz
}
}

// Equals Clients equal check
func (cl *Client) Equals(o *Client) bool {
cl.Sort()
o.Sort()

bssCl := cl.PrepareDiff()
bssO := o.PrepareDiff()

defer func() {
cl.AfterDiff(bssCl)
o.AfterDiff(bssO)
}()

return utils.JsonEquals(cl, o)
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/client/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,23 @@ var _ = Describe("Types", func() {
})
})
})

Context("Client", func() {
Context("Equals", func() {
var (
cl1 *model.Client
cl2 *model.Client
)
BeforeEach(func() {
cl1 = &model.Client{Name: utils.Ptr("foo"), BlockedServicesSchedule: &model.Schedule{TimeZone: utils.Ptr("UTC")}}
cl2 = &model.Client{Name: utils.Ptr("foo"), BlockedServicesSchedule: &model.Schedule{TimeZone: utils.Ptr("Local")}}
})

It("should equal if only timezone differs on empty blocked service schedule", func() {
Ω(cl1.Equals(cl2)).Should(BeTrue())
})
})
})
Context("BlockedServices", func() {
Context("Equals", func() {
It("should be equal", func() {
Expand Down

0 comments on commit 4afccfa

Please sign in to comment.