Testing using an existing test database

Ruby N. alert…

I tried searching for an answer for this question, but the search
criteria weren’t very apparent. Here goes:

If you want to use an existing database to run your unit tests against,
how do you do that? By default, it seems that Rails attempts to
recreate the test database each time, but that’s not an option for me,
because of the fact that I’m using enterprise databases – they’re too
big to drop and recreate each time, even if I did have the permissions
to do so (which I don’t). Currently, my unit test just seems to hang and
do nothing, at least after the first run (when it appears to run and
finish, but I’m not entirely sure).

Thanks very much!

Gabe H. wrote:

Ruby N. alert…

Your secret is safe with us!

If you want to use an existing database to run your unit tests against,
how do you do that?

If you have a nice database, and if you want to write unit tests that
hit it
as fixtures, and if you already have a few healthy unit tests, run this
rake
task:

namespace :test do
desc ‘Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.’

task :extract_fixtures => :environment do
sql = “SELECT * FROM %s”
skip_tables = [“schema_info”]
ActiveRecord::Base.establish_connection
(ActiveRecord::Base.connection.tables - skip_tables).each do
|table_name|
i = “000”
File.open("#{RAILS_ROOT}/tmp/#{table_name}.yml", ‘w’) do |file|
data = ActiveRecord::Base.connection.select_all(sql %
table_name)
file.write data.inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end

end

Don’t be fooled by imitations. Some versions of test:extract_fixtures
will
overwrite your test/fixtures folder. That is absurd, because you should
already have fixtures in there.

Next, look up a plugin called fixture_scenarios. It lets you load a
batch of
fixtures from an alternate folder, not just the test/fixtures folder.

And remove records you don’t need from the “enterprise database” before
extracting them - you never need a huge amount of records just to write
tests.

And /obviously/ don’t test on the live database!

Your project probably got in this “enterprise” situation by not writing
enough unit tests…

By default, it seems that Rails attempts to
recreate the test database each time, but that’s not an option for me,
because of the fact that I’m using enterprise databases – they’re too
big to drop and recreate each time,

The main point of the fixture system is to write unit tests. You need
to
think about one or two records, and a manipulation between them. A test
case
should use the pattern Assemble Activate Assert, and the fixtures are
for
the assembly part:

def test_case
thang = thangs(:Good_Thang)
thang.activate(42)
assert{ thang.disposition() == :good }
end

You should never need a billion records to do that. But if you load a
billion records into your scenario, they will slow down testing.

even if I did have the permissions
to do so (which I don’t).

If you don’t have permission to make a copy of the main database and use
it
for tests, quit.

Currently, my unit test just seems to hang and
do nothing, at least after the first run (when it appears to run and
finish, but I’m not entirely sure).

Start with unit tests that only use a very few fixtures that you wrote
by
hand into your test/fixtures folder. Only after you get going with them
should you then write tests that use scenarios of fixtures extracted
from
your production database.


Phlip
http://www.oreillynet.com/onlamp/blog/2008/05/dynamic_languages_vs_editors.html

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs