Rick Bradley
2005-Oct-28 17:24 UTC
[solution] Testing w/ fixtures on databases with no-delete constraints
As I wrote some time ago in the thread entitled "An
''Enterprise'' Rails
story and a help with nested transaction support", [0] we were having
problem with running tests with fixtures due to the fact that many of
our tables have auditing constraints, including prohibitions on the
deletion of rows.
We managed to get past this problem, not by adding nested transaction
support to AR (which I hear occasionally is "still on the way"), but
by intercepting insertion and deletion of fixtures before they hit the
database. Now things are working well for us.
Our (0.14.2) test/test_helper.rb file looks like this:
#----------------------------- snip ----------------------------------------
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) +
"/../config/environment")
require ''test_help''
# neuter fixture deletions and multiple fixture insertions to work with database
constraints
class Fixture
attr_reader :class_name
end
class Fixtures
@@inserted_fixture_list ||= {}
def insert_fixtures
return if @@inserted_fixture_list[values[0].class_name]
@@inserted_fixture_list[values[0].class_name] = true
values.each do |fixture|
@connection.execute "INSERT INTO #{@table_name} (#{fixture.key_list})
VALUES (#{fixture.value_list})", ''Fixture Insert''
end
end
def delete_existing_fixtures() end
end
class Test::Unit::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = true
end
#----------------------------- snip ----------------------------------------
[0] http://article.gmane.org/gmane.comp.lang.ruby.rails/24863
Rick
--
http://www.rickbradley.com MUPRN: 33
| of the tightest bass
random email haiku | players you''ll ever see. He
| is incredible.
Rick Bradley
2005-Oct-28 17:48 UTC
Re: [solution] Testing w/ fixtures on databases with no-delete constraints
Sorry to reply to myself, but this should actually be just a tad more
robust for test/test_helper.rb (simply delegating to the original
insert_fixtures method -- I had started out this way but lost the alias
and delegation along the way and neglected to put it back):
------------------------ snip ----------------------------------------------
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) +
"/../config/environment")
require ''test_help''
# neuter fixture deletions and multiple fixture insertions to work with database
constraints
class Fixture
attr_reader :class_name
end
class Fixtures
@@inserted_fixture_list ||= {}
alias :original_insert_fixtures :insert_fixtures
def insert_fixtures
return if @@inserted_fixture_list[values[0].class_name]
@@inserted_fixture_list[values[0].class_name] = true
original_insert_fixtures
end
def delete_existing_fixtures() end
end
class Test::Unit::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = true
end
------------------------ snip ----------------------------------------------
Jeremy Kemper reminded me that instantiated fixtures are a lot slower
than not, so adjust for your particular tastes.
Rick
--
http://www.rickbradley.com MUPRN: 495
| worries about the
random email haiku | pre-valet-a-tor... all we
| need is some good temp.