45 lines
748 B
Go
45 lines
748 B
Go
package sqliteutil
|
|
|
|
import (
|
|
"database/sql"
|
|
"embed"
|
|
"testing"
|
|
)
|
|
|
|
//go:embed test-migrations
|
|
var testMigrationFS embed.FS
|
|
|
|
func TestMigrate(t *testing.T) {
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := Migrate(db, testMigrationFS); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Shouldn't have any effect.
|
|
if err := Migrate(db, testMigrationFS); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
query := `SELECT EXISTS(SELECT 1 FROM users WHERE UserID=$1)`
|
|
var exists bool
|
|
|
|
if err = db.QueryRow(query, 1).Scan(&exists); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if exists {
|
|
t.Fatal("1 shouldn't exist")
|
|
}
|
|
|
|
if err = db.QueryRow(query, 2).Scan(&exists); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !exists {
|
|
t.Fatal("2 should exist")
|
|
}
|
|
|
|
}
|