require "bundler/setup" require "pg_search" if ENV["TRAVIS"] begin require 'coveralls' Coveralls.wear! rescue LoadError end end begin if defined? JRUBY_VERSION require 'activerecord-jdbcpostgresql-adapter' error_class = ActiveRecord::JDBCError else require "pg" error_class = PGError end end begin database_user = if ENV["TRAVIS"] "postgres" else ENV["USER"] end ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :database => 'pg_search_test', :username => database_user, :min_messages => 'warning') connection = ActiveRecord::Base.connection postgresql_version = connection.send(:postgresql_version) connection.execute("SELECT 1") rescue error_class at_exit do puts "-" * 80 puts "Unable to connect to database. Please run:" puts puts " createdb pg_search_test" puts "-" * 80 end raise $! end if ENV["LOGGER"] require "logger" ActiveRecord::Base.logger = Logger.new(STDOUT) end def install_extension_if_missing(name, query, expected_result) connection = ActiveRecord::Base.connection postgresql_version = connection.send(:postgresql_version) result = connection.select_value(query) raise "Unexpected output for #{query}: #{result.inspect}" unless result.downcase == expected_result.downcase rescue => e begin if postgresql_version >= 90100 ActiveRecord::Base.connection.execute "CREATE EXTENSION #{name};" else share_path = `pg_config --sharedir`.strip ActiveRecord::Base.connection.execute File.read(File.join(share_path, 'contrib', "#{name}.sql")) puts $!.message end rescue => e2 at_exit do puts "-" * 80 puts "Please install the #{name} contrib module" puts "-" * 80 end raise e2 end end install_extension_if_missing("pg_trgm", "SELECT 'abcdef' % 'cdef'", "t") unless postgresql_version < 90000 install_extension_if_missing("unaccent", "SELECT unaccent('foo')", "foo") end install_extension_if_missing("fuzzystrmatch", "SELECT dmetaphone('foo')", "f") def load_sql(filename) connection = ActiveRecord::Base.connection file_contents = File.read(File.join(File.dirname(__FILE__), '..', 'sql', filename)) connection.execute(file_contents) end if postgresql_version < 80400 unless connection.select_value("SELECT 1 FROM pg_catalog.pg_aggregate WHERE aggfnoid = 'array_agg'::REGPROC") == "1" load_sql("array_agg.sql") end load_sql("unnest.sql") end load_sql("dmetaphone.sql") require "with_model" RSpec.configure do |config| config.extend WithModel end RSpec::Matchers::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::BuiltIn::MatchArray) DOCUMENTS_SCHEMA = lambda do |t| t.belongs_to :searchable, :polymorphic => true t.text :content t.timestamps end