Debian Codegraph @

sqlite3_opt_serialize_test.go

raw
1 //go:build !libsqlite3 || sqlite_serialize
2
3 package sqlite3
4
5 import (
6 "context"
7 "database/sql"
8 "os"
9 "testing"
10 )
11
12 func TestSerializeDeserialize(t *testing.T) {
13 // Connect to the source database.
14 srcTempFilename := TempFilename(t)
15 defer os.Remove(srcTempFilename)
16 srcDb, err := sql.Open(driverName, srcTempFilename)
17 if err != nil {
18 t.Fatal("Failed to open the source database:", err)
19 }
20 defer srcDb.Close()
21 err = srcDb.Ping()
22 if err != nil {
23 t.Fatal("Failed to connect to the source database:", err)
24 }
25
26 // Connect to the destination database.
27 destTempFilename := TempFilename(t)
28 defer os.Remove(destTempFilename)
29 destDb, err := sql.Open(driverName, destTempFilename)
30 if err != nil {
31 t.Fatal("Failed to open the destination database:", err)
32 }
33 defer destDb.Close()
34 err = destDb.Ping()
35 if err != nil {
36 t.Fatal("Failed to connect to the destination database:", err)
37 }
38
39 // Write data to source database.
40 _, err = srcDb.Exec(`CREATE TABLE foo (name string)`)
41 if err != nil {
42 t.Fatal("Failed to create table in source database:", err)
43 }
44 _, err = srcDb.Exec(`INSERT INTO foo(name) VALUES("alice")`)
45 if err != nil {
46 t.Fatal("Failed to insert data into source database", err)
47 }
48
49 // Serialize the source database
50 srcConn, err := srcDb.Conn(context.Background())
51 if err != nil {
52 t.Fatal("Failed to get connection to source database:", err)
53 }
54 defer srcConn.Close()
55
56 var serialized []byte
57 if err := srcConn.Raw(func(raw any) error {
58 var err error
59 serialized, err = raw.(*SQLiteConn).Serialize("")
60 return err
61 }); err != nil {
62 t.Fatal("Failed to serialize source database:", err)
63 }
64 srcConn.Close()
65
66 // Confirm that the destination database is initially empty.
67 var destTableCount int
68 err = destDb.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'").Scan(&destTableCount)
69 if err != nil {
70 t.Fatal("Failed to check the destination table count:", err)
71 }
72 if destTableCount != 0 {
73 t.Fatalf("The destination database is not empty; %v table(s) found.", destTableCount)
74 }
75
76 // Deserialize to destination database
77 destConn, err := destDb.Conn(context.Background())
78 if err != nil {
79 t.Fatal("Failed to get connection to destination database:", err)
80 }
81 defer destConn.Close()
82
83 if err := destConn.Raw(func(raw any) error {
84 return raw.(*SQLiteConn).Deserialize(serialized, "")
85 }); err != nil {
86 t.Fatal("Failed to deserialize source database:", err)
87 }
88 destConn.Close()
89
90 // Confirm that destination database has been loaded correctly.
91 var destRowCount int
92 err = destDb.QueryRow(`SELECT COUNT(*) FROM foo`).Scan(&destRowCount)
93 if err != nil {
94 t.Fatal("Failed to count rows in destination database table", err)
95 }
96 if destRowCount != 1 {
97 t.Fatalf("Destination table does not have the expected records")
98 }
99 }