AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <stdio.h>4#include <string.h>5 6/* The wrapper is provided in the module for static functions */7extern void test_failConstraintFunc(sqlite3_context *ctx, int NotUsed, sqlite3_value **argv);8 9static void xCallFail(sqlite3_context *ctx, int argc, sqlite3_value **argv){10 /* Forward directly to the function under test */11 test_failConstraintFunc(ctx, argc, argv);12}13 14static int register_call_fail(sqlite3 *db){15 return sqlite3_create_function(db, "call_fail", 2, SQLITE_UTF8, 0, xCallFail, 0, 0);16}17 18void setUp(void) {19 /* No global setup needed */20}21 22void tearDown(void) {23 /* No global teardown needed */24}25 26static void prepare_and_bind(sqlite3 *db, const char *zMsg, int errCode, sqlite3_stmt **ppStmt){27 int rc = sqlite3_prepare_v2(db, "SELECT call_fail(?,?)", -1, ppStmt, 0);28 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);29 rc = sqlite3_bind_text(*ppStmt, 1, zMsg, -1, SQLITE_TRANSIENT);30 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);31 rc = sqlite3_bind_int(*ppStmt, 2, errCode);32 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);33}34 35void test_failConstraintFunc_sets_constraint_error_and_message(void) {36 sqlite3 *db = 0;37 sqlite3_stmt *stmt = 0;38 int rc;39 40 rc = sqlite3_open(":memory:", &db);41 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);42 43 rc = register_call_fail(db);44 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);45 46 const char *msg = "custom constraint failure";47 prepare_and_bind(db, msg, SQLITE_CONSTRAINT, &stmt);48 49 rc = sqlite3_step(stmt);50 TEST_ASSERT_EQUAL_INT(SQLITE_CONSTRAINT, rc);51 52 const char *err = sqlite3_errmsg(db);53 TEST_ASSERT_NOT_NULL(err);54 TEST_ASSERT_EQUAL_STRING(msg, err);55 56 sqlite3_finalize(stmt);57 sqlite3_close(db);58}59 60void test_failConstraintFunc_sets_TOOBIG_error_and_message(void) {61 sqlite3 *db = 0;62 sqlite3_stmt *stmt = 0;63 int rc;64 65 rc = sqlite3_open(":memory:", &db);66 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);67 68 rc = register_call_fail(db);69 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);70 71 const char *msg = "payload too big error";72 prepare_and_bind(db, msg, SQLITE_TOOBIG, &stmt);73 74 rc = sqlite3_step(stmt);75 TEST_ASSERT_EQUAL_INT(SQLITE_TOOBIG, rc);76 77 const char *err = sqlite3_errmsg(db);78 TEST_ASSERT_NOT_NULL(err);79 TEST_ASSERT_EQUAL_STRING(msg, err);80 81 sqlite3_finalize(stmt);82 sqlite3_close(db);83}84 85void test_failConstraintFunc_handles_empty_message(void) {86 sqlite3 *db = 0;87 sqlite3_stmt *stmt = 0;88 int rc;89 90 rc = sqlite3_open(":memory:", &db);91 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);92 93 rc = register_call_fail(db);94 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);95 96 const char *msg = "";97 prepare_and_bind(db, msg, SQLITE_MISMATCH, &stmt);98 99 rc = sqlite3_step(stmt);100 TEST_ASSERT_EQUAL_INT(SQLITE_MISMATCH, rc);101 102 const char *err = sqlite3_errmsg(db);103 TEST_ASSERT_NOT_NULL(err);104 TEST_ASSERT_EQUAL_STRING(msg, err);105 106 sqlite3_finalize(stmt);107 sqlite3_close(db);108}109 110int main(void) {111 UNITY_BEGIN();112 RUN_TEST(test_failConstraintFunc_sets_constraint_error_and_message);113 RUN_TEST(test_failConstraintFunc_sets_TOOBIG_error_and_message);114 RUN_TEST(test_failConstraintFunc_handles_empty_message);115 return UNITY_END();116}