1 /* Public domain, no copyright. Use at your own risk. */
2 /* only sqlite3 backend is tested, I will assume the */
3 /* behaviour is the same with other backends */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <jansson.h>
10
11 #include <check.h>
12 #ifndef _HOEL_SQLITE
13 #define _HOEL_SQLITE
14 #endif
15 #include "hoel.h"
16
17 #define DEFAULT_BD_PATH "/tmp/test.db"
18 #define WRONG_BD_PATH "nope.db"
19
20 #define UNSAFE_STRING "un'safe' (\"string\")#!/$%*];"
21
22 #define SELECT_DATA_1 "SELECT integer_col, string_col, date_col FROM test_table WHERE integer_col = 1"
23 #define SELECT_DATA_2 "SELECT integer_col, string_col, date_col FROM test_table WHERE integer_col = 2"
24 #define SELECT_DATA_ERROR "SELECT integer_col, string_col, date_col FROM test_table WHERE integer_col = 'error'"
25 #define SELECT_DATA_ALL "SELECT * FROM test_table"
26
27 #define INSERT_DATA_1 "INSERT INTO test_table (integer_col, string_col, date_col) VALUES (1, 'value1', date('now'))"
28 #define INSERT_DATA_2 "INSERT INTO test_table (integer_col, string_col, date_col) VALUES (2, 'value2', strftime('%s','2016-06-22 00:52:56'))"
29 #define INSERT_DATA_ERROR "INSERT INTO test_table (integer_col, string_col, date_col) VALUES ('error', 'value error', date('now'))"
30
31 #define DELETE_DATA_1 "DELETE FROM test_table WHERE integer_col = 1"
32 #define DELETE_DATA_2 "DELETE FROM test_table WHERE integer_col = 2"
33 #define DELETE_DATA_ERROR "DELETE FROM test_table WHERE wrong_table = 1"
34 #define DELETE_DATA_ALL "DELETE FROM test_table"
35
36 #define UPDATE_DATA_1 "UPDATE test_table SET string_col='new value1' WHERE integer_col = 1"
37
38 const char * db_path;
39
print_result(struct _h_result result)40 void print_result(struct _h_result result) {
41 size_t col, row, i;
42 printf("rows: %u, col: %u\n", result.nb_rows, result.nb_columns);
43 for (row = 0; row<result.nb_rows; row++) {
44 for (col=0; col<result.nb_columns; col++) {
45 switch(result.data[row][col].type) {
46 case HOEL_COL_TYPE_INT:
47 printf("| %lld ", ((struct _h_type_int *)result.data[row][col].t_data)->value);
48 break;
49 case HOEL_COL_TYPE_DOUBLE:
50 printf("| %f ", ((struct _h_type_double *)result.data[row][col].t_data)->value);
51 break;
52 case HOEL_COL_TYPE_TEXT:
53 printf("| %s ", ((struct _h_type_text *)result.data[row][col].t_data)->value);
54 break;
55 case HOEL_COL_TYPE_BLOB:
56 for (i=0; i<((struct _h_type_blob *)result.data[row][col].t_data)->length; i++) {
57 printf("%c", *((char*)(((struct _h_type_blob *)result.data[row][col].t_data)->value+i)));
58 if (i%80 == 0 && i>0) {
59 printf("\n");
60 }
61 }
62 break;
63 case HOEL_COL_TYPE_NULL:
64 printf("| null ");
65 break;
66 }
67 }
68 printf("|\n");
69 }
70 }
71
START_TEST(test_hoel_init)72 START_TEST(test_hoel_init)
73 {
74 struct _h_connection * conn;
75 conn = h_connect_sqlite(db_path);
76 ck_assert_ptr_ne(conn, NULL);
77 ck_assert_ptr_eq(h_connect_sqlite(WRONG_BD_PATH), NULL);
78 ck_assert_int_eq(h_close_db(conn), H_OK);
79 ck_assert_int_ne(h_close_db(NULL), H_OK);
80 ck_assert_int_eq(h_clean_connection(conn), H_OK);
81 ck_assert_int_ne(h_clean_connection(NULL), H_OK);
82 }
83 END_TEST
84
START_TEST(test_hoel_escape_string)85 START_TEST(test_hoel_escape_string)
86 {
87 struct _h_connection * conn;
88 char * escaped;
89 json_t * j_query, * j_result;
90 int res;
91
92 conn = h_connect_sqlite(db_path);
93 ck_assert_ptr_ne(conn, NULL);
94 escaped = h_escape_string(conn, "value");
95 ck_assert_str_eq(escaped, "value");
96 h_free(escaped);
97 escaped = h_escape_string(conn, "unsafe ' value\"!");
98 ck_assert_str_eq(escaped, "unsafe '' value\"!");
99 h_free(escaped);
100
101 j_query = json_pack("{sss{siss}}", "table", "test_table", "values", "integer_col", 666, "string_col", UNSAFE_STRING);
102 res = h_insert(conn, j_query, NULL);
103 json_decref(j_query);
104 ck_assert_int_eq(res, H_OK);
105 j_query = json_pack("{sss[s]s{si}}", "table", "test_table", "columns", "string_col", "where", "integer_col", 666);
106 res = h_select(conn, j_query, &j_result, NULL);
107 json_decref(j_query);
108 ck_assert_int_eq(res, H_OK);
109 ck_assert_str_eq(UNSAFE_STRING, json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")));
110 json_decref(j_result);
111
112 ck_assert_int_eq(h_close_db(conn), H_OK);
113 ck_assert_int_eq(h_clean_connection(conn), H_OK);
114 }
115 END_TEST
116
START_TEST(test_hoel_escape_string_with_quotes)117 START_TEST(test_hoel_escape_string_with_quotes)
118 {
119 struct _h_connection * conn;
120 char * escaped;
121 json_t * j_query, * j_result;
122 int res;
123
124 conn = h_connect_sqlite(db_path);
125 ck_assert_ptr_ne(conn, NULL);
126 escaped = h_escape_string_with_quotes(conn, "value");
127 ck_assert_str_eq(escaped, "'value'");
128 h_free(escaped);
129 escaped = h_escape_string_with_quotes(conn, "unsafe ' value\"!");
130 ck_assert_str_eq(escaped, "'unsafe '' value\"!'");
131 h_free(escaped);
132
133 j_query = json_pack("{sss{siss}}", "table", "test_table", "values", "integer_col", 666, "string_col", UNSAFE_STRING);
134 res = h_insert(conn, j_query, NULL);
135 json_decref(j_query);
136 ck_assert_int_eq(res, H_OK);
137 j_query = json_pack("{sss[s]s{si}}", "table", "test_table", "columns", "string_col", "where", "integer_col", 666);
138 res = h_select(conn, j_query, &j_result, NULL);
139 json_decref(j_query);
140 ck_assert_int_eq(res, H_OK);
141 ck_assert_str_eq(UNSAFE_STRING, json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")));
142 json_decref(j_result);
143
144 ck_assert_int_eq(h_close_db(conn), H_OK);
145 ck_assert_int_eq(h_clean_connection(conn), H_OK);
146 }
147 END_TEST
148
START_TEST(test_hoel_insert)149 START_TEST(test_hoel_insert)
150 {
151 struct _h_connection * conn;
152 struct _h_result result;
153 struct _h_data * last_id;
154 conn = h_connect_sqlite(db_path);
155 ck_assert_ptr_ne(conn, NULL);
156 ck_assert_int_eq(h_query_delete(conn, DELETE_DATA_ALL), H_OK);
157 ck_assert_int_eq(h_query_insert(conn, INSERT_DATA_1), H_OK);
158 last_id = h_query_last_insert_id(conn);
159 ck_assert_int_eq(last_id->type, HOEL_COL_TYPE_INT);
160 ck_assert_int_gt(((struct _h_type_int *)last_id->t_data)->value, 0);
161 ck_assert_int_eq(h_clean_data(last_id), H_OK);
162 h_free(last_id);
163 ck_assert_ptr_ne(last_id, NULL);
164 ck_assert_int_eq(h_query_insert(conn, NULL), H_ERROR_PARAMS);
165 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_1, &result), H_OK);
166 ck_assert_int_eq(result.nb_rows, 1);
167 ck_assert_int_eq(result.nb_columns, 3);
168 ck_assert_int_eq(result.data[0][0].type, HOEL_COL_TYPE_INT);
169 ck_assert_int_eq(((struct _h_type_int *)result.data[0][0].t_data)->value, 1);
170 ck_assert_int_eq(result.data[0][1].type, HOEL_COL_TYPE_TEXT);
171 ck_assert_str_eq(((struct _h_type_text *)result.data[0][1].t_data)->value, "value1");
172 ck_assert_int_eq(h_clean_result(&result), H_OK);
173 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_ERROR, &result), H_OK);
174 ck_assert_int_eq(result.nb_rows, 0);
175 ck_assert_int_eq(result.nb_columns, 3);
176 ck_assert_int_eq(h_clean_result(&result), H_OK);
177 ck_assert_int_eq(h_query_delete(conn, DELETE_DATA_1), H_OK);
178 ck_assert_int_eq(h_close_db(conn), H_OK);
179 ck_assert_int_eq(h_clean_connection(conn), H_OK);
180 }
181 END_TEST
182
START_TEST(test_hoel_update)183 START_TEST(test_hoel_update)
184 {
185 struct _h_connection * conn;
186 struct _h_result result;
187 conn = h_connect_sqlite(db_path);
188 ck_assert_ptr_ne(conn, NULL);
189 ck_assert_int_eq(h_query_insert(conn, INSERT_DATA_1), H_OK);
190 ck_assert_int_eq(h_query_insert(conn, NULL), H_ERROR_PARAMS);
191 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_1, &result), H_OK);
192 ck_assert_int_eq(result.nb_rows, 1);
193 ck_assert_int_eq(result.nb_columns, 3);
194 ck_assert_int_eq(((struct _h_type_int *)result.data[0][0].t_data)->value, 1);
195 ck_assert_str_eq(((struct _h_type_text *)result.data[0][1].t_data)->value, "value1");
196 ck_assert_int_eq(h_clean_result(&result), H_OK);
197 ck_assert_int_eq(h_query_update(conn, UPDATE_DATA_1), H_OK);
198 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_1, &result), H_OK);
199 ck_assert_int_eq(result.nb_rows, 1);
200 ck_assert_int_eq(result.nb_columns, 3);
201 ck_assert_int_eq(((struct _h_type_int *)result.data[0][0].t_data)->value, 1);
202 ck_assert_str_eq(((struct _h_type_text *)result.data[0][1].t_data)->value, "new value1");
203 ck_assert_int_eq(h_clean_result(&result), H_OK);
204 ck_assert_int_eq(h_query_delete(conn, DELETE_DATA_1), H_OK);
205 ck_assert_int_eq(h_close_db(conn), H_OK);
206 ck_assert_int_eq(h_clean_connection(conn), H_OK);
207 }
208 END_TEST
209
START_TEST(test_hoel_delete)210 START_TEST(test_hoel_delete)
211 {
212 struct _h_connection * conn;
213 struct _h_result result;
214 conn = h_connect_sqlite(db_path);
215 ck_assert_ptr_ne(conn, NULL);
216 ck_assert_int_eq(h_query_insert(conn, INSERT_DATA_1), H_OK);
217 ck_assert_int_eq(h_query_insert(conn, INSERT_DATA_2), H_OK);
218 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_1, &result), H_OK);
219 ck_assert_int_eq(result.nb_rows, 1);
220 ck_assert_int_eq(result.nb_columns, 3);
221 ck_assert_int_eq(result.data[0][0].type, HOEL_COL_TYPE_INT);
222 ck_assert_int_eq(((struct _h_type_int *)result.data[0][0].t_data)->value, 1);
223 ck_assert_int_eq(result.data[0][1].type, HOEL_COL_TYPE_TEXT);
224 ck_assert_str_eq(((struct _h_type_text *)result.data[0][1].t_data)->value, "value1");
225 ck_assert_int_eq(h_clean_result(&result), H_OK);
226 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_2, &result), H_OK);
227 ck_assert_int_eq(result.nb_rows, 1);
228 ck_assert_int_eq(result.nb_columns, 3);
229 ck_assert_int_eq(result.data[0][0].type, HOEL_COL_TYPE_INT);
230 ck_assert_int_eq(((struct _h_type_int *)result.data[0][0].t_data)->value, 2);
231 ck_assert_int_eq(result.data[0][1].type, HOEL_COL_TYPE_TEXT);
232 ck_assert_str_eq(((struct _h_type_text *)result.data[0][1].t_data)->value, "value2");
233 ck_assert_int_eq(h_clean_result(&result), H_OK);
234 ck_assert_int_eq(h_query_delete(conn, DELETE_DATA_1), H_OK);
235 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_ALL, &result), H_OK);
236 ck_assert_int_eq(result.nb_rows, 1);
237 ck_assert_int_eq(h_clean_result(&result), H_OK);
238 ck_assert_int_eq(h_query_delete(conn, DELETE_DATA_2), H_OK);
239 ck_assert_int_eq(h_query_select(conn, SELECT_DATA_ALL, &result), H_OK);
240 ck_assert_int_eq(result.nb_rows, 0);
241 ck_assert_int_eq(h_clean_result(&result), H_OK);
242 ck_assert_int_eq(h_close_db(conn), H_OK);
243 ck_assert_int_eq(h_clean_connection(conn), H_OK);
244 }
245 END_TEST
246
START_TEST(test_hoel_json_insert)247 START_TEST(test_hoel_json_insert)
248 {
249 struct _h_connection * conn;
250 char * str_query = NULL;
251 json_t * j_query = json_pack("{sss{sisss{ss}}}",
252 "table",
253 "test_table",
254 "values",
255 "integer_col",
256 1,
257 "string_col",
258 "value1",
259 "date_col",
260 "raw",
261 "date('now')"), * j_result = NULL;
262 conn = h_connect_sqlite(db_path);
263 ck_assert_ptr_ne(conn, NULL);
264 ck_assert_int_eq(h_insert(conn, j_query, &str_query), H_OK);
265 json_decref(j_query);
266 j_query = json_pack("{sss{si}}",
267 "table",
268 "test_table",
269 "where",
270 "integer_col",
271 1);
272 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
273 ck_assert_ptr_ne(j_result, NULL);
274 ck_assert_int_eq(json_is_array(j_result), 1);
275 ck_assert_int_eq(json_array_size(j_result), 1);
276 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
277 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
278 json_decref(j_result);
279 ck_assert_int_eq(strlen(str_query), strlen("INSERT INTO test_table (integer_col,string_col,date_col) VALUES (1,'value1',date('now'))"));
280 h_free(str_query);
281 ck_assert_int_eq(h_delete(conn, j_query, NULL), H_OK);
282 json_decref(j_query);
283 ck_assert_int_eq(h_close_db(conn), H_OK);
284 ck_assert_int_eq(h_clean_connection(conn), H_OK);
285 }
286 END_TEST
287
START_TEST(test_hoel_json_update)288 START_TEST(test_hoel_json_update)
289 {
290 struct _h_connection * conn;
291 char * str_query = NULL;
292 json_t * j_query = json_pack("{sss{sisss{ss}}}",
293 "table",
294 "test_table",
295 "values",
296 "integer_col",
297 1,
298 "string_col",
299 "value1",
300 "date_col",
301 "raw",
302 "date('now')"),
303 * j_result = NULL;
304 conn = h_connect_sqlite(db_path);
305 ck_assert_ptr_ne(conn, NULL);
306 ck_assert_int_eq(h_insert(conn, j_query, NULL), H_OK);
307 json_decref(j_query);
308 j_query = json_pack("{sss{si}}",
309 "table",
310 "test_table",
311 "where",
312 "integer_col",
313 1);
314 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
315 json_decref(j_query);
316 ck_assert_ptr_ne(j_result, NULL);
317 ck_assert_int_eq(json_is_array(j_result), 1);
318 ck_assert_int_eq(json_array_size(j_result), 1);
319 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
320 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
321 json_decref(j_result);
322 j_query = json_pack("{sss{ss}s{si}}",
323 "table",
324 "test_table",
325 "set",
326 "string_col",
327 "new value1",
328 "where",
329 "integer_col",
330 1);
331 ck_assert_int_eq(h_update(conn, j_query, &str_query), H_OK);
332 json_decref(j_query);
333 ck_assert_int_eq(strlen(str_query), strlen("UPDATE test_table SET string_col='new value1' WHERE integer_col='1'"));
334 h_free(str_query);
335 j_query = json_pack("{sss{si}}",
336 "table",
337 "test_table",
338 "where",
339 "integer_col",
340 1);
341 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
342 ck_assert_ptr_ne(j_result, NULL);
343 ck_assert_int_eq(json_array_size(j_result), 1);
344 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "new value1");
345 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
346 json_decref(j_result);
347 ck_assert_int_eq(h_delete(conn, j_query, NULL), H_OK);
348 json_decref(j_query);
349 ck_assert_int_eq(h_close_db(conn), H_OK);
350 ck_assert_int_eq(h_clean_connection(conn), H_OK);
351 }
352 END_TEST
353
START_TEST(test_hoel_json_delete)354 START_TEST(test_hoel_json_delete)
355 {
356 struct _h_connection * conn;
357 char * str_query = NULL;
358 json_t * j_query = json_pack("{sss{sisss{ss}}}",
359 "table",
360 "test_table",
361 "values",
362 "integer_col",
363 1,
364 "string_col",
365 "value1",
366 "date_col",
367 "raw",
368 "date('now')"),
369 * j_result = NULL;
370 conn = h_connect_sqlite(db_path);
371 ck_assert_ptr_ne(conn, NULL);
372 ck_assert_int_eq(h_insert(conn, j_query, NULL), H_OK);
373 json_decref(j_query);
374 j_query = json_pack("{sss{sisss{ss}}}",
375 "table",
376 "test_table",
377 "values",
378 "integer_col",
379 2,
380 "string_col",
381 "value2",
382 "date_col",
383 "raw",
384 "strftime('%s','2016-06-22 00:52:56')");
385 ck_assert_int_eq(h_insert(conn, j_query, NULL), H_OK);
386 json_decref(j_query);
387 j_query = json_pack("{ss}",
388 "table",
389 "test_table");
390 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
391 json_decref(j_query);
392 ck_assert_ptr_ne(j_result, NULL);
393 ck_assert_int_eq(json_is_array(j_result), 1);
394 ck_assert_int_eq(json_array_size(j_result), 2);
395 json_decref(j_result);
396 j_query = json_pack("{sss{si}}",
397 "table",
398 "test_table",
399 "where",
400 "integer_col",
401 1);
402 ck_assert_int_eq(h_delete(conn, j_query, &str_query), H_OK);
403 ck_assert_int_eq(strlen(str_query), strlen("DELETE FROM test_table WHERE integer_col='1'"));
404 h_free(str_query);
405 json_decref(j_query);
406 j_query = json_pack("{ss}",
407 "table",
408 "test_table");
409 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
410 json_decref(j_query);
411 ck_assert_int_eq(json_array_size(j_result), 1);
412 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value2");
413 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 2);
414 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "date_col")), 1466556776);
415 json_decref(j_result);
416 j_query = json_pack("{ss}",
417 "table",
418 "test_table");
419 ck_assert_int_eq(h_delete(conn, j_query, NULL), H_OK);
420 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
421 json_decref(j_result);
422 ck_assert_int_eq(json_array_size(j_result), 0);
423 json_decref(j_query);
424 ck_assert_int_eq(h_close_db(conn), H_OK);
425 ck_assert_int_eq(h_clean_connection(conn), H_OK);
426 }
427 END_TEST
428
START_TEST(test_hoel_json_select)429 START_TEST(test_hoel_json_select)
430 {
431 struct _h_connection * conn;
432 char * str_query = NULL;
433 json_t * j_query = json_pack("{sss{sisss{ss}}}",
434 "table",
435 "test_table",
436 "values",
437 "integer_col",
438 1,
439 "string_col",
440 "value1",
441 "date_col",
442 "raw",
443 "date('now')"),
444 * j_result = NULL;
445 conn = h_connect_sqlite(db_path);
446 ck_assert_ptr_ne(conn, NULL);
447 ck_assert_int_eq(h_insert(conn, j_query, NULL), H_OK);
448 json_decref(j_query);
449 j_query = json_pack("{sss{sisss{ss}}}",
450 "table",
451 "test_table",
452 "values",
453 "integer_col",
454 2,
455 "string_col",
456 "value2",
457 "date_col",
458 "raw",
459 "strftime('%s','2016-06-22 00:52:56')");
460 ck_assert_int_eq(h_insert(conn, j_query, NULL), H_OK);
461 json_decref(j_query);
462
463 j_query = json_pack("{ss}",
464 "table",
465 "test_table");
466 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
467 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE 1=1 "));
468 ck_assert_int_eq(json_array_size(j_result), 2);
469 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
470 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
471 json_decref(j_query);
472 json_decref(j_result);
473 h_free(str_query);
474
475 j_query = json_pack("{sss{si}}",
476 "table",
477 "test_table",
478 "where",
479 "integer_col",
480 1);
481 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
482 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE integer_col='1' "));
483 ck_assert_int_eq(json_array_size(j_result), 1);
484 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
485 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
486 json_decref(j_query);
487 json_decref(j_result);
488 h_free(str_query);
489
490 j_query = json_pack("{sss{ss}}",
491 "table",
492 "test_table",
493 "where",
494 "string_col",
495 "value1");
496 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
497 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE string_col='value1' "));
498 ck_assert_int_eq(json_array_size(j_result), 1);
499 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
500 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
501 json_decref(j_query);
502 json_decref(j_result);
503 h_free(str_query);
504
505 j_query = json_pack("{sss{ss}}",
506 "table",
507 "test_table",
508 "where",
509 "string_col",
510 "value'to'escape");
511 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
512 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE string_col='value''to''escape' "));
513 ck_assert_int_eq(json_array_size(j_result), 0);
514 json_decref(j_query);
515 json_decref(j_result);
516 h_free(str_query);
517
518 j_query = json_pack("{sss{s{ss}}}",
519 "table",
520 "test_table",
521 "where",
522 "integer_col",
523 "operator",
524 "NOT NULL");
525 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
526 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE integer_col IS NOT NULL "));
527 ck_assert_int_eq(json_array_size(j_result), 2);
528 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
529 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
530 json_decref(j_query);
531 json_decref(j_result);
532 h_free(str_query);
533
534 j_query = json_pack("{sss{s{ssss}}}",
535 "table",
536 "test_table",
537 "where",
538 "integer_col",
539 "operator",
540 "raw",
541 "value",
542 ">6");
543 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
544 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE integer_col >6 "));
545 ck_assert_int_eq(json_array_size(j_result), 0);
546 json_decref(j_query);
547 json_decref(j_result);
548 h_free(str_query);
549
550 j_query = json_pack("{sss{s{ss}s{ssss}}}",
551 "table",
552 "test_table",
553 "where",
554 "string_col",
555 "operator",
556 "NOT NULL",
557 "integer_col",
558 "operator",
559 "raw",
560 "value",
561 ">=1");
562 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
563 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE string_col IS NOT NULL AND integer_col >=1 "));
564 ck_assert_int_eq(json_array_size(j_result), 2);
565 ck_assert_str_eq(json_string_value(json_object_get(json_array_get(j_result, 0), "string_col")), "value1");
566 ck_assert_int_eq(json_integer_value(json_object_get(json_array_get(j_result, 0), "integer_col")), 1);
567 json_decref(j_query);
568 json_decref(j_result);
569 h_free(str_query);
570
571 j_query = json_pack("{sss{so}}",
572 "table",
573 "test_table",
574 "where",
575 "integer_col",
576 json_null());
577 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
578 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE integer_col IS NULL "));
579 ck_assert_int_eq(json_array_size(j_result), 0);
580 json_decref(j_query);
581 json_decref(j_result);
582 h_free(str_query);
583
584 str_query = NULL;
585 j_result = NULL;
586 j_query = json_pack("{sss{s{sss[ii]}}}",
587 "table",
588 "test_table",
589 "where",
590 "integer_col",
591 "operator",
592 "IN",
593 "value",
594 42,
595 66);
596 ck_assert_int_eq(h_select(conn, j_query, &j_result, &str_query), H_OK);
597 ck_assert_int_eq(strlen(str_query), strlen("SELECT * FROM test_table WHERE integer_col IN (42,66) "));
598 ck_assert_int_eq(json_array_size(j_result), 0);
599 json_decref(j_query);
600 json_decref(j_result);
601 h_free(str_query);
602
603 j_query = json_pack("{ss}",
604 "table",
605 "test_table");
606 ck_assert_int_eq(h_delete(conn, j_query, NULL), H_OK);
607 ck_assert_int_eq(h_select(conn, j_query, &j_result, NULL), H_OK);
608 json_decref(j_result);
609 ck_assert_int_eq(json_array_size(j_result), 0);
610 json_decref(j_query);
611 ck_assert_int_eq(h_close_db(conn), H_OK);
612 ck_assert_int_eq(h_clean_connection(conn), H_OK);
613 }
614 END_TEST
615
hoel_suite(void)616 static Suite *hoel_suite(void)
617 {
618 Suite *s;
619 TCase *tc_core;
620
621 s = suite_create("Hoel core tests");
622 tc_core = tcase_create("test_hoel_core");
623 tcase_add_test(tc_core, test_hoel_init);
624 tcase_add_test(tc_core, test_hoel_escape_string);
625 tcase_add_test(tc_core, test_hoel_escape_string_with_quotes);
626 tcase_add_test(tc_core, test_hoel_insert);
627 tcase_add_test(tc_core, test_hoel_update);
628 tcase_add_test(tc_core, test_hoel_delete);
629 tcase_add_test(tc_core, test_hoel_json_insert);
630 tcase_add_test(tc_core, test_hoel_json_update);
631 tcase_add_test(tc_core, test_hoel_json_delete);
632 tcase_add_test(tc_core, test_hoel_json_select);
633 tcase_set_timeout(tc_core, 30);
634 suite_add_tcase(s, tc_core);
635
636 return s;
637 }
638
main(int argc,char * argv[])639 int main(int argc, char *argv[])
640 {
641 int number_failed;
642 Suite *s;
643 SRunner *sr;
644 //y_init_logs("Hoel", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting Hoel core tests");
645 if (argc > 1) {
646 db_path = argv[1];
647 } else {
648 db_path = DEFAULT_BD_PATH;
649 }
650 s = hoel_suite();
651 sr = srunner_create(s);
652
653 srunner_run_all(sr, CK_VERBOSE);
654 number_failed = srunner_ntests_failed(sr);
655 srunner_free(sr);
656
657 //y_close_logs();
658 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
659 }
660