Code

t/: Added initial version of unit tests for utils/dbi.
[sysdb.git] / t / utils / dbi_test.c
1 /*
2  * SysDB - t/utils/dbi_test.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "libsysdb_test.h"
29 #include "utils/dbi.h"
31 #include <check.h>
32 #include <dbi/dbi.h>
34 /*
35  * private variables
36  */
38 static sdb_dbi_client_t *client;
40 /*
41  * mocked functions
42  */
44 /* dbi_driver, dbi_conn, dbi_result are void pointers */
46 dbi_driver
47 dbi_driver_open(const char *name)
48 {
49         if (strcmp(name, "mockdriver"))
50                 return NULL;
51         return (dbi_driver)strdup(name);
52 } /* dbi_driver_open */
54 dbi_driver
55 dbi_driver_list(dbi_driver curr)
56 {
57         if (!curr)
58                 return "mockdriver";
59         return NULL;
60 } /* dbi_driver_list */
62 const char *
63 dbi_driver_get_name(dbi_driver driver)
64 {
65         return (const char *)driver;
66 } /* dbi_driver_get_name */
68 int
69 dbi_conn_set_option(dbi_conn __attribute__((unused)) conn,
70                 const char __attribute__((unused)) *key,
71                 const char __attribute__((unused)) *value)
72 {
73         return 0;
74 } /* dbi_conn_set_option */
76 const char *
77 dbi_conn_get_option_list(dbi_conn __attribute__((unused)) conn,
78                 const char __attribute__((unused)) *key)
79 {
80         return NULL;
81 } /* dbi_conn_get_option_list */
83 dbi_conn
84 dbi_conn_open(dbi_driver driver)
85 {
86         if (strcmp((const char *)driver, "mockdriver"))
87                 return NULL;
88         return (dbi_conn)"mockconnection";
89 } /* dbi_conn_open */
91 static int dbi_conn_connect_called = 0;
92 int
93 dbi_conn_connect(dbi_conn conn)
94 {
95         ++dbi_conn_connect_called;
96         if (strcmp((const char *)conn, "mockconnection"))
97                 return DBI_ERROR_NOCONN;
98         return 0;
99 } /* dbi_conn_connect */
101 int
102 dbi_conn_ping(dbi_conn conn)
104         if (strcmp((const char *)conn, "mockconnection"))
105                 return 0;
106         return 1;
107 } /* dbi_conn_connect */
109 void
110 dbi_conn_close(dbi_conn __attribute__((unused)) conn)
112         return;
113 } /* dbi_conn_close */
115 /*
116  * private helper functions
117  */
119 static void
120 setup(void)
122         client = sdb_dbi_client_create("mockdriver", "mockdatabase");
123         fail_unless(client != NULL,
124                         "sdb_dbi_client_create() = NULL; expected client object");
125 } /* setup */
127 static void
128 teardown(void)
130         sdb_dbi_client_destroy(client);
131         client = NULL;
132 } /* teardown */
134 /*
135  * tests
136  */
138 START_TEST(test_client_connect)
140         int check = sdb_dbi_client_connect(client);
141         fail_unless(check == 0,
142                         "sdb_dbi_client_connect() = %i; expected: 0", check);
144         fail_unless(dbi_conn_connect_called == 1,
145                         "sdb_dbi_client_create() called dbi_conn_connect %i times; "
146                         "expected: 1", dbi_conn_connect_called);
148 END_TEST
150 START_TEST(test_client_check_conn)
152         int check = sdb_dbi_client_check_conn(client);
153         fail_unless(check == 0,
154                         "sdb_dbi_client_check_conn() = %i; expected: 0", check);
156         /* the first call will actually connect to the database */
157         fail_unless(dbi_conn_connect_called == 1,
158                         "sdb_dbi_client_check_conn() called dbi_conn_connect %i times; "
159                         "expected: 1", dbi_conn_connect_called);
161         check = sdb_dbi_client_check_conn(client);
162         fail_unless(check == 0,
163                         "sdb_dbi_client_check_conn() = %i; expected: 0", check);
165         /* should not reconnect */
166         fail_unless(dbi_conn_connect_called == 1,
167                         "sdb_dbi_client_check_conn() called dbi_conn_connect %i time(s); "
168                         "expected: 0", dbi_conn_connect_called - 1);
170 END_TEST
172 /*
173  * test API
174  */
176 Suite *
177 util_dbi_suite(void)
179         Suite *s = suite_create("utils::dbi");
180         TCase *tc;
182         tc = tcase_create("core");
183         tcase_add_checked_fixture(tc, setup, teardown);
184         tcase_add_test(tc, test_client_connect);
185         tcase_add_test(tc, test_client_check_conn);
186         suite_add_tcase(s, tc);
188         return s;
189 } /* util_llist_suite */
191 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */