File size: 3,119 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
#include "sqliteInt.h"
#include "unity.h"
#include <stdio.h>
#include <string.h>

/* The wrapper is provided in the module for static functions */
extern void test_failConstraintFunc(sqlite3_context *ctx, int NotUsed, sqlite3_value **argv);

static void xCallFail(sqlite3_context *ctx, int argc, sqlite3_value **argv){
  /* Forward directly to the function under test */
  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) {
  /* No global setup needed */
}

void tearDown(void) {
  /* No global teardown needed */
}

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();
}