Sha256: 19adc3f86415fb13de472fd1dd7bd1f8798b855c38c4ae4eab2e867a165c673a

Contents?: true

Size: 1.46 KB

Versions: 21

Compression:

Stored size: 1.46 KB

Contents

ENV["RAILS_ENV"] = "test"

require 'test/unit'
require 'rubygems'
require 'yaml'
require 'active_record'
require 'mysql'

require 'app/models/bhf/widget.rb'

def bhf_widget( fixture_name )
  id = @@fixtures['bhf_widget'][ fixture_name.to_s ][ 'id' ]
  Bhf::Widget.find( id )
end

def load_schema
  config = YAML::load( IO.read( File.dirname(__FILE__) + '/database.yml') )

  # Manually initialize the database
  conn = Mysql.real_connect( config['mysql']['host'], config['mysql']['username'], config['mysql']['password'] )
  conn.query( "CREATE DATABASE IF NOT EXISTS #{config['mysql']['database']}" )

  ActiveRecord::Base.establish_connection( config['mysql'] )
  ActiveRecord::Base.connection()

  load(File.dirname(__FILE__) + "/../lib/rails/generators/bhf/templates/schema.rb")

  @@fixtures = {}

  load_fixture( 'bhf_widget' )
end

def load_fixture( table )
  @@fixtures[ table ] = {}
  fixture = YAML::load( IO.read( File.dirname(__FILE__) + "/fixtures/#{table}.yml") )
  @@fixtures[ table ] = fixture

  klass = class_eval table.titleize.gsub(/ /, '::')

  fixture.each do |record_name, record|
    record.each do |column, value|
      if ( match = column.match(/(.*)_id/) )
        fixture_reference = "bhf_" + match[1].pluralize
        if value.is_a? Symbol
          r = class_eval "#{fixture_reference}( '#{value}' )"
          record[ column ] = r.id
        end
      end
    end

    r = klass.create( record )
    @@fixtures[ table ][ record_name ][ 'id' ] = r.id
  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bhf-1.0.0.beta16 test/test_helper.rb
bhf-1.0.0.beta15 test/test_helper.rb
bhf-1.0.0.beta14 test/test_helper.rb
bhf-1.0.0.beta13 test/test_helper.rb
bhf-1.0.0.beta12 test/test_helper.rb
bhf-1.0.0.beta11 test/test_helper.rb
bhf-1.0.0.beta10 test/test_helper.rb
bhf-1.0.0.beta9 test/test_helper.rb
bhf-1.0.0.beta8 test/test_helper.rb
bhf-1.0.0.beta7 test/test_helper.rb
bhf-1.0.0.beta6 test/test_helper.rb
bhf-1.0.0.beta5 test/test_helper.rb
bhf-1.0.0.beta4 test/test_helper.rb
bhf-1.0.0.beta3 test/test_helper.rb
bhf-1.0.0.beta2 test/test_helper.rb
bhf-1.0.0.beta1 test/test_helper.rb
bhf-0.10.17 test/test_helper.rb
bhf-0.10.16 test/test_helper.rb
bhf-0.10.15 test/test_helper.rb
bhf-0.10.14 test/test_helper.rb