Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS 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 | * NDNS 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 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "db-mgr.hpp" |
| 21 | #include "../boost-test.hpp" |
| 22 | |
| 23 | #include <boost/filesystem.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndns { |
| 27 | namespace tests { |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(DbMgr) |
| 30 | |
| 31 | static const boost::filesystem::path TEST_DATABASE = BUILDDIR "/tests/unit/db-mgr-ndns.db"; |
| 32 | |
| 33 | class DbMgrFixture |
| 34 | { |
| 35 | public: |
| 36 | DbMgrFixture() |
| 37 | : session(TEST_DATABASE.string().c_str()) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | ~DbMgrFixture() |
| 42 | { |
| 43 | session.close(); |
| 44 | boost::filesystem::remove(TEST_DATABASE); |
| 45 | } |
| 46 | |
| 47 | public: |
| 48 | ndns::DbMgr session; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | BOOST_FIXTURE_TEST_CASE(Basic, DbMgrFixture) |
| 53 | { |
| 54 | BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CONNECTED); |
| 55 | |
| 56 | session.close(); |
| 57 | BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CLOSED); |
| 58 | |
| 59 | // reopen |
| 60 | session.open(); |
| 61 | BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CONNECTED); |
| 62 | } |
| 63 | |
| 64 | BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture) |
| 65 | { |
| 66 | Zone zone1; |
| 67 | zone1.setName("/net"); |
| 68 | zone1.setTtl(time::seconds(4600)); |
| 69 | BOOST_CHECK_NO_THROW(session.insert(zone1)); |
| 70 | BOOST_CHECK_GT(zone1.getId(), 0); |
| 71 | |
| 72 | Zone zone2; |
| 73 | zone2.setName("/net"); |
| 74 | session.lookup(zone2); |
| 75 | BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId()); |
| 76 | BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl()); |
| 77 | |
| 78 | BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute |
| 79 | |
| 80 | zone2.setId(0); |
| 81 | BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError); |
| 82 | |
| 83 | BOOST_CHECK_NO_THROW(session.remove(zone1)); |
| 84 | BOOST_CHECK_EQUAL(zone1.getId(), 0); |
| 85 | |
| 86 | // record shouldn't exist at this point |
| 87 | BOOST_CHECK_NO_THROW(session.lookup(zone2)); |
| 88 | BOOST_CHECK_EQUAL(zone2.getId(), 0); |
| 89 | } |
| 90 | |
| 91 | BOOST_AUTO_TEST_SUITE_END() |
| 92 | |
| 93 | } // namespace tests |
| 94 | } // namespace ndns |
| 95 | } // namespace ndn |