Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/sqlite3-statement.hpp" |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 26 | #include <cstring> |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 27 | #include <filesystem> |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 28 | #include <sqlite3.h> |
| 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::tests { |
| 31 | |
| 32 | using ndn::util::Sqlite3Statement; |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 34 | class Sqlite3DbFixture |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 35 | { |
| 36 | public: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 37 | Sqlite3DbFixture() |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 38 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 39 | std::filesystem::create_directories(m_path); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 40 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 41 | int result = sqlite3_open_v2((m_path / "sqlite3-statement.db").c_str(), &db, |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 42 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 43 | #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING |
| 44 | "unix-dotfile" |
| 45 | #else |
| 46 | nullptr |
| 47 | #endif |
| 48 | ); |
| 49 | |
| 50 | if (result != SQLITE_OK) { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 51 | BOOST_FAIL("Sqlite3 database cannot be opened/created: " + m_path.native()); |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 55 | ~Sqlite3DbFixture() |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 56 | { |
| 57 | sqlite3_close(db); |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 58 | std::filesystem::remove_all(m_path); |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 61 | protected: |
| 62 | sqlite3* db = nullptr; |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 63 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 64 | private: |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 65 | const std::filesystem::path m_path{UNIT_TESTS_TMPDIR}; |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 68 | BOOST_AUTO_TEST_SUITE(Util) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 69 | BOOST_FIXTURE_TEST_SUITE(TestSqlite3Statement, Sqlite3DbFixture) |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 70 | |
| 71 | BOOST_AUTO_TEST_CASE(Basic) |
| 72 | { |
| 73 | // create table |
| 74 | BOOST_CHECK_NO_THROW(Sqlite3Statement(db, "CREATE TABLE test (t1 int, t2 text)").step()); |
| 75 | |
| 76 | // insert data into table |
| 77 | BOOST_CHECK_NO_THROW(Sqlite3Statement(db, "INSERT INTO test VALUES (1, 'test1')").step()); |
| 78 | |
| 79 | { |
| 80 | Sqlite3Statement stmt(db, "INSERT INTO test VALUES (2, ?)"); |
| 81 | stmt.bind(1, "test2", std::strlen("test2"), SQLITE_STATIC); |
| 82 | stmt.step(); |
| 83 | } |
| 84 | |
| 85 | { |
| 86 | Sqlite3Statement stmt(db, "INSERT INTO test VALUES (3, ?)"); |
| 87 | stmt.bind(1, "test3", SQLITE_TRANSIENT); |
| 88 | stmt.step(); |
| 89 | } |
| 90 | |
| 91 | Block block(100); |
| 92 | block.encode(); |
| 93 | { |
| 94 | Sqlite3Statement stmt(db, "INSERT INTO test VALUES (4, ?)"); |
| 95 | stmt.bind(1, block, SQLITE_STATIC); |
| 96 | stmt.step(); |
| 97 | } |
| 98 | |
| 99 | { |
| 100 | Sqlite3Statement stmt(db, "INSERT INTO test VALUES (5, ?)"); |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 101 | stmt.bind(1, reinterpret_cast<const void*>(block.data()), block.size(), SQLITE_STATIC); |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 102 | stmt.step(); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | Sqlite3Statement stmt(db, "INSERT INTO test VALUES (?, ?)"); |
| 107 | stmt.bind(1, 6); |
| 108 | stmt.bind(2, "test", SQLITE_TRANSIENT); |
| 109 | stmt.step(); |
| 110 | } |
| 111 | |
| 112 | // check content of the table |
| 113 | |
| 114 | { |
| 115 | Sqlite3Statement stmt(db, "SELECT count(*) FROM test"); |
| 116 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 117 | BOOST_CHECK_EQUAL(stmt.getInt(0), 6); |
| 118 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_DONE); |
| 119 | } |
| 120 | |
| 121 | { |
| 122 | Sqlite3Statement stmt(db, "SELECT t1, t2 FROM test ORDER BY t1"); |
| 123 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 124 | BOOST_CHECK_EQUAL(stmt.getInt(0), 1); |
| 125 | BOOST_CHECK_EQUAL(stmt.getString(1), "test1"); |
| 126 | |
| 127 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 128 | BOOST_CHECK_EQUAL(stmt.getInt(0), 2); |
| 129 | BOOST_CHECK_EQUAL(stmt.getString(1), "test2"); |
| 130 | |
| 131 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 132 | BOOST_CHECK_EQUAL(stmt.getInt(0), 3); |
| 133 | BOOST_CHECK_EQUAL(stmt.getString(1), "test3"); |
| 134 | |
| 135 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 136 | BOOST_CHECK_EQUAL(stmt.getInt(0), 4); |
| 137 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 138 | Block newBlock = stmt.getBlock(1); |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(newBlock.type(), 100); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(newBlock, block); |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 141 | |
| 142 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 143 | BOOST_CHECK_EQUAL(stmt.getInt(0), 5); |
| 144 | BOOST_CHECK_EQUAL(stmt.getSize(1), block.size()); |
| 145 | BOOST_CHECK_EQUAL_COLLECTIONS(block.begin(), block.end(), |
| 146 | stmt.getBlob(1), stmt.getBlob(1) + stmt.getSize(1)); |
| 147 | |
| 148 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_ROW); |
| 149 | BOOST_CHECK_EQUAL(stmt.step(), SQLITE_DONE); |
| 150 | } |
| 151 | } |
| 152 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 153 | BOOST_AUTO_TEST_SUITE_END() // TestSqlite3Statement |
| 154 | BOOST_AUTO_TEST_SUITE_END() // Util |
Mickey Sweatt | 7f17790 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 155 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 156 | } // namespace ndn::tests |