-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats_test.go
131 lines (125 loc) · 2.8 KB
/
stats_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package codeowners
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCalculateOwnershipStats(t *testing.T) {
tests := []struct {
name string
files Owners
expected OwnerStats
}{
{
name: "empty files list",
files: Owners{},
expected: OwnerStats{
TotalFiles: 0,
OwnedFiles: 0,
UnownedFiles: 0,
OwnerCount: 0,
FilesPerOwner: []FilesPerOwner{},
},
},
{
name: "single owned file",
files: Owners{
{
Path: "file1.txt",
Owners: []string{"@teamA"},
},
},
expected: OwnerStats{
TotalFiles: 1,
OwnedFiles: 1,
UnownedFiles: 0,
OwnerCount: 1,
FilesPerOwner: []FilesPerOwner{
{
Owner: "@teamA",
Count: 1,
Percentage: 100.0,
},
},
},
},
{
name: "single unowned file",
files: Owners{
{
Path: "file1.txt",
Owners: []string{"(unowned)"},
},
},
expected: OwnerStats{
TotalFiles: 1,
OwnedFiles: 0,
UnownedFiles: 1,
OwnerCount: 0,
FilesPerOwner: []FilesPerOwner{
{
Owner: "(unowned)",
Count: 1,
Percentage: 100.0,
},
},
},
},
{
name: "multiple files with different owners",
files: Owners{
{
Path: "file1.txt",
Owners: []string{"@teamA", "@teamB"},
},
{
Path: "file2.txt",
Owners: []string{"@teamA"},
},
{
Path: "file3.txt",
Owners: []string{"(unowned)"},
},
},
expected: OwnerStats{
TotalFiles: 3,
OwnedFiles: 2,
UnownedFiles: 1,
OwnerCount: 2,
FilesPerOwner: []FilesPerOwner{
{
Owner: "@teamA",
Count: 2,
Percentage: 66.67,
},
{
Owner: "@teamB",
Count: 1,
Percentage: 33.33,
},
{
Owner: "(unowned)",
Count: 1,
Percentage: 33.33,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CalculateOwnershipStats(tt.files)
assert.Equal(t, tt.expected.TotalFiles, result.TotalFiles)
assert.Equal(t, tt.expected.OwnedFiles, result.OwnedFiles)
assert.Equal(t, tt.expected.UnownedFiles, result.UnownedFiles)
assert.Equal(t, tt.expected.OwnerCount, result.OwnerCount)
// For FilesPerOwner, we need to check each field separately due to floating point comparison
assert.Equal(t, len(tt.expected.FilesPerOwner), len(result.FilesPerOwner))
for i, expectedOwner := range tt.expected.FilesPerOwner {
assert.Equal(t, expectedOwner.Owner, result.FilesPerOwner[i].Owner)
assert.Equal(t, expectedOwner.Count, result.FilesPerOwner[i].Count)
// Use InDelta for floating point comparison with a small delta
assert.InDelta(t, expectedOwner.Percentage, result.FilesPerOwner[i].Percentage, 0.009)
}
})
}
}