You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
244 lines
6.5 KiB
Go
244 lines
6.5 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_IsValidSQLiteOnDisk(t *testing.T) {
|
|
path := mustTempFile()
|
|
defer os.Remove(path)
|
|
|
|
dsn := fmt.Sprintf("file:%s", path)
|
|
db, err := sql.Open("sqlite3", dsn)
|
|
if err != nil {
|
|
t.Fatalf("failed to create SQLite database: %s", err.Error())
|
|
}
|
|
_, err = db.Exec("CREATE TABLE foo (name TEXT)")
|
|
if err != nil {
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
}
|
|
if err := db.Close(); err != nil {
|
|
t.Fatalf("failed to close database: %s", err.Error())
|
|
}
|
|
|
|
if !IsValidSQLiteFile(path) {
|
|
t.Fatalf("good SQLite file marked as invalid")
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("failed to read SQLite file: %s", err.Error())
|
|
}
|
|
if !IsValidSQLiteData(data) {
|
|
t.Fatalf("good SQLite data marked as invalid")
|
|
}
|
|
}
|
|
|
|
func Test_IsWALModeEnabledOnDiskDELETE(t *testing.T) {
|
|
path := mustTempFile()
|
|
defer os.Remove(path)
|
|
|
|
dsn := fmt.Sprintf("file:%s", path)
|
|
db, err := sql.Open("sqlite3", dsn)
|
|
if err != nil {
|
|
t.Fatalf("failed to create SQLite database: %s", err.Error())
|
|
}
|
|
_, err = db.Exec("CREATE TABLE foo (name TEXT)")
|
|
if err != nil {
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
}
|
|
if err := db.Close(); err != nil {
|
|
t.Fatalf("failed to close database: %s", err.Error())
|
|
}
|
|
|
|
if !IsDELETEModeEnabledSQLiteFile(path) {
|
|
t.Fatalf("DELETE file marked as non-DELETE")
|
|
}
|
|
if IsWALModeEnabledSQLiteFile(path) {
|
|
t.Fatalf("non WAL file marked as WAL")
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("failed to read SQLite file: %s", err.Error())
|
|
}
|
|
if IsWALModeEnabled(data) {
|
|
t.Fatalf("non WAL data marked as WAL")
|
|
}
|
|
if !IsDELETEModeEnabled(data) {
|
|
t.Fatalf("data marked as non-DELETE")
|
|
}
|
|
}
|
|
|
|
func Test_IsWALModeEnabledOnDiskWAL(t *testing.T) {
|
|
path := mustTempFile()
|
|
defer os.Remove(path)
|
|
|
|
dsn := fmt.Sprintf("file:%s", path)
|
|
db, err := sql.Open("sqlite3", dsn)
|
|
if err != nil {
|
|
t.Fatalf("failed to create SQLite database: %s", err.Error())
|
|
}
|
|
_, err = db.Exec("CREATE TABLE foo (name TEXT)")
|
|
if err != nil {
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
}
|
|
_, err = db.Exec("PRAGMA journal_mode=WAL")
|
|
if err != nil {
|
|
t.Fatalf("failed to enable WAL mode: %s", err.Error())
|
|
}
|
|
if err := db.Close(); err != nil {
|
|
t.Fatalf("failed to close database: %s", err.Error())
|
|
}
|
|
|
|
if !IsWALModeEnabledSQLiteFile(path) {
|
|
t.Fatalf("WAL file marked as non-WAL")
|
|
}
|
|
if IsDELETEModeEnabledSQLiteFile(path) {
|
|
t.Fatalf("WAL file marked as DELETE")
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("failed to read SQLite file: %s", err.Error())
|
|
}
|
|
if !IsWALModeEnabled(data) {
|
|
t.Fatalf("WAL data marked as non-WAL")
|
|
}
|
|
if IsDELETEModeEnabled(data) {
|
|
t.Fatalf("WAL data marked as DELETE")
|
|
}
|
|
}
|
|
|
|
// Add tests that check a WAL file is created, and that a checkpoint succeeds
|
|
func Test_WALDatabaseCreatedOK(t *testing.T) {
|
|
path := mustTempFile()
|
|
defer os.Remove(path)
|
|
|
|
db, err := Open(path, false, true)
|
|
if err != nil {
|
|
t.Fatalf("failed to open database in WAL mode: %s", err.Error())
|
|
}
|
|
defer db.Close()
|
|
|
|
if !db.WALEnabled() {
|
|
t.Fatalf("WAL mode not enabled")
|
|
}
|
|
|
|
if db.InMemory() {
|
|
t.Fatalf("on-disk WAL database marked as in-memory")
|
|
}
|
|
|
|
if _, err := db.ExecuteStringStmt("CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)"); err != nil {
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
}
|
|
|
|
if !IsWALModeEnabledSQLiteFile(path) {
|
|
t.Fatalf("SQLite file not marked as WAL")
|
|
}
|
|
|
|
walPath := path + "-wal"
|
|
if _, err := os.Stat(walPath); os.IsNotExist(err) {
|
|
t.Fatalf("WAL file does not exist")
|
|
}
|
|
|
|
if err := db.Checkpoint(5 * time.Second); err != nil {
|
|
t.Fatalf("failed to checkpoint database in WAL mode: %s", err.Error())
|
|
}
|
|
}
|
|
|
|
func test_FileCreationOnDisk(t *testing.T, db *DB) {
|
|
defer db.Close()
|
|
if db.InMemory() {
|
|
t.Fatal("on-disk database marked as in-memory")
|
|
}
|
|
if db.FKEnabled() {
|
|
t.Fatal("FK constraints marked as enabled")
|
|
}
|
|
|
|
// Confirm checkpoint works on all types of on-disk databases. Worst case, this
|
|
// should be ignored.
|
|
if err := db.Checkpoint(5 * time.Second); err != nil {
|
|
t.Fatalf("failed to checkpoint database in DELETE mode: %s", err.Error())
|
|
}
|
|
}
|
|
|
|
// test_ConnectionIsolationOnDisk test that ISOLATION behavior of on-disk databases doesn't
|
|
// change unexpectedly.
|
|
func test_ConnectionIsolationOnDisk(t *testing.T, db *DB) {
|
|
r, err := db.ExecuteStringStmt("CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)")
|
|
if err != nil {
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
}
|
|
if exp, got := `[{}]`, asJSON(r); exp != got {
|
|
t.Fatalf("unexpected results for query, expected %s, got %s", exp, got)
|
|
}
|
|
|
|
r, err = db.ExecuteStringStmt("BEGIN")
|
|
if err != nil {
|
|
t.Fatalf("failed to create table: %s", err.Error())
|
|
}
|
|
if exp, got := `[{}]`, asJSON(r); exp != got {
|
|
t.Fatalf("unexpected results for query, expected %s, got %s", exp, got)
|
|
}
|
|
|
|
r, err = db.ExecuteStringStmt(`INSERT INTO foo(name) VALUES("fiona")`)
|
|
if err != nil {
|
|
t.Fatalf("error executing insertion into table: %s", err.Error())
|
|
}
|
|
if exp, got := `[{"last_insert_id":1,"rows_affected":1}]`, asJSON(r); exp != got {
|
|
t.Fatalf("unexpected results for execute, expected %s, got %s", exp, got)
|
|
}
|
|
|
|
q, err := db.QueryStringStmt("SELECT * FROM foo")
|
|
if err != nil {
|
|
t.Fatalf("failed to query empty table: %s", err.Error())
|
|
}
|
|
if exp, got := `[{"columns":["id","name"],"types":["integer","text"]}]`, asJSON(q); exp != got {
|
|
t.Fatalf("unexpected results for query, expected %s, got %s", exp, got)
|
|
}
|
|
|
|
_, err = db.ExecuteStringStmt("COMMIT")
|
|
if err != nil {
|
|
t.Fatalf("error executing insertion into table: %s", err.Error())
|
|
}
|
|
|
|
q, err = db.QueryStringStmt("SELECT * FROM foo")
|
|
if err != nil {
|
|
t.Fatalf("failed to query empty table: %s", err.Error())
|
|
}
|
|
if exp, got := `[{"columns":["id","name"],"types":["integer","text"],"values":[[1,"fiona"]]}]`, asJSON(q); exp != got {
|
|
t.Fatalf("unexpected results for query, expected %s, got %s", exp, got)
|
|
}
|
|
}
|
|
|
|
func Test_DatabaseCommonOnDiskOperations(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
testFunc func(*testing.T, *DB)
|
|
}{
|
|
{"FileCreationOnDisk", test_FileCreationOnDisk},
|
|
{"ConnectionIsolationOnDisk", test_ConnectionIsolationOnDisk},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
db, path := mustCreateOnDiskDatabase()
|
|
defer db.Close()
|
|
defer os.Remove(path)
|
|
t.Run(tc.name+":disk", func(t *testing.T) {
|
|
tc.testFunc(t, db)
|
|
})
|
|
|
|
db, path = mustCreateOnDiskDatabaseWAL()
|
|
defer db.Close()
|
|
defer os.Remove(path)
|
|
t.Run(tc.name+":wal", func(t *testing.T) {
|
|
tc.testFunc(t, db)
|
|
})
|
|
}
|
|
}
|