-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
backup_test.go
86 lines (77 loc) · 1.84 KB
/
backup_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
// Copyright 2023 Roxy Light
// SPDX-License-Identifier: ISC
package sqlite_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
func TestBackup(t *testing.T) {
for _, isFull := range []bool{false, true} {
name := "Full"
if !isFull {
name = "Incremental"
}
t.Run(name, func(t *testing.T) {
src, err := sqlite.OpenConn(":memory:")
if err != nil {
t.Fatal(err)
}
defer src.Close()
err = sqlitex.ExecuteTransient(src, `CREATE TABLE foo (x INTEGER PRIMARY KEY NOT NULL);`, nil)
if err != nil {
t.Fatal(err)
}
err = sqlitex.ExecuteTransient(src, `INSERT INTO foo VALUES (1), (2), (3), (42);`, nil)
if err != nil {
t.Fatal(err)
}
dst, err := sqlite.OpenConn(":memory:")
if err != nil {
t.Fatal(err)
}
defer dst.Close()
backup, err := sqlite.NewBackup(dst, "", src, "")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := backup.Close(); err != nil {
t.Error(err)
}
}()
if isFull {
more, err := backup.Step(-1)
if more || err != nil {
t.Errorf("backup.Step(-1) = %t, %v; want false, <nil>", more, err)
}
} else {
for {
more, err := backup.Step(1)
t.Logf("after Step: Remaining() = %d, PageCount() = %d", backup.Remaining(), backup.PageCount())
if !more {
if err != nil {
t.Error("backup.Step(1):", err)
}
break
}
}
}
var got []int
err = sqlitex.ExecuteTransient(dst, `SELECT x FROM foo ORDER BY x;`, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
got = append(got, stmt.ColumnInt(0))
return nil
},
})
if err != nil {
t.Error(err)
}
want := []int{1, 2, 3, 42}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("foo (-want +got):\n%s", diff)
}
})
}
}