Chromium Code Reviews| Index: components/translate/core/browser/ranker_model_unittest.cc |
| diff --git a/components/translate/core/browser/ranker_model_unittest.cc b/components/translate/core/browser/ranker_model_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..607c97caf5e874e15c8a175e376bd133d3ed9720 |
| --- /dev/null |
| +++ b/components/translate/core/browser/ranker_model_unittest.cc |
| @@ -0,0 +1,76 @@ |
| +// Copyright 2017 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/translate/core/browser/ranker_model.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/time/time.h" |
| +#include "components/translate/core/browser/proto/ranker_model.pb.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +using chrome_intelligence::RankerModel; |
| + |
| +const char kModelURL[] = "https://some.url.net/model"; |
| + |
| +int64_t InSeconds(const base::Time& t) { |
|
droger
2017/03/22 14:30:07
Time should be passed by value (here and in other
Roger McFarlane (Chromium)
2017/03/22 20:31:43
Done.
|
| + return (t - base::Time()).InSeconds(); |
| +} |
| + |
| +std::unique_ptr<RankerModel> NewModel(const std::string& model_url, |
| + const base::Time& last_modified, |
| + const base::TimeDelta& cache_duration) { |
| + auto model = base::MakeUnique<RankerModel>(); |
| + auto* metadata = model->mutable_proto()->mutable_metadata(); |
| + if (!model_url.empty()) |
| + metadata->set_source(model_url); |
| + if (!last_modified.is_null()) { |
| + auto last_modified_sec = InSeconds(last_modified); |
| + metadata->set_last_modified_sec(last_modified_sec); |
| + } |
| + if (!cache_duration.is_zero()) |
| + metadata->set_cache_duration_sec(cache_duration.InSeconds()); |
| + |
| + auto* translate = model->mutable_proto()->mutable_translate(); |
| + translate->set_version(1); |
| + |
| + auto* logit = translate->mutable_logistic_regression_model(); |
| + logit->set_bias(0.1f); |
| + logit->set_accept_ratio_weight(0.2f); |
| + logit->set_decline_ratio_weight(0.3f); |
| + logit->set_ignore_ratio_weight(0.4f); |
| + return model; |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(RankerModelTest, Serialization) { |
| + auto last_modified = base::Time::Now(); |
| + auto cache_duration = base::TimeDelta::FromDays(3); |
| + auto original_model = NewModel(kModelURL, last_modified, cache_duration); |
| + auto original_model_as_string = original_model->SerializeAsString(); |
|
droger
2017/03/22 14:30:07
Nit, optional: I think the code would be clearer w
Roger McFarlane (Chromium)
2017/03/22 20:31:43
Done.
|
| + auto serialized_model = RankerModel::FromString(original_model_as_string); |
| + auto serialized_model_as_string = serialized_model->SerializeAsString(); |
| + |
| + EXPECT_EQ(serialized_model_as_string, original_model_as_string); |
| + EXPECT_EQ(serialized_model->GetSourceURL(), kModelURL); |
| + EXPECT_EQ(serialized_model->proto().metadata().last_modified_sec(), |
| + InSeconds(last_modified)); |
| + EXPECT_EQ(serialized_model->proto().metadata().cache_duration_sec(), |
| + cache_duration.InSeconds()); |
| +} |
| + |
| +TEST(RankerModelTest, IsExpired) { |
| + const auto today = base::Time::Now(); |
| + const auto days_15 = base::TimeDelta::FromDays(15); |
| + const auto days_30 = base::TimeDelta::FromDays(30); |
| + const auto days_60 = base::TimeDelta::FromDays(60); |
| + |
| + EXPECT_FALSE(NewModel(kModelURL, today, days_30)->IsExpired()); |
| + EXPECT_FALSE(NewModel(kModelURL, today - days_15, days_30)->IsExpired()); |
| + EXPECT_TRUE(NewModel(kModelURL, today - days_60, days_30)->IsExpired()); |
| +} |