FOSSology  4.7.1
Open Source License Compliance by Open Source Software
test_database_handler.cc
Go to the documentation of this file.
1 /*
2  SPDX-License-Identifier: GPL-2.0-only
3  Author: Dietmar Helmut Leher <helmut.leher.ext@vaillant-group.com>
4  SPDX-FileCopyrightText: © 2026 Vaillant GmbH
5 */
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 
19 
20 #include <map>
21 #include <vector>
22 
23 class ReuserDatabaseHandlerTest : public CPPUNIT_NS::TestFixture
24 {
25  CPPUNIT_TEST_SUITE(ReuserDatabaseHandlerTest);
32  CPPUNIT_TEST_SUITE_END();
33 
34 protected:
43  {
45 
46  // Simulate: two rows for pfile 101 (highest id first), one row for pfile 50.
47  handler.onGetClearingDecisionMapByPfile =
48  [](int /*uploadId*/, int /*groupId*/) -> std::map<int, int>
49  {
50  return {{101, 20}, {50, 10}};
51  };
52 
53  auto result = handler.getClearingDecisionMapByPfile(1, 1);
54 
55  CPPUNIT_ASSERT_EQUAL(2u, static_cast<unsigned>(result.size()));
56  CPPUNIT_ASSERT(result.find(101) != result.end());
57  CPPUNIT_ASSERT_EQUAL(20, result.at(101));
58  CPPUNIT_ASSERT(result.find(50) != result.end());
59  CPPUNIT_ASSERT_EQUAL(10, result.at(50));
60  }
61 
69  {
71  // Callback not set -> default returns empty map.
72  auto result = handler.getClearingDecisionMapByPfile(99, 1);
73  CPPUNIT_ASSERT(result.empty());
74  }
75 
84  {
86 
87  handler.onGetUploadTreePksForPfiles =
88  [](int /*uploadId*/,
89  const std::vector<int>& /*pfileIds*/)
90  -> std::map<int, std::vector<int>>
91  {
92  return {{101, {1001, 1002}}, {50, {1200}}};
93  };
94 
95  std::vector<int> pfiles = {101, 50};
96  auto result = handler.getUploadTreePksForPfiles(1, pfiles);
97 
98  CPPUNIT_ASSERT_EQUAL(2u, static_cast<unsigned>(result.size()));
99 
100  CPPUNIT_ASSERT(result.find(101) != result.end());
101  CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(result.at(101).size()));
102  CPPUNIT_ASSERT_EQUAL(1001, result.at(101)[0]);
103  CPPUNIT_ASSERT_EQUAL(1002, result.at(101)[1]);
104 
105  CPPUNIT_ASSERT(result.find(50) != result.end());
106  CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(result.at(50).size()));
107  CPPUNIT_ASSERT_EQUAL(1200, result.at(50)[0]);
108  }
109 
117  {
119  // Callback not set and empty input -> must return empty map.
120  auto result = handler.getUploadTreePksForPfiles(1, {});
121  CPPUNIT_ASSERT(result.empty());
122  }
123 
131  {
133 
134  handler.onGetReusedUploads =
135  [](int /*uploadId*/, int /*groupId*/) -> std::vector<ReuseTriple>
136  {
137  return {{10, 1, REUSE_MAIN}, {20, 2, REUSE_ENHANCED | REUSE_COPYRIGHT}};
138  };
139 
140  auto result = handler.getReusedUploads(5, 1);
141 
142  CPPUNIT_ASSERT_EQUAL(2u, static_cast<unsigned>(result.size()));
143  CPPUNIT_ASSERT_EQUAL(10, result[0].reusedUploadId);
144  CPPUNIT_ASSERT_EQUAL(REUSE_MAIN, result[0].reuseMode);
145  CPPUNIT_ASSERT_EQUAL(20, result[1].reusedUploadId);
146  CPPUNIT_ASSERT_EQUAL(REUSE_ENHANCED | REUSE_COPYRIGHT,
147  result[1].reuseMode);
148  }
149 
158  {
160 
161  handler.onGetParentItemBounds =
162  [](int /*uploadId*/, ItemTreeBounds& out) -> bool
163  {
164  out = {500, "uploadtree", 7, 1, 42};
165  return true;
166  };
167 
168  ItemTreeBounds bounds{};
169  bool ok = handler.getParentItemBounds(7, bounds);
170 
171  CPPUNIT_ASSERT(ok);
172  CPPUNIT_ASSERT_EQUAL(500, bounds.uploadtree_pk);
173  CPPUNIT_ASSERT_EQUAL(std::string("uploadtree"), bounds.uploadTreeTableName);
174  CPPUNIT_ASSERT_EQUAL(7, bounds.upload_fk);
175  CPPUNIT_ASSERT_EQUAL(1, bounds.lft);
176  CPPUNIT_ASSERT_EQUAL(42, bounds.rgt);
177  }
178 };
179 
180 CPPUNIT_TEST_SUITE_REGISTRATION(ReuserDatabaseHandlerTest);
Mock ReuserDatabaseHandler for unit tests.
Test double for ReuserDatabaseHandler.
bool getParentItemBounds(int uploadId, ItemTreeBounds &out) override
Fetch the parent item bounds for a given upload.
std::map< int, std::vector< int > > getUploadTreePksForPfiles(int uploadId, const std::vector< int > &pfileIds) override
For a set of pfile ids, return a map pfile_fk to [uploadtree_pk].
std::vector< ReuseTriple > getReusedUploads(int uploadId, int groupId) override
Return the list of uploads that should be reused for uploadId.
std::map< int, int > getClearingDecisionMapByPfile(int uploadId, int groupId) override
Build a pfile_fk to clearing_decision_pk map for uploadId.
void testGetClearingDecisionMapReturnsFirstDecisionPerPfile()
getClearingDecisionMapByPfile returns one entry per distinct pfile_fk.
void testGetClearingDecisionMapEmptyOnNoData()
getClearingDecisionMapByPfile returns an empty map when no callback is set.
void testGetParentItemBoundsPopulatesStruct()
getParentItemBounds populates all ItemTreeBounds fields on success.
void testGetReusedUploadsReturnedInOrder()
getReusedUploads preserves the order and content of ReuseTriple entries.
void testGetUploadTreePksEmptyOnEmptyInput()
getUploadTreePksForPfiles returns an empty map for an empty pfile list.
void testGetUploadTreePksGroupedByPfile()
getUploadTreePksForPfiles groups uploadtree_pk values by pfile_fk.
Bounds of an item within an uploadtree table.
Definition: ReuserTypes.hpp:14