You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diary/db/db_test.go

68 lines
1.4 KiB

package db
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
)
func assert_string(t *testing.T, expected string, actual string) {
if actual != expected {
t.Fatalf("(%s, %s)", expected, actual)
}
}
func assert_int(t *testing.T, expected int, actual int) {
if actual != expected {
t.Fatalf("(%d, %d)", expected, actual)
}
}
func TestInitBasic(t *testing.T) {
var db_location = ":memory:"
ctx, err := Init(db_location)
if err != nil {
t.Fatalf(err.Error())
}
// check that the tables exist
var table_name string
ctx.Db.QueryRow(
"SELECT name FROM sqlite_master WHERE type='table' AND name='diary_log';",
).Scan(&table_name)
assert_string(t, "diary_log", table_name)
// Check that the version stored is correct
var version int
ctx.Db.QueryRow("PRAGMA user_version").Scan(&version)
assert_int(t, __version, ctx.Version)
assert_int(t, __version, version)
}
func TestInitWrongVersion(t *testing.T) {
file, err := ioutil.TempFile("", "log_test_init_wrong.*.db")
defer os.Remove(file.Name()) // clean up
db, err := sql.Open("sqlite3", file.Name())
if err != nil {
log.Fatal(err.Error())
}
db.Exec(fmt.Sprintf(`PRAGMA user_version=%d`, __version+1))
var version int
db.QueryRow("PRAGMA user_version").Scan(&version)
_, err = Init(file.Name())
if err == nil {
t.Fatal("No error when error expected")
}
assert_string(
t,
fmt.Sprintf("Wrong version. Expected %d got %d", __version, __version+1),
err.Error(),
)
}