1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "cli/cmd_db_exec.hpp"
33 #include <atf-c++.hpp>
35 #include "utils/format/macros.hpp"
36 #include "utils/sqlite/database.hpp"
37 #include "utils/sqlite/statement.ipp"
39 namespace sqlite
= utils::sqlite
;
45 /// Performs a test for the cli::format_cell() function.
47 /// \tparam Cell The type of the value to insert into the test column.
48 /// \param column_type The SQL type of the test column.
49 /// \param value The value to insert into the test column.
50 /// \param exp_value The expected return value of cli::format_cell().
51 template< class Cell
>
53 do_format_cell_test(const std::string column_type
,
54 const Cell
& value
, const std::string
& exp_value
)
56 sqlite::database db
= sqlite::database::in_memory();
58 sqlite::statement create
= db
.create_statement(
59 F("CREATE TABLE test (column %s)") % column_type
);
60 create
.step_without_results();
62 sqlite::statement insert
= db
.create_statement(
63 "INSERT INTO test (column) VALUES (:column)");
64 insert
.bind(":column", value
);
65 insert
.step_without_results();
67 sqlite::statement query
= db
.create_statement("SELECT * FROM test");
68 ATF_REQUIRE(query
.step());
69 ATF_REQUIRE_EQ(exp_value
, cli::format_cell(query
, 0));
70 ATF_REQUIRE(!query
.step());
74 } // anonymous namespace
77 ATF_TEST_CASE_WITHOUT_HEAD(format_cell__blob
);
78 ATF_TEST_CASE_BODY(format_cell__blob
)
80 const char* contents
= "Some random contents";
82 "BLOB", sqlite::blob(contents
, std::strlen(contents
)),
83 F("BLOB of %s bytes") % strlen(contents
));
87 ATF_TEST_CASE_WITHOUT_HEAD(format_cell__float
);
88 ATF_TEST_CASE_BODY(format_cell__float
)
90 do_format_cell_test("FLOAT", 3.5, "3.5");
94 ATF_TEST_CASE_WITHOUT_HEAD(format_cell__integer
);
95 ATF_TEST_CASE_BODY(format_cell__integer
)
97 do_format_cell_test("INTEGER", 123456, "123456");
101 ATF_TEST_CASE_WITHOUT_HEAD(format_cell__null
);
102 ATF_TEST_CASE_BODY(format_cell__null
)
104 do_format_cell_test("TEXT", sqlite::null(), "NULL");
108 ATF_TEST_CASE_WITHOUT_HEAD(format_cell__text
);
109 ATF_TEST_CASE_BODY(format_cell__text
)
111 do_format_cell_test("TEXT", "Hello, world", "Hello, world");
115 ATF_TEST_CASE_WITHOUT_HEAD(format_headers
);
116 ATF_TEST_CASE_BODY(format_headers
)
118 sqlite::database db
= sqlite::database::in_memory();
120 sqlite::statement create
= db
.create_statement(
121 "CREATE TABLE test (c1 TEXT, c2 TEXT, c3 TEXT)");
122 create
.step_without_results();
124 sqlite::statement query
= db
.create_statement(
125 "SELECT c1, c2, c3 AS c3bis FROM test");
126 ATF_REQUIRE_EQ("c1,c2,c3bis", cli::format_headers(query
));
130 ATF_TEST_CASE_WITHOUT_HEAD(format_row
);
131 ATF_TEST_CASE_BODY(format_row
)
133 sqlite::database db
= sqlite::database::in_memory();
135 sqlite::statement create
= db
.create_statement(
136 "CREATE TABLE test (c1 TEXT, c2 BLOB)");
137 create
.step_without_results();
139 const char* memory
= "BLOB contents";
140 sqlite::statement insert
= db
.create_statement(
141 "INSERT INTO test VALUES (:v1, :v2)");
142 insert
.bind(":v1", "A string");
143 insert
.bind(":v2", sqlite::blob(memory
, std::strlen(memory
)));
144 insert
.step_without_results();
146 sqlite::statement query
= db
.create_statement("SELECT * FROM test");
149 (F("A string,BLOB of %s bytes") % std::strlen(memory
)).str(),
150 cli::format_row(query
));
154 ATF_INIT_TEST_CASES(tcs
)
156 ATF_ADD_TEST_CASE(tcs
, format_cell__blob
);
157 ATF_ADD_TEST_CASE(tcs
, format_cell__float
);
158 ATF_ADD_TEST_CASE(tcs
, format_cell__integer
);
159 ATF_ADD_TEST_CASE(tcs
, format_cell__null
);
160 ATF_ADD_TEST_CASE(tcs
, format_cell__text
);
162 ATF_ADD_TEST_CASE(tcs
, format_headers
);
164 ATF_ADD_TEST_CASE(tcs
, format_row
);