From d12ec968c41233a51aec6307cb901400675eff44 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Wed, 21 Jan 2015 16:44:59 +1100 Subject: [PATCH] elasticsearch to be turned off as a default in dev and test --- app/models/crop.rb | 30 +++++++++++-------- config/application.yml.example | 9 ++++++ .../harvests/harvesting_a_crop_spec.rb | 3 +- .../plantings/planting_a_crop_spec.rb | 7 +++-- spec/features/seeds/adding_seeds_spec.rb | 3 +- spec/features/shared_examples/crop_suggest.rb | 24 ++++++++++----- spec/models/crop_spec.rb | 3 +- spec/support/database_cleaner.rb | 1 - spec/support/elastisearch_helpers.rb | 18 +++++++++++ 9 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 spec/support/elastisearch_helpers.rb diff --git a/app/models/crop.rb b/app/models/crop.rb index d6937b838..fcaf84147 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -40,7 +40,7 @@ class Crop < ActiveRecord::Base tokenizer: { gs_edgeNGram_tokenizer: { type: "edgeNGram", - min_gram: 4, + min_gram: 3, max_gram: 10, token_chars: [ "letter", "digit" ] } @@ -243,17 +243,21 @@ class Crop < ActiveRecord::Base # Crop.search(string) def self.search(query) - search_str = query.nil? ? "" : query.downcase - response = __elasticsearch__.search( { - query: { - multi_match: { - query: "#{search_str}", - fields: ["name", "scientific_names.scientific_name", "alternate_names.name"] - } - }, - size: 50 - } - ) - return response.records.to_a + if ENV['GROWSTUFF_ELASTICSEARCH'] == "true" + search_str = query.nil? ? "" : query.downcase + response = __elasticsearch__.search( { + query: { + multi_match: { + query: "#{search_str}", + fields: ["name", "scientific_names.scientific_name", "alternate_names.name"] + } + }, + size: 50 + } + ) + return response.records.to_a + else + where("name ILIKE ?", "%#{query}%") + end end end diff --git a/config/application.yml.example b/config/application.yml.example index f00978be0..03e8afe2c 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -62,6 +62,13 @@ GROWSTUFF_PAYPAL_USERNAME: "dummy" GROWSTUFF_PAYPAL_PASSWORD: "dummy" GROWSTUFF_PAYPAL_SIGNATURE: "dummy" +# Elasticsearch is used for flexible search and it requires another component +# to be installed. To make it easy for people who don't need to test this feature +# it's been turned off for test and development environment as a default. +# If you want to test this functionality, install elasticsearch and +# set this flag to "true". +GROWSTUFF_ELASTICSEARCH: "false" + ############################################################################## # Other environments # You can override the above for staging, production, etc. @@ -78,6 +85,8 @@ test: staging: GROWSTUFF_SITE_NAME: Growstuff (staging) + GROWSTUFF_ELASTICSEARCH: "true" production: GROWSTUFF_SITE_NAME: Growstuff + GROWSTUFF_ELASTICSEARCH: "true" diff --git a/spec/features/harvests/harvesting_a_crop_spec.rb b/spec/features/harvests/harvesting_a_crop_spec.rb index 40c0eb8f8..c1148cf4b 100644 --- a/spec/features/harvests/harvesting_a_crop_spec.rb +++ b/spec/features/harvests/harvesting_a_crop_spec.rb @@ -7,12 +7,13 @@ feature "Harvesting a crop", :js => true do background do login_as(member) visit new_harvest_path + sync_elasticsearch([maize]) end it_behaves_like "crop suggest", "harvest", "crop" scenario "Creating a new harvest", :js => true do - fill_autocomplete "crop", :with => "m" + fill_autocomplete "crop", :with => "mai" select_from_autocomplete "maize" within "form#new_harvest" do fill_in "When?", :with => "2014-06-15" diff --git a/spec/features/plantings/planting_a_crop_spec.rb b/spec/features/plantings/planting_a_crop_spec.rb index 16b47973f..bda85fadc 100644 --- a/spec/features/plantings/planting_a_crop_spec.rb +++ b/spec/features/plantings/planting_a_crop_spec.rb @@ -9,12 +9,13 @@ feature "Planting a crop", :js => true do background do login_as(member) visit new_planting_path + sync_elasticsearch([maize]) end it_behaves_like "crop suggest", "planting" scenario "Creating a new planting" do - fill_autocomplete "crop", :with => "m" + fill_autocomplete "crop", :with => "mai" select_from_autocomplete "maize" within "form#new_planting" do fill_in "When", :with => "2014-06-15" @@ -41,7 +42,7 @@ feature "Planting a crop", :js => true do end scenario "Marking a planting as finished" do - fill_autocomplete "crop", :with => "m" + fill_autocomplete "crop", :with => "mai" select_from_autocomplete "maize" within "form#new_planting" do fill_in "When?", :with => "2014-07-01" @@ -77,7 +78,7 @@ feature "Planting a crop", :js => true do end scenario "Marking a planting as finished without a date" do - fill_autocomplete "crop", :with => "m" + fill_autocomplete "crop", :with => "mai" select_from_autocomplete "maize" within "form#new_planting" do check "Mark as finished" diff --git a/spec/features/seeds/adding_seeds_spec.rb b/spec/features/seeds/adding_seeds_spec.rb index 668771850..f42c05758 100644 --- a/spec/features/seeds/adding_seeds_spec.rb +++ b/spec/features/seeds/adding_seeds_spec.rb @@ -7,12 +7,13 @@ feature "Harvesting a crop", :js => true do background do login_as(member) visit new_seed_path + sync_elasticsearch([maize]) end it_behaves_like "crop suggest", "seed", "crop" scenario "Adding a new seed", :js => true do - fill_autocomplete "crop", :with => "m" + fill_autocomplete "crop", :with => "mai" select_from_autocomplete "maize" within "form#new_seed" do fill_in "Quantity:", :with => 42 diff --git a/spec/features/shared_examples/crop_suggest.rb b/spec/features/shared_examples/crop_suggest.rb index 141dfbabf..9856282ca 100644 --- a/spec/features/shared_examples/crop_suggest.rb +++ b/spec/features/shared_examples/crop_suggest.rb @@ -1,29 +1,39 @@ require 'rails_helper' shared_examples "crop suggest" do |resource| - let!(:popcorn) { FactoryGirl.create(:popcorn) } + let!(:pea) { FactoryGirl.create(:crop, :name => 'pea') } let!(:pear) { FactoryGirl.create(:pear) } let!(:tomato) { FactoryGirl.create(:tomato) } let!(:roma) { FactoryGirl.create(:roma) } + background do + sync_elasticsearch([pea, pear, maize, tomato]) + end + scenario "See text in crop auto suggest field" do expect(page).to have_selector("input[placeholder='e.g. lettuce']") end scenario "Typing in the crop name displays suggestions" do within "form#new_#{resource}" do - fill_autocomplete "crop", :with => "p" + fill_autocomplete "crop", :with => "pe" + end + + expect(page).to_not have_content("pear") + expect(page).to_not have_content("pea") + + within "form#new_#{resource}" do + fill_autocomplete "crop", :with => "pea" end expect(page).to have_content("pear") - expect(page).to have_content("popcorn") + expect(page).to have_content("pea") within "form#new_#{resource}" do fill_autocomplete "crop", :with => "pear" end expect(page).to have_content("pear") - expect(page).to_not have_content("popcorn") select_from_autocomplete("pear") @@ -32,16 +42,16 @@ shared_examples "crop suggest" do |resource| scenario "Typing and pausing does not affect input" do within "form#new_#{resource}" do - fill_autocomplete "crop", :with => "p" + fill_autocomplete "crop", :with => "pea" end expect(page).to have_content("pear") - expect(find_field("crop").value).to eq("p") + expect(find_field("crop").value).to eq("pea") end scenario "Searching for a crop casts a wide net on results" do within "form#new_#{resource}" do - fill_autocomplete "crop", :with => "to" + fill_autocomplete "crop", :with => "tom" end expect(page).to have_content("tomato") diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 012a74ed5..9cda818e8 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -328,8 +328,7 @@ describe Crop do context "search" do before :each do @mushroom = FactoryGirl.create(:crop, :name => 'mushroom') - @mushroom.__elasticsearch__.index_document - Crop.__elasticsearch__.refresh_index! + sync_elasticsearch([@mushroom]) end it "finds exact matches" do Crop.search('mushroom').should eq [@mushroom] diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 9b5ec34cd..b3e908378 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -14,7 +14,6 @@ RSpec.configure do |config| config.before(:each) do DatabaseCleaner.start - Crop.__elasticsearch__.create_index! force: true end config.after(:each) do diff --git a/spec/support/elastisearch_helpers.rb b/spec/support/elastisearch_helpers.rb new file mode 100644 index 000000000..0e80ea321 --- /dev/null +++ b/spec/support/elastisearch_helpers.rb @@ -0,0 +1,18 @@ +module ElasticsearchHelpers + def sync_elasticsearch(crops) + if ENV['GROWSTUFF_ELASTICSEARCH'] == "true" + crops.each {|crop| crop.__elasticsearch__.index_document} + Crop.__elasticsearch__.refresh_index! + end + end +end + +RSpec.configure do |config| + config.include ElasticsearchHelpers + + config.before(:each) do + if ENV['GROWSTUFF_ELASTICSEARCH'] == "true" + Crop.__elasticsearch__.create_index! force: true + end + end +end \ No newline at end of file