File indexing completed on 2024-05-19 05:42:11
0001 // ct_lvtmdb_soci_helper.t.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <ct_lvtmdb_namespaceobject.h> 0021 #include <ct_lvtmdb_objectstore.h> 0022 #include <ct_lvtmdb_soci_helper.h> 0023 #include <ct_lvtmdb_soci_writer.h> 0024 0025 #include <filesystem> 0026 #include <iostream> 0027 #include <unordered_set> 0028 0029 #include <catch2-local-includes.h> 0030 0031 namespace fs = std::filesystem; 0032 0033 using namespace Codethink::lvtmdb; 0034 0035 struct DBTestResult { 0036 struct HashFunc { 0037 size_t operator()(const DBTestResult& data) const 0038 { 0039 return data.id; 0040 } 0041 }; 0042 0043 int id; 0044 int version; 0045 long long parentId; 0046 std::string name; 0047 std::string qualifiedName; 0048 0049 bool operator==(const DBTestResult& other) const 0050 { 0051 return this->id == other.id && this->version == other.version && this->parentId == other.parentId 0052 && this->name == other.name && this->qualifiedName == other.qualifiedName; 0053 } 0054 }; 0055 0056 struct RawDBColsHashFunc2 { 0057 size_t operator()(const std::any& data) const 0058 { 0059 return std::any_cast<int>(data); 0060 } 0061 }; 0062 0063 TEST_CASE("Run single query helper") 0064 { 0065 fs::path project_path = fs::temp_directory_path(); 0066 auto tmpDBPath = project_path / "database.db"; 0067 0068 { 0069 ObjectStore store1; 0070 NamespaceObject *store1_parent = nullptr; 0071 NamespaceObject *store1_b = nullptr; 0072 NamespaceObject *store1_c = nullptr; 0073 store1.withRWLock([&] { 0074 store1_parent = store1.getOrAddNamespace("parent", "parent", nullptr); 0075 store1_b = store1.getOrAddNamespace("b", "b", store1_parent); 0076 store1_c = store1.getOrAddNamespace("c", "c", store1_parent); 0077 CHECK(store1.namespaces().size() == 3); 0078 }); 0079 std::error_code ec; 0080 std::filesystem::remove(tmpDBPath, ec); 0081 SociWriter writer; 0082 CHECK(writer.createOrOpen(tmpDBPath.string())); 0083 store1.writeToDatabase(writer); 0084 } 0085 0086 soci::session db; 0087 db.open(*soci::factory_sqlite3(), tmpDBPath.string()); 0088 auto result = SociHelper::runSingleQuery(db, "SELECT * FROM namespace_declaration"); 0089 auto obtainedData = std::unordered_set<DBTestResult, DBTestResult::HashFunc>{}; 0090 for (auto&& rowdata : result) { 0091 obtainedData.insert({std::any_cast<int>(rowdata[0].value()), 0092 std::any_cast<int>(rowdata[1].value()), 0093 rowdata[2].has_value() ? std::any_cast<long long>(rowdata[2].value()) : -1, 0094 std::any_cast<std::string>(rowdata[3].value()), 0095 std::any_cast<std::string>(rowdata[4].value())}); 0096 } 0097 0098 auto expectedData = std::unordered_set<DBTestResult, DBTestResult::HashFunc>{{1, 0, -1, "parent", "parent"}, 0099 {2, 0, 1, "b", "b"}, 0100 {3, 0, 1, "c", "c"}}; 0101 REQUIRE(obtainedData == expectedData); 0102 }