File: ukm_metrics_table_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (274 lines) | stat: -rw-r--r-- 10,245 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/segmentation_platform/internal/database/ukm_metrics_table.h"

#include <memory>

#include "base/rand_util.h"
#include "components/segmentation_platform/internal/database/ukm_database_test_utils.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace segmentation_platform {

namespace {

UkmMetricsTable::MetricsRow GetSampleMetricsRow() {
  static auto event_id_generator = MetricsRowEventId::Generator();
  static auto event_hash_generator = UkmEventHash::Generator();
  static auto metric_hash_generator = UkmMetricHash::Generator();
  return UkmMetricsTable::MetricsRow{
      .event_timestamp = base::Time::Now(),
      .source_id = base::RandInt(0, 1000),
      .event_id = event_id_generator.GenerateNextId(),
      .event_hash = event_hash_generator.GenerateNextId(),
      .metric_hash = metric_hash_generator.GenerateNextId(),
      .metric_value = base::RandInt(-1000, 1000)};
}

}  // namespace

class UkmMetricsTableTest : public testing::Test {
 public:
  UkmMetricsTableTest() = default;
  ~UkmMetricsTableTest() override = default;

  void SetUp() override {
    db_ = std::make_unique<sql::Database>(sql::test::kTestTag);
    bool opened = db_->OpenInMemory();
    ASSERT_TRUE(opened);
    metrics_table_ = std::make_unique<UkmMetricsTable>(db_.get());
  }

  void TearDown() override {
    metrics_table_.reset();
    db_.reset();
  }

  UkmMetricsTable& metrics_table() { return *metrics_table_; }

  sql::Database& db() { return *db_; }

 private:
  std::unique_ptr<sql::Database> db_;
  std::unique_ptr<UkmMetricsTable> metrics_table_;
};

TEST_F(UkmMetricsTableTest, CreateTable) {
  ASSERT_TRUE(metrics_table().InitTable());

  EXPECT_TRUE(db().DoesTableExist(UkmMetricsTable::kTableName));
  EXPECT_TRUE(db().DoesIndexExist("event_timestamp_index"));
  EXPECT_TRUE(db().DoesIndexExist("url_id_index"));
  EXPECT_TRUE(db().DoesIndexExist("ukm_source_id_index"));
  EXPECT_TRUE(db().DoesIndexExist("event_hash_index"));

  // Creating table again should be noop.
  ASSERT_TRUE(metrics_table().InitTable());

  EXPECT_TRUE(db().DoesTableExist(UkmMetricsTable::kTableName));
}

TEST_F(UkmMetricsTableTest, InsertRow) {
  ASSERT_TRUE(metrics_table().InitTable());

  EXPECT_TRUE(db().DoesTableExist(UkmMetricsTable::kTableName));

  // Invalid rows should not inserted.
  UkmMetricsTable::MetricsRow row;
  EXPECT_FALSE(metrics_table().AddUkmEvent(row));
  auto row1 = GetSampleMetricsRow();
  row1.event_timestamp = base::Time();
  EXPECT_FALSE(metrics_table().AddUkmEvent(row1));

  test_util::AssertRowsInMetricsTable(db(), {});

  auto row2 = GetSampleMetricsRow();
  EXPECT_TRUE(metrics_table().AddUkmEvent(row2));
  test_util::AssertRowsInMetricsTable(db(), {row2});

  auto row3 = GetSampleMetricsRow();
  EXPECT_TRUE(metrics_table().AddUkmEvent(row3));
  test_util::AssertRowsInMetricsTable(db(), {row2, row3});
}

TEST_F(UkmMetricsTableTest, DeleteForUrl) {
  auto url_id_generator = UrlId::Generator();
  const UrlId url_id1 = url_id_generator.GenerateNextId();
  const UrlId url_id2 = url_id_generator.GenerateNextId();
  const UrlId url_id3 = url_id_generator.GenerateNextId();

  ASSERT_TRUE(metrics_table().InitTable());

  // Delete on empty table does nothing.
  EXPECT_TRUE(metrics_table().DeleteEventsForUrls({}));
  test_util::AssertRowsInMetricsTable(db(), {});
  EXPECT_TRUE(metrics_table().DeleteEventsForUrls({url_id1}));
  test_util::AssertRowsInMetricsTable(db(), {});

  auto row1 = GetSampleMetricsRow();
  row1.url_id = url_id1;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row1));
  auto row2 = GetSampleMetricsRow();
  row2.url_id = url_id1;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row2));
  auto row3 = GetSampleMetricsRow();
  row3.url_id = url_id2;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row3));
  auto row4 = GetSampleMetricsRow();
  row4.url_id = url_id2;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row4));

  test_util::AssertRowsInMetricsTable(db(), {row1, row2, row3, row4});

  // Remove empty URL list.
  EXPECT_TRUE(metrics_table().DeleteEventsForUrls({}));
  test_util::AssertRowsInMetricsTable(db(), {row1, row2, row3, row4});

  // Remove matching URL ID, should remove 2 rows.
  EXPECT_TRUE(metrics_table().DeleteEventsForUrls({url_id1}));
  test_util::AssertRowsInMetricsTable(db(), {row3, row4});

  // Remove non-existent URL IDs, no change to db.
  EXPECT_TRUE(metrics_table().DeleteEventsForUrls({url_id1, url_id3}));
  test_util::AssertRowsInMetricsTable(db(), {row3, row4});

  auto row5 = GetSampleMetricsRow();
  row5.url_id = url_id3;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row5));
  auto row6 = GetSampleMetricsRow();
  row6.url_id = url_id3;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row6));

  test_util::AssertRowsInMetricsTable(db(), {row3, row4, row5, row6});

  // Remove all URL IDs, should clear the table.
  EXPECT_TRUE(metrics_table().DeleteEventsForUrls({url_id1, url_id2, url_id3}));
  test_util::AssertRowsInMetricsTable(db(), {});
}

TEST_F(UkmMetricsTableTest, DeleteBeforeTimestamp) {
  const base::Time kTimestamp1 = base::Time::Now();
  const base::Time kTimestamp2 = kTimestamp1 + base::Seconds(1);
  const base::Time kTimestamp3 = kTimestamp1 + base::Seconds(2);
  const base::Time kTimestamp4 = kTimestamp1 + base::Seconds(3);
  const base::Time kTimestamp5 = kTimestamp1 + base::Seconds(4);
  const UrlId kUrl1 = UrlId::FromUnsafeValue(1);
  const UrlId kUrl2 = UrlId::FromUnsafeValue(2);
  const UrlId kUrl3 = UrlId::FromUnsafeValue(3);
  const UrlId kUrl4 = UrlId::FromUnsafeValue(4);

  ASSERT_TRUE(metrics_table().InitTable());

  // Delete on empty table does nothing.
  std::vector<UrlId> empty_set;
  EXPECT_EQ(empty_set,
            metrics_table().DeleteEventsBeforeTimestamp(base::Time()));
  test_util::AssertRowsInMetricsTable(db(), {});
  EXPECT_EQ(empty_set,
            metrics_table().DeleteEventsBeforeTimestamp(kTimestamp1));
  test_util::AssertRowsInMetricsTable(db(), {});

  auto row1 = GetSampleMetricsRow();
  row1.event_timestamp = kTimestamp1;
  row1.url_id = kUrl1;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row1));
  auto row2 = GetSampleMetricsRow();
  row2.event_timestamp = kTimestamp2;
  row2.url_id = kUrl2;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row2));
  auto row3 = GetSampleMetricsRow();
  row3.event_timestamp = kTimestamp3;
  row3.url_id = kUrl3;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row3));
  auto row4 = GetSampleMetricsRow();
  row4.event_timestamp = kTimestamp4;
  row4.url_id = kUrl4;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row4));

  test_util::AssertRowsInMetricsTable(db(), {row1, row2, row3, row4});

  // Delete with time before all rows does nothing.
  EXPECT_EQ(empty_set,
            metrics_table().DeleteEventsBeforeTimestamp(base::Time()));
  test_util::AssertRowsInMetricsTable(db(), {row1, row2, row3, row4});
  EXPECT_EQ(empty_set, metrics_table().DeleteEventsBeforeTimestamp(
                           kTimestamp1 - base::Seconds(1)));
  test_util::AssertRowsInMetricsTable(db(), {row1, row2, row3, row4});

  // Remove single row.
  EXPECT_EQ(std::vector<UrlId>({kUrl1}),
            metrics_table().DeleteEventsBeforeTimestamp(kTimestamp1));
  test_util::AssertRowsInMetricsTable(db(), {row2, row3, row4});

  // Add more rows with UrlId3.
  auto row5 = GetSampleMetricsRow();
  row5.event_timestamp = kTimestamp5;
  row5.url_id = kUrl3;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row5));
  auto row6 = GetSampleMetricsRow();
  row6.event_timestamp = kTimestamp5;
  row6.url_id = kUrl3;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row6));
  test_util::AssertRowsInMetricsTable(db(), {row2, row3, row4, row5, row6});

  // Remove bunch of rows. UrlId3 should not be part of removed list since 2
  // other metrics reference it.
  EXPECT_EQ(std::vector<UrlId>({kUrl2, kUrl4}),
            metrics_table().DeleteEventsBeforeTimestamp(kTimestamp4));
  test_util::AssertRowsInMetricsTable(db(), {row5, row6});

  // Insert entry with an older timestamp out of order and remove old entries
  // should still work.
  EXPECT_TRUE(metrics_table().AddUkmEvent(row1));
  test_util::AssertRowsInMetricsTable(db(), {row5, row6, row1});
  EXPECT_EQ(std::vector<UrlId>({kUrl1}),
            metrics_table().DeleteEventsBeforeTimestamp(kTimestamp4));
  test_util::AssertRowsInMetricsTable(db(), {row5, row6});

  // Removing multiple entries with same timestamp and url should return the
  // right url to be removed.
  EXPECT_EQ(std::vector<UrlId>({kUrl3}),
            metrics_table().DeleteEventsBeforeTimestamp(kTimestamp5));
  test_util::AssertRowsInMetricsTable(db(), {});
}

TEST_F(UkmMetricsTableTest, MatchHashesTest) {
  const UkmEventHash event_hash1 = UkmEventHash::FromUnsafeValue(1);
  const UkmEventHash event_hash2 =
      UkmEventHash::FromUnsafeValue(0x9CEA8CBC362AB242);
  const UkmEventHash event_hash3 =
      UkmEventHash::FromUnsafeValue(std::numeric_limits<uint64_t>::max());
  ASSERT_TRUE(metrics_table().InitTable());

  UkmMetricsTable::MetricsRow row1 = GetSampleMetricsRow();
  row1.event_hash = event_hash1;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row1));
  UkmMetricsTable::MetricsRow row2 = GetSampleMetricsRow();
  row2.event_hash = event_hash2;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row2));
  UkmMetricsTable::MetricsRow row3 = GetSampleMetricsRow();
  row3.event_hash = event_hash3;
  EXPECT_TRUE(metrics_table().AddUkmEvent(row3));

  auto result1 = test_util::GetMetricsRowWithQuery(
      "SELECT * FROM metrics WHERE event_hash = '1'", db());
  ASSERT_EQ(1u, result1.size());
  test_util::ExpectRowIsEqual(row1, result1[0]);

  auto result2 = test_util::GetMetricsRowWithQuery(
      "SELECT * FROM metrics WHERE event_hash = '9CEA8CBC362AB242'", db());
  ASSERT_EQ(1u, result2.size());
  test_util::ExpectRowIsEqual(row2, result2[0]);

  auto result3 = test_util::GetMetricsRowWithQuery(
      "SELECT * FROM metrics WHERE event_hash = 'FFFFFFFFFFFFFFFF'", db());
  ASSERT_EQ(1u, result3.size());
  test_util::ExpectRowIsEqual(row2, result2[0]);
}

}  // namespace segmentation_platform