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

Side by Side Diff: components/translate/core/browser/ranker_model_unittest.cc

Issue 2925733002: Move ranker_model_loader to a new component. (Closed)
Patch Set: Adressing sdefresne's comments. Created 3 years, 5 months 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/translate/core/browser/ranker_model.h"
6
7 #include <memory>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/time/time.h"
11 #include "components/translate/core/browser/proto/ranker_model.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 using chrome_intelligence::RankerModel;
17
18 const char kModelURL[] = "https://some.url.net/model";
19
20 int64_t InSeconds(const base::Time t) {
21 return (t - base::Time()).InSeconds();
22 }
23
24 std::unique_ptr<RankerModel> NewModel(const std::string& model_url,
25 base::Time last_modified,
26 base::TimeDelta cache_duration) {
27 std::unique_ptr<RankerModel> model = base::MakeUnique<RankerModel>();
28 auto* metadata = model->mutable_proto()->mutable_metadata();
29 if (!model_url.empty())
30 metadata->set_source(model_url);
31 if (!last_modified.is_null())
32 metadata->set_last_modified_sec(InSeconds(last_modified));
33 if (!cache_duration.is_zero())
34 metadata->set_cache_duration_sec(cache_duration.InSeconds());
35
36 auto* translate = model->mutable_proto()->mutable_translate();
37 translate->set_version(1);
38
39 auto* logit = translate->mutable_logistic_regression_model();
40 logit->set_bias(0.1f);
41 logit->set_accept_ratio_weight(0.2f);
42 logit->set_decline_ratio_weight(0.3f);
43 logit->set_ignore_ratio_weight(0.4f);
44 return model;
45 }
46
47 } // namespace
48
49 TEST(RankerModelTest, Serialization) {
50 base::Time last_modified = base::Time::Now();
51 base::TimeDelta cache_duration = base::TimeDelta::FromDays(3);
52 std::unique_ptr<RankerModel> original_model =
53 NewModel(kModelURL, last_modified, cache_duration);
54 std::string original_model_str = original_model->SerializeAsString();
55 std::unique_ptr<RankerModel> serialized_model =
56 RankerModel::FromString(original_model_str);
57 std::string serialized_model_str = serialized_model->SerializeAsString();
58
59 EXPECT_EQ(serialized_model_str, original_model_str);
60 EXPECT_EQ(serialized_model->GetSourceURL(), kModelURL);
61 EXPECT_EQ(serialized_model->proto().metadata().last_modified_sec(),
62 InSeconds(last_modified));
63 EXPECT_EQ(serialized_model->proto().metadata().cache_duration_sec(),
64 cache_duration.InSeconds());
65 }
66
67 TEST(RankerModelTest, IsExpired) {
68 base::Time today = base::Time::Now();
69 base::TimeDelta days_15 = base::TimeDelta::FromDays(15);
70 base::TimeDelta days_30 = base::TimeDelta::FromDays(30);
71 base::TimeDelta days_60 = base::TimeDelta::FromDays(60);
72
73 EXPECT_FALSE(NewModel(kModelURL, today, days_30)->IsExpired());
74 EXPECT_FALSE(NewModel(kModelURL, today - days_15, days_30)->IsExpired());
75 EXPECT_TRUE(NewModel(kModelURL, today - days_60, days_30)->IsExpired());
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698