-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
102 lines (88 loc) · 2.26 KB
/
database_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
package turso
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListDatabases(t *testing.T) {
databaseService := newMockDatabaseService()
resp, err := databaseService.ListDatabases(context.Background())
require.NoError(t, err)
assert.Len(t, resp.Databases, 1)
}
func TestGetDatabase(t *testing.T) {
databaseService := newMockDatabaseService()
resp, err := databaseService.GetDatabase(context.Background(), "my-db")
require.NoError(t, err)
assert.Equal(t, resp.Database.Name, "my-db")
}
func TestDeleteDatabase(t *testing.T) {
databaseService := newMockDatabaseService()
resp, err := databaseService.DeleteDatabase(context.Background(), "my-db")
require.NoError(t, err)
assert.Equal(t, resp.Database, "my-db")
}
func TestCreateDatabase(t *testing.T) {
body := `{"database":{"DbId":"0eb771dd-6906-11ee-8553-eaa7715aeaf2","Hostname":"[databaseName]-[organizationName].turso.io","Name":"my-db"}}`
client := &Client{
cfg: &Config{
BaseURL: "http://localhost",
},
client: &MockHTTPRequestDoer{
Response: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(body))),
},
},
}
// happy path
databaseService := DatabaseService{client: client}
req := CreateDatabaseRequest{
Name: "my-db",
}
resp, err := databaseService.CreateDatabase(context.Background(), req)
require.NoError(t, err)
assert.Equal(t, resp.Database.Name, "my-db")
// test error
req = CreateDatabaseRequest{
Name: "myAWESOMEdb",
}
resp, err = databaseService.CreateDatabase(context.Background(), req)
assert.Error(t, err)
assert.Nil(t, resp)
}
func TestValidateDatabaseName(t *testing.T) {
tests := []struct {
name string
input string
expectErr bool
}{
{
name: "happy path, simple name",
input: "mydatabase-123",
expectErr: false,
},
{
name: "name with uppercase",
input: "ORG-123ABC",
expectErr: true,
},
{
name: "long name",
input: "MECPJTpHtBEyUNBAujXw6mxCjN4ARLPJ3",
expectErr: true,
},
}
for _, test := range tests {
result := validateDatabaseName(test.input)
if test.expectErr {
require.Error(t, result)
} else {
require.NoError(t, result)
}
}
}