-
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.
Add
persistence.CreatePreservationTask()
method
- Add `CreatePreservationTask()` to the ent client - Add `CreatePreservationTask()` to the persistence service - Regenerate the persistence service mocks - Add a `convertPreservationTask()` function to convert a `db.PreservationTask` to a `datatype.PreservationTask` struct
- Loading branch information
Showing
6 changed files
with
283 additions
and
14 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
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,55 @@ | ||
package entclient | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/artefactual-sdps/enduro/internal/datatypes" | ||
) | ||
|
||
func (c *client) CreatePreservationTask(ctx context.Context, pt *datatypes.PreservationTask) error { | ||
// Validate required fields. | ||
taskID, err := uuid.Parse(pt.TaskID) | ||
if err != nil { | ||
return newParseError(err, "TaskID") | ||
} | ||
if pt.Name == "" { | ||
return newRequiredFieldError("Name") | ||
} | ||
if pt.PreservationActionID == 0 { | ||
return newRequiredFieldError("PreservationActionID") | ||
} | ||
// TODO: Validate Status. | ||
|
||
// Handle nullable fields. | ||
var startedAt *time.Time | ||
if pt.StartedAt.Valid { | ||
startedAt = &pt.StartedAt.Time | ||
} | ||
|
||
var completedAt *time.Time | ||
if pt.CompletedAt.Valid { | ||
completedAt = &pt.CompletedAt.Time | ||
} | ||
|
||
q := c.ent.PreservationTask.Create(). | ||
SetTaskID(taskID). | ||
SetName(pt.Name). | ||
SetStatus(int8(pt.Status)). | ||
SetNillableStartedAt(startedAt). | ||
SetNillableCompletedAt(completedAt). | ||
SetNote(pt.Note). | ||
SetPreservationActionID(int(pt.PreservationActionID)) | ||
|
||
r, err := q.Save(ctx) | ||
if err != nil { | ||
return newDBErrorWithDetails(err, "create preservation task") | ||
} | ||
|
||
// Update value of pt with data from DB (e.g. ID). | ||
*pt = *convertPreservationTask(r) | ||
|
||
return nil | ||
} |
124 changes: 124 additions & 0 deletions
124
internal/persistence/ent/client/preservation_task_test.go
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,124 @@ | ||
package entclient_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"gotest.tools/v3/assert" | ||
|
||
"github.com/artefactual-sdps/enduro/internal/datatypes" | ||
"github.com/artefactual-sdps/enduro/internal/enums" | ||
) | ||
|
||
func TestCreatePreservationTask(t *testing.T) { | ||
taskID := "ef0193bf-a622-4a8b-b860-cda605a426b5" | ||
started := sql.NullTime{Time: time.Now(), Valid: true} | ||
completed := sql.NullTime{Time: started.Time.Add(time.Second), Valid: true} | ||
|
||
type params struct { | ||
pt *datatypes.PreservationTask | ||
zeroPreservationActionID bool | ||
} | ||
tests := []struct { | ||
name string | ||
args params | ||
want *datatypes.PreservationTask | ||
wantErr string | ||
}{ | ||
{ | ||
name: "Saves a new preservation task in the DB", | ||
args: params{ | ||
pt: &datatypes.PreservationTask{ | ||
TaskID: taskID, | ||
Name: "PT1", | ||
Status: enums.PreservationTaskStatusInProgress, | ||
StartedAt: started, | ||
CompletedAt: completed, | ||
Note: "PT1 Note", | ||
}, | ||
}, | ||
want: &datatypes.PreservationTask{ | ||
ID: 1, | ||
TaskID: taskID, | ||
Name: "PT1", | ||
Status: enums.PreservationTaskStatusInProgress, | ||
StartedAt: started, | ||
CompletedAt: completed, | ||
Note: "PT1 Note", | ||
}, | ||
}, | ||
{ | ||
name: "Errors on invalid TaskID", | ||
args: params{ | ||
pt: &datatypes.PreservationTask{ | ||
TaskID: "123456", | ||
}, | ||
}, | ||
wantErr: "invalid data error: parse error: field \"TaskID\": invalid UUID length: 6", | ||
}, | ||
{ | ||
name: "Required field error for missing Name", | ||
args: params{ | ||
pt: &datatypes.PreservationTask{ | ||
TaskID: "ef0193bf-a622-4a8b-b860-cda605a426b5", | ||
}, | ||
}, | ||
wantErr: "invalid data error: field \"Name\" is required", | ||
}, | ||
{ | ||
name: "Required field error for missing PreservationActionID", | ||
args: params{ | ||
pt: &datatypes.PreservationTask{ | ||
TaskID: taskID, | ||
Name: "PT1", | ||
Status: enums.PreservationTaskStatusInProgress, | ||
}, | ||
zeroPreservationActionID: true, | ||
}, | ||
wantErr: "invalid data error: field \"PreservationActionID\" is required", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
entc, svc := setUpClient(t, logr.Discard()) | ||
ctx := context.Background() | ||
pkg, _ := createPackage( | ||
entc, | ||
"Test package", | ||
enums.PackageStatusInProgress, | ||
) | ||
pa, _ := createPreservationAction( | ||
entc, | ||
pkg.ID, | ||
enums.PreservationActionStatusInProgress, | ||
) | ||
|
||
pt := *tt.args.pt // Make a local copy of pt. | ||
|
||
if !tt.args.zeroPreservationActionID { | ||
pt.PreservationActionID = uint(pa.ID) | ||
} | ||
|
||
err := svc.CreatePreservationTask(ctx, &pt) | ||
if tt.wantErr != "" { | ||
assert.Error(t, err, tt.wantErr) | ||
return | ||
} | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, pt.ID, tt.want.ID) | ||
assert.Equal(t, pt.TaskID, tt.want.TaskID) | ||
assert.Equal(t, pt.Name, tt.want.Name) | ||
assert.Equal(t, pt.Status, tt.want.Status) | ||
assert.Equal(t, pt.StartedAt, tt.want.StartedAt) | ||
assert.Equal(t, pt.CompletedAt, tt.want.CompletedAt) | ||
assert.Equal(t, pt.Note, tt.want.Note) | ||
assert.Equal(t, pt.PreservationActionID, uint(pa.ID)) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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