-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil_test.go
141 lines (127 loc) · 3.43 KB
/
util_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
package sqlite
import (
"context"
"testing"
)
func TestDropAll(t *testing.T) {
ctx := context.Background()
db := openTestDB(t)
conn, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
err = ExecScript(conn, `
ATTACH 'file:s1?mode=memory' AS "db two";
BEGIN;
CREATE TABLE "db two".customer (
cust_id INTEGER PRIMARY KEY,
cust_name TEXT,
cust_addr TEXT
);
CREATE INDEX "db two".custname ON customer (cust_name);
CREATE VIEW "db two".customer_address AS
SELECT cust_id, cust_addr FROM "db two".customer;
CREATE TRIGGER "db two".cust_addr_chng
INSTEAD OF UPDATE OF cust_addr ON "db two".customer_address
BEGIN
UPDATE customer SET cust_addr=NEW.cust_addr
WHERE cust_id=NEW.cust_id;
END;
-- Creates an auto-index we cannot delete.
CREATE TABLE "db two".textkey (key TEXT PRIMARY KEY, val INTEGER);
CREATE TABLE customer (
cust_id INTEGER PRIMARY KEY,
cust_name TEXT,
cust_addr TEXT
);
CREATE INDEX custname ON customer (cust_name);
CREATE VIEW customer_address AS
SELECT cust_id, cust_addr FROM customer;
CREATE TRIGGER cust_addr_chng
INSTEAD OF UPDATE OF cust_addr ON customer_address
BEGIN
UPDATE customer SET cust_addr=NEW.cust_addr
WHERE cust_id=NEW.cust_id;
END;
COMMIT;`)
if err != nil {
t.Fatal(err)
}
if err := DropAll(ctx, conn, "db two"); err != nil {
t.Fatal(err)
}
var count int
if err := conn.QueryRowContext(ctx, "SELECT count(*) FROM \"db two\".sqlite_schema").Scan(&count); err != nil {
t.Fatal(err)
}
if count != 0 {
t.Fatalf("%d unexpected 'db two' schema entries", count)
}
if err := conn.QueryRowContext(ctx, "SELECT count(*) FROM main.sqlite_schema").Scan(&count); err != nil {
t.Fatal(err)
}
if count != 4 {
t.Fatalf("%d main schema entries, want 4", count)
}
if err := DropAll(ctx, conn, "main"); err != nil {
t.Fatal(err)
}
if err := conn.QueryRowContext(ctx, "SELECT count(*) FROM main.sqlite_schema").Scan(&count); err != nil {
t.Fatal(err)
}
if count != 0 {
t.Fatalf("%d unexpected main schema entries", count)
}
}
func TestCopyAll(t *testing.T) {
ctx := context.Background()
db := openTestDB(t)
conn, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
err = ExecScript(conn, `
BEGIN;
CREATE TABLE customer (
cust_id INTEGER PRIMARY KEY,
cust_name TEXT,
cust_addr TEXT
);
CREATE INDEX custname ON customer (cust_name);
CREATE VIEW customer_address AS
SELECT cust_id, cust_addr FROM customer;
CREATE TRIGGER cust_addr_chng
INSTEAD OF UPDATE OF cust_addr ON customer_address
BEGIN
UPDATE customer SET cust_addr=NEW.cust_addr
WHERE cust_id=NEW.cust_id;
END;
COMMIT;
INSERT INTO customer (cust_id, cust_name, cust_addr) VALUES (1, 'joe', 'eldorado');
-- Creates an auto-index we should not copy.
CREATE TABLE textkey (key TEXT PRIMARY KEY, val INTEGER);
ATTACH 'file:s1?mode=memory' AS "db two";
`)
if err != nil {
t.Fatal(err)
}
if err := CopyAll(ctx, conn, "db two", "main"); err != nil {
t.Fatal(err)
}
var name string
if err := conn.QueryRowContext(ctx, "SELECT cust_name FROM \"db two\".customer WHERE cust_id=1").Scan(&name); err != nil {
t.Fatal(err)
}
if name != "joe" {
t.Fatalf("name=%q, want %q", name, "joe")
}
var count int
if err := conn.QueryRowContext(ctx, "SELECT count(*) FROM \"db two\".sqlite_schema").Scan(&count); err != nil {
t.Fatal(err)
}
if count != 6 {
t.Fatalf("dst schema count=%d, want 4", count)
}
}