Beispiel #1
0
func TestCustomConstraint(t *testing.T) {
	setUp(t)
	constraint := &Constraint{
		Name: "accounts_balance_check",
		GetError: func(e *pq.Error) *Error {
			return &Error{
				Message:  "Cannot write a negative balance",
				Severity: e.Severity,
				Table:    e.Table,
				Detail:   e.Detail,
				Code:     string(e.Code),
			}
		},
	}
	RegisterConstraint(constraint)
	_, err := db.Exec("INSERT INTO accounts (id, email, balance) VALUES ($1, $2, -1)", uuid, email)
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "Cannot write a negative balance")
		test.AssertEquals(t, e.Table, "accounts")
	default:
		t.Fail()
	}
}
Beispiel #2
0
func TestNotNull(t *testing.T) {
	setUp(t)
	_, err := db.Exec("INSERT INTO accounts (id) VALUES (null)")
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "No id was provided. Please provide a id")
		test.AssertEquals(t, e.Column, "id")
		test.AssertEquals(t, e.Table, "accounts")
	default:
		t.Fail()
	}
}
Beispiel #3
0
func TestDefaultConstraint(t *testing.T) {
	// this test needs to go before the Register() below... not great, add an
	// unregister or clear out the map or something
	setUp(t)
	_, err := db.Exec("INSERT INTO accounts (id, email, balance) VALUES ($1, $2, -1)", uuid, email)
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "new row for relation \"accounts\" violates check constraint \"accounts_balance_check\"")
		test.AssertEquals(t, e.Table, "accounts")
	default:
		t.Fail()
	}
}
Beispiel #4
0
func TestForeignKeyFailure(t *testing.T) {
	setUp(t)
	query := "INSERT INTO payments (id, account_id) VALUES ($1, $2)"
	_, err := db.Exec(query, uuid, uuid2)

	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "Can't save to payments because the account_id (91f47e99-d616-4d8c-9c02-cbd13bceac60) isn't present in the accounts table")
		test.AssertEquals(t, e.Column, "")
		test.AssertEquals(t, e.Table, "payments")
		test.AssertEquals(t, e.Code, CodeForeignKeyViolation)
	default:
		t.Fail()
	}
	tearDown(t)
}
Beispiel #5
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)
}
Beispiel #6
0
func TestTooLargeInt(t *testing.T) {
	setUp(t)
	_, err := db.Exec("INSERT INTO accounts (id, email, balance) VALUES ($1, $2, 40000)", uuid, email)
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "Smallint too large or too small")
	default:
		t.Fail()
	}
}
Beispiel #7
0
func TestInvalidEnum(t *testing.T) {
	setUp(t)
	_, err := db.Exec("INSERT INTO accounts (id, email, balance, status) VALUES ($1, $2, 1, 'blah')", uuid, email)
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "Invalid account_status: \"blah\"")
	default:
		t.Fail()
	}
}
Beispiel #8
0
func TestInvalidUUID(t *testing.T) {
	setUp(t)
	_, err := db.Exec("INSERT INTO accounts (id) VALUES ('foo')")
	dberr := GetError(err)
	switch e := dberr.(type) {
	case *Error:
		test.AssertEquals(t, e.Error(), "Invalid input syntax for type uuid: \"foo\"")
	default:
		t.Fail()
	}
}
Beispiel #9
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)
}
Beispiel #10
0
func TestValueFinder(t *testing.T) {
	test.AssertEquals(t, findValue("Key (id)=(blah) already exists."), "blah")
	test.AssertEquals(t, findValue("Key (foo)=(foo blah) already exists."), "foo blah")
	test.AssertEquals(t, findValue("Unknown detail message"), "")
}
Beispiel #11
0
func TestColumnFinder(t *testing.T) {
	test.AssertEquals(t, findColumn("Key (id)=(blah) already exists."), "id")
	test.AssertEquals(t, findColumn("Key (foo bar)=(blah) already exists."), "foo bar")
	test.AssertEquals(t, findColumn("Unknown detail message"), "")
}
Beispiel #12
0
func TestCapitalize(t *testing.T) {
	test.AssertEquals(t, capitalize("foo"), "Foo")
	test.AssertEquals(t, capitalize("foo bar baz"), "Foo bar baz")
}
Beispiel #13
0
func TestNilError(t *testing.T) {
	test.AssertEquals(t, GetError(nil), nil)
}