xref: /freebsd/contrib/kyua/utils/sqlite/statement_test.cpp (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
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/statement.ipp"
30 
31 extern "C" {
32 #include <stdint.h>
33 }
34 
35 #include <cstring>
36 #include <iostream>
37 
38 #include <atf-c++.hpp>
39 
40 #include "utils/sqlite/database.hpp"
41 #include "utils/sqlite/test_utils.hpp"
42 
43 namespace sqlite = utils::sqlite;
44 
45 
46 ATF_TEST_CASE_WITHOUT_HEAD(step__ok);
47 ATF_TEST_CASE_BODY(step__ok)
48 {
49     sqlite::database db = sqlite::database::in_memory();
50     sqlite::statement stmt = db.create_statement(
51         "CREATE TABLE foo (a INTEGER PRIMARY KEY)");
52     ATF_REQUIRE_THROW(sqlite::error, db.exec("SELECT * FROM foo"));
53     ATF_REQUIRE(!stmt.step());
54     db.exec("SELECT * FROM foo");
55 }
56 
57 
58 ATF_TEST_CASE_WITHOUT_HEAD(step__many);
59 ATF_TEST_CASE_BODY(step__many)
60 {
61     sqlite::database db = sqlite::database::in_memory();
62     create_test_table(raw(db));
63     sqlite::statement stmt = db.create_statement(
64         "SELECT prime FROM test ORDER BY prime");
65     for (int i = 0; i < 5; i++)
66         ATF_REQUIRE(stmt.step());
67     ATF_REQUIRE(!stmt.step());
68 }
69 
70 
71 ATF_TEST_CASE_WITHOUT_HEAD(step__fail);
72 ATF_TEST_CASE_BODY(step__fail)
73 {
74     sqlite::database db = sqlite::database::in_memory();
75     sqlite::statement stmt = db.create_statement(
76         "CREATE TABLE foo (a INTEGER PRIMARY KEY)");
77     ATF_REQUIRE(!stmt.step());
78     REQUIRE_API_ERROR("sqlite3_step", stmt.step());
79 }
80 
81 
82 ATF_TEST_CASE_WITHOUT_HEAD(step_without_results__ok);
83 ATF_TEST_CASE_BODY(step_without_results__ok)
84 {
85     sqlite::database db = sqlite::database::in_memory();
86     sqlite::statement stmt = db.create_statement(
87         "CREATE TABLE foo (a INTEGER PRIMARY KEY)");
88     ATF_REQUIRE_THROW(sqlite::error, db.exec("SELECT * FROM foo"));
89     stmt.step_without_results();
90     db.exec("SELECT * FROM foo");
91 }
92 
93 
94 ATF_TEST_CASE_WITHOUT_HEAD(step_without_results__fail);
95 ATF_TEST_CASE_BODY(step_without_results__fail)
96 {
97     sqlite::database db = sqlite::database::in_memory();
98     db.exec("CREATE TABLE foo (a INTEGER PRIMARY KEY)");
99     db.exec("INSERT INTO foo VALUES (3)");
100     sqlite::statement stmt = db.create_statement(
101         "INSERT INTO foo VALUES (3)");
102     REQUIRE_API_ERROR("sqlite3_step", stmt.step_without_results());
103 }
104 
105 
106 ATF_TEST_CASE_WITHOUT_HEAD(column_count);
107 ATF_TEST_CASE_BODY(column_count)
108 {
109     sqlite::database db = sqlite::database::in_memory();
110     db.exec("CREATE TABLE foo (a INTEGER PRIMARY KEY, b INTEGER, c TEXT);"
111             "INSERT INTO foo VALUES (5, 3, 'foo');");
112     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
113     ATF_REQUIRE(stmt.step());
114     ATF_REQUIRE_EQ(3, stmt.column_count());
115     ATF_REQUIRE(!stmt.step());
116 }
117 
118 
119 ATF_TEST_CASE_WITHOUT_HEAD(column_name__ok);
120 ATF_TEST_CASE_BODY(column_name__ok)
121 {
122     sqlite::database db = sqlite::database::in_memory();
123     db.exec("CREATE TABLE foo (first INTEGER PRIMARY KEY, second TEXT);"
124             "INSERT INTO foo VALUES (5, 'foo');");
125     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
126     ATF_REQUIRE(stmt.step());
127     ATF_REQUIRE_EQ("first", stmt.column_name(0));
128     ATF_REQUIRE_EQ("second", stmt.column_name(1));
129     ATF_REQUIRE(!stmt.step());
130 }
131 
132 
133 ATF_TEST_CASE_WITHOUT_HEAD(column_name__fail);
134 ATF_TEST_CASE_BODY(column_name__fail)
135 {
136     sqlite::database db = sqlite::database::in_memory();
137     db.exec("CREATE TABLE foo (first INTEGER PRIMARY KEY);"
138             "INSERT INTO foo VALUES (5);");
139     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
140     ATF_REQUIRE(stmt.step());
141     ATF_REQUIRE_EQ("first", stmt.column_name(0));
142     REQUIRE_API_ERROR("sqlite3_column_name", stmt.column_name(1));
143     ATF_REQUIRE(!stmt.step());
144 }
145 
146 
147 ATF_TEST_CASE_WITHOUT_HEAD(column_type__ok);
148 ATF_TEST_CASE_BODY(column_type__ok)
149 {
150     sqlite::database db = sqlite::database::in_memory();
151     db.exec("CREATE TABLE foo (a_blob BLOB,"
152             "                  a_float FLOAT,"
153             "                  an_integer INTEGER,"
154             "                  a_null BLOB,"
155             "                  a_text TEXT);"
156             "INSERT INTO foo VALUES (x'0102', 0.3, 5, NULL, 'foo bar');"
157             "INSERT INTO foo VALUES (NULL, NULL, NULL, NULL, NULL);");
158     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
159     ATF_REQUIRE(stmt.step());
160     ATF_REQUIRE(sqlite::type_blob == stmt.column_type(0));
161     ATF_REQUIRE(sqlite::type_float == stmt.column_type(1));
162     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(2));
163     ATF_REQUIRE(sqlite::type_null == stmt.column_type(3));
164     ATF_REQUIRE(sqlite::type_text == stmt.column_type(4));
165     ATF_REQUIRE(stmt.step());
166     for (int i = 0; i < stmt.column_count(); i++)
167         ATF_REQUIRE(sqlite::type_null == stmt.column_type(i));
168     ATF_REQUIRE(!stmt.step());
169 }
170 
171 
172 ATF_TEST_CASE_WITHOUT_HEAD(column_type__out_of_range);
173 ATF_TEST_CASE_BODY(column_type__out_of_range)
174 {
175     sqlite::database db = sqlite::database::in_memory();
176     db.exec("CREATE TABLE foo (a INTEGER PRIMARY KEY);"
177             "INSERT INTO foo VALUES (1);");
178     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
179     ATF_REQUIRE(stmt.step());
180     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
181     ATF_REQUIRE(sqlite::type_null == stmt.column_type(1));
182     ATF_REQUIRE(sqlite::type_null == stmt.column_type(512));
183     ATF_REQUIRE(!stmt.step());
184 }
185 
186 
187 ATF_TEST_CASE_WITHOUT_HEAD(column_id__ok);
188 ATF_TEST_CASE_BODY(column_id__ok)
189 {
190     sqlite::database db = sqlite::database::in_memory();
191     db.exec("CREATE TABLE foo (bar INTEGER PRIMARY KEY, "
192             "                  baz INTEGER);"
193             "INSERT INTO foo VALUES (1, 2);");
194     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
195     ATF_REQUIRE(stmt.step());
196     ATF_REQUIRE_EQ(0, stmt.column_id("bar"));
197     ATF_REQUIRE_EQ(1, stmt.column_id("baz"));
198     ATF_REQUIRE_EQ(0, stmt.column_id("bar"));
199     ATF_REQUIRE_EQ(1, stmt.column_id("baz"));
200     ATF_REQUIRE(!stmt.step());
201 }
202 
203 
204 ATF_TEST_CASE_WITHOUT_HEAD(column_id__missing);
205 ATF_TEST_CASE_BODY(column_id__missing)
206 {
207     sqlite::database db = sqlite::database::in_memory();
208     db.exec("CREATE TABLE foo (bar INTEGER PRIMARY KEY, "
209             "                  baz INTEGER);"
210             "INSERT INTO foo VALUES (1, 2);");
211     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
212     ATF_REQUIRE(stmt.step());
213     ATF_REQUIRE_EQ(0, stmt.column_id("bar"));
214     try {
215         stmt.column_id("bazo");
216         fail("invalid_column_error not raised");
217     } catch (const sqlite::invalid_column_error& e) {
218         ATF_REQUIRE_EQ("bazo", e.column_name());
219     }
220     ATF_REQUIRE(!stmt.step());
221 }
222 
223 
224 ATF_TEST_CASE_WITHOUT_HEAD(column_blob);
225 ATF_TEST_CASE_BODY(column_blob)
226 {
227     sqlite::database db = sqlite::database::in_memory();
228     db.exec("CREATE TABLE foo (a INTEGER, b BLOB, c INTEGER);"
229             "INSERT INTO foo VALUES (NULL, x'cafe', NULL);");
230     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
231     ATF_REQUIRE(stmt.step());
232     const sqlite::blob blob = stmt.column_blob(1);
233     ATF_REQUIRE_EQ(0xca, static_cast< const uint8_t* >(blob.memory)[0]);
234     ATF_REQUIRE_EQ(0xfe, static_cast< const uint8_t* >(blob.memory)[1]);
235     ATF_REQUIRE(!stmt.step());
236 }
237 
238 
239 ATF_TEST_CASE_WITHOUT_HEAD(column_double);
240 ATF_TEST_CASE_BODY(column_double)
241 {
242     sqlite::database db = sqlite::database::in_memory();
243     db.exec("CREATE TABLE foo (a INTEGER, b DOUBLE, c INTEGER);"
244             "INSERT INTO foo VALUES (NULL, 0.5, NULL);");
245     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
246     ATF_REQUIRE(stmt.step());
247     ATF_REQUIRE_EQ(0.5, stmt.column_double(1));
248     ATF_REQUIRE(!stmt.step());
249 }
250 
251 
252 ATF_TEST_CASE_WITHOUT_HEAD(column_int__ok);
253 ATF_TEST_CASE_BODY(column_int__ok)
254 {
255     sqlite::database db = sqlite::database::in_memory();
256     db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
257             "INSERT INTO foo VALUES (NULL, 987, NULL);");
258     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
259     ATF_REQUIRE(stmt.step());
260     ATF_REQUIRE_EQ(987, stmt.column_int(1));
261     ATF_REQUIRE(!stmt.step());
262 }
263 
264 
265 ATF_TEST_CASE_WITHOUT_HEAD(column_int__overflow);
266 ATF_TEST_CASE_BODY(column_int__overflow)
267 {
268     sqlite::database db = sqlite::database::in_memory();
269     db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
270             "INSERT INTO foo VALUES (NULL, 4294967419, NULL);");
271     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
272     ATF_REQUIRE(stmt.step());
273     ATF_REQUIRE_EQ(123, stmt.column_int(1));
274     ATF_REQUIRE(!stmt.step());
275 }
276 
277 
278 ATF_TEST_CASE_WITHOUT_HEAD(column_int64);
279 ATF_TEST_CASE_BODY(column_int64)
280 {
281     sqlite::database db = sqlite::database::in_memory();
282     db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
283             "INSERT INTO foo VALUES (NULL, 4294967419, NULL);");
284     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
285     ATF_REQUIRE(stmt.step());
286     ATF_REQUIRE_EQ(4294967419LL, stmt.column_int64(1));
287     ATF_REQUIRE(!stmt.step());
288 }
289 
290 
291 ATF_TEST_CASE_WITHOUT_HEAD(column_text);
292 ATF_TEST_CASE_BODY(column_text)
293 {
294     sqlite::database db = sqlite::database::in_memory();
295     db.exec("CREATE TABLE foo (a INTEGER, b TEXT, c INTEGER);"
296             "INSERT INTO foo VALUES (NULL, 'foo bar', NULL);");
297     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
298     ATF_REQUIRE(stmt.step());
299     ATF_REQUIRE_EQ("foo bar", stmt.column_text(1));
300     ATF_REQUIRE(!stmt.step());
301 }
302 
303 
304 ATF_TEST_CASE_WITHOUT_HEAD(column_bytes__blob);
305 ATF_TEST_CASE_BODY(column_bytes__blob)
306 {
307     sqlite::database db = sqlite::database::in_memory();
308     db.exec("CREATE TABLE foo (a BLOB);"
309             "INSERT INTO foo VALUES (x'12345678');");
310     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
311     ATF_REQUIRE(stmt.step());
312     ATF_REQUIRE_EQ(4, stmt.column_bytes(0));
313     ATF_REQUIRE(!stmt.step());
314 }
315 
316 
317 ATF_TEST_CASE_WITHOUT_HEAD(column_bytes__text);
318 ATF_TEST_CASE_BODY(column_bytes__text)
319 {
320     sqlite::database db = sqlite::database::in_memory();
321     db.exec("CREATE TABLE foo (a TEXT);"
322             "INSERT INTO foo VALUES ('foo bar');");
323     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
324     ATF_REQUIRE(stmt.step());
325     ATF_REQUIRE_EQ(7, stmt.column_bytes(0));
326     ATF_REQUIRE(!stmt.step());
327 }
328 
329 
330 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_blob__ok);
331 ATF_TEST_CASE_BODY(safe_column_blob__ok)
332 {
333     sqlite::database db = sqlite::database::in_memory();
334     db.exec("CREATE TABLE foo (a INTEGER, b BLOB, c INTEGER);"
335             "INSERT INTO foo VALUES (NULL, x'cafe', NULL);");
336     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
337     ATF_REQUIRE(stmt.step());
338     const sqlite::blob blob = stmt.safe_column_blob("b");
339     ATF_REQUIRE_EQ(0xca, static_cast< const uint8_t* >(blob.memory)[0]);
340     ATF_REQUIRE_EQ(0xfe, static_cast< const uint8_t* >(blob.memory)[1]);
341     ATF_REQUIRE(!stmt.step());
342 }
343 
344 
345 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_blob__fail);
346 ATF_TEST_CASE_BODY(safe_column_blob__fail)
347 {
348     sqlite::database db = sqlite::database::in_memory();
349     db.exec("CREATE TABLE foo (a INTEGER);"
350             "INSERT INTO foo VALUES (123);");
351     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
352     ATF_REQUIRE(stmt.step());
353     ATF_REQUIRE_THROW(sqlite::invalid_column_error,
354                       stmt.safe_column_blob("b"));
355     ATF_REQUIRE_THROW_RE(sqlite::error, "not a blob",
356                          stmt.safe_column_blob("a"));
357     ATF_REQUIRE(!stmt.step());
358 }
359 
360 
361 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_double__ok);
362 ATF_TEST_CASE_BODY(safe_column_double__ok)
363 {
364     sqlite::database db = sqlite::database::in_memory();
365     db.exec("CREATE TABLE foo (a INTEGER, b DOUBLE, c INTEGER);"
366             "INSERT INTO foo VALUES (NULL, 0.5, NULL);");
367     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
368     ATF_REQUIRE(stmt.step());
369     ATF_REQUIRE_EQ(0.5, stmt.safe_column_double("b"));
370     ATF_REQUIRE(!stmt.step());
371 }
372 
373 
374 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_double__fail);
375 ATF_TEST_CASE_BODY(safe_column_double__fail)
376 {
377     sqlite::database db = sqlite::database::in_memory();
378     db.exec("CREATE TABLE foo (a INTEGER);"
379             "INSERT INTO foo VALUES (NULL);");
380     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
381     ATF_REQUIRE(stmt.step());
382     ATF_REQUIRE_THROW(sqlite::invalid_column_error,
383                       stmt.safe_column_double("b"));
384     ATF_REQUIRE_THROW_RE(sqlite::error, "not a float",
385                          stmt.safe_column_double("a"));
386     ATF_REQUIRE(!stmt.step());
387 }
388 
389 
390 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int__ok);
391 ATF_TEST_CASE_BODY(safe_column_int__ok)
392 {
393     sqlite::database db = sqlite::database::in_memory();
394     db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
395             "INSERT INTO foo VALUES (NULL, 987, NULL);");
396     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
397     ATF_REQUIRE(stmt.step());
398     ATF_REQUIRE_EQ(987, stmt.safe_column_int("b"));
399     ATF_REQUIRE(!stmt.step());
400 }
401 
402 
403 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int__fail);
404 ATF_TEST_CASE_BODY(safe_column_int__fail)
405 {
406     sqlite::database db = sqlite::database::in_memory();
407     db.exec("CREATE TABLE foo (a TEXT);"
408             "INSERT INTO foo VALUES ('def');");
409     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
410     ATF_REQUIRE(stmt.step());
411     ATF_REQUIRE_THROW(sqlite::invalid_column_error,
412                       stmt.safe_column_int("b"));
413     ATF_REQUIRE_THROW_RE(sqlite::error, "not an integer",
414                          stmt.safe_column_int("a"));
415     ATF_REQUIRE(!stmt.step());
416 }
417 
418 
419 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int64__ok);
420 ATF_TEST_CASE_BODY(safe_column_int64__ok)
421 {
422     sqlite::database db = sqlite::database::in_memory();
423     db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
424             "INSERT INTO foo VALUES (NULL, 4294967419, NULL);");
425     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
426     ATF_REQUIRE(stmt.step());
427     ATF_REQUIRE_EQ(4294967419LL, stmt.safe_column_int64("b"));
428     ATF_REQUIRE(!stmt.step());
429 }
430 
431 
432 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int64__fail);
433 ATF_TEST_CASE_BODY(safe_column_int64__fail)
434 {
435     sqlite::database db = sqlite::database::in_memory();
436     db.exec("CREATE TABLE foo (a TEXT);"
437             "INSERT INTO foo VALUES ('abc');");
438     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
439     ATF_REQUIRE(stmt.step());
440     ATF_REQUIRE_THROW(sqlite::invalid_column_error,
441                       stmt.safe_column_int64("b"));
442     ATF_REQUIRE_THROW_RE(sqlite::error, "not an integer",
443                          stmt.safe_column_int64("a"));
444     ATF_REQUIRE(!stmt.step());
445 }
446 
447 
448 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_text__ok);
449 ATF_TEST_CASE_BODY(safe_column_text__ok)
450 {
451     sqlite::database db = sqlite::database::in_memory();
452     db.exec("CREATE TABLE foo (a INTEGER, b TEXT, c INTEGER);"
453             "INSERT INTO foo VALUES (NULL, 'foo bar', NULL);");
454     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
455     ATF_REQUIRE(stmt.step());
456     ATF_REQUIRE_EQ("foo bar", stmt.safe_column_text("b"));
457     ATF_REQUIRE(!stmt.step());
458 }
459 
460 
461 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_text__fail);
462 ATF_TEST_CASE_BODY(safe_column_text__fail)
463 {
464     sqlite::database db = sqlite::database::in_memory();
465     db.exec("CREATE TABLE foo (a INTEGER);"
466             "INSERT INTO foo VALUES (NULL);");
467     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
468     ATF_REQUIRE(stmt.step());
469     ATF_REQUIRE_THROW(sqlite::invalid_column_error,
470                       stmt.safe_column_text("b"));
471     ATF_REQUIRE_THROW_RE(sqlite::error, "not a string",
472                          stmt.safe_column_text("a"));
473     ATF_REQUIRE(!stmt.step());
474 }
475 
476 
477 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_bytes__ok__blob);
478 ATF_TEST_CASE_BODY(safe_column_bytes__ok__blob)
479 {
480     sqlite::database db = sqlite::database::in_memory();
481     db.exec("CREATE TABLE foo (a BLOB);"
482             "INSERT INTO foo VALUES (x'12345678');");
483     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
484     ATF_REQUIRE(stmt.step());
485     ATF_REQUIRE_EQ(4, stmt.safe_column_bytes("a"));
486     ATF_REQUIRE(!stmt.step());
487 }
488 
489 
490 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_bytes__ok__text);
491 ATF_TEST_CASE_BODY(safe_column_bytes__ok__text)
492 {
493     sqlite::database db = sqlite::database::in_memory();
494     db.exec("CREATE TABLE foo (a TEXT);"
495             "INSERT INTO foo VALUES ('foo bar');");
496     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
497     ATF_REQUIRE(stmt.step());
498     ATF_REQUIRE_EQ(7, stmt.safe_column_bytes("a"));
499     ATF_REQUIRE(!stmt.step());
500 }
501 
502 
503 ATF_TEST_CASE_WITHOUT_HEAD(safe_column_bytes__fail);
504 ATF_TEST_CASE_BODY(safe_column_bytes__fail)
505 {
506     sqlite::database db = sqlite::database::in_memory();
507     db.exec("CREATE TABLE foo (a TEXT);"
508             "INSERT INTO foo VALUES (NULL);");
509     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
510     ATF_REQUIRE(stmt.step());
511     ATF_REQUIRE_THROW(sqlite::invalid_column_error,
512                       stmt.safe_column_bytes("b"));
513     ATF_REQUIRE_THROW_RE(sqlite::error, "not a blob or a string",
514                          stmt.safe_column_bytes("a"));
515     ATF_REQUIRE(!stmt.step());
516 }
517 
518 
519 ATF_TEST_CASE_WITHOUT_HEAD(reset);
520 ATF_TEST_CASE_BODY(reset)
521 {
522     sqlite::database db = sqlite::database::in_memory();
523     db.exec("CREATE TABLE foo (a TEXT);"
524             "INSERT INTO foo VALUES ('foo bar');");
525     sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
526     ATF_REQUIRE(stmt.step());
527     ATF_REQUIRE(!stmt.step());
528     stmt.reset();
529     ATF_REQUIRE(stmt.step());
530     ATF_REQUIRE(!stmt.step());
531 }
532 
533 
534 ATF_TEST_CASE_WITHOUT_HEAD(bind__blob);
535 ATF_TEST_CASE_BODY(bind__blob)
536 {
537     sqlite::database db = sqlite::database::in_memory();
538     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
539 
540     const unsigned char blob[] = {0xca, 0xfe};
541     stmt.bind(1, sqlite::blob(static_cast< const void* >(blob), 2));
542     ATF_REQUIRE(stmt.step());
543     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
544     ATF_REQUIRE_EQ(3, stmt.column_int(0));
545     ATF_REQUIRE(sqlite::type_blob == stmt.column_type(1));
546     const unsigned char* ret_blob =
547         static_cast< const unsigned char* >(stmt.column_blob(1).memory);
548     ATF_REQUIRE(std::memcmp(blob, ret_blob, 2) == 0);
549     ATF_REQUIRE(!stmt.step());
550 }
551 
552 
553 ATF_TEST_CASE_WITHOUT_HEAD(bind__double);
554 ATF_TEST_CASE_BODY(bind__double)
555 {
556     sqlite::database db = sqlite::database::in_memory();
557     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
558 
559     stmt.bind(1, 0.5);
560     ATF_REQUIRE(stmt.step());
561     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
562     ATF_REQUIRE_EQ(3, stmt.column_int(0));
563     ATF_REQUIRE(sqlite::type_float == stmt.column_type(1));
564     ATF_REQUIRE_EQ(0.5, stmt.column_double(1));
565     ATF_REQUIRE(!stmt.step());
566 }
567 
568 
569 ATF_TEST_CASE_WITHOUT_HEAD(bind__int);
570 ATF_TEST_CASE_BODY(bind__int)
571 {
572     sqlite::database db = sqlite::database::in_memory();
573     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
574 
575     stmt.bind(1, 123);
576     ATF_REQUIRE(stmt.step());
577     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
578     ATF_REQUIRE_EQ(3, stmt.column_int(0));
579     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(1));
580     ATF_REQUIRE_EQ(123, stmt.column_int(1));
581     ATF_REQUIRE(!stmt.step());
582 }
583 
584 
585 ATF_TEST_CASE_WITHOUT_HEAD(bind__int64);
586 ATF_TEST_CASE_BODY(bind__int64)
587 {
588     sqlite::database db = sqlite::database::in_memory();
589     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
590 
591     stmt.bind(1, static_cast< int64_t >(4294967419LL));
592     ATF_REQUIRE(stmt.step());
593     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
594     ATF_REQUIRE_EQ(3, stmt.column_int(0));
595     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(1));
596     ATF_REQUIRE_EQ(4294967419LL, stmt.column_int64(1));
597     ATF_REQUIRE(!stmt.step());
598 }
599 
600 
601 ATF_TEST_CASE_WITHOUT_HEAD(bind__null);
602 ATF_TEST_CASE_BODY(bind__null)
603 {
604     sqlite::database db = sqlite::database::in_memory();
605     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
606 
607     stmt.bind(1, sqlite::null());
608     ATF_REQUIRE(stmt.step());
609     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
610     ATF_REQUIRE_EQ(3, stmt.column_int(0));
611     ATF_REQUIRE(sqlite::type_null == stmt.column_type(1));
612     ATF_REQUIRE(!stmt.step());
613 }
614 
615 
616 ATF_TEST_CASE_WITHOUT_HEAD(bind__text);
617 ATF_TEST_CASE_BODY(bind__text)
618 {
619     sqlite::database db = sqlite::database::in_memory();
620     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
621 
622     const std::string str = "Hello";
623     stmt.bind(1, str);
624     ATF_REQUIRE(stmt.step());
625     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
626     ATF_REQUIRE_EQ(3, stmt.column_int(0));
627     ATF_REQUIRE(sqlite::type_text == stmt.column_type(1));
628     ATF_REQUIRE_EQ(str, stmt.column_text(1));
629     ATF_REQUIRE(!stmt.step());
630 }
631 
632 
633 ATF_TEST_CASE_WITHOUT_HEAD(bind__text__transient);
634 ATF_TEST_CASE_BODY(bind__text__transient)
635 {
636     sqlite::database db = sqlite::database::in_memory();
637     sqlite::statement stmt = db.create_statement("SELECT 3, :foo");
638 
639     {
640         const std::string str = "Hello";
641         stmt.bind(":foo", str);
642     }
643 
644     ATF_REQUIRE(stmt.step());
645     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
646     ATF_REQUIRE_EQ(3, stmt.column_int(0));
647     ATF_REQUIRE(sqlite::type_text == stmt.column_type(1));
648     ATF_REQUIRE_EQ(std::string("Hello"), stmt.column_text(1));
649     ATF_REQUIRE(!stmt.step());
650 }
651 
652 
653 ATF_TEST_CASE_WITHOUT_HEAD(bind__by_name);
654 ATF_TEST_CASE_BODY(bind__by_name)
655 {
656     sqlite::database db = sqlite::database::in_memory();
657     sqlite::statement stmt = db.create_statement("SELECT 3, :foo");
658 
659     const std::string str = "Hello";
660     stmt.bind(":foo", str);
661     ATF_REQUIRE(stmt.step());
662     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
663     ATF_REQUIRE_EQ(3, stmt.column_int(0));
664     ATF_REQUIRE(sqlite::type_text == stmt.column_type(1));
665     ATF_REQUIRE_EQ(str, stmt.column_text(1));
666     ATF_REQUIRE(!stmt.step());
667 }
668 
669 
670 ATF_TEST_CASE_WITHOUT_HEAD(bind_parameter_count);
671 ATF_TEST_CASE_BODY(bind_parameter_count)
672 {
673     sqlite::database db = sqlite::database::in_memory();
674     sqlite::statement stmt = db.create_statement("SELECT 3, ?, ?");
675     ATF_REQUIRE_EQ(2, stmt.bind_parameter_count());
676 }
677 
678 
679 ATF_TEST_CASE_WITHOUT_HEAD(bind_parameter_index);
680 ATF_TEST_CASE_BODY(bind_parameter_index)
681 {
682     sqlite::database db = sqlite::database::in_memory();
683     sqlite::statement stmt = db.create_statement("SELECT 3, :foo, ?, :bar");
684     ATF_REQUIRE_EQ(1, stmt.bind_parameter_index(":foo"));
685     ATF_REQUIRE_EQ(3, stmt.bind_parameter_index(":bar"));
686 }
687 
688 
689 ATF_TEST_CASE_WITHOUT_HEAD(bind_parameter_name);
690 ATF_TEST_CASE_BODY(bind_parameter_name)
691 {
692     sqlite::database db = sqlite::database::in_memory();
693     sqlite::statement stmt = db.create_statement("SELECT 3, :foo, ?, :bar");
694     ATF_REQUIRE_EQ(":foo", stmt.bind_parameter_name(1));
695     ATF_REQUIRE_EQ(":bar", stmt.bind_parameter_name(3));
696 }
697 
698 
699 ATF_TEST_CASE_WITHOUT_HEAD(clear_bindings);
700 ATF_TEST_CASE_BODY(clear_bindings)
701 {
702     sqlite::database db = sqlite::database::in_memory();
703     sqlite::statement stmt = db.create_statement("SELECT 3, ?");
704 
705     stmt.bind(1, 5);
706     ATF_REQUIRE(stmt.step());
707     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
708     ATF_REQUIRE_EQ(3, stmt.column_int(0));
709     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(1));
710     ATF_REQUIRE_EQ(5, stmt.column_int(1));
711     stmt.clear_bindings();
712     stmt.reset();
713 
714     ATF_REQUIRE(stmt.step());
715     ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
716     ATF_REQUIRE_EQ(3, stmt.column_int(0));
717     ATF_REQUIRE(sqlite::type_null == stmt.column_type(1));
718 
719     ATF_REQUIRE(!stmt.step());
720 }
721 
722 
723 ATF_INIT_TEST_CASES(tcs)
724 {
725     ATF_ADD_TEST_CASE(tcs, step__ok);
726     ATF_ADD_TEST_CASE(tcs, step__many);
727     ATF_ADD_TEST_CASE(tcs, step__fail);
728 
729     ATF_ADD_TEST_CASE(tcs, step_without_results__ok);
730     ATF_ADD_TEST_CASE(tcs, step_without_results__fail);
731 
732     ATF_ADD_TEST_CASE(tcs, column_count);
733 
734     ATF_ADD_TEST_CASE(tcs, column_name__ok);
735     ATF_ADD_TEST_CASE(tcs, column_name__fail);
736 
737     ATF_ADD_TEST_CASE(tcs, column_type__ok);
738     ATF_ADD_TEST_CASE(tcs, column_type__out_of_range);
739 
740     ATF_ADD_TEST_CASE(tcs, column_id__ok);
741     ATF_ADD_TEST_CASE(tcs, column_id__missing);
742 
743     ATF_ADD_TEST_CASE(tcs, column_blob);
744     ATF_ADD_TEST_CASE(tcs, column_double);
745     ATF_ADD_TEST_CASE(tcs, column_int__ok);
746     ATF_ADD_TEST_CASE(tcs, column_int__overflow);
747     ATF_ADD_TEST_CASE(tcs, column_int64);
748     ATF_ADD_TEST_CASE(tcs, column_text);
749 
750     ATF_ADD_TEST_CASE(tcs, column_bytes__blob);
751     ATF_ADD_TEST_CASE(tcs, column_bytes__text);
752 
753     ATF_ADD_TEST_CASE(tcs, safe_column_blob__ok);
754     ATF_ADD_TEST_CASE(tcs, safe_column_blob__fail);
755     ATF_ADD_TEST_CASE(tcs, safe_column_double__ok);
756     ATF_ADD_TEST_CASE(tcs, safe_column_double__fail);
757     ATF_ADD_TEST_CASE(tcs, safe_column_int__ok);
758     ATF_ADD_TEST_CASE(tcs, safe_column_int__fail);
759     ATF_ADD_TEST_CASE(tcs, safe_column_int64__ok);
760     ATF_ADD_TEST_CASE(tcs, safe_column_int64__fail);
761     ATF_ADD_TEST_CASE(tcs, safe_column_text__ok);
762     ATF_ADD_TEST_CASE(tcs, safe_column_text__fail);
763 
764     ATF_ADD_TEST_CASE(tcs, safe_column_bytes__ok__blob);
765     ATF_ADD_TEST_CASE(tcs, safe_column_bytes__ok__text);
766     ATF_ADD_TEST_CASE(tcs, safe_column_bytes__fail);
767 
768     ATF_ADD_TEST_CASE(tcs, reset);
769 
770     ATF_ADD_TEST_CASE(tcs, bind__blob);
771     ATF_ADD_TEST_CASE(tcs, bind__double);
772     ATF_ADD_TEST_CASE(tcs, bind__int64);
773     ATF_ADD_TEST_CASE(tcs, bind__int);
774     ATF_ADD_TEST_CASE(tcs, bind__null);
775     ATF_ADD_TEST_CASE(tcs, bind__text);
776     ATF_ADD_TEST_CASE(tcs, bind__text__transient);
777     ATF_ADD_TEST_CASE(tcs, bind__by_name);
778 
779     ATF_ADD_TEST_CASE(tcs, bind_parameter_count);
780     ATF_ADD_TEST_CASE(tcs, bind_parameter_index);
781     ATF_ADD_TEST_CASE(tcs, bind_parameter_name);
782 
783     ATF_ADD_TEST_CASE(tcs, clear_bindings);
784 }
785