-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueriable_test.go
61 lines (55 loc) · 1.53 KB
/
queriable_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
package brightbox
import (
"context"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func testAll[I any](
t *testing.T,
allInstances func(c *Client, ctx context.Context) ([]I, error),
typeName string,
apiPath string,
instanceRef string,
) *I {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "GET",
ExpectURL: "/1.0/" + apiPath,
ExpectBody: "",
GiveBody: readJSON(apiPath),
},
)
defer ts.Close()
assert.Assert(t, is.Nil(err), "Connect returned an error")
collection, err := allInstances(client, context.Background())
assert.Assert(t, is.Nil(err), "All["+typeName+"] returned an error")
assert.Assert(t, collection != nil, "All["+typeName+"] returned nil")
assert.Equal(t, 1, len(collection), "wrong number of "+instanceRef+" returned")
return &collection[0]
}
func testInstance[I any](
t *testing.T,
fetchInstance func(*Client, context.Context, string) (*I, error),
typeName string,
expectPath string,
jsonPath string,
instanceID string,
) *I {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "GET",
ExpectURL: "/1.0/" + expectPath,
ExpectBody: "",
GiveBody: readJSON(jsonPath),
},
)
defer ts.Close()
assert.Assert(t, is.Nil(err), "Connect returned an error")
instance, err := fetchInstance(client, context.Background(), instanceID)
assert.Assert(t, is.Nil(err), "Instance["+typeName+"] returned an error")
assert.Assert(t, instance != nil, "Instance["+typeName+"] returned nil")
return instance
}