Testing with foreign keys

I’m looking for a plugin/whatever for preloading fixtures, to run
tests with FKs defined.

Specifying load order just doesn’t work for me, I need to purge the
test db, load all fixtures, and only then create the FK constraints.

Any suggestions?

Isak

On 11/10/06, Isak H. [email protected] wrote:

Isak

Isak,

I use the foreign key support offered by the redhillonrails_core plugin.

Redhill plugins:
http://www.redhillconsulting.com.au/rails_plugins.html

redhillonrails_core plugin svn repo:
svn://rubyforge.org/var/svn/redhillonrails/trunk/vendor/plugins/redhillonrails_core

To run tests I just clone the development database.

$ rake db:test:clone_structure

Now your test database has all your foreign keys. Just specify your
fixtures in order (in your tests) and you shouldn’t have any problems.
For example if a customer belongs to an account and has a foreign key
contraint customers(account_id) to accounts(id) then the following
order will load the fixtures fine.

class CustomersControllerTest < Test::Unit::TestCase
fixtures :accounts, :customers
end

If you really want to preload all the fixtures then just make a custom
rake task:

desc “Load fixtures into the current environment’s database in order”
task :load => :environment do
require ‘active_record/fixtures’
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
[
:accounts,
:customers,
:jobs
].each{|f| Fixtures.create_fixtures(‘test/fixtures’, f) }
end

Hope this helps.


Zack C.
http://depixelate.com

What we’re doing is define all the fixtures in test_helper.rb so you
don’t have to define them in each test file.