Skip to content

Commit dc0dfd7

Browse files
committed
Adds unit tests for DB
1 parent 198e3b2 commit dc0dfd7

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

components/brave_rewards/browser/publisher_info_database.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ PublisherInfoDatabase::GetPanelPublisher(
302302
bool initialized = Init();
303303
DCHECK(initialized);
304304

305-
if (!initialized) {
305+
if (!initialized || filter.id.empty()) {
306306
return nullptr;
307307
}
308308

@@ -408,7 +408,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo(
408408
bool initialized = Init();
409409
DCHECK(initialized);
410410

411-
if (!initialized) {
411+
if (!initialized || info.id.empty()) {
412412
return false;
413413
}
414414

@@ -443,7 +443,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfos(
443443
bool initialized = Init();
444444
DCHECK(initialized);
445445

446-
if (!initialized) {
446+
if (!initialized || list.size() == 0) {
447447
return false;
448448
}
449449

@@ -453,7 +453,10 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfos(
453453
}
454454

455455
for (const auto& info : list) {
456-
InsertOrUpdateActivityInfo(info);
456+
if (!InsertOrUpdateActivityInfo(info)) {
457+
transaction.Rollback();
458+
return false;
459+
}
457460
}
458461

459462
return transaction.Commit();

components/brave_rewards/browser/publisher_info_database_unittest.cc

+100
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,106 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateRecurringDonation) {
401401
EXPECT_FALSE(info_sql_2.Step());
402402
}
403403

404+
TEST_F(PublisherInfoDatabaseTest, GetPanelPublisher) {
405+
base::ScopedTempDir temp_dir;
406+
base::FilePath db_file;
407+
CreateTempDatabase(&temp_dir, &db_file);
408+
409+
/**
410+
* Publisher ID is missing
411+
*/
412+
ledger::ActivityInfoFilter filter_1;
413+
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), nullptr);
414+
415+
/**
416+
* Empty table
417+
*/
418+
ledger::ActivityInfoFilter filter_2;
419+
filter_2.id = "test";
420+
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), nullptr);
421+
422+
/**
423+
* Ignore month and year filter
424+
*/
425+
ledger::PublisherInfo info_1;
426+
info_1.id = "brave.com";
427+
info_1.url = "https://brave.com";
428+
info_1.percent = 11;
429+
info_1.month = ledger::ACTIVITY_MONTH::JANUARY;
430+
info_1.year = 2019;
431+
info_1.reconcile_stamp = 10;
432+
433+
bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1);
434+
EXPECT_TRUE(success);
435+
436+
ledger::ActivityInfoFilter filter_3;
437+
filter_3.id = "brave.com";
438+
filter_3.month = ledger::ACTIVITY_MONTH::ANY;
439+
filter_3.year = -1;
440+
filter_3.reconcile_stamp = 10;
441+
std::unique_ptr<ledger::PublisherInfo> result =
442+
publisher_info_database_->GetPanelPublisher(filter_3);
443+
EXPECT_TRUE(result);
444+
EXPECT_EQ(result->id, "brave.com");
445+
}
446+
447+
TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfos) {
448+
base::ScopedTempDir temp_dir;
449+
base::FilePath db_file;
450+
CreateTempDatabase(&temp_dir, &db_file);
451+
452+
/**
453+
* Good path
454+
*/
455+
ledger::PublisherInfo info_1;
456+
info_1.id = "brave.com";
457+
info_1.url = "https://brave.com";
458+
info_1.percent = 11;
459+
info_1.month = ledger::ACTIVITY_MONTH::JANUARY;
460+
info_1.year = 2019;
461+
info_1.reconcile_stamp = 10;
462+
463+
ledger::PublisherInfo info_2;
464+
info_2.id = "clifton.io";
465+
info_2.url = "https://clifton.io";
466+
info_2.percent = 11;
467+
info_2.month = ledger::ACTIVITY_MONTH::JANUARY;
468+
info_2.year = 2019;
469+
info_2.reconcile_stamp = 10;
470+
471+
ledger::PublisherInfoList list;
472+
list.push_back(info_1);
473+
list.push_back(info_2);
474+
475+
bool success = publisher_info_database_->InsertOrUpdateActivityInfos(list);
476+
EXPECT_TRUE(success);
477+
478+
/**
479+
* Empty list
480+
*/
481+
ledger::PublisherInfoList list_empty;
482+
483+
success = publisher_info_database_->InsertOrUpdateActivityInfos(list_empty);
484+
EXPECT_FALSE(success);
485+
486+
/**
487+
* One publisher has empty ID
488+
*/
489+
490+
ledger::PublisherInfo info_3;
491+
info_3.id = "";
492+
info_3.url = "https://page.io";
493+
info_3.percent = 11;
494+
info_3.month = ledger::ACTIVITY_MONTH::JANUARY;
495+
info_3.year = 2019;
496+
info_3.reconcile_stamp = 10;
497+
498+
list.push_back(info_3);
499+
500+
success = publisher_info_database_->InsertOrUpdateActivityInfos(list);
501+
EXPECT_FALSE(success);
502+
}
503+
404504
TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {
405505

406506
}

0 commit comments

Comments
 (0)