Code

t/: Added initial version of unit tests for utils/dbi.
authorSebastian Harl <sh@tokkee.org>
Tue, 27 Aug 2013 16:08:32 +0000 (18:08 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 27 Aug 2013 16:08:32 +0000 (18:08 +0200)
The test cases mocks out any (expected) calls to libdbi.

t/Makefile.am
t/libsysdb_test.c
t/libsysdb_test.h
t/utils/dbi_test.c [new file with mode: 0644]

index 338ecbc19745048fece29d459cca401ae92e4f3a..e90840598c541199a2c2438603a1f95615bef0e8 100644 (file)
@@ -6,6 +6,7 @@ TESTS = libsysdb_test
 check_PROGRAMS = libsysdb_test
 libsysdb_test_SOURCES = \
                libsysdb_test.c libsysdb_test.h \
+               utils/dbi_test.c \
                utils/llist_test.c
 libsysdb_test_CFLAGS = $(AM_CFLAGS) @CHECK_CFLAGS@
 libsysdb_test_LDADD = $(top_builddir)/src/libsysdb.la @CHECK_LIBS@
index ab241492ea77db4bccf276676cfc8e46a49d5d62..79ba4218d284e1a7fde38e54125c8302ed287206 100644 (file)
@@ -44,6 +44,13 @@ main(void)
        failed += srunner_ntests_failed(sr);
        srunner_free(sr);
 
+       /* t/utils/dbi_test */
+       s = util_dbi_suite();
+       sr = srunner_create(s);
+       srunner_run_all(sr, CK_NORMAL);
+       failed += srunner_ntests_failed(sr);
+       srunner_free(sr);
+
        return failed;
 } /* main */
 
index e776bf4324f17fb70f6b141b0b3f5202668d89d7..b098ccdba99110868529fced198ae14230b23131 100644 (file)
  * test suites
  */
 
+/* t/utils/dbi_test */
+Suite *
+util_dbi_suite(void);
+
 /* t/utils/llist_test */
 Suite *
 util_llist_suite(void);
diff --git a/t/utils/dbi_test.c b/t/utils/dbi_test.c
new file mode 100644 (file)
index 0000000..7db9bc2
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * SysDB - t/utils/dbi_test.c
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "libsysdb_test.h"
+#include "utils/dbi.h"
+
+#include <check.h>
+#include <dbi/dbi.h>
+
+/*
+ * private variables
+ */
+
+static sdb_dbi_client_t *client;
+
+/*
+ * mocked functions
+ */
+
+/* dbi_driver, dbi_conn, dbi_result are void pointers */
+
+dbi_driver
+dbi_driver_open(const char *name)
+{
+       if (strcmp(name, "mockdriver"))
+               return NULL;
+       return (dbi_driver)strdup(name);
+} /* dbi_driver_open */
+
+dbi_driver
+dbi_driver_list(dbi_driver curr)
+{
+       if (!curr)
+               return "mockdriver";
+       return NULL;
+} /* dbi_driver_list */
+
+const char *
+dbi_driver_get_name(dbi_driver driver)
+{
+       return (const char *)driver;
+} /* dbi_driver_get_name */
+
+int
+dbi_conn_set_option(dbi_conn __attribute__((unused)) conn,
+               const char __attribute__((unused)) *key,
+               const char __attribute__((unused)) *value)
+{
+       return 0;
+} /* dbi_conn_set_option */
+
+const char *
+dbi_conn_get_option_list(dbi_conn __attribute__((unused)) conn,
+               const char __attribute__((unused)) *key)
+{
+       return NULL;
+} /* dbi_conn_get_option_list */
+
+dbi_conn
+dbi_conn_open(dbi_driver driver)
+{
+       if (strcmp((const char *)driver, "mockdriver"))
+               return NULL;
+       return (dbi_conn)"mockconnection";
+} /* dbi_conn_open */
+
+static int dbi_conn_connect_called = 0;
+int
+dbi_conn_connect(dbi_conn conn)
+{
+       ++dbi_conn_connect_called;
+       if (strcmp((const char *)conn, "mockconnection"))
+               return DBI_ERROR_NOCONN;
+       return 0;
+} /* dbi_conn_connect */
+
+int
+dbi_conn_ping(dbi_conn conn)
+{
+       if (strcmp((const char *)conn, "mockconnection"))
+               return 0;
+       return 1;
+} /* dbi_conn_connect */
+
+void
+dbi_conn_close(dbi_conn __attribute__((unused)) conn)
+{
+       return;
+} /* dbi_conn_close */
+
+/*
+ * private helper functions
+ */
+
+static void
+setup(void)
+{
+       client = sdb_dbi_client_create("mockdriver", "mockdatabase");
+       fail_unless(client != NULL,
+                       "sdb_dbi_client_create() = NULL; expected client object");
+} /* setup */
+
+static void
+teardown(void)
+{
+       sdb_dbi_client_destroy(client);
+       client = NULL;
+} /* teardown */
+
+/*
+ * tests
+ */
+
+START_TEST(test_client_connect)
+{
+       int check = sdb_dbi_client_connect(client);
+       fail_unless(check == 0,
+                       "sdb_dbi_client_connect() = %i; expected: 0", check);
+
+       fail_unless(dbi_conn_connect_called == 1,
+                       "sdb_dbi_client_create() called dbi_conn_connect %i times; "
+                       "expected: 1", dbi_conn_connect_called);
+}
+END_TEST
+
+START_TEST(test_client_check_conn)
+{
+       int check = sdb_dbi_client_check_conn(client);
+       fail_unless(check == 0,
+                       "sdb_dbi_client_check_conn() = %i; expected: 0", check);
+
+       /* the first call will actually connect to the database */
+       fail_unless(dbi_conn_connect_called == 1,
+                       "sdb_dbi_client_check_conn() called dbi_conn_connect %i times; "
+                       "expected: 1", dbi_conn_connect_called);
+
+       check = sdb_dbi_client_check_conn(client);
+       fail_unless(check == 0,
+                       "sdb_dbi_client_check_conn() = %i; expected: 0", check);
+
+       /* should not reconnect */
+       fail_unless(dbi_conn_connect_called == 1,
+                       "sdb_dbi_client_check_conn() called dbi_conn_connect %i time(s); "
+                       "expected: 0", dbi_conn_connect_called - 1);
+}
+END_TEST
+
+/*
+ * test API
+ */
+
+Suite *
+util_dbi_suite(void)
+{
+       Suite *s = suite_create("utils::dbi");
+       TCase *tc;
+
+       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);
+       suite_add_tcase(s, tc);
+
+       return s;
+} /* util_llist_suite */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+