Sha256: bb239e4e4762480573aae4bb2b66fc0b52bd457e6b57f6a012f1f3f10b0b08d0

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

#
#  Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
#  Schmidt
#

require File.expand_path(File.dirname(__FILE__)) + "/../bootstrap"

require 'active_record'
require 'lhm/table'

module IntegrationHelper

  #
  # Connectivity
  #

  def connect!
    ActiveRecord::Base.establish_connection(
      :adapter => 'mysql',
      :database => 'lhm',
      :username => '',
      :host => 'localhost'
    )

    ActiveRecord::Migration.verbose = !!ENV["VERBOSE"]
  end

  def connection
    ActiveRecord::Base.connection
  end

  def select_one(*args)
    connection.select_one(*args)
  end

  def select_value(*args)
    connection.select_value(*args)
  end

  def execute(*args)
    connection.execute(*args)
  end

  #
  # Test Data
  #

  def fixture(name)
    File.read($fixtures.join("#{ name }.ddl"))
  end

  def table_create(fixture_name)
    execute "drop table if exists `#{ fixture_name }`"
    execute fixture(fixture_name)
    table_read(fixture_name)
  end

  def table_read(fixture_name)
    Lhm::Table.parse(fixture_name, connection)
  end

  def table_exists?(table)
    connection.table_exists?(table.name)
  end

  #
  # Database Helpers
  #

  def count(table, column, value)
    query = "select count(*) from #{ table } where #{ column } = '#{ value }'"
    select_value(query).to_i
  end

  def count_all(table)
    query = "select count(*) from #{ table }"
    select_value(query).to_i
  end

  def key?(table, cols, type = :non_unique)
    non_unique = type == :non_unique ? 1 : 0
    query = "show indexes in #{ table.name } where key_name = '#{ table.idx_name(cols) }' and non_unique = #{ non_unique }"
    !!select_value(query)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lhm-1.0.0.rc2 spec/integration/integration_helper.rb