Sha256: b2358c3c0f4f4caa203a02d158d2c4d13f1c8336524f6b685b2306f0a090503c

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

require 'mysql'
require 'erb'
require 'yaml'

class SphinxHelper
  attr_accessor :host, :username, :password
  attr_reader   :path
  
  def initialize
    @host     = "localhost"
    @username = "anonymous"
    @password = ""

    if File.exist?("spec/fixtures/sql/conf.yml")
      config    = YAML.load(File.open("spec/fixtures/sql/conf.yml"))
      @host     = config["host"]
      @username = config["username"]
      @password = config["password"]
    end
    
    @path = File.expand_path(File.dirname(__FILE__))
  end
  
  def setup_mysql
    server = Mysql.new @host, @username, @password

    unless server.list_dbs.include?("riddle_sphinx_spec")
      server.create_db "riddle_sphinx_spec"
    end

    server.query "USE riddle_sphinx_spec;"
    
    structure = File.open("spec/fixtures/sql/structure.sql") { |f| f.read }
    # Block ensures multiple statements can be run
    server.query(structure) { }
    data      = File.open("spec/fixtures/sql/data.sql") { |f|
      while line = f.gets
        server.query line
      end
    }

    server.close
  end
  
  def reset
    setup_mysql
    index
  end
  
  def generate_configuration
    template = File.open("spec/fixtures/sphinx/configuration.erb") { |f| f.read }
    File.open("spec/fixtures/sphinx/spec.conf", "w") { |f|
      f.puts ERB.new(template).result(binding)
    }
  end
  
  def index
    cmd = "indexer --config #{@path}/fixtures/sphinx/spec.conf --all"
    cmd << " --rotate" if running?
    `#{cmd}`
  end
  
  def start
    return if running?

    cmd = "searchd --config #{@path}/fixtures/sphinx/spec.conf"
    `#{cmd}`

    sleep(1)

    unless running?
      puts "Failed to start searchd daemon. Check fixtures/sphinx/searchd.log."
    end
  end
  
  def stop
    return unless running?
    `kill #{pid}`
  end
  
  def pid
    if File.exists?("#{@path}/fixtures/sphinx/searchd.pid")
      `cat #{@path}/fixtures/sphinx/searchd.pid`[/\d+/]
    else
      nil
    end
  end

  def running?
    pid && `ps #{pid} | wc -l`.to_i > 1
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
riddle-0.9.8.1112 spec/sphinx_helper.rb
ultrasphinx-1.11 vendor/riddle/spec/sphinx_helper.rb
ultrasphinx-1.7 vendor/riddle/spec/sphinx_helper.rb
ultrasphinx-1.8 vendor/riddle/spec/sphinx_helper.rb
ultrasphinx-1.9 vendor/riddle/spec/sphinx_helper.rb