pgutil/errors_test.go
2024-10-29 19:05:09 +01:00

37 lines
751 B
Go

package pgutil
import (
"database/sql"
"testing"
)
func TestErrors(t *testing.T) {
db, err := sql.Open(
"postgres",
"host=127.0.0.1 dbname=test sslmode=disable user=test password=test")
if err != nil {
t.Fatal(err)
}
if err := DropAllTables(db); err != nil {
t.Fatal(err)
}
if err := Migrate(db, testMigrationFS); err != nil {
t.Fatal(err)
}
_, err = db.Exec(`INSERT INTO users(UserID, Email) VALUES (2, 'q@r.com')`)
if !ErrIsDuplicateKey(err) {
t.Fatal(err)
}
_, err = db.Exec(`INSERT INTO users(UserID, Email) VALUES (3, 'c@d.com')`)
if !ErrIsDuplicateKey(err) {
t.Fatal(err)
}
_, err = db.Exec(`INSERT INTO user_notes(UserID, NoteID, Note) VALUES (4, 1, 'hello')`)
if !ErrIsForeignKey(err) {
t.Fatal(err)
}
}