blob: d3ae6873671bf3ea612d3250203d268a41bfc2cc [file] [log] [blame]
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento49f3a5f2017-09-23 01:36:33 -04002/*
Davide Pesavento11904062022-04-14 22:33:28 -04003 * Copyright (c) 2014-2022, Regents of the University of California.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07004 *
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 Afanasyevb0c78ea2014-04-15 18:12:04 -070018 */
19
Weiqi Shif0330d52014-07-09 10:54:27 -070020#include "storage/sqlite-storage.hpp"
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070021
Shuo Chenca329182014-03-19 18:05:18 -070022#include "../sqlite-fixture.hpp"
23#include "../dataset-fixtures.hpp"
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070024
25#include <boost/test/unit_test.hpp>
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040026#include <random>
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070027
Davide Pesavento11904062022-04-14 22:33:28 -040028namespace repo::tests {
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070029
Weiqi Shif0330d52014-07-09 10:54:27 -070030BOOST_AUTO_TEST_SUITE(SqliteStorage)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070031
32template<class Dataset>
33class Fixture : public SqliteFixture, public Dataset
34{
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070035public:
weijia yuan3aa8d2b2018-03-06 15:35:57 -080036 std::map<Name, std::shared_ptr<Data>> nameToDataMap;
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070037};
38
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070039BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertReadDelete, T, CommonDatasets, Fixture<T>)
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070040{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080041 std::vector<Name> names;
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070042
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070043 // Insert
Davide Pesavento11904062022-04-14 22:33:28 -040044 for (auto i = this->data.begin(); i != this->data.end(); ++i) {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080045 Name name = Name();
46 this->handle->insert(**i);
47 name = (*i)->getFullName();
48 this->nameToDataMap.insert(std::make_pair(name, *i));
49 names.push_back(name);
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040050 }
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050051 BOOST_CHECK_EQUAL(this->handle->size(), static_cast<int64_t>(this->data.size()));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070052
Davide Pesavento49f3a5f2017-09-23 01:36:33 -040053 std::mt19937 rng{std::random_device{}()};
weijia yuan3aa8d2b2018-03-06 15:35:57 -080054 std::shuffle(names.begin(), names.end(), rng);
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070055
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070056 // Read (all items should exist)
weijia yuan3aa8d2b2018-03-06 15:35:57 -080057 for (auto i = names.begin(); i != names.end(); ++i) {
58 std::shared_ptr<Data> retrievedData = this->handle->read(*i);
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070059
weijia yuan3aa8d2b2018-03-06 15:35:57 -080060 BOOST_REQUIRE(this->nameToDataMap.count(*i) > 0);
61 BOOST_CHECK_EQUAL(*this->nameToDataMap[*i], *retrievedData);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070062 }
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050063 BOOST_CHECK_EQUAL(this->handle->size(), static_cast<int64_t>(this->data.size()));
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070064
65 // Delete
weijia yuan3aa8d2b2018-03-06 15:35:57 -080066 for (auto i = names.begin(); i != names.end(); ++i) {
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070067 BOOST_CHECK_EQUAL(this->handle->erase(*i), true);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070068 }
69
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070070 BOOST_CHECK_EQUAL(this->handle->size(), 0);
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070071}
72
73BOOST_AUTO_TEST_SUITE_END()
74
Davide Pesavento11904062022-04-14 22:33:28 -040075} // namespace repo::tests