-
Notifications
You must be signed in to change notification settings - Fork 17
/
api_database_test.go
126 lines (116 loc) · 2.89 KB
/
api_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
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 td_client
import "testing"
func TestShowDatabase(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(listDatabasesResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
// db found
database, err := client.ShowDatabase("sample_datasets")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
if database.Name != "sample_datasets" {
t.Fatalf("database name mismatch: %s", database.Name)
}
// db not found
database, err = client.ShowDatabase("not_found_db")
if err == nil {
t.Fatal("err expected")
}
if err.Error() != "Database 'not_found_db' does not exist" {
t.Fatalf("unexpected err message: %s", err.Error())
}
}
func TestListDatabases(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(listDatabasesResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
databaseList, err := client.ListDatabases()
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
if len(*databaseList) != 3 {
t.Fatal("not expected database count")
}
}
func TestDeleteDatabase(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(deleteDatabasesResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
err = client.DeleteDatabase("test")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
}
func TestCreateDatabase(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(createDatabasesResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
err = client.CreateDatabase("test", nil)
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
}
const listDatabasesResponse = `
{
"databases":[
{
"id": "1",
"user_id": 1,
"description": "desc 1",
"name":"test",
"created_at":"2016-11-07 09:37:37 UTC",
"updated_at":"2016-11-07 09:37:37 UTC",
"count":69664,
"organization":null,
"permission":"administrator",
"delete_protected": true
},
{
"id": "2",
"user_id": 1,
"name":"sample_datasets",
"created_at":"2014-10-04 01:13:11 UTC",
"updated_at":"2016-04-21 06:31:45 UTC",
"count":8812278,
"organization":null,
"permission":"query_only",
"delete_protected": false
},
{
"id": "3",
"user_id": 1,
"description": "desc 3",
"name":"information_schema",
"created_at":"2016-12-15 05:24:21 UTC",
"updated_at":"2016-12-15 05:24:21 UTC",
"count":0,
"organization":null,
"permission":"query_only",
"delete_protected": false
}
]
}
`
const deleteDatabasesResponse = `
{
"database":"test"
}
`
const createDatabasesResponse = `
{
"database":"test"
}
`