Skip to content

Commit

Permalink
Getting Sphinx working within specs, to try to get a better collectio…
Browse files Browse the repository at this point in the history
…n class happening
  • Loading branch information
pat committed Jul 22, 2008
1 parent 662cd9d commit b881199
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
*.tmproj
rdoc
spec/fixtures/database.yml
tmp
2 changes: 1 addition & 1 deletion lib/thinking_sphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module ThinkingSphinx
module Version #:nodoc:
Major = 0
Minor = 9
Tiny = 7
Tiny = 8

String = [Major, Minor, Tiny].join('.')
end
Expand Down
27 changes: 14 additions & 13 deletions lib/thinking_sphinx/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Configuration
:pid_file, :searchd_file_path, :address, :port, :allow_star,
:min_prefix_len, :min_infix_len, :mem_limit, :max_matches, :morphology,
:charset_type, :charset_table, :ignore_chars, :html_strip,
:html_remove_elements, :app_root
:html_remove_elements, :database_yml_file, :app_root

attr_reader :environment

Expand All @@ -58,6 +58,7 @@ def initialize(app_root = Dir.pwd)
self.app_root = Merb.root if defined?(Merb)
self.app_root ||= app_root

self.database_yml_file = "#{self.app_root}/config/database.yml"
self.config_file = "#{self.app_root}/config/#{environment}.sphinx.conf"
self.searchd_log_file = "#{self.app_root}/log/searchd.log"
self.query_log_file = "#{self.app_root}/log/searchd.query.log"
Expand Down Expand Up @@ -97,7 +98,7 @@ def environment
def build(file_path=nil)
load_models
file_path ||= "#{self.config_file}"
database_confs = YAML::load(ERB.new(IO.read("#{app_root}/config/database.yml")).result)
database_confs = YAML::load(ERB.new(IO.read("#{self.database_yml_file}")).result)
database_confs.symbolize_keys!
database_conf = database_confs[environment.to_sym]
database_conf.symbolize_keys!
Expand Down Expand Up @@ -130,14 +131,14 @@ def build(file_path=nil)
infixed_fields = []

model.indexes.each_with_index do |index, i|
file.write index.to_config(i, database_conf, charset_type)
file.write index.to_config(model, i, database_conf, charset_type)

create_array_accum if index.adapter == :postgres
sources << "#{model.indexes.first.name}_#{i}_core"
delta_sources << "#{model.indexes.first.name}_#{i}_delta" if index.delta?
sources << "#{ThinkingSphinx::Index.name(model)}_#{i}_core"
delta_sources << "#{ThinkingSphinx::Index.name(model)}_#{i}_delta" if index.delta?
end

source_list = sources.collect { |s| "source = #{s}" }.join("\n")
source_list = sources.collect { |s| "source = #{s}" }.join("\n")
delta_list = delta_sources.collect { |s| "source = #{s}" }.join("\n")

file.write core_index_for_model(model, source_list)
Expand Down Expand Up @@ -194,10 +195,10 @@ def parse_config
def core_index_for_model(model, sources)
output = <<-INDEX
index #{model.indexes.first.name}_core
index #{ThinkingSphinx::Index.name(model)}_core
{
#{sources}
path = #{self.searchd_file_path}/#{model.indexes.first.name}_core
path = #{self.searchd_file_path}/#{ThinkingSphinx::Index.name(model)}_core
charset_type = #{self.charset_type}
INDEX

Expand Down Expand Up @@ -230,22 +231,22 @@ def core_index_for_model(model, sources)

def delta_index_for_model(model, sources)
<<-INDEX
index #{model.indexes.first.name}_delta : #{model.indexes.first.name}_core
index #{ThinkingSphinx::Index.name(model)}_delta : #{ThinkingSphinx::Index.name(model)}_core
{
#{sources}
path = #{self.searchd_file_path}/#{model.indexes.first.name}_delta
path = #{self.searchd_file_path}/#{ThinkingSphinx::Index.name(model)}_delta
}
INDEX
end

def distributed_index_for_model(model)
sources = ["local = #{model.indexes.first.name}_core"]
sources = ["local = #{ThinkingSphinx::Index.name(model)}_core"]
if model.indexes.any? { |index| index.delta? }
sources << "local = #{model.indexes.first.name}_delta"
sources << "local = #{ThinkingSphinx::Index.name(model)}_delta"
end

<<-INDEX
index #{model.indexes.first.name}
index #{ThinkingSphinx::Index.name(model)}
{
type = distributed
#{ sources.join("\n ") }
Expand Down
10 changes: 7 additions & 3 deletions lib/thinking_sphinx/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def initialize(model, &block)
end

def name
self.class.name(@model)
end

def self.name(model)
model.name.underscore.tr(':/\\', '_')
end

Expand All @@ -46,7 +50,7 @@ def empty?(part = :core)
File.size?("#{config.searchd_file_path}/#{self.name}_#{part}.spa").nil?
end

def to_config(index, database_conf, charset_type)
def to_config(model, index, database_conf, charset_type)
# Set up associations and joins
link!

Expand All @@ -65,7 +69,7 @@ def to_config(index, database_conf, charset_type)

config = <<-SOURCE
source #{model.indexes.first.name}_#{index}_core
source #{self.class.name(model)}_#{index}_core
{
type = #{db_adapter}
sql_host = #{database_conf[:host] || "localhost"}
Expand All @@ -86,7 +90,7 @@ def to_config(index, database_conf, charset_type)
if delta?
config += <<-SOURCE
source #{model.indexes.first.name}_#{index}_delta : #{model.indexes.first.name}_#{index}_core
source #{self.class.name(model)}_#{index}_delta : #{self.class.name(model)}_#{index}_core
{
sql_query_pre =
sql_query_pre = #{charset_type == "utf-8" && adapter == :mysql ? "SET NAMES utf8" : ""}
Expand Down
9 changes: 6 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
sphinx.setup_mysql
require 'spec/fixtures/models'

# puts "Models:", ThinkingSphinx.indexed_models

config.before :all do
%w( tmp tmp/config tmp/log tmp/db ).each do |path|
FileUtils.mkdir_p "#{Dir.pwd}/#{path}"
end

Kernel.const_set :RAILS_ROOT, "#{Dir.pwd}/tmp" unless defined?(RAILS_ROOT)

# sphinx.start
sphinx.setup_sphinx
sphinx.start
end

config.before :each do
Expand All @@ -30,8 +33,8 @@
end

config.after :all do
# sphinx.stop
sphinx.stop

FileUtils.rm_r "#{Dir.pwd}/tmp"
# FileUtils.rm_r "#{Dir.pwd}/tmp"
end
end
55 changes: 55 additions & 0 deletions spec/sphinx_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,62 @@ def setup_mysql
}
end

def setup_sphinx
@configuration = ThinkingSphinx::Configuration.new
File.open("spec/fixtures/sphinx/database.yml", "w") do |file|
YAML.dump({@configuration.environment => {
:adapter => 'mysql',
:host => @host,
:database => "thinking_sphinx",
:username => @username,
:password => @password
}}, file)
end
FileUtils.mkdir_p(@configuration.searchd_file_path)

@configuration.database_yml_file = "spec/fixtures/sphinx/database.yml"
@configuration.build

index
end

def reset
setup_mysql
end

def index
cmd = "indexer --config #{@configuration.config_file} --all"
cmd << " --rotate" if running?
`#{cmd}`
end

def start
return if running?

cmd = "searchd --config #{@configuration.config_file}"
`#{cmd}`

sleep(1)

unless running?
puts "Failed to start searchd daemon. Check #{@configuration.searchd_log_file}."
end
end

def stop
return unless running?
`kill #{pid}`
end

def pid
if File.exists?("#{@configuration.pid_file}")
`cat #{@configuration.pid_file}`[/\d+/]
else
nil
end
end

def running?
pid && `ps #{pid} | wc -l`.to_i > 1
end
end
120 changes: 79 additions & 41 deletions spec/unit/thinking_sphinx/search_spec.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
require 'spec/spec_helper'

describe ThinkingSphinx::Search do
describe "search_for_id method" do
before :each do
@client = Riddle::Client.stub_instance(
:filters => [],
:filters= => true,
:id_range= => true,
:query => {
:matches => []
}
)

ThinkingSphinx::Search.stub_methods(
:client_from_options => @client,
:search_conditions => ["", []]
)
end

it "should set the client id range to focus on the given id" do
ThinkingSphinx::Search.search_for_id 42, "an_index"

@client.should have_received(:id_range=).with(42..42)
end

it "should query on the given index" do
ThinkingSphinx::Search.search_for_id 42, "an_index"

@client.should have_received(:query).with("", "an_index")
end

it "should return true if a record is returned" do
@client.stub_method(:query => {
:matches => [24]
})

ThinkingSphinx::Search.search_for_id(42, "an_index").should be_true
end

it "should return false if no records are returned" do
ThinkingSphinx::Search.search_for_id(42, "an_index").should be_false
end
end
# describe "search_for_id method" do
# before :each do
# @client = Riddle::Client.stub_instance(
# :filters => [],
# :filters= => true,
# :id_range= => true,
# :query => {
# :matches => []
# }
# )
#
# ThinkingSphinx::Search.stub_methods(
# :client_from_options => @client,
# :search_conditions => ["", []]
# )
# end
#
# after :each do
# ThinkingSphinx::Search.unstub_method(:client_from_options)
# end
#
# it "should set the client id range to focus on the given id" do
# ThinkingSphinx::Search.search_for_id 42, "an_index"
#
# @client.should have_received(:id_range=).with(42..42)
# end
#
# it "should query on the given index" do
# ThinkingSphinx::Search.search_for_id 42, "an_index"
#
# @client.should have_received(:query).with("", "an_index")
# end
#
# it "should return true if a record is returned" do
# @client.stub_method(:query => {
# :matches => [24]
# })
#
# ThinkingSphinx::Search.search_for_id(42, "an_index").should be_true
# end
#
# it "should return false if no records are returned" do
# ThinkingSphinx::Search.search_for_id(42, "an_index").should be_false
# end
# end

describe "instance_from_result method" do
before :each do
Expand Down Expand Up @@ -160,4 +164,38 @@
).should == [@person_a, @person_b, @person_c]
end
end

describe "search result" do
before :each do
@results = ThinkingSphinx::Search.search ""
end

it "should respond to previous_page" do
#
end

it "should respond to next_page" do
#
end

it "should respond to current_page" do
#
end

it "should respond to total_pages" do
#
end

it "should respond to total_entries" do
#
end

it "should respond to offset" do
#
end

it "should be a subclass of Array" do
#
end
end
end
2 changes: 1 addition & 1 deletion thinking-sphinx.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "thinking-sphinx"
s.version = "0.9.7"
s.version = "0.9.8"
s.summary = "A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching."
s.description = "A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching."
s.author = "Pat Allan"
Expand Down

0 comments on commit b881199

Please sign in to comment.