I banged my head on this for a while... but here's essentially what it comes
down to.
################ test/test_helper.rb
if ENV['TEST_TS'] # test Thinking Sphinx based features
unless $ts_setup
# set updates and deltas to be enabled
ThinkingSphinx.updates_enabled = true
ThinkingSphinx.deltas_enabled = true
ThinkingSphinx::Configuration.instance.load_models
$ts_setup = true
$ts_debug = ENV['TEST_TS'].to_i == 2
end
end
def index_database!
begin # based on
vendor/plugins/thinking-sphinx/lib/thinking_sphinx/tasks.rb
puts "== ThinkingSphinx enabled" if $ts_debug
config = ThinkingSphinx::Configuration.instance
FileUtils.mkdir_p config.searchd_file_path
if ThinkingSphinx.sphinx_running?
cmd = "#{config.bin_path}searchd --stop --config
#{config.config_file}"
print "Stopping..." if $ts_debug; STDOUT.flush
system "%s > /tmp/sphinx.log 2>&1" % cmd
puts "done." if $ts_debug
end
Dir["#{config.searchd_file_path}/*.spl"].each { |file| File.delete(file)
}
cmd = "#{config.bin_path}indexer --config #{config.config_file} --all" #
reindex
print "Reindexing..." if $ts_debug; STDOUT.flush
system "%s > /tmp/sphinx.log 2>&1" % cmd
puts "done." if $ts_debug
cmd = "#{config.bin_path}searchd --pidfile --config
#{config.config_file}"
print "Starting..." if $ts_debug; STDOUT.flush
system "%s > /tmp/sphinx.log 2>&1" % cmd
puts "done." if $ts_debug
sleep(2)
if ThinkingSphinx.sphinx_running?
puts "Started successfully (pid #{ThinkingSphinx.sphinx_pid})." if
$ts_debug
else
puts "[ERROR] Failed to start searchd daemon. Check
#{config.searchd_log_file}." if $ts_debug
end
rescue Exception => e
puts e.message
end
end
################ {model}_test.rb
if ENV['TEST_TS']
##
## Deltas Indexed by Workling Job
##
class ModelSearchTest < ActiveSupport::TestCase
self.use_transactional_fixtures = false
def test_thinking_sphinx_is_running
assert ThinkingSphinx.sphinx_running?
end
def test_delta_indexes_are_turned_on
assert ThinkingSphinx.updates_enabled?
assert ThinkingSphinx.deltas_enabled?
end
# == In order to run:
#
# Verify searchd starts on 3313 and points to test config:
#
# RAILS_ENV=test rake ts:conf
# RAILS_ENV=test rake ts:in
# RAILS_ENV=test rake ts:start
#
# Run the test:
#
# TEST_TS=1 rake
#
# Or, more specifically:
#
# cd test; TEST_TS=1 ruby unit/{model}_test.rb; cd ..
#
# This will make sure to reindex the database with the fixture data,
start
# sphinx/searchd, and then enable any TS-based tests.
#
def test_changes_that_would_update_the_index_are_indexed_as_a_delta
RudeQueue.delete_all
index_database!
assert_equal 1, Model.search("first").size
assert_equal 0, Model.search("second").size
m = Model.search("first").first
assert s.update_attributes(:title => s.title.gsub("first", "second"))
# since we're in a transaction for fixtures, run the after_commit
callback
# ThinkingSphinx uses to call the delta callbacks manually
m.after_commit_callback
# allow the sphinx index to be applied
process_queue_items
assert_equal 0, Model.search("first").size
assert_equal 1, Model.search("second").size
end
end
end
Now, you might want to create a config/sphinx.yml that specifies the port
for the test environment that's different than your development mode...
ThinkingSphinx docs should be sufficient for this and other options, but it
could look simply like this:
test:
port: 3313
Now, as the test indicates, we need to start up the test searchd, which
includes generating the config file and indexing:
RAILS_ENV=test rake ts:conf
RAILS_ENV=test rake ts:in
RAILS_ENV=test rake ts:start
If you don't write a config/sphinx.yml file, you need to edit the
config/test.sphinx.conf file with the proper port (3313 for instance).
In this test I'm actually making sure that the delayed delta indexer we set
up using Workling works, though you could easily change the test to make
sure it's only testing searches, etc, not requiring the after_commit
callback nor having to mess with queues.
Then, run the tests:
TEST_TS=1 rake
This will run your whole suite, along with the TS specific tests, otherwise
it will skip them.
Hope this helps.
Matt
On Thu, May 21, 2009 at 2:42 PM, Pat Allan <[email protected]> wrote:
>
> Hi Colin
>
> Generally I don't recommend testing external libraries (Rails,
> Thinking Sphinx, etc). The only exception to this would be integration
> tests - but definitely not in unit tests.
>
> As for integration tests, there's no neat way to handle things just
> yet, but it's on my list.
>
> Sorry I'm not flush with helpful ideas :/
>
> --
> Pat
>
> On 21/05/2009, at 3:26 AM, Colin Ramsay wrote:
>
> >
> > So I was thinking to write a unit test to check that my searching with
> > Sphinx was working as I expected. I fired up rake
> > thinking_sphinx:start, and then wrote a test which pushed a couple of
> > records to the database and then ran Model.search("query"). However,
> > because the index had not been rebuilt since saving the records and
> > running the search, I get no results.
> >
> > What is the recommended approach for this?
> >
> > Thanks in advance,
> > Colin.
> >
> > >
>
>
> >
>
--
Matt Todd
Highgroove Studios
www.highgroove.com
cell: 404-314-2612
blog: maraby.org
Scout - Web Monitoring and Reporting Software
www.scoutapp.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en
-~----------~----~----~----~------~----~------~--~---