Skip to content

Commit

Permalink
Fixes panel query for the publisher
Browse files Browse the repository at this point in the history
Related #1589
  • Loading branch information
NejcZdovc committed Feb 12, 2019
1 parent 56e8e57 commit 2b50d59
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
18 changes: 9 additions & 9 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,17 @@ PublisherInfoDatabase::GetPanelPublisher(
}

sql::Statement info_sql(db_.GetUniqueStatement(
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, "
"pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi "
"LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id "
"WHERE pi.publisher_id=? AND ((ai.month = ? "
"AND ai.year = ? AND ai.reconcile_stamp = ?) OR "
"ai.percent IS NULL) LIMIT 1"));
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, "
"pi.provider, pi.verified, pi.excluded, "
"("
"SELECT IFNULL(percent, 0) FROM activity_info WHERE "
"publisher_id = ? AND reconcile_stamp = ? "
") as percent "
"FROM publisher_info AS pi WHERE pi.publisher_id = ? LIMIT 1"));

info_sql.BindString(0, filter.id);
info_sql.BindInt(1, filter.month);
info_sql.BindInt(2, filter.year);
info_sql.BindInt64(3, filter.reconcile_stamp);
info_sql.BindInt64(1, filter.reconcile_stamp);
info_sql.BindString(2, filter.id);

if (info_sql.Step()) {
std::unique_ptr<ledger::PublisherInfo> info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,65 @@ TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {

}

TEST_F(PublisherInfoDatabaseTest, GetPanelPublisher) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateTempDatabase(&temp_dir, &db_file);

/**
* Publisher ID is missing
*/
ledger::ActivityInfoFilter filter_1;
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), nullptr);

/**
* Empty table
*/
ledger::ActivityInfoFilter filter_2;
filter_2.id = "test";
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), nullptr);

/**
* Ignore month and year filter
*/
ledger::PublisherInfo info_1;
info_1.id = "brave.com";
info_1.url = "https://brave.com";
info_1.percent = 11;
info_1.month = ledger::ACTIVITY_MONTH::JANUARY;
info_1.year = 2019;
info_1.reconcile_stamp = 10;

bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1);
EXPECT_TRUE(success);

ledger::ActivityInfoFilter filter_3;
filter_3.id = "brave.com";
filter_3.month = ledger::ACTIVITY_MONTH::ANY;
filter_3.year = -1;
filter_3.reconcile_stamp = 10;
std::unique_ptr<ledger::PublisherInfo> result =
publisher_info_database_->GetPanelPublisher(filter_3);
EXPECT_TRUE(result);
EXPECT_EQ(result->id, "brave.com");

/**
* Still get data if reconcile stamp is not found
*/
info_1.id = "page.com";
info_1.url = "https://page.com";
info_1.reconcile_stamp = 9;

success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1);
EXPECT_TRUE(success);

ledger::ActivityInfoFilter filter_4;
filter_4.id = "page.com";
filter_4.reconcile_stamp = 10;
result = publisher_info_database_->GetPanelPublisher(filter_4);
EXPECT_TRUE(result);
EXPECT_EQ(result->id, "page.com");
EXPECT_EQ(result->percent, 0u);
}

} // namespace brave_rewards

0 comments on commit 2b50d59

Please sign in to comment.