From 863a6d88b8df2fba8253e4c68cb0c936dd602a2b Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Nov 2008 22:35:26 +0000 Subject: [PATCH] Check for the test schema in the helper, rather having an aaa_create_tables_test. This allows the schema to autoload even when running an individual test (as opposed to the full suite). --- activerecord/RUNNING_UNIT_TESTS | 14 +++-------- activerecord/test/cases/aaa_create_tables_test.rb | 24 --------------------- activerecord/test/cases/helper.rb | 15 +++++++++++++ 3 files changed, 19 insertions(+), 34 deletions(-) delete mode 100644 activerecord/test/cases/aaa_create_tables_test.rb diff --git a/activerecord/RUNNING_UNIT_TESTS b/activerecord/RUNNING_UNIT_TESTS index 39fc867..2e0e9a2 100644 --- a/activerecord/RUNNING_UNIT_TESTS +++ b/activerecord/RUNNING_UNIT_TESTS @@ -3,13 +3,13 @@ The default names for the test databases are "activerecord_unittest" and "activerecord_unittest2". If you want to use another database name then be sure to update the connection adapter setups you want to test with in -test/connections//connection.rb. -When you have the database online, you can import the fixture tables with -the test/schema/*.sql files. +test/connections//connection.rb. Make sure that you create database objects with the same user that you specified in connection.rb otherwise (on Postgres, at least) tests for default values will fail. +When you first run the tests the schema will be automatically loaded. + == Running with Rake The easiest way to run the unit tests is through Rake. The default task runs @@ -27,10 +27,4 @@ you can do so with: rake test_mysql TEST=test/cases/base_test.rb -That'll run the base suite using the MySQL-Ruby adapter. Some tests rely on the schema -being initialized - you can initialize the schema with: - - rake test_mysql TEST=test/cases/aaa_create_tables_test.rb - - - +That'll run the base suite using the MySQL-Ruby adapter. diff --git a/activerecord/test/cases/aaa_create_tables_test.rb b/activerecord/test/cases/aaa_create_tables_test.rb deleted file mode 100644 index 3911afa..0000000 --- a/activerecord/test/cases/aaa_create_tables_test.rb +++ /dev/null @@ -1,24 +0,0 @@ -# The filename begins with "aaa" to ensure this is the first test. -require "cases/helper" - -class AAACreateTablesTest < ActiveRecord::TestCase - self.use_transactional_fixtures = false - - def test_load_schema - eval(File.read(SCHEMA_ROOT + "/schema.rb")) - if File.exists?(adapter_specific_schema_file) - eval(File.read(adapter_specific_schema_file)) - end - assert true - end - - def test_drop_and_create_courses_table - eval(File.read(SCHEMA_ROOT + "/schema2.rb")) - assert true - end - - private - def adapter_specific_schema_file - SCHEMA_ROOT + '/' + ActiveRecord::Base.connection.adapter_name.downcase + '_specific_schema.rb' - end -end diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 2382bfe..5a6e070 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -42,6 +42,21 @@ ActiveRecord::Base.connection.class.class_eval do alias_method_chain :execute, :query_record end +# If the "accounts" table doesn't exist, we assume the schema has not been loaded and so do that +# automatically +unless ActiveRecord::Base.connection.table_exists?("accounts") + puts "Detected missing test schema, loading it now..." + + eval(File.read(SCHEMA_ROOT + "/schema.rb")) + + adapter_specific_schema_file = SCHEMA_ROOT + '/' + ActiveRecord::Base.connection.adapter_name.downcase + '_specific_schema.rb' + if File.exists?(adapter_specific_schema_file) + eval(File.read(adapter_specific_schema_file)) + end + + eval(File.read(SCHEMA_ROOT + "/schema2.rb")) +end + # Make with_scope public for tests class << ActiveRecord::Base public :with_scope, :with_exclusive_scope -- 1.5.6.3