Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(587)

Unified Diff: components/precache/core/precache_statistics_table_unittest.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Reduced CL down to just the PrecacheDatabase Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/precache/core/precache_statistics_table_unittest.cc
diff --git a/components/precache/core/precache_statistics_table_unittest.cc b/components/precache/core/precache_statistics_table_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bc5d5a85e30dfdb738d03d4842a0e28700b44903
--- /dev/null
+++ b/components/precache/core/precache_statistics_table_unittest.cc
@@ -0,0 +1,108 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/precache/core/precache_statistics_table.h"
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
+#include "sql/connection.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace precache {
+
+namespace {
+
+typedef PrecacheStatisticsTable::PrecacheStatistics PrecacheStatistics;
+typedef PrecacheStatisticsTable::PrecacheStatisticsMap PrecacheStatisticsMap;
+
+base::Time GetTimeFromString(const char* time_string) {
+ base::Time time;
+ EXPECT_TRUE(base::Time::FromString(time_string, &time));
+ return time;
+}
+
+class PrecacheStatisticsTableTest : public testing::Test {
+ public:
+ PrecacheStatisticsTableTest() {}
+ virtual ~PrecacheStatisticsTableTest() {}
+
+ protected:
+ virtual void SetUp() OVERRIDE {
+ precache_statistics_table_.reset(new PrecacheStatisticsTable());
+ db_.reset(new sql::Connection());
+ ASSERT_TRUE(db_->OpenInMemory());
+ precache_statistics_table_->Init(db_.get());
+ }
+
+ scoped_ptr<PrecacheStatisticsTable> precache_statistics_table_;
+ scoped_ptr<sql::Connection> db_;
+};
+
+TEST_F(PrecacheStatisticsTableTest, IncreaseDailyStats) {
+ const base::Time kFetchDay = GetTimeFromString("11 Oct 2013, 00:00:00");
+
+ precache_statistics_table_->IncreaseDailyStats(
+ kFetchDay + base::TimeDelta::FromHours(5),
+ PrecacheStatistics(5, 4, 3, 2, 1));
+ precache_statistics_table_->IncreaseDailyStats(
+ kFetchDay + base::TimeDelta::FromHours(10),
+ PrecacheStatistics(50, 40, 30, 20, 10));
+
+ PrecacheStatisticsMap expected_stats_map;
+ expected_stats_map[kFetchDay] = PrecacheStatistics(55, 44, 33, 22, 11);
+
+ PrecacheStatisticsMap actual_stats_map;
+ precache_statistics_table_->GetAllStatsUntil(base::Time::Max(),
+ &actual_stats_map);
+
+ EXPECT_EQ(expected_stats_map, actual_stats_map);
+}
+
+TEST_F(PrecacheStatisticsTableTest, GetAllStatsUntil) {
+ const base::Time kBeforeDate = GetTimeFromString("11 Oct 2013, 00:00:00");
+ const base::Time kEndDate = GetTimeFromString("12 Oct 2013, 00:00:00");
+ const base::Time kAfterDate = GetTimeFromString("13 Oct 2013, 00:00:00");
+ const PrecacheStatistics kStats(5, 4, 3, 2, 1);
+
+ precache_statistics_table_->IncreaseDailyStats(kBeforeDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kEndDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kAfterDate, kStats);
+
+ PrecacheStatisticsMap expected_stats_map;
+ expected_stats_map[kBeforeDate] = kStats;
+ expected_stats_map[kEndDate] = kStats;
+
+ PrecacheStatisticsMap actual_stats_map;
+ precache_statistics_table_->GetAllStatsUntil(kEndDate, &actual_stats_map);
+
+ EXPECT_EQ(expected_stats_map, actual_stats_map);
+}
+
+TEST_F(PrecacheStatisticsTableTest, DeleteAllStatsUntil) {
+ const base::Time kBeforeDate = GetTimeFromString("11 Oct 2013, 00:00:00");
+ const base::Time kEndDate = GetTimeFromString("12 Oct 2013, 00:00:00");
+ const base::Time kAfterDate = GetTimeFromString("13 Oct 2013, 00:00:00");
+ const PrecacheStatistics kStats(5, 4, 3, 2, 1);
+
+ precache_statistics_table_->IncreaseDailyStats(kBeforeDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kEndDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kAfterDate, kStats);
+
+ precache_statistics_table_->DeleteAllStatsUntil(kEndDate);
+
+ PrecacheStatisticsMap expected_stats_map;
+ expected_stats_map[kAfterDate] = kStats;
+
+ PrecacheStatisticsMap actual_stats_map;
+ precache_statistics_table_->GetAllStatsUntil(base::Time::Max(),
+ &actual_stats_map);
+
+ EXPECT_EQ(expected_stats_map, actual_stats_map);
+}
+
+} // namespace
+
+} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698