-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathserialize_test.go
More file actions
122 lines (108 loc) · 3.09 KB
/
serialize_test.go
File metadata and controls
122 lines (108 loc) · 3.09 KB
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
package sqlite_test
import (
"reflect"
"strconv"
"strings"
"testing"
"crawshaw.io/sqlite"
"crawshaw.io/sqlite/sqlitex"
)
func TestSerialize(t *testing.T) {
conn, err := sqlite.OpenConn(":memory:", 0)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
// Create table and insert a few records
err = sqlitex.Exec(conn, "CREATE TABLE mytable (v1 PRIMARY KEY, v2, v3);", nil)
if err != nil {
t.Fatal(err)
}
err = sqlitex.Exec(conn,
"INSERT INTO mytable (v1, v2, v3) VALUES ('foo', 'bar', 'baz'), ('foo2', 'bar2', 'baz2');", nil)
if err != nil {
t.Fatal(err)
}
// Serialize
ser := conn.Serialize("")
if ser == nil {
t.Fatal("unexpected nil")
}
origLen := len(ser.Bytes())
t.Logf("Initial serialized size: %v", origLen)
// Create new connection, confirm table not there
conn, err = sqlite.OpenConn(":memory:", 0)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
err = sqlitex.Exec(conn, "SELECT * FROM mytable ORDER BY v1;", nil)
if err == nil || !strings.Contains(err.Error(), "no such table") {
t.Fatalf("expected no-table error, got: %v", err)
}
// Deserialize into connection and allow resizing
err = conn.Deserialize(ser, sqlite.SQLITE_DESERIALIZE_FREEONCLOSE|sqlite.SQLITE_DESERIALIZE_RESIZEABLE)
if err != nil {
t.Fatal(err)
}
// Confirm data there
data := [][3]string{}
err = sqlitex.Exec(conn, "SELECT * FROM mytable ORDER BY v1;", func(stmt *sqlite.Stmt) error {
data = append(data, [3]string{stmt.ColumnText(0), stmt.ColumnText(1), stmt.ColumnText(2)})
return nil
})
if err != nil {
t.Fatal(err)
}
expected := [][3]string{{"foo", "bar", "baz"}, {"foo2", "bar2", "baz2"}}
if !reflect.DeepEqual(expected, data) {
t.Fatalf("expected %v, got %v", expected, data)
}
// Confirm 1000 inserts can be made
for i := 0; i < 1000; i++ {
toAppend := strconv.Itoa(i + 3)
err = sqlitex.Exec(conn, "INSERT INTO mytable (v1, v2, v3) VALUES ('foo"+
toAppend+"', 'bar"+toAppend+"', 'baz3"+toAppend+"')", nil)
if err != nil {
t.Fatal(err)
}
}
// Serialize again, this time with no-copy
ser = conn.Serialize("")
if ser == nil {
t.Fatal("unexpected nil")
}
newLen := len(ser.Bytes())
if newLen <= origLen {
t.Fatalf("expected %v > %v", newLen, origLen)
}
t.Logf("New serialized size: %v", newLen)
// Copy the serialized bytes but to not let sqlite own them
ser = sqlite.NewSerialized(ser.Schema(), ser.Bytes(), false)
// Create new conn, deserialize read only
conn, err = sqlite.OpenConn(":memory:", 0)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
err = conn.Deserialize(ser, sqlite.SQLITE_DESERIALIZE_READONLY)
if err != nil {
t.Fatal(err)
}
// Count
var total int64
err = sqlitex.Exec(conn, "SELECT COUNT(1) FROM mytable;", func(stmt *sqlite.Stmt) error {
total = stmt.ColumnInt64(0)
return nil
})
if err != nil {
t.Fatal(err)
} else if total != 1002 {
t.Fatalf("expected 1002, got %v", total)
}
// Try to insert again
err = sqlitex.Exec(conn, "INSERT INTO mytable (v1, v2, v3) VALUES ('a', 'b', 'c');", nil)
if err == nil || !strings.Contains(err.Error(), "readonly") {
t.Fatalf("expected readonly error, got: %v", err)
}
}