summaryrefslogtreecommitdiff
path: root/tests/test_sqlite.cc
blob: a6f56229913701a73fa128c4636992d8709bf113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include <boost/test/unit_test.hpp>

#include <generic/util/sqlite.h>

using namespace aptitude::sqlite;

// Allocates a database in memory for testing purposes.
struct memory_db_fixture
{
  boost::shared_ptr<db> tmpdb;

  memory_db_fixture()
    : tmpdb(db::create(":memory:"))
  {
  }
};

// Allocates a database in memory for testing purposes and populates
// it with some test data.
//
// Creates a table "test" with columns A (primary key), B, C, and
// inserts some values:
//
// A       B            C
// 50      "aardvark"   -5
// 51      "balderdash" -5
// 52      "contusion"  x'5412'
struct test_db_fixture : public memory_db_fixture
{
  test_db_fixture()
  {
    statement::prepare(*tmpdb, "create table test(A integer primary key, B text, C integer)")->exec();
    statement::prepare(*tmpdb, "insert into test (A, B, C) values (50, 'aardvark', -5)")->exec();
    statement::prepare(*tmpdb, "insert into test (A, B, C) values (51, 'balderdash', -5)")->exec();
    statement::prepare(*tmpdb, "insert into test (A, B, C) values (52, 'contusion', X'5412')")->exec();
  }
};

BOOST_AUTO_TEST_CASE(cantOpenDb)
{
  // Test that a failed open throws an exception (don't know how to
  // test that it doesn't leak, which is the other thing we want).
  //
  // Note: This test will fail if the ridiculous name it uses exists;
  // a nice enhancement would be to generate names on the fly with a
  // RNG.

  BOOST_REQUIRE_THROW(db::create("ridiculous-and-not-existing-database-name-foo-12983474yf4yrt1839y4vcf8913bh4fiuv",
				 SQLITE_OPEN_READWRITE),
		      exception);
}

BOOST_FIXTURE_TEST_CASE(prepareStatement, memory_db_fixture)
{
  statement::prepare(*tmpdb, "create table foo(bar int)");
  statement::prepare(*tmpdb, std::string("create table foo(bar int)"));
}

BOOST_FIXTURE_TEST_CASE(prepareStatementFail, memory_db_fixture)
{
  BOOST_REQUIRE_THROW(statement::prepare(*tmpdb, "select * from bar"),
		      exception);
}

// Test that we can create the test DB and do nothing else.
BOOST_FIXTURE_TEST_CASE(testSetupDb, test_db_fixture)
{
}

BOOST_FIXTURE_TEST_CASE(testGetBlob, test_db_fixture)
{
  boost::shared_ptr<statement> stmt =
    statement::prepare(*tmpdb, "select A, B, C from test where A = 52");

  {
    statement::execution ex(*stmt);
    BOOST_REQUIRE(ex.step());

    int len = -1;
    const void *val;

    val = stmt->get_blob(0, len);
    const char * const fiftytwo = "52";
    BOOST_CHECK_EQUAL(len, strlen(fiftytwo));
    BOOST_CHECK_EQUAL_COLLECTIONS(fiftytwo, fiftytwo + strlen(fiftytwo),
				  reinterpret_cast<const char *>(val),
				  reinterpret_cast<const char *>(val) + len);

    val = stmt->get_blob(1, len);
    const char * const contusion = "contusion";
    BOOST_CHECK_EQUAL(len, strlen(contusion));
    BOOST_CHECK_EQUAL_COLLECTIONS(contusion, contusion + strlen(contusion),
				  reinterpret_cast<const char *>(val),
				  reinterpret_cast<const char *>(val) + len);

    val = stmt->get_blob(2, len);
    const char arr[2] = { 0x54, 0x12 };
    BOOST_CHECK_EQUAL(len, sizeof(arr));
    BOOST_CHECK_EQUAL_COLLECTIONS(arr, arr + sizeof(arr),
				  reinterpret_cast<const char *>(val),
				  reinterpret_cast<const char *>(val) + len);

    BOOST_CHECK(!ex.step());
    BOOST_CHECK_THROW(stmt->get_blob(2, len),
		      exception);
  }
}

BOOST_FIXTURE_TEST_CASE(testGetDouble, test_db_fixture)
{
  boost::shared_ptr<statement> stmt =
    statement::prepare(*tmpdb, "select C from test where A = 51");

  {
    statement::execution ex(*stmt);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(stmt->get_double(0), -5);

    BOOST_CHECK(!ex.step());
    BOOST_CHECK_THROW(stmt->get_double(0),
		      exception);
  }
}

BOOST_FIXTURE_TEST_CASE(testGetInt, test_db_fixture)
{
  boost::shared_ptr<statement> stmt =
    statement::prepare(*tmpdb, "select A from test where A <> 51 order by A");

  {
    statement::execution ex(*stmt);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(stmt->get_int(0), 50);

    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(stmt->get_int(0), 52);

    BOOST_CHECK(!ex.step());
    BOOST_CHECK_THROW(stmt->get_int(0),
		      exception);
  }
}

BOOST_FIXTURE_TEST_CASE(testGetInt64, test_db_fixture)
{
  boost::shared_ptr<statement> stmt =
    statement::prepare(*tmpdb, "select A from test where A <> 51 order by A");

  {
    statement::execution ex(*stmt);
    BOOST_REQUIRE(ex.step());
    BOOST_CHECK_EQUAL(stmt->get_int64(0), 50);

    BOOST_REQUIRE(ex.step());
    BOOST_CHECK_EQUAL(stmt->get_int64(0), 52);

    BOOST_CHECK(!ex.step());
    BOOST_CHECK_THROW(stmt->get_int64(0),
		      exception);
  }
}

BOOST_FIXTURE_TEST_CASE(testGetString, test_db_fixture)
{
  boost::shared_ptr<statement> stmt =
    statement::prepare(*tmpdb, "select B from test where C = -5 order by A");

  {
    statement::execution ex(*stmt);
    BOOST_REQUIRE(ex.step());
    BOOST_CHECK_EQUAL(stmt->get_string(0), "aardvark");

    BOOST_REQUIRE(ex.step());
    BOOST_CHECK_EQUAL(stmt->get_string(0), "balderdash");

    BOOST_CHECK(!ex.step());
    BOOST_CHECK_THROW(stmt->get_string(0),
		      exception);
  }
}

BOOST_FIXTURE_TEST_CASE(testGetCachedStatement, memory_db_fixture)
{
  tmpdb->set_statement_cache_limit(2);
  db::statement_proxy p1(tmpdb->get_cached_statement("create table foo(bar int)"));
  db::statement_proxy p2(tmpdb->get_cached_statement("create table foo(bar int)"));
  p2.reset();
  p1.reset();

  db::statement_proxy p3(tmpdb->get_cached_statement("create table foo(bar int)"));
  db::statement_proxy p4(tmpdb->get_cached_statement("create table bar(foo int)"));

  // Test that statements are being reused.
  statement * const stmt1(&*p4);

  p3.reset();
  p4.reset();

  db::statement_proxy p5(tmpdb->get_cached_statement("create table bar(foo int)"));

  statement * const stmt2(&*p5);

  BOOST_CHECK_EQUAL(stmt1, stmt2);

  // Use the statements for some trivial operations.
  db::statement_proxy p6(tmpdb->get_cached_statement("create table foo(bar int)"));
  p6->exec();

  db::statement_proxy p7(tmpdb->get_cached_statement("insert into foo (bar) values (5)"));
  db::statement_proxy p8(tmpdb->get_cached_statement("select bar from foo"));

  p7->exec();

  {
    statement::execution ex(*p8);
    BOOST_REQUIRE(ex.step());
    BOOST_CHECK_EQUAL(p8->get_int(0), 5);
    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(getCachedStatementFail, memory_db_fixture)
{
  BOOST_REQUIRE_THROW(statement::prepare(*tmpdb, "select * from bar"),
		      exception);
  db::statement_proxy p1(tmpdb->get_cached_statement("create table foo(bar int)"));
  db::statement_proxy p2(tmpdb->get_cached_statement("create table foo(bar int)"));
}

struct parameter_binding_test : public test_db_fixture
{
  boost::shared_ptr<statement> get_C_statement;
  boost::shared_ptr<statement> put_statement;
  static const int test_A = 10;

  parameter_binding_test()
  {
    get_C_statement = statement::prepare(*tmpdb, "select C from test where A = ?");
    put_statement = statement::prepare(*tmpdb, "insert into test (A, B, C) values (?, NULL, ?)");
  }
};

BOOST_FIXTURE_TEST_CASE(testBindBlob, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  const char data[8] = { 0x54, 0x10, 0x20, 0x67,
			 0xd9, 0x45, 0xbd, 0x1a };
  BOOST_CHECK_THROW(put_statement->bind_blob(3, data, sizeof(data)),
		    exception);
  put_statement->bind_blob(2, data, sizeof(data));
  put_statement->exec();

  // Test that the data is still there.
  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_BLOB);

    int blob_bytes;
    const void *blob(get_C_statement->get_blob(0, blob_bytes));
    BOOST_CHECK_EQUAL(blob_bytes, sizeof(data));
    BOOST_CHECK_EQUAL_COLLECTIONS(reinterpret_cast<const char *>(blob),
				  reinterpret_cast<const char *>(blob) + blob_bytes,
				  data, data + sizeof(data));

    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(testBindDouble, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  const double data = 9876543.123;

  BOOST_CHECK_THROW(put_statement->bind_double(3, data),
		    exception);
  put_statement->bind_double(2, data);
  put_statement->exec();

  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_FLOAT);

    BOOST_CHECK_EQUAL(get_C_statement->get_double(0), data);

    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(testBindInt, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  const int data = 0x948291ff;

  BOOST_CHECK_THROW(put_statement->bind_int(3, data),
		    exception);
  put_statement->bind_int(2, data);
  put_statement->exec();

  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_INTEGER);

    BOOST_CHECK_EQUAL(get_C_statement->get_int(0), data);

    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(testBindInt64, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  const sqlite_int64 data = 0x948291ff01234567LL;

  BOOST_CHECK_THROW(put_statement->bind_int64(3, data),
		    exception);
  put_statement->bind_int64(2, data);
  put_statement->exec();

  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_INTEGER);

    BOOST_CHECK_EQUAL(get_C_statement->get_int64(0), data);

    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(testBindNull, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  BOOST_CHECK_THROW(put_statement->bind_null(3),
		    exception);
  put_statement->bind_null(2);
  put_statement->exec();

  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_NULL);

    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(testBindString, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  const std::string data("abracadabra");

  BOOST_CHECK_THROW(put_statement->bind_string(3, data),
		    exception);
  put_statement->bind_string(2, data);
  put_statement->exec();

  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_TEXT);

    BOOST_CHECK_EQUAL(get_C_statement->get_string(0), data);

    BOOST_CHECK(!ex.step());
  }
}

BOOST_FIXTURE_TEST_CASE(testBindZeroBlob, parameter_binding_test)
{
  put_statement->bind_int(1, test_A);

  const char testBlobData[7] = { 0, 0, 0, 0, 0, 0, 0 };

  BOOST_CHECK_THROW(put_statement->bind_zeroblob(3, sizeof(testBlobData)),
		    exception);
  put_statement->bind_zeroblob(2, sizeof(testBlobData));
  put_statement->exec();

  get_C_statement->bind_int(1, test_A);
  {
    statement::execution ex(*get_C_statement);
    BOOST_REQUIRE(ex.step());

    BOOST_CHECK_EQUAL(get_C_statement->get_column_type(0), SQLITE_BLOB);

    int blob_bytes;
    const void *blob(get_C_statement->get_blob(0, blob_bytes));
    BOOST_CHECK_EQUAL(blob_bytes, sizeof(testBlobData));
    BOOST_CHECK_EQUAL_COLLECTIONS(reinterpret_cast<const char *>(blob),
				  reinterpret_cast<const char *>(blob) + blob_bytes,
				  testBlobData, testBlobData + sizeof(testBlobData));

    BOOST_CHECK(!ex.step());
  }
}

struct test_blob_fixture : public test_db_fixture
{
  sqlite_int64 blob_rowid;

  test_blob_fixture()
  {
    boost::shared_ptr<statement> s(statement::prepare(*tmpdb, "select ROWID from test where A = 52"));

    {
      statement::execution ex(*s);
      BOOST_REQUIRE(ex.step());
      blob_rowid = s->get_int64(0);
      BOOST_CHECK(!ex.step());
    }
  }
};

BOOST_FIXTURE_TEST_CASE(testOpenBlob, test_blob_fixture)
{
  blob::open(*tmpdb,
	     "main",
	     "test",
	     "C",
	     blob_rowid);

  BOOST_CHECK_THROW(blob::open(*tmpdb,
			       "nosuchdb",
			       "test",
			       "C",
			       blob_rowid),
		    exception);
  BOOST_CHECK_THROW(blob::open(*tmpdb,
			       "main",
			       "nosuchtable",
			       "C",
			       blob_rowid),
		    exception);
  BOOST_CHECK_THROW(blob::open(*tmpdb,
			       "main",
			       "test",
			       "NOSUCHCOLUMN",
			       blob_rowid),
		    exception);
  // This rowid is guaranteed not to map to anything because the rowid
  // is always equal to the primary key.
  BOOST_CHECK_THROW(blob::open(*tmpdb,
			       "main",
			       "test",
			       "C",
			       100),
		    exception);
}

BOOST_FIXTURE_TEST_CASE(testBlobSize, test_blob_fixture)
{
  boost::shared_ptr<blob> b = blob::open(*tmpdb,
					 "main",
					 "test",
					 "C",
					 blob_rowid);

  BOOST_CHECK_EQUAL(b->size(), 2);
}

BOOST_FIXTURE_TEST_CASE(testBlobRead, test_blob_fixture)
{
  boost::shared_ptr<blob> b = blob::open(*tmpdb,
					 "main",
					 "test",
					 "C",
					 blob_rowid);

  char contents[3];
  const char expected[] = { 0x54, 0x12 };
  b->read(0, contents, 2);
  BOOST_CHECK_EQUAL_COLLECTIONS(contents, contents + 2,
				expected, expected + 2);
  b->read(0, contents, 2);
  BOOST_CHECK_EQUAL_COLLECTIONS(contents, contents + 2,
				expected, expected + 2);

  BOOST_CHECK_THROW(b->read(0, contents, 3), exception);
}

BOOST_FIXTURE_TEST_CASE(testBlobWrite, test_blob_fixture)
{
  const char data[2] = { 0x54, 0x11 };
  {
    boost::shared_ptr<blob> b = blob::open(*tmpdb,
					   "main",
					   "test",
					   "C",
					   blob_rowid);

    b->write(1, data + 1, 1);
    BOOST_CHECK_THROW(b->write(1, data, 2), exception);
  }

  boost::shared_ptr<statement> stmt =
    statement::prepare(*tmpdb, "select C from test where rowid = ?");
  stmt->bind_int64(1, blob_rowid);
  {
    statement::execution ex(*stmt);
    BOOST_REQUIRE(ex.step());

    int len = -1;
    const void *val = stmt->get_blob(0, len);
    BOOST_CHECK_EQUAL(len, sizeof(data));
    BOOST_CHECK_EQUAL_COLLECTIONS(data, data + sizeof(data),
				  reinterpret_cast<const char *>(val),
				  reinterpret_cast<const char *>(val) + len);

    BOOST_CHECK(!ex.step());
  }
}