Skip to content

Commit

Permalink
feat: Enhance WithEmptyPayloadAndId method
Browse files Browse the repository at this point in the history
Enhance WithEmptyPayloadAndId method to avoid storing duplicate data in DB.

Signed-off-by: Jack Chen <jack@iotechsys.com>
  • Loading branch information
jackchenjc committed Aug 26, 2024
1 parent 2a18803 commit 5095a2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 8 additions & 5 deletions models/schedulejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func (c CronScheduleDef) GetBaseScheduleDef() BaseScheduleDef {

type ScheduleAction interface {
GetBaseScheduleAction() BaseScheduleAction
// WithEmptyPayload returns a copy of the ScheduleAction with empty payload, which is used by ScheduleActionRecord to remove the payload before storing the record into database
WithEmptyPayload() ScheduleAction
// WithEmptyPayloadAndId returns a copy of the ScheduleAction with empty payload and Id, which is used by ScheduleActionRecord to remove the payload and id before storing the record into database
WithEmptyPayloadAndId() ScheduleAction
// WithId returns a copy of the ScheduleAction with ID or generates a new ID if the ID is empty, which is used to identify the action and record in the database
WithId() ScheduleAction
}
Expand Down Expand Up @@ -197,7 +197,8 @@ type EdgeXMessageBusAction struct {
func (m EdgeXMessageBusAction) GetBaseScheduleAction() BaseScheduleAction {
return m.BaseScheduleAction
}
func (m EdgeXMessageBusAction) WithEmptyPayload() ScheduleAction {
func (m EdgeXMessageBusAction) WithEmptyPayloadAndId() ScheduleAction {
m.Id = ""
m.Payload = nil
return m
}
Expand All @@ -218,7 +219,8 @@ type RESTAction struct {
func (r RESTAction) GetBaseScheduleAction() BaseScheduleAction {
return r.BaseScheduleAction
}
func (r RESTAction) WithEmptyPayload() ScheduleAction {
func (r RESTAction) WithEmptyPayloadAndId() ScheduleAction {
r.Id = ""
r.Payload = nil
return r
}
Expand All @@ -238,7 +240,8 @@ type DeviceControlAction struct {
func (d DeviceControlAction) GetBaseScheduleAction() BaseScheduleAction {
return d.BaseScheduleAction
}
func (d DeviceControlAction) WithEmptyPayload() ScheduleAction {
func (d DeviceControlAction) WithEmptyPayloadAndId() ScheduleAction {
d.Id = ""
d.Payload = nil
return d
}
Expand Down
16 changes: 10 additions & 6 deletions models/schedulejob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ var scheduleJobWithInvalidScheduleAction = `{

var edgeXMessageBusAction = EdgeXMessageBusAction{
BaseScheduleAction: BaseScheduleAction{
Id: ExampleUUID,
Type: common.ActionEdgeXMessageBus,
ContentType: TestContentType,
Payload: []byte(TestPayload),
Expand All @@ -173,6 +174,7 @@ var edgeXMessageBusAction = EdgeXMessageBusAction{

var restAction = RESTAction{
BaseScheduleAction: BaseScheduleAction{
Id: ExampleUUID,
Type: common.ActionREST,
ContentType: TestContentType,
Payload: []byte(TestPayload),
Expand All @@ -182,6 +184,7 @@ var restAction = RESTAction{

var deviceControlAction = DeviceControlAction{
BaseScheduleAction: BaseScheduleAction{
Id: ExampleUUID,
Type: common.ActionDeviceControl,
ContentType: TestContentType,
Payload: []byte(TestPayload),
Expand Down Expand Up @@ -317,20 +320,21 @@ func TestScheduleAction_GetBaseScheduleAction(t *testing.T) {
}
}

func TestScheduleAction_WithEmptyPayload(t *testing.T) {
func TestScheduleAction_WithEmptyPayloadAndId(t *testing.T) {
tests := []struct {
name string
action ScheduleAction
expected ScheduleAction
}{
{"EdgeXMessageBusAction", edgeXMessageBusAction, edgeXMessageBusAction.WithEmptyPayload()},
{"RESTAction", restAction, restAction.WithEmptyPayload()},
{"DeviceControlAction", deviceControlAction, deviceControlAction.WithEmptyPayload()},
{"EdgeXMessageBusAction", edgeXMessageBusAction, edgeXMessageBusAction.WithEmptyPayloadAndId()},
{"RESTAction", restAction, restAction.WithEmptyPayloadAndId()},
{"DeviceControlAction", deviceControlAction, deviceControlAction.WithEmptyPayloadAndId()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.action.WithEmptyPayload()
assert.Nil(t, result.GetBaseScheduleAction().Payload, "WithEmptyPayload did not result in empty Payload.")
result := tt.action.WithEmptyPayloadAndId()
assert.Nil(t, result.GetBaseScheduleAction().Payload, "WithEmptyPayloadAndId did not result in empty Payload.")
assert.Equal(t, "", result.GetBaseScheduleAction().Id, "WithEmptyPayloadAndId did not result in empty Id.")
})
}
}
Expand Down

0 comments on commit 5095a2a

Please sign in to comment.