-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix preservation action started at time
Mark `sql.NullTime` as valid before sending to persistence.
- Loading branch information
Showing
2 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package workflow | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"go.artefactual.dev/tools/mockutil" | ||
temporalsdk_testsuite "go.temporal.io/sdk/testsuite" | ||
"go.uber.org/mock/gomock" | ||
"gotest.tools/v3/assert" | ||
|
||
"github.com/artefactual-sdps/enduro/internal/datatypes" | ||
"github.com/artefactual-sdps/enduro/internal/enums" | ||
package_fake "github.com/artefactual-sdps/enduro/internal/package_/fake" | ||
) | ||
|
||
func TestCreatePreservationActionLocalActivity(t *testing.T) { | ||
t.Parallel() | ||
|
||
startedAt := time.Date(2024, 6, 13, 17, 50, 13, 0, time.UTC) | ||
completedAt := time.Date(2024, 6, 13, 17, 50, 14, 0, time.UTC) | ||
|
||
type test struct { | ||
name string | ||
params *createPreservationActionLocalActivityParams | ||
mockCalls func(m *package_fake.MockServiceMockRecorder) | ||
want uint | ||
wantErr string | ||
} | ||
for _, tt := range []test{ | ||
{ | ||
name: "Creates a preservation action", | ||
params: &createPreservationActionLocalActivityParams{ | ||
WorkflowID: "workflow-id", | ||
Type: enums.PreservationActionTypeCreateAip, | ||
Status: enums.PreservationActionStatusDone, | ||
StartedAt: startedAt, | ||
CompletedAt: completedAt, | ||
PackageID: 1, | ||
}, | ||
mockCalls: func(m *package_fake.MockServiceMockRecorder) { | ||
m.CreatePreservationAction(mockutil.Context(), &datatypes.PreservationAction{ | ||
WorkflowID: "workflow-id", | ||
Type: enums.PreservationActionTypeCreateAip, | ||
Status: enums.PreservationActionStatusDone, | ||
StartedAt: sql.NullTime{Time: startedAt, Valid: true}, | ||
CompletedAt: sql.NullTime{Time: completedAt, Valid: true}, | ||
PackageID: 1, | ||
}).DoAndReturn(func(ctx context.Context, pa *datatypes.PreservationAction) error { | ||
pa.ID = 1 | ||
return nil | ||
}) | ||
}, | ||
want: 1, | ||
}, | ||
{ | ||
name: "Does not pass zero dates", | ||
params: &createPreservationActionLocalActivityParams{ | ||
WorkflowID: "workflow-id", | ||
Type: enums.PreservationActionTypeCreateAip, | ||
Status: enums.PreservationActionStatusDone, | ||
PackageID: 1, | ||
}, | ||
mockCalls: func(m *package_fake.MockServiceMockRecorder) { | ||
m.CreatePreservationAction(mockutil.Context(), &datatypes.PreservationAction{ | ||
WorkflowID: "workflow-id", | ||
Type: enums.PreservationActionTypeCreateAip, | ||
Status: enums.PreservationActionStatusDone, | ||
PackageID: 1, | ||
}).DoAndReturn(func(ctx context.Context, pa *datatypes.PreservationAction) error { | ||
pa.ID = 1 | ||
return nil | ||
}) | ||
}, | ||
want: 1, | ||
}, | ||
{ | ||
name: "Fails if there is a persistence error", | ||
params: &createPreservationActionLocalActivityParams{ | ||
WorkflowID: "workflow-id", | ||
Type: enums.PreservationActionTypeCreateAip, | ||
Status: enums.PreservationActionStatusDone, | ||
PackageID: 1, | ||
}, | ||
mockCalls: func(m *package_fake.MockServiceMockRecorder) { | ||
m.CreatePreservationAction(mockutil.Context(), &datatypes.PreservationAction{ | ||
WorkflowID: "workflow-id", | ||
Type: enums.PreservationActionTypeCreateAip, | ||
Status: enums.PreservationActionStatusDone, | ||
PackageID: 1, | ||
}).Return(fmt.Errorf("persistence error")) | ||
}, | ||
wantErr: "persistence error", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ts := &temporalsdk_testsuite.WorkflowTestSuite{} | ||
env := ts.NewTestActivityEnvironment() | ||
svc := package_fake.NewMockService(gomock.NewController(t)) | ||
if tt.mockCalls != nil { | ||
tt.mockCalls(svc.EXPECT()) | ||
} | ||
|
||
enc, err := env.ExecuteLocalActivity( | ||
createPreservationActionLocalActivity, | ||
svc, | ||
tt.params, | ||
) | ||
if tt.wantErr != "" { | ||
assert.ErrorContains(t, err, tt.wantErr) | ||
return | ||
} | ||
assert.NilError(t, err) | ||
|
||
var res uint | ||
_ = enc.Get(&res) | ||
assert.DeepEqual(t, res, tt.want) | ||
}) | ||
} | ||
} |