diff options
author | Daniel Burrows <dburrows@debian.org> | 2009-07-26 21:24:47 -0700 |
---|---|---|
committer | Daniel Burrows <dburrows@debian.org> | 2009-07-26 21:24:47 -0700 |
commit | 7ce980960ddbab3c5dd1760d13ad812048c48c2e (patch) | |
tree | c99be78511f54797118fb03ac424c594fc411f94 /tests/test_sqlite.cc | |
parent | 4c6a886c9fe665869f73a7e1479cfb65f471dc18 (diff) | |
download | aptitude-7ce980960ddbab3c5dd1760d13ad812048c48c2e.tar.gz |
Wrap enough functionality in the statement class to allow the user to execute statements and retrieve results.
This explicitly doesn't try to deal with the inherent non-threadsafety
of the sqlite statement type. That will only come up if you're trying
to reuse statements, presumably for performance, and once we're doing
that, the code that mediates reuse should ensure that only one client
has its hands on a given statement at once. (even non-threaded reuse
is dangerous!)
Diffstat (limited to 'tests/test_sqlite.cc')
-rw-r--r-- | tests/test_sqlite.cc | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/tests/test_sqlite.cc b/tests/test_sqlite.cc index 2fe6ac6c..598bd194 100644 --- a/tests/test_sqlite.cc +++ b/tests/test_sqlite.cc @@ -15,6 +15,27 @@ struct memory_db_fixture } }; +// 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 @@ -40,3 +61,109 @@ 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"); + + BOOST_REQUIRE(stmt->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(!stmt->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"); + + BOOST_REQUIRE(stmt->step()); + + BOOST_CHECK_EQUAL(stmt->get_double(0), -5); + + BOOST_CHECK(!stmt->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"); + + BOOST_REQUIRE(stmt->step()); + + BOOST_CHECK_EQUAL(stmt->get_int(0), 50); + + BOOST_REQUIRE(stmt->step()); + + BOOST_CHECK_EQUAL(stmt->get_int(0), 52); + + BOOST_CHECK(!stmt->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"); + + BOOST_REQUIRE(stmt->step()); + BOOST_CHECK_EQUAL(stmt->get_int64(0), 50); + + BOOST_REQUIRE(stmt->step()); + BOOST_CHECK_EQUAL(stmt->get_int64(0), 52); + + BOOST_CHECK(!stmt->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"); + + BOOST_REQUIRE(stmt->step()); + BOOST_CHECK_EQUAL(stmt->get_string(0), "aardvark"); + + BOOST_REQUIRE(stmt->step()); + BOOST_CHECK_EQUAL(stmt->get_string(0), "balderdash"); + + BOOST_CHECK(!stmt->step()); + BOOST_CHECK_THROW(stmt->get_string(0), + exception); +} |