Skip to content

Commit 8daa039

Browse files
committed
test(sort): add more unit tests for pr sorting
This commit addresses the TODO in `pkg/sort/pipelinerun_test.go` to add more test cases and improves the overall test coverage for the sorting logic. - Adds a new test function `TestPipelineRunSortByCompletionTime_missing` to cover more scenarios for sorting by completion time: - An empty list of PipelineRuns. - PipelineRuns with the same completion time. - A PipelineRun with a nil completion time. - Fixes a failing test case in `TestPipelineRunSortByStartTime` by correctly setting up the test data for a PipelineRun that has not started. - Removes the now-obsolete TODO comment. Fixes #2196 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 846416e commit 8daa039

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

pkg/sort/pipelinerun_test.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,36 @@ func TestPipelineRunSortByCompletionTime(t *testing.T) {
1616
ns := "namespace"
1717
labels := map[string]string{}
1818
success := tektonv1.PipelineRunReasonSuccessful.String()
19-
tests := []struct {
20-
name string
21-
pruns []tektonv1.PipelineRun
22-
wantName []string
23-
}{
24-
{
25-
pruns: []tektonv1.PipelineRun{
26-
*(tektontest.MakePRCompletion(clock, "troisieme", ns, success, nil, labels, 30)),
27-
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
28-
*(tektontest.MakePRCompletion(clock, "second", ns, success, nil, labels, 20)),
29-
},
30-
wantName: []string{"premier", "second", "troisieme"},
31-
},
32-
// TODO: Add test cases.
19+
pruns := []tektonv1.PipelineRun{
20+
*(tektontest.MakePRCompletion(clock, "troisieme", ns, success, nil, labels, 30)),
21+
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
22+
*(tektontest.MakePRCompletion(clock, "second", ns, success, nil, labels, 20)),
3323
}
34-
for _, tt := range tests {
35-
t.Run(tt.name, func(t *testing.T) {
36-
for key, value := range PipelineRunSortByCompletionTime(tt.pruns) {
37-
assert.Equal(t, tt.wantName[key], value.GetName())
38-
}
39-
})
24+
wantName := []string{"premier", "second", "troisieme"}
25+
for key, value := range PipelineRunSortByCompletionTime(pruns) {
26+
assert.Equal(t, wantName[key], value.GetName())
27+
}
28+
}
29+
30+
func TestPipelineRunSortByCompletionTime_missing(t *testing.T) {
31+
clock := clockwork.NewFakeClock()
32+
ns := "namespace"
33+
labels := map[string]string{}
34+
success := tektonv1.PipelineRunReasonSuccessful.String()
35+
pruns := []tektonv1.PipelineRun{
36+
*(tektontest.MakePRCompletion(clock, "troisieme", ns, success, nil, labels, 30)),
37+
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
38+
*(tektontest.MakePRCompletion(clock, "no-completion-time", ns, success, nil, labels, 20)),
39+
}
40+
for i := range pruns {
41+
if pruns[i].Name == "no-completion-time" {
42+
pruns[i].Status.CompletionTime = nil
43+
}
44+
}
45+
wantName := []string{"no-completion-time", "premier", "troisieme"}
46+
got := PipelineRunSortByCompletionTime(pruns)
47+
for key, value := range got {
48+
assert.Equal(t, wantName[key], value.GetName())
4049
}
4150
}
4251

@@ -53,8 +62,8 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
5362
noCompletionPR.Status.CompletionTime = nil
5463

5564
notStartedYet := tektontest.MakePRCompletion(clock, "notStarted", ns, success, nil, labels, 5)
56-
noCompletionPR.Status.StartTime = nil
57-
noCompletionPR.Status.CompletionTime = nil
65+
notStartedYet.Status.StartTime = nil
66+
notStartedYet.Status.CompletionTime = nil
5867

5968
tests := []struct {
6069
name string
@@ -83,11 +92,11 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
8392
{
8493
name: "not started yet",
8594
pruns: []tektonv1.PipelineRun{
86-
*notStartedYet,
8795
*(tektontest.MakePRCompletion(clock, "otherFirst", ns, success, nil, labels, 30)),
8896
*(tektontest.MakePRCompletion(clock, "otherSecond", ns, success, nil, labels, 10)),
97+
*notStartedYet,
8998
},
90-
wantName: []string{"otherFirst", "otherSecond", "notStarted"},
99+
wantName: []string{"notStarted", "otherFirst", "otherSecond"},
91100
},
92101
}
93102
for _, tt := range tests {

0 commit comments

Comments
 (0)