check_test.go 1.2 KB

1234567891011121314151617181920212223242526272829
  1. package pgserver
  2. import (
  3. "testing"
  4. )
  5. // A CHECK violation reports SQLSTATE 23514 on both the simple and the extended
  6. // protocol and, inside a transaction block, leaves the transaction failed like
  7. // the other integrity errors.
  8. func TestCheckConstraintSQLStateAndTransactionState(t *testing.T) {
  9. for _, extended := range []bool{false, true} {
  10. c, client := newDataConnection(t)
  11. runQuery(t, c, client, &Message{Type: MsgQuery, Data: []byte("CREATE TABLE t (id INTEGER PRIMARY KEY, age INTEGER CHECK (age > 0))\x00")})
  12. runQuery(t, c, client, &Message{Type: MsgQuery, Data: []byte("BEGIN\x00")})
  13. var messages []*Message
  14. if extended {
  15. messages = runExtendedExecution(t, c, client, "INSERT INTO t VALUES (1,-1)")
  16. } else {
  17. messages = runQuery(t, c, client, &Message{Type: MsgQuery, Data: []byte("INSERT INTO t VALUES (1,-1)\x00")})
  18. }
  19. if code := errorCode(messages[0]); code != ErrCodeCheckViolation {
  20. t.Fatalf("extended=%v SQLSTATE=%q, want %q", extended, code, ErrCodeCheckViolation)
  21. }
  22. if c.txStatus != TxStatusFailed {
  23. t.Fatalf("extended=%v transaction not failed: %c", extended, c.txStatus)
  24. }
  25. runQuery(t, c, client, &Message{Type: MsgQuery, Data: []byte("ROLLBACK\x00")})
  26. }
  27. }