Permalink
Browse files

Fixing sphinx_scopes (issue 19) - test suite was flawed.

  • Loading branch information...
1 parent 7a29dcf commit 131ae4c20ce92de246e21346512dc1f5c46bb4b2 @pat committed Aug 2, 2009
@@ -20,6 +20,14 @@ Feature: Sphinx Scopes
And I use the with_id scope set to 99
Then I should get 1 result
+ Scenario: Mixing Field and ID Scopes
+ Given Sphinx is running
+ And I am searching on people
+ When I use the with_first_name scope set to "Andrew"
+ And I use the ids_only scope
+ Then I should get 7 results
+ And I should have an array of integers
+
Scenario: Non-field/filter Scopes
Given Sphinx is running
And I am searching on people
View
@@ -38,7 +38,7 @@ module ThinkingSphinx
module Version #:nodoc:
Major = 1
Minor = 2
- Tiny = 4
+ Tiny = 5
String = [Major, Minor, Tiny].join('.')
end
@@ -14,7 +14,9 @@ def sphinx_scope(method, &block)
metaclass.instance_eval do
define_method(method) do |*args|
- options = block.call(*args)
+ options = {:classes => classes_option}
+ options.merge! block.call(*args)
+
ThinkingSphinx::Search.new(options)
end
end
@@ -187,6 +187,11 @@ def excerpt_for(string, model = nil)
).first
end
+ def search(*args)
+ merge_search ThinkingSphinx::Search.new(*args)
+ self
+ end
+
private
def config
@@ -624,7 +629,11 @@ def is_scope?(method)
end
def add_scope(method, *args, &block)
- search = options[:classes].first.send(method, *args, &block)
+ merge_search options[:classes].first.send(method, *args, &block)
+ end
+
+ def merge_search(search)
+ search.args.each { |arg| args << arg }
search.options.keys.each do |key|
if HashOptions.include?(key)
@@ -633,6 +642,7 @@ def add_scope(method, *args, &block)
elsif ArrayOptions.include?(key)
options[key] ||= []
options[key] += search.options[key]
+ options[key].uniq!
else
options[key] = search.options[key]
end
View
@@ -102,6 +102,11 @@ class Alpha < ActiveRecord::Base
set_property :field_weights => {"name" => 10}
end
+ sphinx_scope(:by_name) { |name|
+ {:conditions => {:name => name}}
+ }
+ sphinx_scope(:ids_only) { {:ids_only => true} }
+
def big_name
name.upcase
end
@@ -57,10 +57,14 @@
Alpha.sphinx_scope(:with_betas) { {:classes => [Beta]} }
end
- it "should return a ThinkingSphinx object" do
+ it "should return a ThinkingSphinx::Search object" do
Alpha.by_name('foo').should be_a(ThinkingSphinx::Search)
end
+ it "should set the classes option" do
+ Alpha.by_name('foo').options[:classes].should == [Alpha]
+ end
+
it "should be able to be called on a ThinkingSphinx::Search object" do
search = ThinkingSphinx::Search.new(:classes => [Alpha])
lambda {
@@ -50,6 +50,24 @@
ThinkingSphinx::Search.new.foo
}.should raise_error(NoMethodError)
end
+
+ it "should accept sphinx scopes" do
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
+
+ lambda {
+ search.by_name('Pat')
+ }.should_not raise_error(NoMethodError)
+ end
+
+ it "should return itself when using a sphinx scope" do
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
+ search.by_name('Pat').object_id.should == search.object_id
+ end
+
+ it "should keep the same search object when chaining multiple scopes" do
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
+ search.by_name('Pat').ids_only.object_id.should == search.object_id
+ end
end
describe '.search' do
@@ -881,6 +899,47 @@
@search.excerpt_for('string', Beta)
end
end
+
+ describe '#search' do
+ before :each do
+ @search = ThinkingSphinx::Search.new('word',
+ :conditions => {:field => 'field'},
+ :with => {:int => 5}
+ )
+ end
+
+ it "should return itself" do
+ @search.search.object_id.should == @search.object_id
+ end
+
+ it "should merge in arguments" do
+ @client.should_receive(:query) do |query, index, comments|
+ query.should == 'word more @field field'
+ end
+
+ @search.search('more').first
+ end
+
+ it "should merge conditions" do
+ @client.should_receive(:query) do |query, index, comments|
+ query.should match(/@name plato/)
+ query.should match(/@field field/)
+ end
+
+ @search.search(:conditions => {:name => 'plato'}).first
+ end
+
+ it "should merge filters" do
+ @search.search(:with => {:float => 1.5}).first
+
+ @client.filters.detect { |filter|
+ filter.attribute == 'float'
+ }.should_not be_nil
+ @client.filters.detect { |filter|
+ filter.attribute == 'int'
+ }.should_not be_nil
+ end
+ end
end
describe ThinkingSphinx::Search, "playing nice with Search model" do
View
@@ -2,11 +2,11 @@
Gem::Specification.new do |s|
s.name = %q{thinking-sphinx}
- s.version = "1.2.4"
+ s.version = "1.2.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Pat Allan"]
- s.date = %q{2009-07-30}
+ s.date = %q{2009-08-02}
s.description = %q{A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching.}
s.email = %q{pat@freelancing-gods.com}
s.files = ["rails/init.rb", "lib/thinking_sphinx/active_record/attribute_updates.rb", "lib/thinking_sphinx/active_record/delta.rb", "lib/thinking_sphinx/active_record/has_many_association.rb", "lib/thinking_sphinx/active_record/scopes.rb", "lib/thinking_sphinx/active_record.rb", "lib/thinking_sphinx/adapters/abstract_adapter.rb", "lib/thinking_sphinx/adapters/mysql_adapter.rb", "lib/thinking_sphinx/adapters/postgresql_adapter.rb", "lib/thinking_sphinx/association.rb", "lib/thinking_sphinx/attribute.rb", "lib/thinking_sphinx/class_facet.rb", "lib/thinking_sphinx/configuration.rb", "lib/thinking_sphinx/core/string.rb", "lib/thinking_sphinx/deltas/datetime_delta.rb", "lib/thinking_sphinx/deltas/default_delta.rb", "lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb", "lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb", "lib/thinking_sphinx/deltas/delayed_delta/job.rb", "lib/thinking_sphinx/deltas/delayed_delta.rb", "lib/thinking_sphinx/deltas.rb", "lib/thinking_sphinx/deploy/capistrano.rb", "lib/thinking_sphinx/excerpter.rb", "lib/thinking_sphinx/facet.rb", "lib/thinking_sphinx/facet_search.rb", "lib/thinking_sphinx/field.rb", "lib/thinking_sphinx/index/builder.rb", "lib/thinking_sphinx/index/faux_column.rb", "lib/thinking_sphinx/index.rb", "lib/thinking_sphinx/property.rb", "lib/thinking_sphinx/rails_additions.rb", "lib/thinking_sphinx/search.rb", "lib/thinking_sphinx/search_methods.rb", "lib/thinking_sphinx/source/internal_properties.rb", "lib/thinking_sphinx/source/sql.rb", "lib/thinking_sphinx/source.rb", "lib/thinking_sphinx/tasks.rb", "lib/thinking_sphinx.rb", "LICENCE", "README.textile", "tasks/distribution.rb", "tasks/testing.rb", "tasks/rails.rake", "vendor/after_commit", "vendor/after_commit/init.rb", "vendor/after_commit/lib", "vendor/after_commit/lib/after_commit", "vendor/after_commit/lib/after_commit/active_record.rb", "vendor/after_commit/lib/after_commit/connection_adapters.rb", "vendor/after_commit/lib/after_commit.rb", "vendor/after_commit/LICENSE", "vendor/after_commit/Rakefile", "vendor/after_commit/README", "vendor/after_commit/test", "vendor/after_commit/test/after_commit_test.rb", "vendor/delayed_job", "vendor/delayed_job/lib", "vendor/delayed_job/lib/delayed", "vendor/delayed_job/lib/delayed/job.rb", "vendor/delayed_job/lib/delayed/message_sending.rb", "vendor/delayed_job/lib/delayed/performable_method.rb", "vendor/delayed_job/lib/delayed/worker.rb", "vendor/riddle", "vendor/riddle/lib", "vendor/riddle/lib/riddle", "vendor/riddle/lib/riddle/client", "vendor/riddle/lib/riddle/client/filter.rb", "vendor/riddle/lib/riddle/client/message.rb", "vendor/riddle/lib/riddle/client/response.rb", "vendor/riddle/lib/riddle/client.rb", "vendor/riddle/lib/riddle/configuration", "vendor/riddle/lib/riddle/configuration/distributed_index.rb", "vendor/riddle/lib/riddle/configuration/index.rb", "vendor/riddle/lib/riddle/configuration/indexer.rb", "vendor/riddle/lib/riddle/configuration/remote_index.rb", "vendor/riddle/lib/riddle/configuration/searchd.rb", "vendor/riddle/lib/riddle/configuration/section.rb", "vendor/riddle/lib/riddle/configuration/source.rb", "vendor/riddle/lib/riddle/configuration/sql_source.rb", "vendor/riddle/lib/riddle/configuration/xml_source.rb", "vendor/riddle/lib/riddle/configuration.rb", "vendor/riddle/lib/riddle/controller.rb", "vendor/riddle/lib/riddle.rb", "spec/lib/thinking_sphinx/active_record/delta_spec.rb", "spec/lib/thinking_sphinx/active_record/has_many_association_spec.rb", "spec/lib/thinking_sphinx/active_record/scopes_spec.rb", "spec/lib/thinking_sphinx/active_record_spec.rb", "spec/lib/thinking_sphinx/association_spec.rb", "spec/lib/thinking_sphinx/attribute_spec.rb", "spec/lib/thinking_sphinx/configuration_spec.rb", "spec/lib/thinking_sphinx/core/string_spec.rb", "spec/lib/thinking_sphinx/excerpter_spec.rb", "spec/lib/thinking_sphinx/facet_search_spec.rb", "spec/lib/thinking_sphinx/facet_spec.rb", "spec/lib/thinking_sphinx/field_spec.rb", "spec/lib/thinking_sphinx/index/builder_spec.rb", "spec/lib/thinking_sphinx/index/faux_column_spec.rb", "spec/lib/thinking_sphinx/index_spec.rb", "spec/lib/thinking_sphinx/rails_additions_spec.rb", "spec/lib/thinking_sphinx/search_methods_spec.rb", "spec/lib/thinking_sphinx/search_spec.rb", "spec/lib/thinking_sphinx/source_spec.rb", "spec/lib/thinking_sphinx_spec.rb"]

0 comments on commit 131ae4c

Please sign in to comment.