forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd1_test.go
238 lines (219 loc) · 7.1 KB
/
d1_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package cloudflare
import (
"context"
"fmt"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
testD1DatabaseID = "480f4f691a284fdd92401ed29f0ac1df"
testD1Result = `{
"created_at": "2014-01-01T05:20:00.12345Z",
"name": "my-database",
"uuid": "480f4f691a284fdd92401ed29f0ac1df",
"version": "beta",
"num_tables": 1,
"file_size": 100
}
`
)
var (
d1CreatedAt, _ = time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
testD1Database = D1Database{
Name: "my-database",
NumTables: 1,
UUID: testD1DatabaseID,
Version: "beta",
CreatedAt: &d1CreatedAt,
FileSize: 100,
}
)
func TestListD1Databases(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/accounts/"+testAccountID+"/d1/database", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
%s
],
"result_info": {
"page": 1,
"per_page": 100,
"count": 1,
"total_count": 1
}
}`, testD1Result)
})
_, _, err := client.ListD1Databases(context.Background(), AccountIdentifier(""), ListD1DatabasesParams{})
if assert.Error(t, err, "Didn't get error for missing Account ID get listing D1 Database") {
assert.Equal(t, err.Error(), errMissingAccountID)
}
actual, _, err := client.ListD1Databases(context.Background(), testAccountRC, ListD1DatabasesParams{})
if assert.NoError(t, err, "ListD1Databases returned error: %v", err) {
expected := []D1Database{testD1Database}
if !assert.Equal(t, expected, actual) {
t.Errorf("ListD1Databases returned %+v, expected %+v", actual, expected)
}
}
}
func TestGetD1Databases(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/accounts/"+testAccountID+"/d1/database/"+testD1DatabaseID, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": %s
}`, testD1Result)
})
_, err := client.GetD1Database(context.Background(), AccountIdentifier(""), "")
if assert.Error(t, err, "Didn't get error for missing Account ID get getting D1 Database") {
assert.Equal(t, err.Error(), errMissingAccountID)
}
actual, err := client.GetD1Database(context.Background(), testAccountRC, testD1DatabaseID)
if assert.NoError(t, err, "GetD1Database returned error: %v", err) {
if !assert.Equal(t, testD1Database, actual) {
t.Errorf("GetD1Database returned %+v, expected %+v", actual, testD1Database)
}
}
}
func TestCreateD1Database(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/accounts/"+testAccountID+"/d1/database", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": %s
}`, testD1Result)
})
_, err := client.CreateD1Database(context.Background(), AccountIdentifier(""), CreateD1DatabaseParams{})
if assert.Error(t, err, "Didn't get error for missing Account ID get creating D1 Database") {
assert.Equal(t, err.Error(), errMissingAccountID)
}
actual, err := client.CreateD1Database(context.Background(), testAccountRC, CreateD1DatabaseParams{
Name: "my-database",
})
if assert.NoError(t, err, "CreateD1Database returned error: %v", err) {
if !assert.Equal(t, testD1Database, actual) {
t.Errorf("CreateD1Database returned %+v, expected %+v", actual, testD1Database)
}
}
}
func TestDeleteD1Database(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/accounts/"+testAccountID+"/d1/database/"+testD1DatabaseID, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": []
}`)
})
err := client.DeleteD1Database(context.Background(), AccountIdentifier(""), "")
if assert.Error(t, err, "Didn't get error for missing Account ID get deleting D1 Database") {
assert.Equal(t, err.Error(), errMissingAccountID)
}
err = client.DeleteD1Database(context.Background(), testAccountRC, testD1DatabaseID)
assert.NoError(t, err, "DeleteD1Database returned error: %v", err)
}
func TestQueryD1Database(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/accounts/"+testAccountID+"/d1/database/"+testD1DatabaseID+"/query", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if got := string(b); got != `{"sql":"SELECT * FROM my-database","params":["param1","param2"]}` {
t.Errorf("request Body is %s, want %s", got, `{"sql":"SELECT * FROM my-database","params":["param1","param2"]}`)
}
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"success": true,
"meta": {
"changed_db": false,
"changes": 0,
"duration": 3.3,
"last_row_id": 3,
"rows_read": 3,
"rows_written": 0,
"size_after": 10
},
"results": [
{
"id": 1,
"name": "test user"
},
{
"id": 2,
"name": "test user 2"
}
]
}
]
}`)
})
_, err := client.QueryD1Database(context.Background(), AccountIdentifier(""), QueryD1DatabaseParams{})
if assert.Error(t, err, "Didn't get error for missing Account ID get querying D1 Database") {
assert.Equal(t, err, ErrMissingAccountID)
}
_, err = client.QueryD1Database(context.Background(), testAccountRC, QueryD1DatabaseParams{})
if assert.Error(t, err, "Didn't get error for missing D1 Database ID get querying D1 Database") {
assert.Equal(t, err, ErrMissingDatabaseID)
}
actual, err := client.QueryD1Database(context.Background(), testAccountRC, QueryD1DatabaseParams{
DatabaseID: testD1DatabaseID,
SQL: "SELECT * FROM my-database",
Parameters: []string{"param1", "param2"},
})
if assert.NoError(t, err, "QueryD1Database returned error: %v", err) {
expected := D1Result{
Success: BoolPtr(true),
Meta: D1DatabaseMetadata{
ChangedDB: BoolPtr(false),
Changes: 0,
Duration: 3.3,
LastRowID: 3,
RowsRead: 3,
RowsWritten: 0,
SizeAfter: 10,
},
Results: []map[string]any{
{
"id": float64(1),
"name": "test user",
},
{
"id": float64(2),
"name": "test user 2",
},
},
}
if !assert.Equal(t, expected, actual[0]) {
t.Errorf("QueryD1Database returned %+v, expected %+v", actual, expected)
}
}
}