Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 20 | #include "storage/sqlite-storage.hpp" |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 21 | |
Shuo Chen | ca32918 | 2014-03-19 18:05:18 -0700 | [diff] [blame] | 22 | #include "../sqlite-fixture.hpp" |
| 23 | #include "../dataset-fixtures.hpp" |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 24 | |
| 25 | #include <boost/test/unit_test.hpp> |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 26 | #include <random> |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 27 | |
| 28 | namespace repo { |
| 29 | namespace tests { |
| 30 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(SqliteStorage) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 32 | |
| 33 | template<class Dataset> |
| 34 | class Fixture : public SqliteFixture, public Dataset |
| 35 | { |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 36 | public: |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 37 | std::map<int64_t, shared_ptr<Data>> idToDataMap; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 40 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertReadDelete, T, CommonDatasets, Fixture<T>) |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 41 | { |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 42 | BOOST_TEST_CHECKPOINT(T::getName()); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 44 | std::vector<int64_t> ids; |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 46 | // Insert |
| 47 | for (typename T::DataContainer::iterator i = this->data.begin(); |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 48 | i != this->data.end(); ++i) { |
| 49 | int64_t id = -1; |
| 50 | BOOST_REQUIRE_NO_THROW(id = this->handle->insert(**i)); |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 51 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 52 | this->idToDataMap.insert(std::make_pair(id, *i)); |
| 53 | ids.push_back(id); |
| 54 | } |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(this->handle->size(), static_cast<int64_t>(this->data.size())); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 56 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 57 | std::mt19937 rng{std::random_device{}()}; |
| 58 | std::shuffle(ids.begin(), ids.end(), rng); |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 60 | // Read (all items should exist) |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 61 | for (std::vector<int64_t>::iterator i = ids.begin(); i != ids.end(); ++i) { |
| 62 | shared_ptr<Data> retrievedData = this->handle->read(*i); |
| 63 | |
| 64 | BOOST_REQUIRE(this->idToDataMap.count(*i) > 0); |
| 65 | BOOST_CHECK_EQUAL(*this->idToDataMap[*i], *retrievedData); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 66 | } |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(this->handle->size(), static_cast<int64_t>(this->data.size())); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 68 | |
| 69 | // Delete |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 70 | for (std::vector<int64_t>::iterator i = ids.begin(); i != ids.end(); ++i) { |
| 71 | BOOST_CHECK_EQUAL(this->handle->erase(*i), true); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(this->handle->size(), 0); |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | BOOST_AUTO_TEST_SUITE_END() |
| 78 | |
| 79 | } // namespace tests |
| 80 | } // namespace repo |