29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
33 #include "ReuserState.hpp"
34 #include "ReuserTypes.hpp"
40 int groupId,
int userId)
42 if (!
db.processBulkReuser(uploadId, groupId, userId))
47 auto reusedUploads =
db.getReusedUploads(uploadId, groupId);
48 for (
const auto& triple : reusedUploads)
51 if (!
db.getParentItemBounds(triple.reusedUploadId, bounds))
54 if (triple.reuseMode & REUSE_ENHANCED)
56 if (!
db.processEnhancedUploadReuse(uploadId, triple.reusedUploadId,
57 groupId, triple.reusedGroupId, userId))
62 if (!
db.processUploadReuse(uploadId, triple.reusedUploadId,
63 groupId, triple.reusedGroupId, userId))
67 if (triple.reuseMode & REUSE_MAIN)
68 db.reuseMainLicense(uploadId, groupId, triple.reusedUploadId,
69 triple.reusedGroupId);
71 if (triple.reuseMode & REUSE_CONF)
72 db.reuseConfSettings(uploadId, triple.reusedUploadId);
74 if (triple.reuseMode & REUSE_COPYRIGHT)
75 db.reuseCopyrights(uploadId, triple.reusedUploadId, userId);
108 CPPUNIT_TEST_SUITE_END();
122 bool processUploadCalled =
false;
123 db.onProcessUploadReuse =
124 [&](int, int, int, int, int) ->
bool
125 { processUploadCalled =
true;
return true; };
128 bool ok = runProcess(
db, 1, 3, 2);
130 CPPUNIT_ASSERT_MESSAGE(
"should succeed with no reuse links", ok);
131 CPPUNIT_ASSERT_MESSAGE(
"processUploadReuse must not be called",
132 !processUploadCalled);
146 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
147 {
return {{42, 3, 0}}; };
150 { out = {100,
"uploadtree_a", 42, 1, 100};
return true; };
152 int processCalls = 0;
154 db.onProcessUploadReuse =
155 [&](int, int, int, int, int) ->
bool
156 { ++processCalls;
return true; };
158 bool ok = runProcess(
db, 3, 3, 2);
160 CPPUNIT_ASSERT_MESSAGE(
"should succeed even if source has no decisions", ok);
161 CPPUNIT_ASSERT_EQUAL_MESSAGE(
162 "processUploadReuse must be called exactly once", 1, processCalls);
175 const int uploadId = 3;
176 const int reusedUpload = 2;
177 const int groupId = 3;
178 const int reusedGroupId = 3;
179 const int userId = 2;
181 db.onGetReusedUploads =
182 [&](
int uid,
int gid) -> std::vector<ReuseTriple>
184 CPPUNIT_ASSERT_EQUAL(uploadId, uid);
185 CPPUNIT_ASSERT_EQUAL(groupId, gid);
186 return {{reusedUpload, reusedGroupId, 0 }};
190 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
192 int capturedUpload = -1;
193 int capturedReused = -1;
194 int capturedGroup = -1;
195 int capturedReusedGroup = -1;
196 int capturedUser = -1;
198 db.onProcessUploadReuse =
199 [&](
int u,
int r,
int g,
int rg,
int usr) ->
bool
204 capturedReusedGroup = rg;
209 bool ok = runProcess(
db, uploadId, groupId, userId);
212 CPPUNIT_ASSERT_EQUAL(uploadId, capturedUpload);
213 CPPUNIT_ASSERT_EQUAL(reusedUpload, capturedReused);
214 CPPUNIT_ASSERT_EQUAL(groupId, capturedGroup);
215 CPPUNIT_ASSERT_EQUAL(reusedGroupId, capturedReusedGroup);
216 CPPUNIT_ASSERT_EQUAL(userId, capturedUser);
230 bool standardCalled =
false;
231 bool enhancedCalled =
false;
234 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
235 {
return {{2, 3, 0}}; };
238 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
240 db.onProcessUploadReuse =
241 [&](int, int, int, int, int) ->
bool
242 { standardCalled =
true;
return true; };
244 db.onProcessEnhancedUploadReuse =
245 [&](int, int, int, int, int) ->
bool
246 { enhancedCalled =
true;
return true; };
248 bool ok = runProcess(
db, 3, 3, 2);
251 CPPUNIT_ASSERT_MESSAGE(
"standard path for repo-scoped clearing",
253 CPPUNIT_ASSERT_MESSAGE(
"enhanced path must not be used", !enhancedCalled);
266 bool standardCalled =
false;
267 bool enhancedCalled =
false;
269 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
270 {
return {{2, 3, REUSE_ENHANCED}}; };
273 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
275 db.onProcessUploadReuse =
276 [&](int, int, int, int, int) ->
bool
277 { standardCalled =
true;
return true; };
279 db.onProcessEnhancedUploadReuse =
280 [&](int, int, int, int, int) ->
bool
281 { enhancedCalled =
true;
return true; };
283 bool ok = runProcess(
db, 3, 3, 2);
286 CPPUNIT_ASSERT_MESSAGE(
"enhanced path must be taken", enhancedCalled);
287 CPPUNIT_ASSERT_MESSAGE(
"standard path must not be taken", !standardCalled);
297 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
298 {
return {{2, 3, REUSE_ENHANCED}}; };
301 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
303 db.onProcessEnhancedUploadReuse =
304 [](int, int, int, int, int) ->
bool {
return false; };
306 bool ok = runProcess(
db, 3, 3, 2);
308 CPPUNIT_ASSERT_MESSAGE(
"failure of enhanced reuse must propagate", !ok);
321 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
322 {
return {{10, 3, 0}, {20, 5, 0}}; };
325 { out = {1,
"uploadtree_a", 0, 1, 100};
return true; };
327 std::vector<int> calledWithReused;
328 db.onProcessUploadReuse =
329 [&](int,
int reused, int, int, int) ->
bool
330 { calledWithReused.push_back(reused);
return true; };
332 bool ok = runProcess(
db, 1, 3, 2);
335 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"both reuse links must be processed",
336 2u,
static_cast<unsigned>(calledWithReused.size()));
337 CPPUNIT_ASSERT_EQUAL(10, calledWithReused[0]);
338 CPPUNIT_ASSERT_EQUAL(20, calledWithReused[1]);
350 bool mainCalled =
false;
351 bool confCalled =
false;
352 bool copyrightCalled =
false;
354 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
355 {
return {{2, 3, REUSE_MAIN}}; };
358 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
360 db.onProcessUploadReuse =
361 [](int, int, int, int, int) ->
bool {
return true; };
363 db.onReuseMainLicense =
364 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
366 db.onReuseConfSettings =
367 [&](int, int) ->
bool { confCalled =
true;
return true; };
369 db.onReuseCopyrights =
370 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
372 bool ok = runProcess(
db, 3, 3, 2);
375 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must be called", mainCalled);
376 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must not be called", !confCalled);
377 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must not be called",
390 bool mainCalled =
false;
391 bool confCalled =
false;
392 bool copyrightCalled =
false;
394 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
395 {
return {{2, 3, REUSE_CONF}}; };
398 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
400 db.onProcessUploadReuse =
401 [](int, int, int, int, int) ->
bool {
return true; };
403 db.onReuseMainLicense =
404 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
406 db.onReuseConfSettings =
407 [&](int, int) ->
bool { confCalled =
true;
return true; };
409 db.onReuseCopyrights =
410 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
412 bool ok = runProcess(
db, 3, 3, 2);
415 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must not be called", !mainCalled);
416 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must be called", confCalled);
417 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must not be called",
430 bool mainCalled =
false;
431 bool confCalled =
false;
432 bool copyrightCalled =
false;
434 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
435 {
return {{2, 3, REUSE_COPYRIGHT}}; };
438 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
440 db.onProcessUploadReuse =
441 [](int, int, int, int, int) ->
bool {
return true; };
443 db.onReuseMainLicense =
444 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
446 db.onReuseConfSettings =
447 [&](int, int) ->
bool { confCalled =
true;
return true; };
449 db.onReuseCopyrights =
450 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
452 bool ok = runProcess(
db, 3, 3, 2);
455 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must not be called", !mainCalled);
456 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must not be called", !confCalled);
457 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must be called", copyrightCalled);
470 bool mainCalled =
false;
471 bool confCalled =
false;
472 bool copyrightCalled =
false;
473 bool standardCalled =
false;
475 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
476 {
return {{2, 3, 0 }}; };
479 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
481 db.onProcessUploadReuse =
482 [&](int, int, int, int, int) ->
bool
483 { standardCalled =
true;
return true; };
485 db.onReuseMainLicense =
486 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
488 db.onReuseConfSettings =
489 [&](int, int) ->
bool { confCalled =
true;
return true; };
491 db.onReuseCopyrights =
492 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
494 bool ok = runProcess(
db, 3, 3, 2);
497 CPPUNIT_ASSERT_MESSAGE(
"standard reuse must be called", standardCalled);
498 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must not be called", !mainCalled);
499 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must not be called", !confCalled);
500 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must not be called",
Mock ReuserDatabaseHandler for unit tests.
Test double for ReuserDatabaseHandler.
Database handler for the reuser agent.
void testMultipleReuseLinksAllProcessed()
All upload_reuse rows for an upload are processed in order.
void testCorrectArgumentsForwardedToProcessUploadReuse()
All five arguments are forwarded correctly to processUploadReuse.
void testOptionalFlagsNotCalledWithoutBits()
reuseMode == 0 -> only processUploadReuse is called, no optional extras.
void testEnhancedReuseUsesEnhancedPath()
REUSE_ENHANCED bit routes to processEnhancedUploadReuse.
void testNoReuseLinkSucceedsWithoutProcessing()
No upload_reuse row exists for this upload.
void testEnhancedReuseFailurePropagates()
A false return from processEnhancedUploadReuse propagates to the caller.
void testReuseMainLicenseFlagOnly()
REUSE_MAIN bit causes only reuseMainLicense to be called.
void testReuseExistsButSourceHasNoClearings()
upload_reuse row exists but the source upload has no clearing decisions.
void testRepoClearingUsesStandardReusePath()
reuseMode == 0 always takes the standard reuse path.
void testReuseConfFlagOnly()
REUSE_CONF bit causes only reuseConfSettings to be called.
void testReuseCopyrightFlagOnly()
REUSE_COPYRIGHT bit causes only reuseCopyrights to be called.
Bounds of an item within an uploadtree table.