-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathlist_test.go
96 lines (87 loc) · 2.21 KB
/
list_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
package query_test
import (
"fmt"
"testing"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/utils/query"
)
var _ networks.ListOptsBuilder = (*query.ListOpts)(nil)
var _ ports.ListOptsBuilder = (*query.ListOpts)(nil)
var _ subnets.ListOptsBuilder = (*query.ListOpts)(nil)
func ExampleListOpts_And_by_id() {
q := query.New(ports.ListOpts{
Name: "Jules",
}).And("id", "123", "321", "12345")
fmt.Println(q)
//Output: ?id=123&id=321&id=12345&name=Jules
}
func ExampleListOpts_And_by_name() {
q := query.New(ports.ListOpts{}).
And("name", "port-1", "port-&321", "the-other-port")
fmt.Println(q)
//Output: ?name=port-1&name=port-%26321&name=the-other-port
}
func ExampleListOpts_And_by_Name_and_tag() {
q := query.New(ports.ListOpts{}).
And("name", "port-1", "port-3").
And("tags", "my-tag")
fmt.Println(q)
//Output: ?name=port-1&name=port-3&tags=my-tag
}
func ExampleListOpts_And_by_id_twice() {
q := query.New(ports.ListOpts{}).
And("id", "1", "2", "3").
And("id", "2", "3", "4")
fmt.Println(q)
//Output: ?id=2&id=3
}
func ExampleListOpts_And_by_id_twice_plus_ListOpts() {
q := query.New(ports.ListOpts{ID: "3"}).
And("id", "1", "2", "3").
And("id", "3", "4", "5")
fmt.Println(q)
//Output: ?id=3
}
func TestToPortListQuery(t *testing.T) {
for _, tc := range [...]struct {
name string
base interface{}
andProperty string
andItems []interface{}
expected string
expectedError bool
}{
{
"valid",
ports.ListOpts{},
"name",
[]interface{}{"port-1"},
"?name=port-1",
false,
},
{
"invalid_field",
ports.ListOpts{},
"door",
[]interface{}{"pod bay"},
"?",
true,
},
} {
t.Run(tc.name, func(t *testing.T) {
q, err := query.New(tc.base).And(tc.andProperty, tc.andItems...).ToPortListQuery()
if q != tc.expected {
t.Errorf("expected query %q, got %q", tc.expected, q)
}
if (err != nil) != tc.expectedError {
if err != nil {
t.Errorf("unexpected error: %v", err)
} else {
t.Errorf("expected error, got nil")
}
}
})
}
}