xref: /freebsd/contrib/kyua/utils/sqlite/c_gate_test.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/sqlite/c_gate.hpp"
30 
31 #include <atf-c++.hpp>
32 
33 #include "utils/fs/path.hpp"
34 #include "utils/optional.ipp"
35 #include "utils/sqlite/database.hpp"
36 #include "utils/sqlite/test_utils.hpp"
37 
38 namespace fs = utils::fs;
39 namespace sqlite = utils::sqlite;
40 
41 
42 ATF_TEST_CASE_WITHOUT_HEAD(connect);
43 ATF_TEST_CASE_BODY(connect)
44 {
45     ::sqlite3* raw_db;
46     ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_open_v2(":memory:", &raw_db,
47                                                 SQLITE_OPEN_READWRITE, NULL));
48     {
49         sqlite::database database = sqlite::database_c_gate::connect(raw_db);
50         create_test_table(raw(database));
51     }
52     // If the wrapper object has closed the SQLite 3 database, we will misbehave
53     // here either by crashing or not finding our test table.
54     verify_test_table(raw_db);
55     ::sqlite3_close(raw_db);
56 }
57 
58 
59 ATF_TEST_CASE_WITHOUT_HEAD(c_database);
60 ATF_TEST_CASE_BODY(c_database)
61 {
62     sqlite::database db = sqlite::database::in_memory();
63     create_test_table(raw(db));
64     {
65         sqlite::database_c_gate gate(db);
66         ::sqlite3* raw_db = gate.c_database();
67         verify_test_table(raw_db);
68     }
69 }
70 
71 
72 ATF_TEST_CASE(database__db_filename);
73 ATF_TEST_CASE_HEAD(database__db_filename)
74 {
75     set_md_var("descr", "The current implementation of db_filename() has no "
76                "means to access the filename of a database connected to a raw "
77                "sqlite3 object");
78 }
79 ATF_TEST_CASE_BODY(database__db_filename)
80 {
81     ::sqlite3* raw_db;
82     ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_open_v2(
83         "test.db", &raw_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
84 
85     sqlite::database database = sqlite::database_c_gate::connect(raw_db);
86     ATF_REQUIRE(!database.db_filename());
87     ::sqlite3_close(raw_db);
88 }
89 
90 
91 ATF_INIT_TEST_CASES(tcs)
92 {
93     ATF_ADD_TEST_CASE(tcs, c_database);
94     ATF_ADD_TEST_CASE(tcs, connect);
95     ATF_ADD_TEST_CASE(tcs, database__db_filename);
96 }
97