Skip to content

Instantly share code, notes, and snippets.

@josevalim josevalim/fixture.rb
Created Jan 18, 2009

Embed
What would you like to do?
class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
def self.destroy_fixtures(table_names)
NestedScenarios.delete_tables(table_names)
end
end
module Test #:nodoc:
module Unit #:nodoc:
class TestCase #:nodoc:
superclass_delegating_accessor :scenario_path
superclass_delegating_accessor :load_root_fixtures
self.load_root_fixtures = true
def self.scenario(scenario_name = nil, options = {})
case scenario_name
when Hash
scenario_name = scenario_name.join('/')
when Symbol, String
scenario_name = scenario_name.to_s
end
self.scenario_path = "#{self.fixture_path}/#{scenario_name}/" if scenario_name
self.fixtures(:all)
end
def self.fixtures(*table_names)
if table_names.first == :all
@@root_table_names = if self.load_root_fixtures
Dir["#{self.fixture_path}/*.yml"] + Dir["#{self.fixture_path}/*.csv"]
else
[]
end
@@scenario_table_names = if self.scenario_path
Dir["#{self.scenario_path}/*.yml"] + Dir["#{self.scenario_path}/*.csv"]
else
[]
end
@@root_table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }
@@scenario_table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }
table_names = @@root_table_names + @@scenario_table_names
table_names.uniq!
else
table_names = table_names.flatten.map { |n| n.to_s }
end
self.fixture_table_names |= table_names
require_fixture_classes(table_names)
setup_fixture_accessors(table_names)
end
private
def load_fixtures
@loaded_fixtures = {}
fixtures = []
if self.load_root_fixtures
fixtures << Fixtures.create_fixtures(self.fixture_path, @@root_table_names, fixture_class_names)
end
if self.scenario_path
fixtures << Fixtures.create_fixtures(self.scenario_path, @@scenario_table_names, fixture_class_names)
end
fixtures.flatten!
unless fixtures.nil?
fixtures.each { |f| @loaded_fixtures[f.table_name] = f }
end
end
def teardown_fixtures
return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
Fixtures.destroy_fixtures(self.fixture_table_names)
unless use_transactional_fixtures?
Fixtures.reset_cache
end
# Rollback changes if a transaction is active.
if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.connection.decrement_open_transactions
end
ActiveRecord::Base.clear_active_connections!
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.