Skip to content

Commit f0229f4

Browse files
committed
gofmt
1 parent b4f078f commit f0229f4

113 files changed

Lines changed: 14192 additions & 14193 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apiclient/client_test.go

Lines changed: 125 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,125 @@
1-
package apiclient_test
2-
3-
import (
4-
"context"
5-
"errors"
6-
"testing"
7-
8-
apiclient "github.com/Alia5/VIIPER/apiclient"
9-
apitypes "github.com/Alia5/VIIPER/apitypes"
10-
11-
"github.com/stretchr/testify/assert"
12-
)
13-
14-
// testClient constructs a client backed by a simple in-memory responder.
15-
// responses maps full, already-filled paths (after path param substitution) to raw JSON payloads.
16-
// If err is non-nil, every request returns that error, simulating dial failures.
17-
func testClient(responses map[string]string, err error) *apiclient.Client {
18-
return apiclient.WithTransport(apiclient.NewMockTransport(func(path string, _ any, _ map[string]string) (string, error) {
19-
if err != nil {
20-
return "", err
21-
}
22-
if out, ok := responses[path]; ok {
23-
return out, nil
24-
}
25-
return "", nil
26-
}))
27-
}
28-
29-
func TestHighLevelClient(t *testing.T) {
30-
tests := []struct {
31-
name string
32-
setup func(responses map[string]string) (err error)
33-
call func(c *apiclient.Client) (any, error)
34-
wantErr string
35-
assertFunc func(t *testing.T, got any)
36-
}{
37-
{
38-
name: "bus create success",
39-
setup: func(responses map[string]string) error { responses["bus/create"] = `{"busId":42}`; return nil },
40-
call: func(c *apiclient.Client) (any, error) { return c.BusCreate(42) },
41-
assertFunc: func(t *testing.T, got any) {
42-
_, ok := got.(*apitypes.BusCreateResponse)
43-
assert.True(t, ok, "expected *apitypes.BusCreateResponse type")
44-
},
45-
},
46-
{
47-
name: "bus create error structured",
48-
setup: func(responses map[string]string) error {
49-
responses["bus/create"] = `{"status":400,"title":"Bad Request","detail":"invalid busId"}`
50-
return nil
51-
},
52-
call: func(c *apiclient.Client) (any, error) { return c.BusCreate(0) },
53-
wantErr: "400 Bad Request: invalid busId",
54-
},
55-
{
56-
name: "devices list",
57-
setup: func(responses map[string]string) error {
58-
responses["bus/{id}/list"] = `{"devices":[{"busId":1,"devId":"1","vid":"0x1234","pid":"0xabcd","type":"x"}]}`
59-
return nil
60-
},
61-
call: func(c *apiclient.Client) (any, error) { return c.DevicesList(1) },
62-
assertFunc: func(t *testing.T, got any) { assert.NotNil(t, got) },
63-
},
64-
{
65-
name: "transport failure",
66-
setup: func(responses map[string]string) error { return errors.New("dial fail") },
67-
call: func(c *apiclient.Client) (any, error) { return c.BusList() },
68-
wantErr: "dial fail",
69-
},
70-
{
71-
name: "blank response error",
72-
setup: func(responses map[string]string) error { return nil },
73-
call: func(c *apiclient.Client) (any, error) { return c.BusList() },
74-
wantErr: "empty response",
75-
},
76-
{
77-
name: "devices list empty",
78-
setup: func(responses map[string]string) error { responses["bus/{id}/list"] = `{"devices":[]}`; return nil },
79-
call: func(c *apiclient.Client) (any, error) { return c.DevicesList(1) },
80-
assertFunc: func(t *testing.T, got any) {
81-
resp := got.(*apitypes.DevicesListResponse)
82-
assert.Len(t, resp.Devices, 0)
83-
},
84-
},
85-
}
86-
87-
for _, tt := range tests {
88-
t.Run(tt.name, func(t *testing.T) {
89-
responses := map[string]string{}
90-
errInject := error(nil)
91-
if tt.setup != nil {
92-
if e := tt.setup(responses); e != nil {
93-
errInject = e
94-
}
95-
}
96-
c := testClient(responses, errInject)
97-
got, err := tt.call(c)
98-
if tt.wantErr != "" {
99-
assert.Error(t, err)
100-
assert.Contains(t, err.Error(), tt.wantErr)
101-
return
102-
}
103-
assert.NoError(t, err)
104-
if tt.assertFunc != nil {
105-
tt.assertFunc(t, got)
106-
}
107-
})
108-
}
109-
}
110-
111-
func TestContextCancellation(t *testing.T) {
112-
c := apiclient.WithTransport(apiclient.NewTransport("127.0.0.1:9")) // address irrelevant due to early cancel
113-
ctx, cancel := context.WithCancel(context.Background())
114-
cancel()
115-
_, err := c.BusListCtx(ctx)
116-
assert.Error(t, err)
117-
}
118-
119-
func TestStrictJSONDecode(t *testing.T) {
120-
responses := map[string]string{}
121-
responses["bus/list"] = `{"buses":[1,2,3],"extra":true}` // extra field should cause decode error
122-
c := testClient(responses, nil)
123-
_, err := c.BusList()
124-
assert.Error(t, err)
125-
}
1+
package apiclient_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
apiclient "github.com/Alia5/VIIPER/apiclient"
9+
apitypes "github.com/Alia5/VIIPER/apitypes"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// testClient constructs a client backed by a simple in-memory responder.
15+
// responses maps full, already-filled paths (after path param substitution) to raw JSON payloads.
16+
// If err is non-nil, every request returns that error, simulating dial failures.
17+
func testClient(responses map[string]string, err error) *apiclient.Client {
18+
return apiclient.WithTransport(apiclient.NewMockTransport(func(path string, _ any, _ map[string]string) (string, error) {
19+
if err != nil {
20+
return "", err
21+
}
22+
if out, ok := responses[path]; ok {
23+
return out, nil
24+
}
25+
return "", nil
26+
}))
27+
}
28+
29+
func TestHighLevelClient(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
setup func(responses map[string]string) (err error)
33+
call func(c *apiclient.Client) (any, error)
34+
wantErr string
35+
assertFunc func(t *testing.T, got any)
36+
}{
37+
{
38+
name: "bus create success",
39+
setup: func(responses map[string]string) error { responses["bus/create"] = `{"busId":42}`; return nil },
40+
call: func(c *apiclient.Client) (any, error) { return c.BusCreate(42) },
41+
assertFunc: func(t *testing.T, got any) {
42+
_, ok := got.(*apitypes.BusCreateResponse)
43+
assert.True(t, ok, "expected *apitypes.BusCreateResponse type")
44+
},
45+
},
46+
{
47+
name: "bus create error structured",
48+
setup: func(responses map[string]string) error {
49+
responses["bus/create"] = `{"status":400,"title":"Bad Request","detail":"invalid busId"}`
50+
return nil
51+
},
52+
call: func(c *apiclient.Client) (any, error) { return c.BusCreate(0) },
53+
wantErr: "400 Bad Request: invalid busId",
54+
},
55+
{
56+
name: "devices list",
57+
setup: func(responses map[string]string) error {
58+
responses["bus/{id}/list"] = `{"devices":[{"busId":1,"devId":"1","vid":"0x1234","pid":"0xabcd","type":"x"}]}`
59+
return nil
60+
},
61+
call: func(c *apiclient.Client) (any, error) { return c.DevicesList(1) },
62+
assertFunc: func(t *testing.T, got any) { assert.NotNil(t, got) },
63+
},
64+
{
65+
name: "transport failure",
66+
setup: func(responses map[string]string) error { return errors.New("dial fail") },
67+
call: func(c *apiclient.Client) (any, error) { return c.BusList() },
68+
wantErr: "dial fail",
69+
},
70+
{
71+
name: "blank response error",
72+
setup: func(responses map[string]string) error { return nil },
73+
call: func(c *apiclient.Client) (any, error) { return c.BusList() },
74+
wantErr: "empty response",
75+
},
76+
{
77+
name: "devices list empty",
78+
setup: func(responses map[string]string) error { responses["bus/{id}/list"] = `{"devices":[]}`; return nil },
79+
call: func(c *apiclient.Client) (any, error) { return c.DevicesList(1) },
80+
assertFunc: func(t *testing.T, got any) {
81+
resp := got.(*apitypes.DevicesListResponse)
82+
assert.Len(t, resp.Devices, 0)
83+
},
84+
},
85+
}
86+
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
responses := map[string]string{}
90+
errInject := error(nil)
91+
if tt.setup != nil {
92+
if e := tt.setup(responses); e != nil {
93+
errInject = e
94+
}
95+
}
96+
c := testClient(responses, errInject)
97+
got, err := tt.call(c)
98+
if tt.wantErr != "" {
99+
assert.Error(t, err)
100+
assert.Contains(t, err.Error(), tt.wantErr)
101+
return
102+
}
103+
assert.NoError(t, err)
104+
if tt.assertFunc != nil {
105+
tt.assertFunc(t, got)
106+
}
107+
})
108+
}
109+
}
110+
111+
func TestContextCancellation(t *testing.T) {
112+
c := apiclient.WithTransport(apiclient.NewTransport("127.0.0.1:9")) // address irrelevant due to early cancel
113+
ctx, cancel := context.WithCancel(context.Background())
114+
cancel()
115+
_, err := c.BusListCtx(ctx)
116+
assert.Error(t, err)
117+
}
118+
119+
func TestStrictJSONDecode(t *testing.T) {
120+
responses := map[string]string{}
121+
responses["bus/list"] = `{"buses":[1,2,3],"extra":true}` // extra field should cause decode error
122+
c := testClient(responses, nil)
123+
_, err := c.BusList()
124+
assert.Error(t, err)
125+
}

0 commit comments

Comments
 (0)