2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 extern DatabaseConnection* connect_to_customer_database();
23 extern unsigned int get_customer_id_by_name(
24 DatabaseConnection * const connection, const char * const customer_name);
26 /* Mock query database function. */
27 static unsigned int mock_query_database(DatabaseConnection* const connection,
28 const char * const query_string,
29 void *** const results) {
30 (void) connection; /* unused */
31 (void) query_string; /* unused */
33 *results = (void **)mock_ptr_type(int *);
34 return mock_ptr_type(int);
37 /* Mock of the connect to database function. */
38 DatabaseConnection* connect_to_database(const char * const database_url,
39 const unsigned int port) {
40 (void) database_url; /* unused */
41 (void) port; /* unused */
43 return (DatabaseConnection*)((size_t)mock());
46 static void test_connect_to_customer_database(void **state) {
47 (void) state; /* unused */
49 will_return(connect_to_database, 0x0DA7ABA53);
51 assert_int_equal((size_t)connect_to_customer_database(), 0x0DA7ABA53);
54 /* This test fails as the mock function connect_to_database() will have no
57 static void fail_connect_to_customer_database(void **state) {
58 (void) state; /* unused */
60 assert_true(connect_to_customer_database() ==
61 (DatabaseConnection*)0x0DA7ABA53);
65 static void test_get_customer_id_by_name(void **state) {
66 DatabaseConnection connection = {
67 "somedatabase.somewhere.com", 12345678, mock_query_database
69 /* Return a single customer ID when mock_query_database() is called. */
70 int customer_ids = 543;
73 (void) state; /* unused */
75 will_return(mock_query_database, &customer_ids);
76 will_return(mock_query_database, 1);
78 rc = get_customer_id_by_name(&connection, "john doe");
79 assert_int_equal(rc, 543);
83 const UnitTest tests[] = {
84 unit_test(test_connect_to_customer_database),
85 unit_test(test_get_customer_id_by_name),
87 return run_tests(tests);