| | #include "sqliteInt.h" |
| | #include "unity.h" |
| | #include <stdio.h> |
| | #include <string.h> |
| |
|
| | |
| | extern void test_failConstraintFunc(sqlite3_context *ctx, int NotUsed, sqlite3_value **argv); |
| |
|
| | static void xCallFail(sqlite3_context *ctx, int argc, sqlite3_value **argv){ |
| | |
| | test_failConstraintFunc(ctx, argc, argv); |
| | } |
| |
|
| | static int register_call_fail(sqlite3 *db){ |
| | return sqlite3_create_function(db, "call_fail", 2, SQLITE_UTF8, 0, xCallFail, 0, 0); |
| | } |
| |
|
| | void setUp(void) { |
| | |
| | } |
| |
|
| | void tearDown(void) { |
| | |
| | } |
| |
|
| | static void prepare_and_bind(sqlite3 *db, const char *zMsg, int errCode, sqlite3_stmt **ppStmt){ |
| | int rc = sqlite3_prepare_v2(db, "SELECT call_fail(?,?)", -1, ppStmt, 0); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| | rc = sqlite3_bind_text(*ppStmt, 1, zMsg, -1, SQLITE_TRANSIENT); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| | rc = sqlite3_bind_int(*ppStmt, 2, errCode); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| | } |
| |
|
| | void test_failConstraintFunc_sets_constraint_error_and_message(void) { |
| | sqlite3 *db = 0; |
| | sqlite3_stmt *stmt = 0; |
| | int rc; |
| |
|
| | rc = sqlite3_open(":memory:", &db); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| |
|
| | rc = register_call_fail(db); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| |
|
| | const char *msg = "custom constraint failure"; |
| | prepare_and_bind(db, msg, SQLITE_CONSTRAINT, &stmt); |
| |
|
| | rc = sqlite3_step(stmt); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_CONSTRAINT, rc); |
| |
|
| | const char *err = sqlite3_errmsg(db); |
| | TEST_ASSERT_NOT_NULL(err); |
| | TEST_ASSERT_EQUAL_STRING(msg, err); |
| |
|
| | sqlite3_finalize(stmt); |
| | sqlite3_close(db); |
| | } |
| |
|
| | void test_failConstraintFunc_sets_TOOBIG_error_and_message(void) { |
| | sqlite3 *db = 0; |
| | sqlite3_stmt *stmt = 0; |
| | int rc; |
| |
|
| | rc = sqlite3_open(":memory:", &db); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| |
|
| | rc = register_call_fail(db); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| |
|
| | const char *msg = "payload too big error"; |
| | prepare_and_bind(db, msg, SQLITE_TOOBIG, &stmt); |
| |
|
| | rc = sqlite3_step(stmt); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_TOOBIG, rc); |
| |
|
| | const char *err = sqlite3_errmsg(db); |
| | TEST_ASSERT_NOT_NULL(err); |
| | TEST_ASSERT_EQUAL_STRING(msg, err); |
| |
|
| | sqlite3_finalize(stmt); |
| | sqlite3_close(db); |
| | } |
| |
|
| | void test_failConstraintFunc_handles_empty_message(void) { |
| | sqlite3 *db = 0; |
| | sqlite3_stmt *stmt = 0; |
| | int rc; |
| |
|
| | rc = sqlite3_open(":memory:", &db); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| |
|
| | rc = register_call_fail(db); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc); |
| |
|
| | const char *msg = ""; |
| | prepare_and_bind(db, msg, SQLITE_MISMATCH, &stmt); |
| |
|
| | rc = sqlite3_step(stmt); |
| | TEST_ASSERT_EQUAL_INT(SQLITE_MISMATCH, rc); |
| |
|
| | const char *err = sqlite3_errmsg(db); |
| | TEST_ASSERT_NOT_NULL(err); |
| | TEST_ASSERT_EQUAL_STRING(msg, err); |
| |
|
| | sqlite3_finalize(stmt); |
| | sqlite3_close(db); |
| | } |
| |
|
| | int main(void) { |
| | UNITY_BEGIN(); |
| | RUN_TEST(test_failConstraintFunc_sets_constraint_error_and_message); |
| | RUN_TEST(test_failConstraintFunc_sets_TOOBIG_error_and_message); |
| | RUN_TEST(test_failConstraintFunc_handles_empty_message); |
| | return UNITY_END(); |
| | } |