blob: a7fa29a761681d29be4cb62fa5150ba9263ee6bc [file] [log] [blame]
Mickey Sweatt7f177902015-06-10 17:20:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/sqlite3-statement.hpp"
23
24#include <boost/filesystem.hpp>
25#include "boost-test.hpp"
26
27#include <sqlite3.h>
28
29namespace ndn {
30namespace util {
31namespace tests {
32
33class Sqlite3StatementTestFixture
34{
35public:
36 Sqlite3StatementTestFixture()
37 : m_path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH))
38 {
39 boost::filesystem::create_directories(m_path);
40 int result = sqlite3_open_v2((m_path / "sqlite3-statement.db").string().c_str(), &db,
41 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
42#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
43 "unix-dotfile"
44#else
45 nullptr
46#endif
47 );
48
49 if (result != SQLITE_OK) {
50 BOOST_FAIL("Sqlite3 database cannot be opened/created: " + m_path.string());
51 }
52 }
53
54 ~Sqlite3StatementTestFixture()
55 {
56 sqlite3_close(db);
57 boost::filesystem::remove_all(m_path);
58 }
59
60private:
61 boost::filesystem::path m_path;
62
63public:
64 sqlite3* db;
65};
66
67BOOST_FIXTURE_TEST_SUITE(UtilSqlite3Statement, Sqlite3StatementTestFixture)
68
69BOOST_AUTO_TEST_CASE(Basic)
70{
71 // create table
72 BOOST_CHECK_NO_THROW(Sqlite3Statement(db, "CREATE TABLE test (t1 int, t2 text)").step());
73
74 // insert data into table
75 BOOST_CHECK_NO_THROW(Sqlite3Statement(db, "INSERT INTO test VALUES (1, 'test1')").step());
76
77 {
78 Sqlite3Statement stmt(db, "INSERT INTO test VALUES (2, ?)");
79 stmt.bind(1, "test2", std::strlen("test2"), SQLITE_STATIC);
80 stmt.step();
81 }
82
83 {
84 Sqlite3Statement stmt(db, "INSERT INTO test VALUES (3, ?)");
85 stmt.bind(1, "test3", SQLITE_TRANSIENT);
86 stmt.step();
87 }
88
89 Block block(100);
90 block.encode();
91 {
92 Sqlite3Statement stmt(db, "INSERT INTO test VALUES (4, ?)");
93 stmt.bind(1, block, SQLITE_STATIC);
94 stmt.step();
95 }
96
97 {
98 Sqlite3Statement stmt(db, "INSERT INTO test VALUES (5, ?)");
99 stmt.bind(1, reinterpret_cast<const void*>(block.wire()), block.size(), SQLITE_STATIC);
100 stmt.step();
101 }
102
103 {
104 Sqlite3Statement stmt(db, "INSERT INTO test VALUES (?, ?)");
105 stmt.bind(1, 6);
106 stmt.bind(2, "test", SQLITE_TRANSIENT);
107 stmt.step();
108 }
109
110 // check content of the table
111
112 {
113 Sqlite3Statement stmt(db, "SELECT count(*) FROM test");
114 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
115 BOOST_CHECK_EQUAL(stmt.getInt(0), 6);
116 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_DONE);
117 }
118
119 {
120 Sqlite3Statement stmt(db, "SELECT t1, t2 FROM test ORDER BY t1");
121 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
122 BOOST_CHECK_EQUAL(stmt.getInt(0), 1);
123 BOOST_CHECK_EQUAL(stmt.getString(1), "test1");
124
125 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
126 BOOST_CHECK_EQUAL(stmt.getInt(0), 2);
127 BOOST_CHECK_EQUAL(stmt.getString(1), "test2");
128
129 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
130 BOOST_CHECK_EQUAL(stmt.getInt(0), 3);
131 BOOST_CHECK_EQUAL(stmt.getString(1), "test3");
132
133 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
134 BOOST_CHECK_EQUAL(stmt.getInt(0), 4);
135
136 Block newBlock;
137 BOOST_CHECK_NO_THROW(newBlock = stmt.getBlock(1));
138 BOOST_CHECK_EQUAL(newBlock.type(), 100);
139 BOOST_CHECK(newBlock == block);
140
141 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
142 BOOST_CHECK_EQUAL(stmt.getInt(0), 5);
143 BOOST_CHECK_EQUAL(stmt.getSize(1), block.size());
144 BOOST_CHECK_EQUAL_COLLECTIONS(block.begin(), block.end(),
145 stmt.getBlob(1), stmt.getBlob(1) + stmt.getSize(1));
146
147 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW);
148 BOOST_CHECK_EQUAL(stmt.step(), SQLITE_DONE);
149 }
150}
151
152BOOST_AUTO_TEST_SUITE_END()
153
154} // namespace tests
155} // namespace util
156} // namespace ndn