From 79c74e3bc87807a06c0b68156602586c6ef4f097 Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Wed, 25 Sep 2013 17:50:42 -0700 Subject: smoke tests for search --- spec/search_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/search_spec.rb diff --git a/spec/search_spec.rb b/spec/search_spec.rb new file mode 100644 index 0000000..aa222fa --- /dev/null +++ b/spec/search_spec.rb @@ -0,0 +1,27 @@ +require 'chef_zero/solr/solr_parser' +require 'chef_zero/solr/solr_doc' + +#p = ChefZero::Solr::SolrParser.new('chef_environment:prod AND roles:redis_history_server AND -redis_slaveof:[a TO z]') + +describe ChefZero::Solr::SolrParser do + let (:all_docs) do + docs = [] + [{'foo' => 1}, + {'foo' => 7}].each_with_index do |h, i| + docs.push ChefZero::Solr::SolrDoc.new(h, i) + end + docs + end + + it "handles terms" do + q = ChefZero::Solr::SolrParser.new('foo:7').parse + results = all_docs.select {|doc| q.matches_doc?(doc) } + results.size.should eq(1) + end + + it "handles ranges" do + q = ChefZero::Solr::SolrParser.new('foo:[1 TO 5]').parse + results = all_docs.select {|doc| q.matches_doc?(doc) } + results.size.should eq(1) + end +end -- cgit v1.2.1 From e1cdcdbd9b1776df95b94cbe9df5978599138e97 Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Wed, 25 Sep 2013 17:52:06 -0700 Subject: fix up to_s end brackets --- lib/chef_zero/solr/query/range_query.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chef_zero/solr/query/range_query.rb b/lib/chef_zero/solr/query/range_query.rb index db92548..4497de3 100644 --- a/lib/chef_zero/solr/query/range_query.rb +++ b/lib/chef_zero/solr/query/range_query.rb @@ -10,7 +10,7 @@ module ChefZero end def to_s - "#{@from_inclusive ? '[' : '{'}#{@from} TO #{@to}#{@to_inclusive ? '[' : '{'}" + "#{@from_inclusive ? '[' : '{'}#{@from} TO #{@to}#{@to_inclusive ? ']' : '}'}" end def matches?(key, value) -- cgit v1.2.1 From 5ad588929b4afc319c052d6640296526fa949376 Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Wed, 25 Sep 2013 18:19:42 -0700 Subject: switch to string tests for less confusion. --- spec/search_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/search_spec.rb b/spec/search_spec.rb index aa222fa..ac81acc 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -6,21 +6,21 @@ require 'chef_zero/solr/solr_doc' describe ChefZero::Solr::SolrParser do let (:all_docs) do docs = [] - [{'foo' => 1}, - {'foo' => 7}].each_with_index do |h, i| + [{'foo' => 'a'}, + {'foo' => 'd'}].each_with_index do |h, i| docs.push ChefZero::Solr::SolrDoc.new(h, i) end docs end it "handles terms" do - q = ChefZero::Solr::SolrParser.new('foo:7').parse + q = ChefZero::Solr::SolrParser.new('foo:d').parse results = all_docs.select {|doc| q.matches_doc?(doc) } results.size.should eq(1) end it "handles ranges" do - q = ChefZero::Solr::SolrParser.new('foo:[1 TO 5]').parse + q = ChefZero::Solr::SolrParser.new('foo:[a TO c]').parse results = all_docs.select {|doc| q.matches_doc?(doc) } results.size.should eq(1) end -- cgit v1.2.1 From 44a1a6c6188e87a4490d49a67e16bc5712abf70f Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Wed, 25 Sep 2013 18:20:05 -0700 Subject: logic was reversed for "to" eval --- lib/chef_zero/solr/query/range_query.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/chef_zero/solr/query/range_query.rb b/lib/chef_zero/solr/query/range_query.rb index 4497de3..afa3fa2 100644 --- a/lib/chef_zero/solr/query/range_query.rb +++ b/lib/chef_zero/solr/query/range_query.rb @@ -13,14 +13,15 @@ module ChefZero "#{@from_inclusive ? '[' : '{'}#{@from} TO #{@to}#{@to_inclusive ? ']' : '}'}" end - def matches?(key, value) + def matches_values?(values) + value = values.first case @from <=> value when -1 return false when 0 return false if !@from_inclusive end - case @to <=> value + case value <=> @to when 1 return false when 0 -- cgit v1.2.1 From bfd53dde9524f203c3495efe048005d94b8a065f Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Thu, 26 Sep 2013 10:05:17 -0700 Subject: cleanup --- spec/search_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/search_spec.rb b/spec/search_spec.rb index ac81acc..32d56e4 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -1,8 +1,6 @@ require 'chef_zero/solr/solr_parser' require 'chef_zero/solr/solr_doc' -#p = ChefZero::Solr::SolrParser.new('chef_environment:prod AND roles:redis_history_server AND -redis_slaveof:[a TO z]') - describe ChefZero::Solr::SolrParser do let (:all_docs) do docs = [] -- cgit v1.2.1 From 57f02150013c08bd121c51999ac2bbfb468e9f7b Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Sat, 28 Sep 2013 08:24:40 -0700 Subject: handle a list of values properly. add matches_doc? --- lib/chef_zero/solr/query/range_query.rb | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/chef_zero/solr/query/range_query.rb b/lib/chef_zero/solr/query/range_query.rb index afa3fa2..5428a6f 100644 --- a/lib/chef_zero/solr/query/range_query.rb +++ b/lib/chef_zero/solr/query/range_query.rb @@ -14,21 +14,28 @@ module ChefZero end def matches_values?(values) - value = values.first - case @from <=> value - when -1 - return false - when 0 - return false if !@from_inclusive + values.any? do |value| + case @from <=> value + when -1 + return false + when 0 + return false if !@from_inclusive + end + case value <=> @to + when 1 + return false + when 0 + return false if !@to_inclusive + end + return true end - case value <=> @to - when 1 - return false - when 0 - return false if !@to_inclusive - end - return true end + + def matches_doc?(doc) + matches_values?(doc[DEFAULT_FIELD]) + end + + DEFAULT_FIELD = "text" end end end -- cgit v1.2.1 From 49f6be7fcd62cdf8f2f6acd4c343c88789ecee1a Mon Sep 17 00:00:00 2001 From: Matt Gleeson Date: Mon, 30 Sep 2013 16:32:56 -0700 Subject: add support for wildcards --- lib/chef_zero/solr/query/range_query.rb | 24 ++++++++++++++---------- spec/search_spec.rb | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/chef_zero/solr/query/range_query.rb b/lib/chef_zero/solr/query/range_query.rb index 5428a6f..625c0bb 100644 --- a/lib/chef_zero/solr/query/range_query.rb +++ b/lib/chef_zero/solr/query/range_query.rb @@ -15,17 +15,21 @@ module ChefZero def matches_values?(values) values.any? do |value| - case @from <=> value - when -1 - return false - when 0 - return false if !@from_inclusive + unless @from == '*' + case @from <=> value + when -1 + return false + when 0 + return false if !@from_inclusive + end end - case value <=> @to - when 1 - return false - when 0 - return false if !@to_inclusive + unless @to == '*' + case value <=> @to + when 1 + return false + when 0 + return false if !@to_inclusive + end end return true end diff --git a/spec/search_spec.rb b/spec/search_spec.rb index 32d56e4..df0522c 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -11,15 +11,22 @@ describe ChefZero::Solr::SolrParser do docs end + def search_for(query) + q = ChefZero::Solr::SolrParser.new(query).parse + all_docs.select {|doc| q.matches_doc?(doc) } + end + it "handles terms" do - q = ChefZero::Solr::SolrParser.new('foo:d').parse - results = all_docs.select {|doc| q.matches_doc?(doc) } - results.size.should eq(1) + search_for('foo:d').size.should eq(1) end it "handles ranges" do - q = ChefZero::Solr::SolrParser.new('foo:[a TO c]').parse - results = all_docs.select {|doc| q.matches_doc?(doc) } - results.size.should eq(1) + search_for('foo:[a TO c]').size.should eq(1) + end + + it "handles wildcard ranges" do + search_for('foo:[* TO c]').size.should eq(1) + search_for('foo:[c TO *]').size.should eq(1) + search_for('foo:[* TO *]').size.should eq(2) end end -- cgit v1.2.1