1
0
Fork 0

Improved tests for job package

This commit is contained in:
Julien Dessaux 2020-02-20 12:41:47 +01:00
parent 211adff0b0
commit e7456142a9
3 changed files with 83 additions and 35 deletions

View file

@ -1,10 +1,33 @@
package job package job
import "testing" import (
"testing"
)
func TestString(t *testing.T) { func TestJob_String(t *testing.T) {
j := Job{Name: "name", Timestamp: 10, Success: true} type fields struct {
if j.String() != "Job { Name: \"name\", Timestamp: \"10\", Success: \"true\" }" { Name string
t.Errorf("test string error : %s", j.String()) Timestamp uint64
Success bool
}
tests := []struct {
name string
fields fields
want string
}{
{"default job", fields{}, "Job { Name: \"\", Timestamp: \"0\", Success: \"false\" }"},
{"a job", fields{Name: "a", Timestamp: 10, Success: true}, "Job { Name: \"a\", Timestamp: \"10\", Success: \"true\" }"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
job := Job{
Name: tt.fields.Name,
Timestamp: tt.fields.Timestamp,
Success: tt.fields.Success,
}
if got := job.String(); got != tt.want {
t.Errorf("Job.String() = %v, want %v", got, tt.want)
}
})
} }
} }

View file

@ -18,6 +18,7 @@ func KeepOldestOnly(jobs []Job) []Job {
// KeepSuccessOnly returns only the successful jobs from a job list (suiatble to write a new spool file) // KeepSuccessOnly returns only the successful jobs from a job list (suiatble to write a new spool file)
func KeepSuccessOnly(jobs []Job) (result []Job) { func KeepSuccessOnly(jobs []Job) (result []Job) {
result = make([]Job, 0)
for _, job := range jobs { for _, job := range jobs {
if job.Success { if job.Success {
result = append(result, job) result = append(result, job)

View file

@ -1,38 +1,62 @@
package job package job
import "testing" import (
"reflect"
"testing"
)
func TestKeepOldestOnly(t *testing.T) { func TestKeepOldestOnly(t *testing.T) {
t.Run("test empty list", func(t *testing.T) { emptyList := []Job{}
var jobs []Job oneJob := []Job{{Name: "a", Timestamp: 10, Success: true}}
if len(KeepOldestOnly(jobs)) != 0 { twoJobs := []Job{
t.Error("empty list failed") {Name: "a", Timestamp: 10, Success: true},
{Name: "a", Timestamp: 5, Success: true},
}
type args struct {
jobs []Job
}
tests := []struct {
name string
args args
want []Job
}{
{"empty list", args{emptyList}, emptyList},
{"one job", args{oneJob}, oneJob},
{"two jobs", args{twoJobs}, oneJob},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := KeepOldestOnly(tt.args.jobs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("KeepOldestOnly() = %v, want %v", got, tt.want)
} }
}) })
t.Run("test functionality", func(t *testing.T) {
var jobs []Job
jobs = append(jobs, Job{Name: "a", Timestamp: 10, Success: true})
jobs = append(jobs, Job{Name: "a", Timestamp: 20, Success: true})
jobs2 := KeepOldestOnly(jobs)
if len(jobs2) != 1 || jobs2[0].Timestamp != 20 {
t.Error("functionality failed")
} }
})
} }
func TestKeepSuccessOnly(t *testing.T) { func TestKeepSuccessOnly(t *testing.T) {
t.Run("test empty list", func(t *testing.T) { emptyList := []Job{}
var jobs []Job oneJob := []Job{{Name: "a", Timestamp: 10, Success: true}}
if len(KeepSuccessOnly(jobs)) != 0 { twoJobs := []Job{
t.Error("empty list failed") {Name: "a", Timestamp: 10, Success: true},
{Name: "a", Timestamp: 5, Success: false},
}
type args struct {
jobs []Job
}
tests := []struct {
name string
args args
wantResult []Job
}{
{"empty list", args{emptyList}, emptyList},
{"one job", args{oneJob}, oneJob},
{"two jobs", args{twoJobs}, oneJob},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := KeepSuccessOnly(tt.args.jobs); !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("KeepSuccessOnly() = %v, want %v", gotResult, tt.wantResult)
} }
}) })
t.Run("test functionality", func(t *testing.T) {
var jobs []Job
jobs = append(jobs, Job{Name: "a", Timestamp: 10, Success: true})
jobs = append(jobs, Job{Name: "b", Timestamp: 20, Success: false})
if len(KeepSuccessOnly(jobs)) != 1 || jobs[0].Name != "a" {
t.Error("functionality failed")
} }
})
} }