forked from psanford/sqlite3vfshttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3vfshttp_test.go
112 lines (93 loc) · 1.77 KB
/
sqlite3vfshttp_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
package sqlite3vfshttp
import (
"database/sql"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
_ "github.com/mattn/go-sqlite3"
"github.com/psanford/sqlite3vfs"
)
func TestSqlite3vfshttp(t *testing.T) {
dir, err := ioutil.TempDir("", "sqlite3vfshttptest")
if err != nil {
t.Fatal(err)
}
db, err := sql.Open("sqlite3", filepath.Join(dir, "test.db"))
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS foo (
id text NOT NULL PRIMARY KEY,
title text
)`)
if err != nil {
t.Fatal(err)
}
rows := []FooRow{
{
ID: "415",
Title: "romantic-swell",
},
{
ID: "610",
Title: "ironically-gnarl",
},
{
ID: "768",
Title: "biophysicist-straddled",
},
}
for _, row := range rows {
_, err = db.Exec(`INSERT INTO foo (id, title) values (?, ?)`, row.ID, row.Title)
if err != nil {
t.Fatal(err)
}
}
err = db.Close()
if err != nil {
t.Fatal(err)
}
s := httptest.NewServer(http.FileServer(http.Dir(dir)))
vfs := HttpVFS{
URL: s.URL + "/test.db",
}
err = sqlite3vfs.RegisterVFS("httpvfs", &vfs)
if err != nil {
t.Fatal(err)
}
db, err = sql.Open("sqlite3", "meaningless_name.db?vfs=httpvfs&mode=ro")
if err != nil {
t.Fatal(err)
}
rowIter, err := db.Query(`SELECT id, title from foo order by id`)
if err != nil {
t.Fatal(err)
}
var gotRows []FooRow
for rowIter.Next() {
var row FooRow
err = rowIter.Scan(&row.ID, &row.Title)
if err != nil {
t.Fatal(err)
}
gotRows = append(gotRows, row)
}
err = rowIter.Close()
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(rows, gotRows) {
t.Fatal(cmp.Diff(rows, gotRows))
}
err = db.Close()
if err != nil {
t.Fatal(err)
}
}
type FooRow struct {
ID string
Title string
}