go/pgutil/errors_test.go

37 lines
751 B
Go
Raw Normal View History

2024-11-11 05:36:55 +00:00
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)
}
}