forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_test.go
140 lines (111 loc) · 3.24 KB
/
github_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
132
133
134
135
136
137
138
139
140
package github
import (
"net/http"
"reflect"
"testing"
gh "github.com/google/go-github/github"
"github.com/stretchr/testify/require"
)
func TestNewGithubClient(t *testing.T) {
httpClient := &http.Client{}
g := &GitHub{}
client, err := g.newGithubClient(httpClient)
require.NoError(t, err)
require.Contains(t, client.BaseURL.String(), "api.github.com")
g.EnterpriseBaseURL = "api.example.com/"
enterpriseClient, err := g.newGithubClient(httpClient)
require.NoError(t, err)
require.Contains(t, enterpriseClient.BaseURL.String(), "api.example.com")
}
func TestSplitRepositoryNameWithWorkingExample(t *testing.T) {
var validRepositoryNames = []struct {
fullName string
owner string
repository string
}{
{"influxdata/telegraf", "influxdata", "telegraf"},
{"influxdata/influxdb", "influxdata", "influxdb"},
{"rawkode/saltstack-dotfiles", "rawkode", "saltstack-dotfiles"},
}
for _, tt := range validRepositoryNames {
t.Run(tt.fullName, func(t *testing.T) {
owner, repository, _ := splitRepositoryName(tt.fullName)
require.Equal(t, tt.owner, owner)
require.Equal(t, tt.repository, repository)
})
}
}
func TestSplitRepositoryNameWithNoSlash(t *testing.T) {
var invalidRepositoryNames = []string{
"influxdata-influxdb",
}
for _, tt := range invalidRepositoryNames {
t.Run(tt, func(t *testing.T) {
_, _, err := splitRepositoryName(tt)
require.Error(t, err)
})
}
}
func TestGetLicenseWhenExists(t *testing.T) {
licenseName := "MIT"
license := gh.License{Name: &licenseName}
repository := gh.Repository{License: &license}
getLicenseReturn := getLicense(&repository)
require.Equal(t, "MIT", getLicenseReturn)
}
func TestGetLicenseWhenMissing(t *testing.T) {
repository := gh.Repository{}
getLicenseReturn := getLicense(&repository)
require.Equal(t, "None", getLicenseReturn)
}
func TestGetTags(t *testing.T) {
licenseName := "MIT"
license := gh.License{Name: &licenseName}
ownerName := "influxdata"
owner := gh.User{Login: &ownerName}
fullName := "influxdata/influxdb"
repositoryName := "influxdb"
language := "Go"
repository := gh.Repository{
FullName: &fullName,
Name: &repositoryName,
License: &license,
Owner: &owner,
Language: &language,
}
getTagsReturn := getTags(&repository)
correctTagsReturn := map[string]string{
"owner": ownerName,
"name": repositoryName,
"language": language,
"license": licenseName,
}
require.Equal(t, true, reflect.DeepEqual(getTagsReturn, correctTagsReturn))
}
func TestGetFields(t *testing.T) {
stars := 1
forks := 2
openIssues := 3
size := 4
subscribers := 5
watchers := 6
repository := gh.Repository{
StargazersCount: &stars,
ForksCount: &forks,
OpenIssuesCount: &openIssues,
Size: &size,
NetworkCount: &forks,
SubscribersCount: &subscribers,
WatchersCount: &watchers,
}
getFieldsReturn := getFields(&repository)
correctFieldReturn := make(map[string]interface{})
correctFieldReturn["stars"] = 1
correctFieldReturn["forks"] = 2
correctFieldReturn["networks"] = 2
correctFieldReturn["open_issues"] = 3
correctFieldReturn["size"] = 4
correctFieldReturn["subscribers"] = 5
correctFieldReturn["watchers"] = 6
require.Equal(t, true, reflect.DeepEqual(getFieldsReturn, correctFieldReturn))
}