Example #1
0
func TestUniqueFailureOnUpdate(t *testing.T) {
	setUp(t)
	query := "INSERT INTO accounts (id, email, balance) VALUES ($1, $2, 1)"
	_, err := db.Exec(query, uuid, email)
	test.AssertNotError(t, err, "")
	_, err = db.Exec(query, uuid2, email2)
	test.AssertNotError(t, err, "")

	_, err = db.Exec("UPDATE accounts SET email = $1 WHERE id = $2", email, uuid2)
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "A email already exists with this value ([email protected])")
		test.AssertEquals(t, e.Column, "email")
		test.AssertEquals(t, e.Table, "accounts")
		test.AssertEquals(t, e.Code, CodeUniqueViolation)
	default:
		t.Fail()
	}
	tearDown(t)
}
Example #2
0
func TestUniqueConstraint(t *testing.T) {
	setUp(t)
	query := "INSERT INTO accounts (id, email, balance) VALUES ($1, $2, 1)"
	_, err := db.Exec(query, uuid, email)
	test.AssertNotError(t, err, "")
	_, err = db.Exec(query, uuid, email)
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "A id already exists with this value (3c7d2b4a-3fc8-4782-a518-4ce9efef51e7)")
		test.AssertEquals(t, e.Column, "id")
		test.AssertEquals(t, e.Table, "accounts")
		test.AssertEquals(t, e.Code, CodeUniqueViolation)
	default:
		t.Fail()
	}
	tearDown(t)
}
Example #3
0
func tearDown(t *testing.T) {
	if db != nil {
		_, err := db.Exec("DELETE FROM accounts CASCADE; DELETE FROM payments CASCADE;")
		test.AssertNotError(t, err, "")
	}
}