forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
162 lines (152 loc) · 3.2 KB
/
http_test.go
File metadata and controls
162 lines (152 loc) · 3.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package list
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_listReposWithLanguage(t *testing.T) {
reg := httpmock.Registry{}
defer reg.Verify(t)
var searchData struct {
Query string
Variables map[string]interface{}
}
reg.Register(
httpmock.GraphQL(`query RepositoryListSearch\b`),
func(req *http.Request) (*http.Response, error) {
jsonData, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonData, &searchData)
if err != nil {
return nil, err
}
respBody, err := os.Open("./fixtures/repoSearch.json")
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: 200,
Request: req,
Body: respBody,
}, nil
},
)
client := http.Client{Transport: ®}
res, err := listRepos(&client, "github.com", 10, "", FilterOptions{
Language: "go",
})
require.NoError(t, err)
assert.Equal(t, 3, res.TotalCount)
assert.Equal(t, true, res.FromSearch)
assert.Equal(t, "octocat", res.Owner)
assert.Equal(t, "octocat/hello-world", res.Repositories[0].NameWithOwner)
assert.Equal(t, float64(10), searchData.Variables["perPage"])
assert.Equal(t, `user:@me language:go fork:true sort:updated-desc`, searchData.Variables["query"])
}
func Test_searchQuery(t *testing.T) {
type args struct {
owner string
filter FilterOptions
}
tests := []struct {
name string
args args
want string
}{
{
name: "blank",
want: "user:@me fork:true sort:updated-desc",
},
{
name: "in org",
args: args{
owner: "cli",
},
want: "user:cli fork:true sort:updated-desc",
},
{
name: "only public",
args: args{
owner: "",
filter: FilterOptions{
Visibility: "public",
},
},
want: "user:@me is:public fork:true sort:updated-desc",
},
{
name: "only private",
args: args{
owner: "",
filter: FilterOptions{
Visibility: "private",
},
},
want: "user:@me is:private fork:true sort:updated-desc",
},
{
name: "only forks",
args: args{
owner: "",
filter: FilterOptions{
Fork: true,
},
},
want: "user:@me fork:only sort:updated-desc",
},
{
name: "no forks",
args: args{
owner: "",
filter: FilterOptions{
Source: true,
},
},
want: "user:@me fork:false sort:updated-desc",
},
{
name: "with language",
args: args{
owner: "",
filter: FilterOptions{
Language: "ruby",
},
},
want: `user:@me language:ruby fork:true sort:updated-desc`,
},
{
name: "only archived",
args: args{
owner: "",
filter: FilterOptions{
Archived: true,
},
},
want: "user:@me fork:true archived:true sort:updated-desc",
},
{
name: "only non-archived",
args: args{
owner: "",
filter: FilterOptions{
NonArchived: true,
},
},
want: "user:@me fork:true archived:false sort:updated-desc",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := searchQuery(tt.args.owner, tt.args.filter); got != tt.want {
t.Errorf("searchQuery() = %q, want %q", got, tt.want)
}
})
}
}