xref: /minix3/external/bsd/kyua-cli/dist/cli/cmd_db_exec_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "cli/cmd_db_exec.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <cstring>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <atf-c++.hpp>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
36*11be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
37*11be35a1SLionel Sambuc #include "utils/sqlite/statement.ipp"
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc namespace {
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc /// Performs a test for the cli::format_cell() function.
46*11be35a1SLionel Sambuc ///
47*11be35a1SLionel Sambuc /// \tparam Cell The type of the value to insert into the test column.
48*11be35a1SLionel Sambuc /// \param column_type The SQL type of the test column.
49*11be35a1SLionel Sambuc /// \param value The value to insert into the test column.
50*11be35a1SLionel Sambuc /// \param exp_value The expected return value of cli::format_cell().
51*11be35a1SLionel Sambuc template< class Cell >
52*11be35a1SLionel Sambuc static void
do_format_cell_test(const std::string column_type,const Cell & value,const std::string & exp_value)53*11be35a1SLionel Sambuc do_format_cell_test(const std::string column_type,
54*11be35a1SLionel Sambuc                     const Cell& value, const std::string& exp_value)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc     sqlite::statement create = db.create_statement(
59*11be35a1SLionel Sambuc         F("CREATE TABLE test (column %s)") % column_type);
60*11be35a1SLionel Sambuc     create.step_without_results();
61*11be35a1SLionel Sambuc 
62*11be35a1SLionel Sambuc     sqlite::statement insert = db.create_statement(
63*11be35a1SLionel Sambuc         "INSERT INTO test (column) VALUES (:column)");
64*11be35a1SLionel Sambuc     insert.bind(":column", value);
65*11be35a1SLionel Sambuc     insert.step_without_results();
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc     sqlite::statement query = db.create_statement("SELECT * FROM test");
68*11be35a1SLionel Sambuc     ATF_REQUIRE(query.step());
69*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(exp_value, cli::format_cell(query, 0));
70*11be35a1SLionel Sambuc     ATF_REQUIRE(!query.step());
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc }  // anonymous namespace
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 
77*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_cell__blob);
ATF_TEST_CASE_BODY(format_cell__blob)78*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_cell__blob)
79*11be35a1SLionel Sambuc {
80*11be35a1SLionel Sambuc     const char* contents = "Some random contents";
81*11be35a1SLionel Sambuc     do_format_cell_test(
82*11be35a1SLionel Sambuc         "BLOB", sqlite::blob(contents, std::strlen(contents)),
83*11be35a1SLionel Sambuc         F("BLOB of %s bytes") % strlen(contents));
84*11be35a1SLionel Sambuc }
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_cell__float);
ATF_TEST_CASE_BODY(format_cell__float)88*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_cell__float)
89*11be35a1SLionel Sambuc {
90*11be35a1SLionel Sambuc     do_format_cell_test("FLOAT", 3.5, "3.5");
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_cell__integer);
ATF_TEST_CASE_BODY(format_cell__integer)95*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_cell__integer)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc     do_format_cell_test("INTEGER", 123456, "123456");
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_cell__null);
ATF_TEST_CASE_BODY(format_cell__null)102*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_cell__null)
103*11be35a1SLionel Sambuc {
104*11be35a1SLionel Sambuc     do_format_cell_test("TEXT", sqlite::null(), "NULL");
105*11be35a1SLionel Sambuc }
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_cell__text);
ATF_TEST_CASE_BODY(format_cell__text)109*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_cell__text)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc     do_format_cell_test("TEXT", "Hello, world", "Hello, world");
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_headers);
ATF_TEST_CASE_BODY(format_headers)116*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_headers)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc     sqlite::statement create = db.create_statement(
121*11be35a1SLionel Sambuc         "CREATE TABLE test (c1 TEXT, c2 TEXT, c3 TEXT)");
122*11be35a1SLionel Sambuc     create.step_without_results();
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc     sqlite::statement query = db.create_statement(
125*11be35a1SLionel Sambuc         "SELECT c1, c2, c3 AS c3bis FROM test");
126*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("c1,c2,c3bis", cli::format_headers(query));
127*11be35a1SLionel Sambuc }
128*11be35a1SLionel Sambuc 
129*11be35a1SLionel Sambuc 
130*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_row);
ATF_TEST_CASE_BODY(format_row)131*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_row)
132*11be35a1SLionel Sambuc {
133*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc     sqlite::statement create = db.create_statement(
136*11be35a1SLionel Sambuc         "CREATE TABLE test (c1 TEXT, c2 BLOB)");
137*11be35a1SLionel Sambuc     create.step_without_results();
138*11be35a1SLionel Sambuc 
139*11be35a1SLionel Sambuc     const char* memory = "BLOB contents";
140*11be35a1SLionel Sambuc     sqlite::statement insert = db.create_statement(
141*11be35a1SLionel Sambuc         "INSERT INTO test VALUES (:v1, :v2)");
142*11be35a1SLionel Sambuc     insert.bind(":v1", "A string");
143*11be35a1SLionel Sambuc     insert.bind(":v2", sqlite::blob(memory, std::strlen(memory)));
144*11be35a1SLionel Sambuc     insert.step_without_results();
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc     sqlite::statement query = db.create_statement("SELECT * FROM test");
147*11be35a1SLionel Sambuc     query.step();
148*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(
149*11be35a1SLionel Sambuc         (F("A string,BLOB of %s bytes") % std::strlen(memory)).str(),
150*11be35a1SLionel Sambuc         cli::format_row(query));
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)154*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_cell__blob);
157*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_cell__float);
158*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_cell__integer);
159*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_cell__null);
160*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_cell__text);
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_headers);
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_row);
165*11be35a1SLionel Sambuc }
166