Cleanup, testing

This commit is contained in:
jdl 2024-10-29 19:05:09 +01:00
parent db198f5391
commit 56996d93a1
3 changed files with 42 additions and 4 deletions

View File

@ -14,10 +14,6 @@ func ErrIsForeignKey(err error) bool {
return ErrHasCode(err, "23503") return ErrHasCode(err, "23503")
} }
func ErrIsCheck(err error) bool {
return ErrHasCode(err, "23514")
}
func ErrIsSerializationFaiilure(err error) bool { func ErrIsSerializationFaiilure(err error) bool {
return ErrHasCode(err, "40001") return ErrHasCode(err, "40001")
} }

36
errors_test.go Normal file
View File

@ -0,0 +1,36 @@
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)
}
}

View File

@ -1,3 +1,9 @@
CREATE TABLE users( CREATE TABLE users(
UserID BIGINT NOT NULL PRIMARY KEY, UserID BIGINT NOT NULL PRIMARY KEY,
Email TEXT NOT NULL UNIQUE); Email TEXT NOT NULL UNIQUE);
CREATE TABLE user_notes(
UserID BIGINT NOT NULL REFERENCES users(UserID),
NoteID BIGINT NOT NULL,
Note Text NOT NULL,
PRIMARY KEY(UserID,NoteID));