Add a test case for error handling.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 22 Dec 2014 18:19:45 +0000 (20:19 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 22 Dec 2014 20:39:06 +0000 (22:39 +0200)
The current behavior isn't actually quite correct with some configurations,
but seems better to add the regression test now anyway, even though the
errors_2.out basically memorizes the bogus behavior. It should be removed
when the bug is fixed.

Makefile.am
test/expected/errors.out [new file with mode: 0644]
test/expected/errors_1.out [new file with mode: 0644]
test/expected/errors_2.out [new file with mode: 0644]
test/src/errors-test.c [new file with mode: 0644]
test/tests

index 1bacce8a36d4974094e47b57169ce9fbd1082791..ed24e0d027c61a9365ba8fbbf8fd0122fc138446 100644 (file)
@@ -108,6 +108,9 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \
    test/expected/declare-fetch-commit-test.out \
    test/expected/deprecated.out \
    test/expected/diagnostic.out \
+   test/expected/errors.out \
+   test/expected/errors_1.out \
+   test/expected/errors_2.out \
    test/expected/error-rollback.out \
    test/expected/getresult.out \
    test/expected/insertreturning.out \
@@ -154,6 +157,7 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \
    test/src/declare-fetch-commit-test.c \
    test/src/deprecated-test.c \
    test/src/diagnostic-test.c \
+   test/src/errors-test.c \
    test/src/error-rollback-test.c \
    test/src/getresult-test.c \
    test/src/insertreturning-test.c \
diff --git a/test/expected/errors.out b/test/expected/errors.out
new file mode 100644 (file)
index 0000000..965f4fc
--- /dev/null
@@ -0,0 +1,12 @@
+\! "./src/errors-test"
+connected
+
+42703=ERROR: column "doesnotexist" does not exist;
+Error while executing the query
+
+42703=ERROR: column "doesnotexist2" does not exist;
+Error while executing the query
+
+42703=ERROR: column "doesnotexist3" does not exist;
+Error while preparing parameters
+disconnecting
diff --git a/test/expected/errors_1.out b/test/expected/errors_1.out
new file mode 100644 (file)
index 0000000..49d169d
--- /dev/null
@@ -0,0 +1,12 @@
+\! "./src/errors-test"
+connected
+
+42703=ERROR: column "doesnotexist" does not exist;
+Error while executing the query
+
+42703=ERROR: column "doesnotexist2" does not exist;
+Error while executing the query
+
+42703=ERROR: column "doesnotexist3" does not exist;
+Error while executing the query
+disconnecting
diff --git a/test/expected/errors_2.out b/test/expected/errors_2.out
new file mode 100644 (file)
index 0000000..3a37f89
--- /dev/null
@@ -0,0 +1,12 @@
+\! "./src/errors-test"
+connected
+
+42703=ERROR: column "doesnotexist" does not exist;
+Error while executing the query
+
+25P02=ERROR: current transaction is aborted, commands ignored until end of transaction block;
+Error while executing the query
+
+25P02=ERROR: current transaction is aborted, commands ignored until end of transaction block;
+Error while executing the query
+disconnecting
diff --git a/test/src/errors-test.c b/test/src/errors-test.c
new file mode 100644 (file)
index 0000000..311d17c
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Test behavior when errors happen. Should get a proper error message, and
+ * the connection should stay in a sane state.
+ *
+ * FIXME: With some combinations of settings, an error can leave a
+ * transaction open, but in aborted state. That's a bug in the driver, but
+ * because I'm not planning to fix it right now, there is an expected output
+ * file (errors_2.out) for that. The bug should be fixed, and the expected
+ * output removed.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int
+main(int argc, char **argv)
+{
+   SQLRETURN   rc;
+   HSTMT       hstmt = SQL_NULL_HSTMT;
+   char        param1[20] = "foo";
+   SQLLEN      cbParam1;
+
+   test_connect();
+
+   rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
+   if (!SQL_SUCCEEDED(rc))
+   {
+       print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+       exit(1);
+   }
+
+   /* Test a simple parse-time error */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT doesnotexist", SQL_NTS);
+   /* Print error, it is expected */
+   if (!SQL_SUCCEEDED(rc))
+       print_diag("", SQL_HANDLE_STMT, hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /* The same, but with a bind parameter */
+
+   /* bind param  */
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_CHAR, /* param type */
+                         20,           /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Test a simple parse-time error */
+   rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT doesnotexist2 WHERE bogus = ?", SQL_NTS);
+   /* Print error, it is expected */
+   if (!SQL_SUCCEEDED(rc))
+       print_diag("", SQL_HANDLE_STMT, hstmt);
+
+   rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+   CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+   /*** Test syntax error with SQLPrepare ***/
+
+   /* Test a simple parse-time error */
+   rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT doesnotexist3 WHERE bogus = ?", SQL_NTS);
+
+   /* bind param  */
+   cbParam1 = SQL_NTS;
+   rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
+                         SQL_C_CHAR,   /* value type */
+                         SQL_CHAR, /* param type */
+                         20,           /* column size */
+                         0,            /* dec digits */
+                         param1,       /* param value ptr */
+                         0,            /* buffer len */
+                         &cbParam1     /* StrLen_or_IndPtr */);
+   CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
+
+   /* Test a simple parse-time error */
+   rc = SQLExecute(hstmt);
+   /* Print error, it is expected */
+   if (!SQL_SUCCEEDED(rc))
+       print_diag("", SQL_HANDLE_STMT, hstmt);
+
+
+   test_disconnect();
+
+   return 0;
+}
index d5f8b6c15bbe4a15f0c5e34b6b907f0e7c067cc8..5d359b74e996767908e9fe63080faa6eaacbf480 100644 (file)
@@ -39,6 +39,7 @@ TESTBINS = src/connect-test \
    src/lfconversion-test \
    src/cte-test \
    src/deprecated-test \
+   src/errors-test \
    src/error-rollback-test \
    src/diagnostic-test \
    src/numeric-test \