forked from goccy/go-zetasqlite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver_test.go
180 lines (172 loc) · 4.73 KB
/
driver_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
package zetasqlite_test
import (
"context"
"database/sql"
"testing"
zetasqlite "github.com/MatanLevy/go-zetasqlite"
"github.com/google/go-cmp/cmp"
)
func TestDriver(t *testing.T) {
db, err := sql.Open("zetasqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX)
)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT Singers (SingerId, FirstName, LastName) VALUES (1, 'John', 'Titor')`); err != nil {
t.Fatal(err)
}
row := db.QueryRow("SELECT SingerID, FirstName, LastName FROM Singers WHERE SingerId = @id", 1)
if row.Err() != nil {
t.Fatal(row.Err())
}
var (
singerID int64
firstName string
lastName string
)
if err := row.Scan(&singerID, &firstName, &lastName); err != nil {
t.Fatal(err)
}
if singerID != 1 || firstName != "John" || lastName != "Titor" {
t.Fatalf("failed to find row %v %v %v", singerID, firstName, lastName)
}
if _, err := db.Exec(`
CREATE VIEW IF NOT EXISTS SingerNames AS SELECT FirstName || ' ' || LastName AS Name FROM Singers`); err != nil {
t.Fatal(err)
}
viewRow := db.QueryRow("SELECT Name FROM SingerNames LIMIT 1")
if viewRow.Err() != nil {
t.Fatal(viewRow.Err())
}
var name string
if err := viewRow.Scan(&name); err != nil {
t.Fatal(err)
}
if name != "John Titor" {
t.Fatalf("failed to find view row")
}
}
func TestRegisterCustomDriver(t *testing.T) {
sql.Register("zetasqlite-custom", &zetasqlite.ZetaSQLiteDriver{
ConnectHook: func(conn *zetasqlite.ZetaSQLiteConn) error {
return conn.SetNamePath([]string{"project-id", "datasetID"})
},
})
db, err := sql.Open("zetasqlite-custom", ":memory:")
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS tableID (Id INT64 NOT NULL)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT `project-id`.datasetID.tableID (Id) VALUES (1)"); err != nil {
t.Fatal(err)
}
row := db.QueryRow("SELECT * FROM project-id.datasetID.tableID WHERE Id = ?", 1)
if row.Err() != nil {
t.Fatal(row.Err())
}
var id int64
if err := row.Scan(&id); err != nil {
t.Fatal(err)
}
if id != 1 {
t.Fatalf("failed to find row %v", id)
}
}
func TestChangedCatalog(t *testing.T) {
t.Run("table", func(t *testing.T) {
db, err := sql.Open("zetasqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
result, err := db.Exec(`
CREATE TABLE IF NOT EXISTS Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX)
)`)
if err != nil {
t.Fatal(err)
}
rows, err := db.Query(`DROP TABLE Singers`)
if err != nil {
t.Fatal(err)
}
resultCatalog, err := zetasqlite.ChangedCatalogFromResult(result)
if err != nil {
t.Fatal(err)
}
if !resultCatalog.Changed() {
t.Fatal("failed to get changed catalog")
}
if len(resultCatalog.Table.Added) != 1 {
t.Fatal("failed to get created table spec")
}
if diff := cmp.Diff(resultCatalog.Table.Added[0].NamePath, []string{"Singers"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
rowsCatalog, err := zetasqlite.ChangedCatalogFromRows(rows)
if err != nil {
t.Fatal(err)
}
if !rowsCatalog.Changed() {
t.Fatal("failed to get changed catalog")
}
if len(rowsCatalog.Table.Deleted) != 1 {
t.Fatal("failed to get deleted table spec")
}
if diff := cmp.Diff(rowsCatalog.Table.Deleted[0].NamePath, []string{"Singers"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
t.Run("function", func(t *testing.T) {
db, err := sql.Open("zetasqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
result, err := db.ExecContext(context.Background(), `CREATE FUNCTION ANY_ADD(x ANY TYPE, y ANY TYPE) AS ((x + 4) / y)`)
if err != nil {
t.Fatal(err)
}
rows, err := db.QueryContext(context.Background(), `DROP FUNCTION ANY_ADD`)
if err != nil {
t.Fatal(err)
}
resultCatalog, err := zetasqlite.ChangedCatalogFromResult(result)
if err != nil {
t.Fatal(err)
}
if !resultCatalog.Changed() {
t.Fatal("failed to get changed catalog")
}
if len(resultCatalog.Function.Added) != 1 {
t.Fatal("failed to get created function spec")
}
if diff := cmp.Diff(resultCatalog.Function.Added[0].NamePath, []string{"ANY_ADD"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
rowsCatalog, err := zetasqlite.ChangedCatalogFromRows(rows)
if err != nil {
t.Fatal(err)
}
if !rowsCatalog.Changed() {
t.Fatal("failed to get changed catalog")
}
if len(rowsCatalog.Function.Deleted) != 1 {
t.Fatal("failed to get deleted function spec")
}
if diff := cmp.Diff(rowsCatalog.Function.Deleted[0].NamePath, []string{"ANY_ADD"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}