| // Copyright (C) 2019 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include <string> |
| #include <utility> |
| |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| #include "icing/document-builder.h" |
| #include "icing/icing-search-engine.h" |
| #include "icing/portable/equals-proto.h" |
| #include "icing/proto/document.pb.h" |
| #include "icing/proto/initialize.pb.h" |
| #include "icing/proto/schema.pb.h" |
| #include "icing/proto/scoring.pb.h" |
| #include "icing/proto/search.pb.h" |
| #include "icing/proto/status.pb.h" |
| #include "icing/proto/term.pb.h" |
| #include "icing/testing/common-matchers.h" |
| #include "icing/testing/tmp-directory.h" |
| |
| namespace icing { |
| namespace lib { |
| namespace { |
| using ::icing::lib::portable_equals_proto::EqualsProto; |
| using ::testing::Eq; |
| |
| std::string GetTestBaseDir() { |
| return GetTestTempDir() + "/icing_with_icu_files"; |
| } |
| |
| IcingSearchEngineOptions GetDefaultIcingOptions() { |
| IcingSearchEngineOptions icing_options; |
| icing_options.set_base_dir(GetTestBaseDir()); |
| return icing_options; |
| } |
| |
| DocumentProto CreateMessageDocument(std::string name_space, std::string uri) { |
| return DocumentBuilder() |
| .SetKey(std::move(name_space), std::move(uri)) |
| .SetSchema("Message") |
| .AddStringProperty("body", "message body") |
| .SetCreationTimestampMs(1) |
| .Build(); |
| } |
| |
| SchemaProto CreateMessageSchema() { |
| SchemaProto schema; |
| auto type = schema.add_types(); |
| type->set_schema_type("Message"); |
| |
| auto body = type->add_properties(); |
| body->set_property_name("body"); |
| body->set_data_type(PropertyConfigProto::DataType::STRING); |
| body->set_cardinality(PropertyConfigProto::Cardinality::REQUIRED); |
| body->mutable_string_indexing_config()->set_term_match_type( |
| TermMatchType::PREFIX); |
| body->mutable_string_indexing_config()->set_tokenizer_type( |
| StringIndexingConfig::TokenizerType::PLAIN); |
| |
| return schema; |
| } |
| |
| ScoringSpecProto GetDefaultScoringSpec() { |
| ScoringSpecProto scoring_spec; |
| scoring_spec.set_rank_by(ScoringSpecProto::RankingStrategy::DOCUMENT_SCORE); |
| return scoring_spec; |
| } |
| |
| TEST(IcingSearchEngineWithIcuFileTest, ShouldInitialize) { |
| IcingSearchEngine icing(GetDefaultIcingOptions()); |
| EXPECT_THAT(icing.Initialize().status().code(), Eq(StatusProto::OK)); |
| EXPECT_THAT(icing.SetSchema(CreateMessageSchema()).status().code(), |
| Eq(StatusProto::OK)); |
| } |
| |
| TEST(IcingSearchEngineWithIcuFileTest, ShouldIndexAndSearch) { |
| IcingSearchEngine icing(GetDefaultIcingOptions()); |
| ASSERT_THAT(icing.Initialize().status().code(), Eq(StatusProto::OK)); |
| ASSERT_THAT(icing.SetSchema(CreateMessageSchema()).status().code(), |
| Eq(StatusProto::OK)); |
| |
| DocumentProto document_one = CreateMessageDocument("namespace", "uri1"); |
| ASSERT_THAT(icing.Put(document_one).status().code(), Eq(StatusProto::OK)); |
| |
| DocumentProto document_two = CreateMessageDocument("namespace", "uri2"); |
| ASSERT_THAT(icing.Put(document_two).status().code(), Eq(StatusProto::OK)); |
| |
| SearchSpecProto search_spec; |
| search_spec.set_term_match_type(TermMatchType::PREFIX); |
| search_spec.set_query("message"); |
| |
| ResultSpecProto result_spec; |
| result_spec.set_num_per_page(1); |
| |
| SearchResultProto expected_search_result_proto; |
| expected_search_result_proto.mutable_status()->set_code(StatusProto::OK); |
| *expected_search_result_proto.mutable_results()->Add()->mutable_document() = |
| document_two; |
| |
| SearchResultProto search_result_proto = |
| icing.Search(search_spec, GetDefaultScoringSpec(), result_spec); |
| EXPECT_THAT(search_result_proto.status().code(), Eq(StatusProto::OK)); |
| // The token is a random number so we don't verify it. |
| expected_search_result_proto.set_next_page_token( |
| search_result_proto.next_page_token()); |
| EXPECT_THAT(search_result_proto, |
| EqualsSearchResultIgnoreStats(expected_search_result_proto)); |
| } |
| |
| } // namespace |
| } // namespace lib |
| } // namespace icing |