Skip to content

Commit 1b33bad

Browse files
committed
test: add autocomplete API discovery coverage
1 parent 33e214a commit 1b33bad

1 file changed

Lines changed: 165 additions & 0 deletions

File tree

cli/completer_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/apache/cloudstack-cloudmonkey/config"
7+
)
8+
9+
func TestFindAutocompleteAPIRelatedNounMatch(t *testing.T) {
10+
arg := &config.APIArg{
11+
Name: "domainid=",
12+
Related: []string{
13+
"createDomain",
14+
"listDomains",
15+
"updateDomain",
16+
},
17+
}
18+
19+
apiFound := &config.API{
20+
Name: "listVirtualMachines",
21+
Verb: "list",
22+
Noun: "virtualmachines",
23+
}
24+
25+
apiMap := map[string][]*config.API{
26+
"list": {
27+
{
28+
Name: "listDomains",
29+
Noun: "domains",
30+
},
31+
},
32+
}
33+
34+
result := findAutocompleteAPI(arg, apiFound, apiMap)
35+
36+
if result == nil {
37+
t.Fatal("expected API, got nil")
38+
}
39+
40+
if result.Name != "listDomains" {
41+
t.Fatalf("expected listDomains, got %s", result.Name)
42+
}
43+
}
44+
45+
func TestFindAutocompleteAPIRelatedFallback(t *testing.T) {
46+
arg := &config.APIArg{
47+
Name: "domainid=",
48+
Related: []string{
49+
"listDomainChildren",
50+
},
51+
}
52+
53+
apiFound := &config.API{
54+
Name: "listVirtualMachines",
55+
Verb: "list",
56+
Noun: "virtualmachines",
57+
}
58+
59+
apiMap := map[string][]*config.API{
60+
"list": {
61+
{
62+
Name: "listDomainChildren",
63+
Noun: "domainchildren",
64+
},
65+
},
66+
}
67+
68+
result := findAutocompleteAPI(arg, apiFound, apiMap)
69+
70+
if result == nil {
71+
t.Fatal("expected API, got nil")
72+
}
73+
74+
if result.Name != "listDomainChildren" {
75+
t.Fatalf("expected listDomainChildren, got %s", result.Name)
76+
}
77+
}
78+
79+
func TestFindAutocompleteAPIEmptyRelatedFallsBackToHeuristic(t *testing.T) {
80+
arg := &config.APIArg{
81+
Name: "zoneid=",
82+
}
83+
84+
apiFound := &config.API{
85+
Name: "listVirtualMachines",
86+
Verb: "list",
87+
Noun: "virtualmachines",
88+
}
89+
90+
apiMap := map[string][]*config.API{
91+
"list": {
92+
{
93+
Name: "listZones",
94+
Noun: "zones",
95+
},
96+
},
97+
}
98+
99+
result := findAutocompleteAPI(arg, apiFound, apiMap)
100+
101+
if result == nil {
102+
t.Fatal("expected API, got nil")
103+
}
104+
105+
if result.Name != "listZones" {
106+
t.Fatalf("expected listZones, got %s", result.Name)
107+
}
108+
}
109+
110+
func TestFindAutocompleteAPINonListRelatedFallsBackToHeuristic(t *testing.T) {
111+
arg := &config.APIArg{
112+
Name: "zoneid=",
113+
Related: []string{
114+
"createZone",
115+
"updateZone",
116+
},
117+
}
118+
119+
apiFound := &config.API{
120+
Name: "listVirtualMachines",
121+
Verb: "list",
122+
Noun: "virtualmachines",
123+
}
124+
125+
apiMap := map[string][]*config.API{
126+
"list": {
127+
{
128+
Name: "listZones",
129+
Noun: "zones",
130+
},
131+
},
132+
}
133+
134+
result := findAutocompleteAPI(arg, apiFound, apiMap)
135+
136+
if result == nil {
137+
t.Fatal("expected API, got nil")
138+
}
139+
140+
if result.Name != "listZones" {
141+
t.Fatalf("expected listZones, got %s", result.Name)
142+
}
143+
}
144+
145+
func TestFindAutocompleteAPIMapTypeReturnsNil(t *testing.T) {
146+
arg := &config.APIArg{
147+
Type: "map",
148+
}
149+
150+
apiFound := &config.API{
151+
Name: "listVirtualMachines",
152+
Verb: "list",
153+
Noun: "virtualmachines",
154+
}
155+
156+
apiMap := map[string][]*config.API{
157+
"list": {},
158+
}
159+
160+
result := findAutocompleteAPI(arg, apiFound, apiMap)
161+
162+
if result != nil {
163+
t.Fatalf("expected nil, got %v", result)
164+
}
165+
}

0 commit comments

Comments
 (0)