• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 #include <utility>
17 
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "icing/document-builder.h"
21 #include "icing/icing-search-engine.h"
22 #include "icing/portable/equals-proto.h"
23 #include "icing/proto/document.pb.h"
24 #include "icing/proto/initialize.pb.h"
25 #include "icing/proto/schema.pb.h"
26 #include "icing/proto/scoring.pb.h"
27 #include "icing/proto/search.pb.h"
28 #include "icing/proto/status.pb.h"
29 #include "icing/proto/term.pb.h"
30 #include "icing/schema-builder.h"
31 #include "icing/testing/common-matchers.h"
32 #include "icing/testing/tmp-directory.h"
33 
34 namespace icing {
35 namespace lib {
36 namespace {
37 
38 using ::icing::lib::portable_equals_proto::EqualsProto;
39 using ::testing::Eq;
40 
GetTestBaseDir()41 std::string GetTestBaseDir() {
42   return GetTestTempDir() + "/icing_with_icu_files";
43 }
44 
GetDefaultIcingOptions()45 IcingSearchEngineOptions GetDefaultIcingOptions() {
46   IcingSearchEngineOptions icing_options;
47   icing_options.set_base_dir(GetTestBaseDir());
48   return icing_options;
49 }
50 
CreateMessageDocument(std::string name_space,std::string uri)51 DocumentProto CreateMessageDocument(std::string name_space, std::string uri) {
52   return DocumentBuilder()
53       .SetKey(std::move(name_space), std::move(uri))
54       .SetSchema("Message")
55       .AddStringProperty("body", "message body")
56       .SetCreationTimestampMs(1)
57       .Build();
58 }
59 
GetDefaultScoringSpec()60 ScoringSpecProto GetDefaultScoringSpec() {
61   ScoringSpecProto scoring_spec;
62   scoring_spec.set_rank_by(ScoringSpecProto::RankingStrategy::DOCUMENT_SCORE);
63   return scoring_spec;
64 }
65 
TEST(IcingSearchEngineWithIcuFileTest,ShouldInitialize)66 TEST(IcingSearchEngineWithIcuFileTest, ShouldInitialize) {
67   IcingSearchEngine icing(GetDefaultIcingOptions());
68   EXPECT_THAT(icing.Initialize().status().code(), Eq(StatusProto::OK));
69 
70   SchemaProto schema =
71       SchemaBuilder()
72           .AddType(SchemaTypeConfigBuilder().SetType("Message").AddProperty(
73               PropertyConfigBuilder()
74                   .SetName("body")
75                   .SetDataTypeString(TERM_MATCH_PREFIX, TOKENIZER_PLAIN)
76                   .SetCardinality(CARDINALITY_REQUIRED)))
77           .Build();
78   EXPECT_THAT(icing.SetSchema(schema).status().code(), Eq(StatusProto::OK));
79 }
80 
TEST(IcingSearchEngineWithIcuFileTest,ShouldIndexAndSearch)81 TEST(IcingSearchEngineWithIcuFileTest, ShouldIndexAndSearch) {
82   IcingSearchEngine icing(GetDefaultIcingOptions());
83   ASSERT_THAT(icing.Initialize().status().code(), Eq(StatusProto::OK));
84 
85   SchemaProto schema =
86       SchemaBuilder()
87           .AddType(SchemaTypeConfigBuilder().SetType("Message").AddProperty(
88               PropertyConfigBuilder()
89                   .SetName("body")
90                   .SetDataTypeString(TERM_MATCH_PREFIX, TOKENIZER_PLAIN)
91                   .SetCardinality(CARDINALITY_REQUIRED)))
92           .Build();
93   ASSERT_THAT(icing.SetSchema(schema).status().code(), Eq(StatusProto::OK));
94 
95   DocumentProto document_one = CreateMessageDocument("namespace", "uri1");
96   ASSERT_THAT(icing.Put(document_one).status().code(), Eq(StatusProto::OK));
97 
98   DocumentProto document_two = CreateMessageDocument("namespace", "uri2");
99   ASSERT_THAT(icing.Put(document_two).status().code(), Eq(StatusProto::OK));
100 
101   SearchSpecProto search_spec;
102   search_spec.set_term_match_type(TermMatchType::PREFIX);
103   search_spec.set_query("message");
104 
105   ResultSpecProto result_spec;
106   result_spec.set_num_per_page(1);
107 
108   SearchResultProto expected_search_result_proto;
109   expected_search_result_proto.mutable_status()->set_code(StatusProto::OK);
110   *expected_search_result_proto.mutable_results()->Add()->mutable_document() =
111       document_two;
112 
113   SearchResultProto search_result_proto =
114       icing.Search(search_spec, GetDefaultScoringSpec(), result_spec);
115   EXPECT_THAT(search_result_proto.status().code(), Eq(StatusProto::OK));
116   // The token is a random number so we don't verify it.
117   expected_search_result_proto.set_next_page_token(
118       search_result_proto.next_page_token());
119   EXPECT_THAT(search_result_proto, EqualsSearchResultIgnoreStatsAndScores(
120                                        expected_search_result_proto));
121 }
122 
123 }  // namespace
124 }  // namespace lib
125 }  // namespace icing
126