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='log';", ).Scan(&table_name) assert_string(t, "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(), ) }