Skip to content

Commit 7f2c15c

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 7f2c15c

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test-e2e: test-e2e-cleanup ## run e2e tests
7474
.PHONY: html-coverage
7575
html-coverage: ## generate html coverage
7676
@mkdir -p tmp
77-
@go test -coverprofile=tmp/c.out ./.../ && go tool cover -html=tmp/c.out
77+
@go test -coverprofile=tmp/c.out ./pkg/... ./cmd/... && go tool cover -html=tmp/c.out
7878

7979
##@ Linting
8080
.PHONY: lint

pkg/sort/pipelinerun_test.go

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,84 @@ func TestPipelineRunSortByCompletionTime(t *testing.T) {
1616
ns := "namespace"
1717
labels := map[string]string{}
1818
success := tektonv1.PipelineRunReasonSuccessful.String()
19+
20+
pruns := []tektonv1.PipelineRun{
21+
*(tektontest.MakePRCompletion(clock, "troisieme", ns, success, nil, labels, 30)),
22+
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
23+
*(tektontest.MakePRCompletion(clock, "second", ns, success, nil, labels, 20)),
24+
}
25+
26+
prunsMissing := []tektonv1.PipelineRun{
27+
*(tektontest.MakePRCompletion(clock, "troisieme", ns, success, nil, labels, 30)),
28+
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
29+
*(tektontest.MakePRCompletion(clock, "no-completion-time", ns, success, nil, labels, 20)),
30+
}
31+
for i := range prunsMissing {
32+
if prunsMissing[i].Name == "no-completion-time" {
33+
prunsMissing[i].Status.CompletionTime = nil
34+
}
35+
}
36+
37+
prunsWithOneMissing := []tektonv1.PipelineRun{
38+
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
39+
*(tektontest.MakePRCompletion(clock, "no-completion-time", ns, success, nil, labels, 20)),
40+
}
41+
for i := range prunsWithOneMissing {
42+
if prunsWithOneMissing[i].Name == "no-completion-time" {
43+
prunsWithOneMissing[i].Status.CompletionTime = nil
44+
}
45+
}
46+
47+
prunsWithJMissing := []tektonv1.PipelineRun{
48+
*(tektontest.MakePRCompletion(clock, "no-completion-time", ns, success, nil, labels, 20)),
49+
*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10)),
50+
}
51+
for i := range prunsWithJMissing {
52+
if prunsWithJMissing[i].Name == "no-completion-time" {
53+
prunsWithJMissing[i].Status.CompletionTime = nil
54+
}
55+
}
56+
1957
tests := []struct {
2058
name string
2159
pruns []tektonv1.PipelineRun
2260
wantName []string
2361
}{
2462
{
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-
},
63+
name: "sort by completion time",
64+
pruns: pruns,
3065
wantName: []string{"premier", "second", "troisieme"},
3166
},
32-
// TODO: Add test cases.
67+
{
68+
name: "sort by completion time with missing",
69+
pruns: prunsMissing,
70+
wantName: []string{"no-completion-time", "premier", "troisieme"},
71+
},
72+
{
73+
name: "sort by completion time with one missing",
74+
pruns: prunsWithOneMissing,
75+
wantName: []string{"no-completion-time", "premier"},
76+
},
77+
{
78+
name: "sort by completion time with j missing",
79+
pruns: prunsWithJMissing,
80+
wantName: []string{"no-completion-time", "premier"},
81+
},
82+
{
83+
name: "empty list",
84+
pruns: []tektonv1.PipelineRun{},
85+
wantName: []string{},
86+
},
87+
{
88+
name: "single item",
89+
pruns: []tektonv1.PipelineRun{*(tektontest.MakePRCompletion(clock, "premier", ns, success, nil, labels, 10))},
90+
wantName: []string{"premier"},
91+
},
3392
}
3493
for _, tt := range tests {
3594
t.Run(tt.name, func(t *testing.T) {
36-
for key, value := range PipelineRunSortByCompletionTime(tt.pruns) {
95+
got := PipelineRunSortByCompletionTime(tt.pruns)
96+
for key, value := range got {
3797
assert.Equal(t, tt.wantName[key], value.GetName())
3898
}
3999
})
@@ -53,8 +113,18 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
53113
noCompletionPR.Status.CompletionTime = nil
54114

55115
notStartedYet := tektontest.MakePRCompletion(clock, "notStarted", ns, success, nil, labels, 5)
56-
noCompletionPR.Status.StartTime = nil
57-
noCompletionPR.Status.CompletionTime = nil
116+
notStartedYet.Status.StartTime = nil
117+
notStartedYet.Status.CompletionTime = nil
118+
119+
prunsWithOneNotStarted := []tektonv1.PipelineRun{
120+
*(tektontest.MakePRCompletion(clock, "otherFirst", ns, success, nil, labels, 30)),
121+
*notStartedYet,
122+
}
123+
124+
prunsWithJNotStarted := []tektonv1.PipelineRun{
125+
*notStartedYet,
126+
*(tektontest.MakePRCompletion(clock, "otherFirst", ns, success, nil, labels, 30)),
127+
}
58128

59129
tests := []struct {
60130
name string
@@ -83,11 +153,31 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
83153
{
84154
name: "not started yet",
85155
pruns: []tektonv1.PipelineRun{
86-
*notStartedYet,
87156
*(tektontest.MakePRCompletion(clock, "otherFirst", ns, success, nil, labels, 30)),
88157
*(tektontest.MakePRCompletion(clock, "otherSecond", ns, success, nil, labels, 10)),
158+
*notStartedYet,
89159
},
90-
wantName: []string{"otherFirst", "otherSecond", "notStarted"},
160+
wantName: []string{"notStarted", "otherFirst", "otherSecond"},
161+
},
162+
{
163+
name: "not started yet single",
164+
pruns: prunsWithOneNotStarted,
165+
wantName: []string{"notStarted", "otherFirst"},
166+
},
167+
{
168+
name: "not started yet single j",
169+
pruns: prunsWithJNotStarted,
170+
wantName: []string{"notStarted", "otherFirst"},
171+
},
172+
{
173+
name: "empty list",
174+
pruns: []tektonv1.PipelineRun{},
175+
wantName: []string{},
176+
},
177+
{
178+
name: "single item",
179+
pruns: []tektonv1.PipelineRun{*startedEarlierPR},
180+
wantName: []string{"earlier"},
91181
},
92182
}
93183
for _, tt := range tests {

0 commit comments

Comments
 (0)