From e7456142a9a56866783bca22df8068d04413db48 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 20 Feb 2020 12:41:47 +0100 Subject: Improved tests for job package --- job/job_test.go | 33 ++++++++++++++++++---- job/utils.go | 1 + job/utils_test.go | 84 +++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 83 insertions(+), 35 deletions(-) (limited to 'job') diff --git a/job/job_test.go b/job/job_test.go index 7420220..cb50e31 100644 --- a/job/job_test.go +++ b/job/job_test.go @@ -1,10 +1,33 @@ package job -import "testing" +import ( + "testing" +) -func TestString(t *testing.T) { - j := Job{Name: "name", Timestamp: 10, Success: true} - if j.String() != "Job { Name: \"name\", Timestamp: \"10\", Success: \"true\" }" { - t.Errorf("test string error : %s", j.String()) +func TestJob_String(t *testing.T) { + type fields struct { + Name 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) + } + }) } } diff --git a/job/utils.go b/job/utils.go index 8bbb6eb..33c25cd 100644 --- a/job/utils.go +++ b/job/utils.go @@ -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) func KeepSuccessOnly(jobs []Job) (result []Job) { + result = make([]Job, 0) for _, job := range jobs { if job.Success { result = append(result, job) diff --git a/job/utils_test.go b/job/utils_test.go index 8f7f013..a3e4dcd 100644 --- a/job/utils_test.go +++ b/job/utils_test.go @@ -1,38 +1,62 @@ package job -import "testing" +import ( + "reflect" + "testing" +) func TestKeepOldestOnly(t *testing.T) { - t.Run("test empty list", func(t *testing.T) { - var jobs []Job - if len(KeepOldestOnly(jobs)) != 0 { - t.Error("empty list failed") - } - }) - 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") - } - }) + emptyList := []Job{} + oneJob := []Job{{Name: "a", Timestamp: 10, Success: true}} + twoJobs := []Job{ + {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) + } + }) + } } func TestKeepSuccessOnly(t *testing.T) { - t.Run("test empty list", func(t *testing.T) { - var jobs []Job - if len(KeepSuccessOnly(jobs)) != 0 { - t.Error("empty list failed") - } - }) - 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") - } - }) + emptyList := []Job{} + oneJob := []Job{{Name: "a", Timestamp: 10, Success: true}} + twoJobs := []Job{ + {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) + } + }) + } } -- cgit v1.2.3