MCPcopy
hub / github.com/rqlite/rqlite / Test_DBVacuumInto

Function Test_DBVacuumInto

db/db_vacuum_test.go:69–154  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

67}
68
69func Test_DBVacuumInto(t *testing.T) {
70 db, path := mustCreateOnDiskDatabaseWAL()
71 defer db.Close()
72 defer os.Remove(path)
73
74 r, err := db.ExecuteStringStmt("CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)")
75 if err != nil {
76 t.Fatalf("failed to create table: %s", err.Error())
77 }
78 if exp, got := `[{}]`, asJSON(r); exp != got {
79 t.Fatalf("unexpected results for query, expected %s, got %s", exp, got)
80 }
81 for range 1000 {
82 _, err = db.ExecuteStringStmt(`INSERT INTO foo(name) VALUES("fiona")`)
83 if err != nil {
84 t.Fatalf("error executing insertion into table: %s", err.Error())
85 }
86 }
87
88 testQ := func(d *DB) {
89 t.Helper()
90 q, err := d.QueryStringStmt("SELECT COUNT(*) FROM foo")
91 if err != nil {
92 t.Fatalf("failed to query empty table: %s", err.Error())
93 }
94 if exp, got := `[{"columns":["COUNT(*)"],"types":["integer"],"values":[[1000]]}]`, asJSON(q); exp != got {
95 t.Fatalf("unexpected results for query, expected %s, got %s", exp, got)
96 }
97 }
98 testQ(db)
99
100 // VACUUM INTO an empty file, open the database, and check it's correct.
101 tmpPath := mustTempFile()
102 defer os.Remove(tmpPath)
103 if err := db.VacuumInto(tmpPath); err != nil {
104 t.Fatalf("failed to vacuum database: %s", err.Error())
105 }
106 vDB, err := Open(tmpPath, false, false)
107 if err != nil {
108 t.Fatalf("failed to open database: %s", err.Error())
109 }
110 defer vDB.Close()
111 testQ(vDB)
112
113 // VACUUM INTO an non-existing file, should still work.
114 tmpPath2 := mustTempPath()
115 defer os.Remove(tmpPath2)
116 if err := db.VacuumInto(tmpPath2); err != nil {
117 t.Fatalf("failed to vacuum database: %s", err.Error())
118 }
119 vDB2, err := Open(tmpPath2, false, false)
120 if err != nil {
121 t.Fatalf("failed to open database: %s", err.Error())
122 }
123 defer vDB2.Close()
124 testQ(vDB2)
125
126 // VACUUM INTO a path containing a single quote should work.

Callers

nothing calls this directly

Calls 11

OpenFunction · 0.85
mustTempPathFunction · 0.85
ExecuteStringStmtMethod · 0.80
asJSONFunction · 0.70
mustTempFileFunction · 0.70
CloseMethod · 0.65
RemoveMethod · 0.65
ErrorMethod · 0.65
QueryStringStmtMethod · 0.45
VacuumIntoMethod · 0.45

Tested by

no test coverage detected