Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Acceptance Tests #448

Merged
merged 5 commits into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions acceptance/search/search_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# 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.

require "search_helper"
require "gcloud/search"

# This test is a ruby version of gcloud-node's search test.

describe "Search", :search do
let(:index) { search.index "#{prefix}-main-index", skip_lookup: true }
let(:chris_where_doc) do
doc = index.document "chris-where"
doc["question"].add "Where did Chris go?"
doc["answer"].add "to the mountains"
doc["tags"].add "chris"
doc["tags"].add "gcloud"
doc["tags"].add "ruby"
doc
end
let(:chris_what_doc) do
doc = index.document "chris-what"
doc["question"].add "What did Chris do?"
doc["answer"].add "hunting"
doc["tags"].add "chris"
doc["tags"].add "gcloud"
doc["tags"].add "ruby"
doc
end
let(:mike_where_doc) do
doc = index.document "mike-where"
doc["question"].add "Where did Mike go?"
doc["answer"].add "comic book store"
doc["tags"].add "mike"
doc["tags"].add "gcloud"
doc["tags"].add "ruby"
doc
end
let(:mike_what_doc) do
doc = index.document "mike-what"
doc["question"].add "What did Mike do?"
doc["answer"].add "bought some comics"
doc["tags"].add "mike"
doc["tags"].add "gcloud"
doc["tags"].add "ruby"
doc
end

before do
if index.find(chris_where_doc.doc_id).nil?
index.save chris_where_doc
index.save chris_what_doc
index.save mike_where_doc
index.save mike_what_doc
end
end

it "creates and deletes a document in a new index" do
new_index = search.index "#{prefix}-new-index", skip_lookup: true
new_doc = new_index.document "new-document"
new_doc["hello"].add "world"
new_index.documents.count.must_equal 0
new_index.save new_doc
new_index.documents.count.must_equal 1
new_index.remove new_doc
new_index.documents.count.must_equal 0
end

it "should get the indexes" do
search.indexes.count.must_be :>, 0
search.indexes.all.count.must_be :>, 0
end

it "should get all documents" do
index.documents.count.must_be :>, 0
index.documents.all.count.must_be :>, 0
end

it "searches" do
search_results = index.search "mountains"
search_results.count.must_equal 1
search_results.first.doc_id.must_equal chris_where_doc.doc_id
search_results.first["title"].must_be :empty?
search_results.first["questions"].must_be :empty?
search_results.first["tags"].must_be :empty?
search_results.first["rank"].must_be :empty?
search_results.first["score"].must_be :empty?

search_results = index.search "where", fields: ["question"]
search_results.count.must_equal 2
search_results.map(&:doc_id).must_include chris_where_doc.doc_id
search_results.map(&:doc_id).must_include mike_where_doc.doc_id
search_results.each do |sr|
sr["question"].wont_be :empty?
sr["answer"].must_be :empty?
sr["tags"].must_be :empty?
sr["rank"].must_be :empty?
sr["score"].must_be :empty?
end

search_results = index.search "chris", fields: ["question", "answer"]
search_results.count.must_equal 2
search_results.map(&:doc_id).must_include chris_where_doc.doc_id
search_results.map(&:doc_id).must_include chris_what_doc.doc_id
search_results.each do |sr|
sr["question"].wont_be :empty?
sr["answer"].wont_be :empty?
sr["tags"].must_be :empty?
sr["rank"].must_be :empty?
sr["score"].must_be :empty?
end

search_results = index.search "mike", fields: "*"
search_results.count.must_equal 2
search_results.map(&:doc_id).must_include mike_where_doc.doc_id
search_results.map(&:doc_id).must_include mike_what_doc.doc_id
search_results.each do |sr|
sr["question"].wont_be :empty?
sr["answer"].wont_be :empty?
sr["tags"].wont_be :empty?
sr["rank"].must_be :empty?
sr["score"].must_be :empty?
end

search_results = index.search "mike", fields: "*", order: "question, answer"
search_results.count.must_equal 2
search_results.map(&:doc_id).must_include mike_where_doc.doc_id
search_results.map(&:doc_id).must_include mike_what_doc.doc_id
search_results.each do |sr|
sr["question"].wont_be :empty?
sr["answer"].wont_be :empty?
sr["tags"].wont_be :empty?
sr["rank"].must_be :empty?
sr["score"].must_be :empty?
end

search_results = index.search "mike", fields: ["*", "rank", "score"], scorer: "generic", order: "score desc"
search_results.count.must_equal 2
search_results.map(&:doc_id).must_include mike_where_doc.doc_id
search_results.map(&:doc_id).must_include mike_what_doc.doc_id
search_results.each do |sr|
sr["question"].wont_be :empty?
sr["answer"].wont_be :empty?
sr["tags"].wont_be :empty?
sr["rank"].wont_be :empty?
sr["score"].wont_be :empty?
end

search_results = index.search "ruby", fields: ["tags", "rank", "score"], scorer: "generic"
search_results.count.must_equal 4
search_results.map(&:doc_id).must_include chris_where_doc.doc_id
search_results.map(&:doc_id).must_include chris_what_doc.doc_id
search_results.map(&:doc_id).must_include mike_where_doc.doc_id
search_results.map(&:doc_id).must_include mike_what_doc.doc_id
search_results.each do |sr|
sr["question"].must_be :empty?
sr["answer"].must_be :empty?
sr["tags"].wont_be :empty?
sr["rank"].wont_be :empty?
sr["score"].wont_be :empty?
end

search_results = index.search "where", fields: ["question_snippet", "answer"], expressions: {name: "question_snippet", expression: "snippet(\"where\", question)"}
search_results.count.must_equal 2
search_results.map(&:doc_id).must_include chris_where_doc.doc_id
search_results.map(&:doc_id).must_include mike_where_doc.doc_id
search_results.each do |sr|
sr["question"].must_be :empty?
sr["question_snippet"].wont_be :empty?
sr["answer"].wont_be :empty?
sr["tags"].must_be :empty?
sr["rank"].must_be :empty?
sr["score"].must_be :empty?
end
end
end
87 changes: 87 additions & 0 deletions acceptance/search_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# 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.

require "helper"
require "gcloud/search"

Gcloud::Backoff.retries = 10

# Create shared search object so we don't create new for each test
$search = Gcloud.search

# create prefix for names of datasets and tables
require "time"
require "securerandom"
t = Time.now.utc.iso8601.gsub ":", "_"
$prefix = "gcloud_ruby_acceptance_#{t}_#{SecureRandom.hex(4)}".downcase.gsub "-", "_"

module Acceptance
##
# Test class for running against a search instance.
# Ensures that there is an active connection for the tests to use.
#
# This class can be used with the spec DSL.
# To do so, add :search to describe:
#
# describe "My search Test", :search do
# it "does a thing" do
# your.code.must_be :thing?
# end
# end
class SearchTest < Minitest::Test
attr_accessor :search
attr_accessor :prefix

##
# Setup project based on available ENV variables
def setup
@search = $search
@prefix = $prefix

refute_nil @search, "You do not have an active search to run the tests."
refute_nil @prefix, "You do not have an search prefix to name the datasets and tables with."

super
end

# Add spec DSL
extend Minitest::Spec::DSL

# Register this spec type for when :search is used.
register_spec_type(self) do |desc, *addl|
addl.include? :search
end
end

def self.run_one_method klass, method_name, reporter
result = nil
(1..3).each do |try|
result = Minitest.run_one_method(klass, method_name)
break if (result.passed? || result.skipped?)
puts "Retrying #{klass}##{method_name} (#{try})"
end
reporter.record result
end
end

def clean_up_search_indexes
puts "Cleaning up search indexes and documents after tests."
$search.indexes(prefix: $prefix).map { |i| i.delete force: true }
rescue => e
puts "Error while cleaning up search indexes and documents after tests.\n\n#{e}"
end

Minitest.after_run do
clean_up_search_indexes
end
4 changes: 2 additions & 2 deletions lib/gcloud/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def self.search project = nil, keyfile = nil, options = {}
# search = gcloud.search
# index = search.index "books"
#
# results = index.search "dark stormy" , order: ["published", "-avg_review"]
# results = index.search "dark stormy", order: "published, avg_review desc"
# documents = index.search query # API call
#
# You can add computed fields with the +expressions+ option, and limit the
Expand All @@ -258,7 +258,7 @@ def self.search project = nil, keyfile = nil, options = {}
# expressions = [{ name: "total_price", expression: "(price + tax)" }]
# results = index.search "cotton T-shirt",
# expressions: expressions,
# fields: [name, total_price, highlight]
# fields: ["name", "total_price", "highlight"]
#
# Just as in documents, Result data is accessible via Fields methods:
#
Expand Down
2 changes: 1 addition & 1 deletion lib/gcloud/search/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def search_request index_id, query, options = {}
orderBy: options[:order],
pageSize: options[:max],
pageToken: options[:token],
returnFields: options[:return_fields],
returnFields: options[:fields],
scorerSize: options[:scorer_size],
scorer: options[:scorer]
}.delete_if { |_, v| v.nil? }
Expand Down
2 changes: 1 addition & 1 deletion lib/gcloud/search/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def rank
# not specified or set to 0), it is set at the time the document is
# created to the number of seconds since January 1, 2011. The rank can be
# used in Index#search options +expressions+, +order+, and
# +return_fields+, where it is referenced as +_rank+.
# +fields+, where it is referenced as +rank+.
def rank= new_rank
@raw["rank"] = new_rank
end
Expand Down
Loading