Skip to content

Commit

Permalink
Tidy up fixture/record comparison by using Fixtures to do the dirty work
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Aug 11, 2010
1 parent 39767b1 commit 5f1fb7a
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions features/step_definitions/database_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,34 @@
records = class_for_table(table_name).all
fixtures = fixtures_for_table(table_name)
records.count.should == fixtures.length
records.each {|record| match_fixture_file_against_record(record, table_name)}
records.each {|record| match_fixture_file_against_record(record)}
end

module FixtureHelpers
def read_fixture_yml(filepath)
yml = File.read filepath
announce "#{filepath} YAML:\n" << yml if @announce
yml
def fixture_path_for(record)
fixture_id_path = ("%08d" % record.id).scan(/..../).join('/')
File.join current_dir, "db/dump", record.class.table_name, "#{fixture_id_path}.yml"
end

def fixture_path_for(record, table_name)
fixture_id_path = ("%08d" % record.id).scan(/..../).join('/')
File.join current_dir, "db/dump", table_name, "#{fixture_id_path}.yml"
def replace_record_with_fixture!(record)
require 'active_record/fixtures'
fixture_path =fixture_path_for(record)
fixture = Fixture.new(YAML.load(File.read(fixture_path)), record.class)
record.destroy
ActiveRecord::Base.connection.insert_fixture fixture, record.class.table_name
record.class.find(record.id)
end

def hash_from_yml(yml)
YAML.parse(yml).transform
end

def match_fixture_file_against_record(record, table_name)
yml_hash = hash_from_yml read_fixture_yml(fixture_path_for(record, table_name))
convert_hash_timestamp_strings_to_datetimes! yml_hash
record.attributes.should == yml_hash
announce "#{table_name.singularize} #{record.id} data matches its fixture data #{fixture_path_for(record, table_name)}" if @announce
def match_fixture_file_against_record(record)
record.attributes.dup.should == replace_record_with_fixture!(record).attributes
announce "#{table_name.singularize} #{record.id} data matches its fixture data #{fixture_path_for(record)}" if @announce
end

def fixtures_for_table(table_name)
fixtures = Dir.glob File.join(current_dir, "db/dump", table_name, '**', '*.yml')
announce "Fixtures for #{table_name}:\n#{fixtures.join("\n")}\n" if @announce
fixtures
end

def convert_hash_timestamp_strings_to_datetimes!(hash)
%w(updated_at created_at).each {|datetime| hash[datetime] = DateTime.parse(hash[datetime])}
end
end
World(FixtureHelpers)

Expand Down

0 comments on commit 5f1fb7a

Please sign in to comment.