Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / t / utils / dbi_test.c
index 2ad1cc29b1d7bb541d4702da265565adc64ac5e7..9d45feb24341f03235223d2b2f8fe68d6ace0d3a 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include "libsysdb_test.h"
 #include "utils/dbi.h"
 
@@ -331,6 +335,17 @@ dbi_result_get_string_idx(dbi_result res, unsigned int i)
        return get_golden_data(res, i).string;
 } /* dbi_result_get_string_idx */
 
+char *
+dbi_result_get_string_copy_idx(dbi_result res, unsigned int i)
+{
+       fail_unless(current_query->field_types[i - 1] == DBI_TYPE_STRING,
+                       "dbi_result_get_string_copy_idx() called for non-string "
+                       "column type %u", current_query->field_types[i - 1]);
+       if (! get_golden_data(res, i).string)
+               return NULL;
+       return strdup(get_golden_data(res, i).string);
+} /* dbi_result_get_string_copy_idx */
+
 time_t
 dbi_result_get_datetime_idx(dbi_result res, unsigned int i)
 {
@@ -379,6 +394,19 @@ dbi_result_get_binary_idx(dbi_result res, unsigned int i)
        return get_golden_data(res, i).binary.datum;
 } /* dbi_result_get_binary_idx */
 
+unsigned char *
+dbi_result_get_binary_copy_idx(dbi_result res, unsigned int i)
+{
+       const char *data;
+       fail_unless(current_query->field_types[i - 1] == DBI_TYPE_BINARY,
+                       "dbi_result_get_binary_copy_idx() called for non-binary "
+                       "column type %u", current_query->field_types[i - 1]);
+       data = (const char *)get_golden_data(res, i).binary.datum;
+       if (! data)
+               return NULL;
+       return (unsigned char *)strdup(data);
+} /* dbi_result_get_binary_copy_idx */
+
 static unsigned long long dbi_result_free_called = 0;
 int
 dbi_result_free(dbi_result res)
@@ -403,6 +431,8 @@ setup(void)
        client = sdb_dbi_client_create("mockdriver", "mockdatabase");
        fail_unless(client != NULL,
                        "sdb_dbi_client_create() = NULL; expected client object");
+
+       dbi_conn_connect_called = 0;
 } /* setup */
 
 static void
@@ -420,14 +450,14 @@ teardown(void)
        client = NULL;
 } /* teardown */
 
-static unsigned long long test_query_callback_called = 0;
+static unsigned long long query_callback_called = 0;
 static int
-test_query_callback(sdb_dbi_client_t *c,
+query_callback(sdb_dbi_client_t *c,
                size_t n, sdb_data_t *data, sdb_object_t *user_data)
 {
        size_t i;
 
-       ++test_query_callback_called;
+       ++query_callback_called;
 
        fail_unless(c == client,
                        "query callback received unexpected client = %p; "
@@ -465,7 +495,7 @@ test_query_callback(sdb_dbi_client_t *c,
                                                data[i].data.decimal, i, expected_data.decimal);
                                break;
                        case SDB_TYPE_STRING:
-                               fail_unless(data[i].data.string == expected_data.string,
+                               fail_unless(!strcmp(data[i].data.string, expected_data.string),
                                                "query callback received unexpected data %s "
                                                "for column %zu; expected: %s",
                                                data[i].data.string, i, expected_data.string);
@@ -485,8 +515,9 @@ test_query_callback(sdb_dbi_client_t *c,
                                                "binary data length %zu for column %zu; "
                                                "expected: %lli", data[i].data.binary.length, i,
                                                expected_data.binary.length);
-                               fail_unless(data[i].data.binary.datum ==
+                               fail_unless(!memcmp(data[i].data.binary.datum,
                                                        expected_data.binary.datum,
+                                                       expected_data.binary.length),
                                                "query callback received unexpected binary data %p "
                                                "for column %zu; expected: %p",
                                                data[i].data.binary.datum, i,
@@ -499,13 +530,13 @@ test_query_callback(sdb_dbi_client_t *c,
                }
        }
        return 0;
-} /* test_query_callback */
+} /* query_callback */
 
 /*
  * tests
  */
 
-START_TEST(test_client_connect)
+START_TEST(test_sdb_dbi_client_connect)
 {
        int check = sdb_dbi_client_connect(client);
        fail_unless(check == 0,
@@ -517,7 +548,7 @@ START_TEST(test_client_connect)
 }
 END_TEST
 
-START_TEST(test_client_check_conn)
+START_TEST(test_sdb_dbi_client_check_conn)
 {
        int check = sdb_dbi_client_check_conn(client);
        fail_unless(check == 0,
@@ -540,11 +571,11 @@ START_TEST(test_client_check_conn)
 }
 END_TEST
 
-START_TEST(test_exec_query)
+START_TEST(test_sdb_dbi_exec_query)
 {
        size_t i;
 
-       int check = sdb_dbi_exec_query(client, "mockquery0", test_query_callback,
+       int check = sdb_dbi_exec_query(client, "mockquery0", query_callback,
                        /* user_data = */ TEST_MAGIC, /* n = */ 0);
        /* not connected yet */
        fail_unless(check < 0,
@@ -558,13 +589,13 @@ START_TEST(test_exec_query)
                unsigned long long expected_callback_calls = 0;
 
                dbi_conn_query_called = 0;
-               test_query_callback_called = 0;
+               query_callback_called = 0;
                dbi_result_free_called = 0;
 
                /* sdb_dbi_exec_query will only use as many type arguments are needed,
                 * so we can safely pass in the maximum number of arguments required
                 * on each call */
-               check = sdb_dbi_exec_query(client, q->name, test_query_callback,
+               check = sdb_dbi_exec_query(client, q->name, query_callback,
                                /* user_data = */ TEST_MAGIC, /* n = */ (int)q->nfields,
                                SDB_TYPE_INTEGER, SDB_TYPE_DECIMAL, SDB_TYPE_STRING,
                                SDB_TYPE_DATETIME, SDB_TYPE_BINARY);
@@ -578,11 +609,11 @@ START_TEST(test_exec_query)
                if (q->nfields)
                        expected_callback_calls = q->nrows;
 
-               fail_unless(test_query_callback_called == expected_callback_calls,
+               fail_unless(query_callback_called == expected_callback_calls,
                                "sdb_dbi_exec_query() did not call the registered callback "
                                "for each result row; got %i call%s; expected: 0",
-                               test_query_callback_called,
-                               (test_query_callback_called == 1) ? "" : "s");
+                               query_callback_called,
+                               (query_callback_called == 1) ? "" : "s");
 
                fail_unless(dbi_result_free_called == 1,
                                "sdb_dbi_exec_query() did not free the query result object");
@@ -602,9 +633,9 @@ util_dbi_suite(void)
 
        tc = tcase_create("core");
        tcase_add_checked_fixture(tc, setup, teardown);
-       tcase_add_test(tc, test_client_connect);
-       tcase_add_test(tc, test_client_check_conn);
-       tcase_add_test(tc, test_exec_query);
+       tcase_add_test(tc, test_sdb_dbi_client_connect);
+       tcase_add_test(tc, test_sdb_dbi_client_check_conn);
+       tcase_add_test(tc, test_sdb_dbi_exec_query);
        suite_add_tcase(s, tc);
 
        return s;