Skip to content

Commit

Permalink
tests fixed 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Diogenesoftoronto committed Jan 30, 2024
1 parent ec4c6e8 commit c09abcd
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 56 deletions.
4 changes: 4 additions & 0 deletions internal/am/job_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func ConvertJobToPreservationTask(job amclient.Job) package_.PreservationTask {
}

func getJobTimestamp(job amclient.Job) (sql.NullTime, sql.NullTime) {
// Check if job has tasks.
if len(job.Tasks) == 0 {
return sql.NullTime{}, sql.NullTime{}
}
st := job.Tasks[0].StartedAt.Time
ct := job.Tasks[0].CompletedAt.Time
var t time.Time
Expand Down
137 changes: 88 additions & 49 deletions internal/am/job_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"entgo.io/ent/dialect/sql"
"github.com/google/uuid"
"github.com/jonboulle/clockwork"
"go.artefactual.dev/amclient"
Expand Down Expand Up @@ -173,52 +174,90 @@ func TestJobTracker(t *testing.T) {
}
}

// func TestConvertJobToPreservationTask(t *testing.T) {

// t.Parallel()
// jobs := []amclient.Job{
// {
// ID: "f60018ac-da79-4769-9509-c6c41d5efe7e",
// LinkID: "70669a5b-01e4-4ea0-ac70-10292f87da05",
// Microservice: "Verify SIP compliance",
// Name: "Move to processing directory",
// Status: amclient.JobStatusComplete,
// Tasks: []amclient.Task{
// {
// ID: "c134198c-9485-4f68-8d94-4da1e03b5e1b",
// ExitCode: 0,
// CreatedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// StartedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// CompletedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// Duration: amclient.TaskDuration(time.Second / 2),
// },
// },
// },
// {
// ID: "c2128d39-2ace-47c5-8cac-39ded8d9c9ef",
// LinkID: "208d441b-6938-44f9-b54a-bd73f05bc764",
// Microservice: "Verify SIP compliance",
// Name: "Verify SIP compliance",
// Status: amclient.JobStatusComplete,
// Tasks: []amclient.Task{
// {
// ID: "6f5beca3-71ad-446c-8f19-3bc4dea16c9b",
// ExitCode: 0,
// CreatedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// StartedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// CompletedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// Duration: amclient.TaskDuration(time.Second / 2),
// },
// },
// },
// }
// type test struct {
// want amclient.Job
// got amclient.Job
// }
// for _, tt := []test{
// want: jobs[0]

// }

// }
func TestConvertJobToPreservationTask(t *testing.T) {
t.Parallel()
var og time.Time
jobs := []amclient.Job{
{
ID: "f60018ac-da79-4769-9509-c6c41d5efe7e",
LinkID: "70669a5b-01e4-4ea0-ac70-10292f87da05",
Microservice: "Verify SIP compliance",
Name: "Move to processing directory",
Status: amclient.JobStatusComplete,
Tasks: []amclient.Task{
{
ID: "c134198c-9485-4f68-8d94-4da1e03b5e1b",
ExitCode: 0,
CreatedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
StartedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
CompletedAt: amclient.TaskDateTime{Time: time.Date(2025, time.January, 18, 1, 27, 49, 0, time.UTC)},
Duration: amclient.TaskDuration(time.Second / 2),
},
},
},
{
ID: "c2128d39-2ace-47c5-8cac-39ded8d9c9ef",
LinkID: "208d441b-6938-44f9-b54a-bd73f05bc764",
Microservice: "Verify SIP compliance",
Name: "Verify SIP compliance",
Status: amclient.JobStatusComplete,
Tasks: []amclient.Task{
{
ID: "6f5beca3-71ad-446c-8f19-3bc4dea16c9b",
ExitCode: 0,
CreatedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
StartedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
CompletedAt: amclient.TaskDateTime{Time: og},
Duration: amclient.TaskDuration(time.Second / 2),
},
},
},
}
type test struct {
name string
job amclient.Job
want []sql.NullTime
}

for _, tt := range []test{
{
name: "Returns correct time values for preservation task.",
job: jobs[0],
want: []sql.NullTime{
{
Time: jobs[0].Tasks[0].StartedAt.Time,
Valid: true,
},
{
Time: jobs[0].Tasks[0].CompletedAt.Time,
Valid: true,
},
},
},
{
name: "Returns zero value of time if time is not valid",
job: jobs[1],
want: []sql.NullTime{
{
Time: jobs[1].Tasks[0].StartedAt.Time,
Valid: true,
},
{
Time: jobs[1].Tasks[0].CompletedAt.Time,
Valid: false,
},
},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
presTask := am.ConvertJobToPreservationTask(tt.job)
got := []sql.NullTime{
presTask.StartedAt, presTask.CompletedAt,
}

assert.DeepEqual(t, got, tt.want)
})
}
}
25 changes: 18 additions & 7 deletions internal/am/poll_transfer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package am_test

import (
// "database/sql"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -28,7 +29,8 @@ var (

func TestPollTransferActivity(t *testing.T) {
t.Parallel()

// clock := clockwork.NewFakeClock()
// nullTime := sql.NullTime{Time: clock.Now(), Valid: true}
transferID := uuid.New().String()
presActionID := uint(1)
sipID := uuid.New().String()
Expand Down Expand Up @@ -65,12 +67,12 @@ func TestPollTransferActivity(t *testing.T) {
LinkID: "045c43ae-d6cf-44f7-97d6-c8a602748565",
Tasks: []amclient.Task{
{
ID: "53666170-0397-4962-8736-23295444b036",
ExitCode: 0,
FileID: "",
Filename: "Images-94ade01c-49ce-49e0-9cc3-805575c676d0",
CreatedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
StartedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
ID: "53666170-0397-4962-8736-23295444b036",
ExitCode: 0,
FileID: "",
Filename: "Images-94ade01c-49ce-49e0-9cc3-805575c676d0",
CreatedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
// StartedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
CompletedAt: amclient.TaskDateTime{Time: time.Date(2024, time.January, 18, 1, 27, 49, 0, time.UTC)},
Duration: amclient.TaskDuration(time.Second / 2),
},
Expand Down Expand Up @@ -161,7 +163,16 @@ func TestPollTransferActivity(t *testing.T) {
for _, job := range jobs {
pt := am.ConvertJobToPreservationTask(job)
pt.PreservationActionID = presActionID
if !pt.CompletedAt.Valid {
pt.CompletedAt.Time = ttime
pt.CompletedAt.Valid = true
}
if !pt.StartedAt.Valid {
pt.StartedAt.Time = ttime
pt.StartedAt.Valid = true
}
m.CreatePreservationTask(mockutil.Context(), &pt).Return(nil)

}
},
want: am.PollTransferActivityResult{
Expand Down

0 comments on commit c09abcd

Please sign in to comment.