I’ve read posts stating that this is due to transactional fixtures, but it looks like we’re not using those. I’ve read other posts that suggest I do some funky stopping, restarting, and reindexing before each test run. That doesn’t seem to help.
ThinkingSphinx runs just fine in the app, but in test it behaves very strangely. After creating a couple of Organization models in my spec, I run a sphinx search. It has the two records that I’m expecting, but also a lot of nils. That’s annoying, but I add a :retry_stale => true and those go away, so I’m satisfied with this first query:
(rdb:1) p Organization.search '', :retry_stale => true
[#<Organization id: 1, ein: nil, name: "Allina", created_at: "2012-09-11 15:03:04", updated_at: "2012-09-11 15:03:04", parent_id: nil, main_phone: "7867685187x894", fax: nil, email: nil, url: nil, crm_id: 1, target_market: false, notes: nil, form990_notes: nil, mec_revenue: 750000.0, legacy_id: nil, teaching_program_type_id: nil, sponsorship_type_id: nil, is_member_council_teaching_hospitals: nil, has_teaching_program: nil, is_major_academic_medical_center: nil, does_participate_in_survey: nil, status: "modified", net_revenue: nil, gross_revenue: nil, source_name: nil, source_id: nil, import_concat_key: nil, bigtime_id: nil, is_non_hc: nil, is_client: nil, contact_record_id: nil, form990_legacy_id: nil, number_of_beds: nil, tax_exempt_status: nil, delta: true>, #<Organization id: 2, ein: nil, name: "HealthPartners", created_at: "2012-09-11 15:03:04", updated_at: "2012-09-11 15:03:04", parent_id: nil, main_phone: "3407862693x0935", fax: nil, email: nil, url: nil, crm_id: 1, target_market: false, notes: nil, form990_notes: nil, mec_revenue: nil, legacy_id: nil, teaching_program_type_id: nil, sponsorship_type_id: nil, is_member_council_teaching_hospitals: nil, has_teaching_program: nil, is_major_academic_medical_center: nil, does_participate_in_survey: nil, status: "modified", net_revenue: nil, gross_revenue: nil, source_name: nil, source_id: nil, import_concat_key: nil, bigtime_id: nil, is_non_hc: nil, is_client: nil, contact_record_id: nil, form990_legacy_id: nil, number_of_beds: nil, tax_exempt_status: nil, delta: true>]
But now if I try to search by name, which is most definitely indexed on my model, I don’t get my record.
(rdb:1) p Organization.search 'Allina', :retry_stale => true
[]
I tried to modify the options to no avail, so I thought I’d just try searching for single letters in star mode. The results are disturbing.
(rdb:1) p Organization.search 'C', :star => true, :retry_stale => true
[#<Organization id: 1, ein: nil, name: "Allina", created_at: "2012-09-11 15:03:04", updated_at: "2012-09-11 15:03:04", parent_id: nil, main_phone: "7867685187x894", fax: nil, email: nil, url: nil, crm_id: 1, target_market: false, notes: nil, form990_notes: nil, mec_revenue: 750000.0, legacy_id: nil, teaching_program_type_id: nil, sponsorship_type_id: nil, is_member_council_teaching_hospitals: nil, has_teaching_program: nil, is_major_academic_medical_center: nil, does_participate_in_survey: nil, status: "modified", net_revenue: nil, gross_revenue: nil, source_name: nil, source_id: nil, import_concat_key: nil, bigtime_id: nil, is_non_hc: nil, is_client: nil, contact_record_id: nil, form990_legacy_id: nil, number_of_beds: nil, tax_exempt_status: nil, delta: true>]
There is clearly no letter ‘C’ in the name field. My model also indexes the status field, which is set to modified, and it indexes the state field of the address association. That value is ‘CA’, so I thought maybe ThinkingSphinx is finding that record with that index. So I thought I’d try to search for ‘CA’, and guess what happened:
(rdb:1) p Organization.search 'CA', :star => true, :retry_stale => true
[]
Nothing! What is with this wonky behavior? Ok, you guys probably want to see some configs and spec helpers and some index definitions:
config/sphinx.yml
development:
enable_star: 1
min_infix_len: 1
test:
enable_star: 1
min_infix_len: 1
spec/spec_helper.rb
Spork.prefork do
require 'headless'
headless = Headless.new(:display => 99)
headless.start
at_exit do
headless.destroy
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'database_cleaner'
#require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.treat_symbols_as_metadata_keys_with_true_values = true
config.infer_base_class_for_anonymous_controllers = false
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
config.include Warden::Test::Helpers, :type => :request
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :truncation, {
:except => %w(sponsorship_types teaching_program_types title_groups positions tax_exempt_organization_types)}
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
end
app/models/organization.rb
define_index do
indexes :name, :sortable => true
indexes address(:state), :as => :state
indexes status
set_property :delta => true
end
We wrote Thinking Spec tests in Test Unit. It worked pretty well.
Include the following in your spec/spec_helper.rb
Also, in each test, have ThinkingSphinx::Test.start in the setup and ThinkingSphinx::Test.stop in the teardown