File size: 7,550 Bytes
7510827 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
#include "sqliteInt.h"
#include "unity.h"
#include <stdlib.h>
#include <string.h>
/* Wrapper provided by build system for static function under test */
extern void test_findConstraintFunc(sqlite3_context *ctx, int NotUsed, sqlite3_value **argv);
/* UDF that forwards to the function under test so we can get a valid sqlite3_context */
static void udf_findc(sqlite3_context *ctx, int argc, sqlite3_value **argv){
/* Forward directly to the function under test. The function only uses the first two args. */
(void)argc; /* Silence unused warning since function ignores argc */
test_findConstraintFunc(ctx, 0, argv);
}
static void register_findc(sqlite3 *db){
int rc = sqlite3_create_function(db, "findc", 2, SQLITE_UTF8, 0, udf_findc, 0, 0);
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "sqlite3_create_function(findc) failed");
}
static void assert_findc_result(sqlite3 *db,
const char *sqlDef, /* CREATE TABLE SQL or other */
const char *consName, /* constraint name, may be NULL */
int expectType, /* SQLITE_INTEGER or SQLITE_NULL */
int expectInt){ /* expected int if expectType==SQLITE_INTEGER */
sqlite3_stmt *stmt = 0;
int rc = sqlite3_prepare_v2(db, "SELECT findc(?,?)", -1, &stmt, 0);
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "prepare SELECT findc failed");
if( sqlDef ){
rc = sqlite3_bind_text(stmt, 1, sqlDef, -1, SQLITE_TRANSIENT);
}else{
rc = sqlite3_bind_null(stmt, 1);
}
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "bind #1 failed");
if( consName ){
rc = sqlite3_bind_text(stmt, 2, consName, -1, SQLITE_TRANSIENT);
}else{
rc = sqlite3_bind_null(stmt, 2);
}
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "bind #2 failed");
rc = sqlite3_step(stmt);
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_ROW, rc, "step did not return row");
int gotType = sqlite3_column_type(stmt, 0);
TEST_ASSERT_EQUAL_INT(expectType, gotType);
if( expectType==SQLITE_INTEGER ){
int got = sqlite3_column_int(stmt, 0);
TEST_ASSERT_EQUAL_INT(expectInt, got);
}
rc = sqlite3_step(stmt);
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_DONE, rc, "expected single row");
rc = sqlite3_finalize(stmt);
TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "finalize failed");
}
/* Unity hooks */
void setUp(void) {
/* No-op */
}
void tearDown(void) {
/* No-op */
}
/* Tests */
static void test_findConstraintFunc_basic_match(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
const char *sql =
"CREATE TABLE t ("
" a INT,"
" CONSTRAINT c1 CHECK(a > 0),"
" b TEXT"
");";
/* Exact name */
assert_findc_result(db, sql, "c1", SQLITE_INTEGER, 1);
/* Not present */
assert_findc_result(db, sql, "c2", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_case_insensitive_and_quoted(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
/* Quoted identifier with spaces; test case-insensitive compare */
const char *sql =
"CREATE TABLE t ("
" a INT,"
" CONSTRAINT \"My Constr\" UNIQUE(a)"
");";
assert_findc_result(db, sql, "My Constr", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "my constr", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "MY CONSTR", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "Other", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_bracket_and_backtick_quoted(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
const char *sql1 =
"CREATE TABLE t1 ("
" a INT,"
" CONSTRAINT [C-Name] CHECK(a IS NOT NULL)"
");";
assert_findc_result(db, sql1, "c-name", SQLITE_INTEGER, 1);
assert_findc_result(db, sql1, "C-Name", SQLITE_INTEGER, 1);
assert_findc_result(db, sql1, "other", SQLITE_INTEGER, 0);
const char *sql2 =
"CREATE TABLE t2 ("
" a INT,"
" CONSTRAINT `bk` CHECK(a > 0)"
");";
assert_findc_result(db, sql2, "bk", SQLITE_INTEGER, 1);
assert_findc_result(db, sql2, "BK", SQLITE_INTEGER, 1);
assert_findc_result(db, sql2, "xx", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_with_comments_and_nested_parens(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
const char *sql =
"/* heading comment */ CREATE /* mid */ TABLE t (\n"
" a INT, -- column a\n"
" CONSTRAINT /*name*/ \"C 1\" CHECK( (a > 0) AND (a < 10) )\n"
"); -- trailer\n";
assert_findc_result(db, sql, "C 1", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "c 1", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "not-there", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_no_named_constraints_returns_0(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
/* Constraints without explicit "CONSTRAINT <name>" should not match any name */
const char *sql =
"CREATE TABLE t ("
" a INT,"
" UNIQUE(a),"
" PRIMARY KEY(a)"
");";
assert_findc_result(db, sql, "anything", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_no_left_paren_in_sql_returns_0(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
/* Not a CREATE TABLE statement, and no '(' token before TK_ILLEGAL */
const char *sql = "DROP TABLE t;";
assert_findc_result(db, sql, "c1", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_null_arguments_return_null(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
/* NULL SQL input */
assert_findc_result(db, NULL, "c1", SQLITE_NULL, 0);
/* NULL constraint name input */
const char *sql =
"CREATE TABLE t ( a INT, CONSTRAINT c1 CHECK(a>0) );";
assert_findc_result(db, sql, NULL, SQLITE_NULL, 0);
/* Both NULL */
assert_findc_result(db, NULL, NULL, SQLITE_NULL, 0);
sqlite3_close(db);
}
static void test_findConstraintFunc_multiple_constraints_both_detected(void){
sqlite3 *db = 0;
TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));
register_findc(db);
const char *sql =
"CREATE TABLE t ("
" a INT, b INT,"
" CONSTRAINT cA CHECK(a>0),"
" CONSTRAINT cB UNIQUE(b)"
");";
assert_findc_result(db, sql, "cA", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "ca", SQLITE_INTEGER, 1); /* case-insensitive */
assert_findc_result(db, sql, "cB", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "cb", SQLITE_INTEGER, 1);
assert_findc_result(db, sql, "nope", SQLITE_INTEGER, 0);
sqlite3_close(db);
}
int main(void){
UNITY_BEGIN();
RUN_TEST(test_findConstraintFunc_basic_match);
RUN_TEST(test_findConstraintFunc_case_insensitive_and_quoted);
RUN_TEST(test_findConstraintFunc_bracket_and_backtick_quoted);
RUN_TEST(test_findConstraintFunc_with_comments_and_nested_parens);
RUN_TEST(test_findConstraintFunc_no_named_constraints_returns_0);
RUN_TEST(test_findConstraintFunc_no_left_paren_in_sql_returns_0);
RUN_TEST(test_findConstraintFunc_null_arguments_return_null);
RUN_TEST(test_findConstraintFunc_multiple_constraints_both_detected);
return UNITY_END();
} |