-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkspaces_test.go
126 lines (107 loc) · 3.61 KB
/
workspaces_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
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
package coze
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWorkspaces(t *testing.T) {
// Test List method
t.Run("List workspaces success", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Verify request method and path
assert.Equal(t, http.MethodGet, req.Method)
assert.Equal(t, "/v1/workspaces", req.URL.Path)
// Verify query parameters
assert.Equal(t, "1", req.URL.Query().Get("page_num"))
assert.Equal(t, "20", req.URL.Query().Get("page_size"))
// Return mock response
return mockResponse(http.StatusOK, &listWorkspaceResp{
Data: &ListWorkspaceResp{
TotalCount: 2,
Workspaces: []*Workspace{
{
ID: "ws1",
Name: "Workspace 1",
IconUrl: "https://example.com/icon1.png",
RoleType: WorkspaceRoleTypeOwner,
WorkspaceType: WorkspaceTypePersonal,
},
{
ID: "ws2",
Name: "Workspace 2",
IconUrl: "https://example.com/icon2.png",
RoleType: WorkspaceRoleTypeAdmin,
WorkspaceType: WorkspaceTypeTeam,
},
},
},
})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
workspaces := newWorkspace(core)
paged, err := workspaces.List(context.Background(), &ListWorkspaceReq{
PageNum: 1,
PageSize: 20,
})
require.NoError(t, err)
assert.False(t, paged.HasMore())
items := paged.Items()
require.Len(t, items, 2)
// Verify first workspace
assert.Equal(t, "ws1", items[0].ID)
assert.Equal(t, "Workspace 1", items[0].Name)
assert.Equal(t, "https://example.com/icon1.png", items[0].IconUrl)
assert.Equal(t, WorkspaceRoleTypeOwner, items[0].RoleType)
assert.Equal(t, WorkspaceTypePersonal, items[0].WorkspaceType)
// Verify second workspace
assert.Equal(t, "ws2", items[1].ID)
assert.Equal(t, "Workspace 2", items[1].Name)
assert.Equal(t, "https://example.com/icon2.png", items[1].IconUrl)
assert.Equal(t, WorkspaceRoleTypeAdmin, items[1].RoleType)
assert.Equal(t, WorkspaceTypeTeam, items[1].WorkspaceType)
})
// Test List method with default pagination
t.Run("List workspaces with default pagination", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Verify default pagination parameters
assert.Equal(t, "1", req.URL.Query().Get("page_num"))
assert.Equal(t, "20", req.URL.Query().Get("page_size"))
// Return mock response
return mockResponse(http.StatusOK, &listWorkspaceResp{
Data: &ListWorkspaceResp{
TotalCount: 0,
Workspaces: []*Workspace{},
},
})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
workspaces := newWorkspace(core)
paged, err := workspaces.List(context.Background(), NewListWorkspaceReq())
require.NoError(t, err)
assert.False(t, paged.HasMore())
assert.Empty(t, paged.Items())
})
// Test List method with error
t.Run("List workspaces with error", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Return error response
return mockResponse(http.StatusBadRequest, &baseResponse{})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
workspaces := newWorkspace(core)
paged, err := workspaces.List(context.Background(), &ListWorkspaceReq{
PageNum: 1,
PageSize: 20,
})
require.Error(t, err)
assert.Nil(t, paged)
})
}