xref: /freebsd/contrib/kyua/cli/cmd_db_exec_test.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
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 "cli/cmd_db_exec.hpp"
30  
31  #include <cstring>
32  
33  #include <atf-c++.hpp>
34  
35  #include "utils/format/macros.hpp"
36  #include "utils/sqlite/database.hpp"
37  #include "utils/sqlite/statement.ipp"
38  
39  namespace sqlite = utils::sqlite;
40  
41  
42  namespace {
43  
44  
45  /// Performs a test for the cli::format_cell() function.
46  ///
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 >
52  static void
do_format_cell_test(const std::string column_type,const Cell & value,const std::string & exp_value)53  do_format_cell_test(const std::string column_type,
54                      const Cell& value, const std::string& exp_value)
55  {
56      sqlite::database db = sqlite::database::in_memory();
57  
58      sqlite::statement create = db.create_statement(
59          F("CREATE TABLE test (column %s)") % column_type);
60      create.step_without_results();
61  
62      sqlite::statement insert = db.create_statement(
63          "INSERT INTO test (column) VALUES (:column)");
64      insert.bind(":column", value);
65      insert.step_without_results();
66  
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());
71  }
72  
73  
74  }  // anonymous namespace
75  
76  
77  ATF_TEST_CASE_WITHOUT_HEAD(format_cell__blob);
ATF_TEST_CASE_BODY(format_cell__blob)78  ATF_TEST_CASE_BODY(format_cell__blob)
79  {
80      const char* contents = "Some random contents";
81      do_format_cell_test(
82          "BLOB", sqlite::blob(contents, std::strlen(contents)),
83          F("BLOB of %s bytes") % strlen(contents));
84  }
85  
86  
87  ATF_TEST_CASE_WITHOUT_HEAD(format_cell__float);
ATF_TEST_CASE_BODY(format_cell__float)88  ATF_TEST_CASE_BODY(format_cell__float)
89  {
90      do_format_cell_test("FLOAT", 3.5, "3.5");
91  }
92  
93  
94  ATF_TEST_CASE_WITHOUT_HEAD(format_cell__integer);
ATF_TEST_CASE_BODY(format_cell__integer)95  ATF_TEST_CASE_BODY(format_cell__integer)
96  {
97      do_format_cell_test("INTEGER", 123456, "123456");
98  }
99  
100  
101  ATF_TEST_CASE_WITHOUT_HEAD(format_cell__null);
ATF_TEST_CASE_BODY(format_cell__null)102  ATF_TEST_CASE_BODY(format_cell__null)
103  {
104      do_format_cell_test("TEXT", sqlite::null(), "NULL");
105  }
106  
107  
108  ATF_TEST_CASE_WITHOUT_HEAD(format_cell__text);
ATF_TEST_CASE_BODY(format_cell__text)109  ATF_TEST_CASE_BODY(format_cell__text)
110  {
111      do_format_cell_test("TEXT", "Hello, world", "Hello, world");
112  }
113  
114  
115  ATF_TEST_CASE_WITHOUT_HEAD(format_headers);
ATF_TEST_CASE_BODY(format_headers)116  ATF_TEST_CASE_BODY(format_headers)
117  {
118      sqlite::database db = sqlite::database::in_memory();
119  
120      sqlite::statement create = db.create_statement(
121          "CREATE TABLE test (c1 TEXT, c2 TEXT, c3 TEXT)");
122      create.step_without_results();
123  
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));
127  }
128  
129  
130  ATF_TEST_CASE_WITHOUT_HEAD(format_row);
ATF_TEST_CASE_BODY(format_row)131  ATF_TEST_CASE_BODY(format_row)
132  {
133      sqlite::database db = sqlite::database::in_memory();
134  
135      sqlite::statement create = db.create_statement(
136          "CREATE TABLE test (c1 TEXT, c2 BLOB)");
137      create.step_without_results();
138  
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();
145  
146      sqlite::statement query = db.create_statement("SELECT * FROM test");
147      query.step();
148      ATF_REQUIRE_EQ(
149          (F("A string,BLOB of %s bytes") % std::strlen(memory)).str(),
150          cli::format_row(query));
151  }
152  
153  
ATF_INIT_TEST_CASES(tcs)154  ATF_INIT_TEST_CASES(tcs)
155  {
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);
161  
162      ATF_ADD_TEST_CASE(tcs, format_headers);
163  
164      ATF_ADD_TEST_CASE(tcs, format_row);
165  }
166