Permalink
Browse files

Do not use instantiated fixtures (in general) in tests. Also, support…

… the use of transactional fixtures by setting the AR_TX_FIXTURES environment variable to "yes".

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1399 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 7f9ffb2 commit 2c0fa3208859aed2c1fd0ce54c8d5215d11d0c75 @jamis jamis committed Jun 10, 2005
@@ -33,3 +33,14 @@ You can also run all the suites on a specific adapter with:
cd test; all.sh "connections/native_mysql"
+== Faster tests
+
+If you are using a database that supports transactions, you can set the
+"AR_TX_FIXTURES" environment variable to "yes" to use transactional fixtures.
+This gives a very large speed boost. With rake:
+
+ rake AR_TX_FIXTURES=yes
+
+Or, by hand:
+
+ AR_TX_FIXTURES=yes ruby -I connections/native_sqlite3 base_test.rb
@@ -441,6 +441,20 @@ def self.setup_fixture_accessors(table_names=nil)
end
end
+ def self.uses_transaction(*methods)
+ @uses_transaction ||= []
+ @uses_transaction.concat methods.map { |m| m.to_s }
+ end
+
+ def self.uses_transaction?(method)
+ @uses_transaction && @uses_transaction.include?(method.to_s)
+ end
+
+ def use_transactional_fixtures?
+ use_transactional_fixtures &&
+ !self.class.uses_transaction?(method_name)
+ end
+
def setup_with_fixtures
if pre_loaded_fixtures && !use_transactional_fixtures
raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
@@ -449,7 +463,7 @@ def setup_with_fixtures
@fixture_cache = Hash.new
# Load fixtures once and begin transaction.
- if use_transactional_fixtures
+ if use_transactional_fixtures?
if @@already_loaded_fixtures[self.class]
@loaded_fixtures = @@already_loaded_fixtures[self.class]
else
@@ -461,6 +475,7 @@ def setup_with_fixtures
# Load fixtures for every test.
else
+ @@already_loaded_fixtures[self.class] = nil
load_fixtures
end
@@ -472,7 +487,7 @@ def setup_with_fixtures
def teardown_with_fixtures
# Rollback changes.
- if use_transactional_fixtures
+ if use_transactional_fixtures?
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.unlock_mutex
end
@@ -19,6 +19,6 @@ def create_fixtures(*table_names)
end
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
-#Test::Unit::TestCase.use_instantiated_fixtures = false
-#Test::Unit::TestCase.use_transactional_fixtures = (ENV['AR_TX_FIXTURES'] == "yes")
+Test::Unit::TestCase.use_instantiated_fixtures = false
+Test::Unit::TestCase.use_transactional_fixtures = (ENV['AR_TX_FIXTURES'] == "yes")
@@ -5,43 +5,43 @@ class AggregationsTest < Test::Unit::TestCase
fixtures :customers
def test_find_single_value_object
- assert_equal 50, @david.balance.amount
- assert_kind_of Money, @david.balance
- assert_equal 300, @david.balance.exchange_to("DKK").amount
+ assert_equal 50, customers(:david).balance.amount
+ assert_kind_of Money, customers(:david).balance
+ assert_equal 300, customers(:david).balance.exchange_to("DKK").amount
end
def test_find_multiple_value_object
- assert_equal @customers["david"]["address_street"], @david.address.street
+ assert_equal customers(:david).address_street, customers(:david).address.street
assert(
- @david.address.close_to?(Address.new("Different Street", @customers["david"]["address_city"], @customers["david"]["address_country"]))
+ customers(:david).address.close_to?(Address.new("Different Street", customers(:david).address_city, customers(:david).address_country))
)
end
def test_change_single_value_object
- @david.balance = Money.new(100)
- @david.save
+ customers(:david).balance = Money.new(100)
+ customers(:david).save
assert_equal 100, Customer.find(1).balance.amount
end
def test_immutable_value_objects
- @david.balance = Money.new(100)
- assert_raises(TypeError) { @david.balance.instance_eval { @amount = 20 } }
+ customers(:david).balance = Money.new(100)
+ assert_raises(TypeError) { customers(:david).balance.instance_eval { @amount = 20 } }
end
def test_inferred_mapping
- assert_equal "35.544623640962634", @david.gps_location.latitude
- assert_equal "-105.9309951055148", @david.gps_location.longitude
+ assert_equal "35.544623640962634", customers(:david).gps_location.latitude
+ assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
- @david.gps_location = GpsLocation.new("39x-110")
+ customers(:david).gps_location = GpsLocation.new("39x-110")
- assert_equal "39", @david.gps_location.latitude
- assert_equal "-110", @david.gps_location.longitude
+ assert_equal "39", customers(:david).gps_location.latitude
+ assert_equal "-110", customers(:david).gps_location.longitude
- @david.save
+ customers(:david).save
- @david.reload
+ customers(:david).reload
- assert_equal "39", @david.gps_location.latitude
- assert_equal "-110", @david.gps_location.longitude
+ assert_equal "39", customers(:david).gps_location.latitude
+ assert_equal "-110", customers(:david).gps_location.longitude
end
-end
+end
@@ -556,6 +556,7 @@ def test_three_levels_of_dependence
assert_nothing_raised { topic.destroy }
end
+ uses_transaction :test_dependence_with_transaction_support_on_failure
def test_dependence_with_transaction_support_on_failure
assert_equal 2, Client.find_all.length
firm = Firm.find_first
@@ -851,7 +852,7 @@ def test_uniq_after_the_fact
def test_uniq_before_the_fact
projects(:active_record).developers << developers(:jamis)
projects(:active_record).developers << developers(:david)
- assert_equal 2, projects(:active_record).developers.size
+ assert_equal 2, projects(:active_record, :reload).developers.size
end
def test_deleting
@@ -933,7 +934,7 @@ def test_associations_with_conditions
assert_equal 1, projects(:active_record).developers_named_david.size
projects(:active_record).developers_named_david.clear
- assert_equal 1, projects(:active_record).developers.size
+ assert_equal 1, projects(:active_record, :reload).developers.size
end
def test_find_in_association
@@ -42,7 +42,7 @@ def test_set_attributes
topic.save
assert_equal("Budget", topic.title)
assert_equal("Jason", topic.author_name)
- assert_equal(@topics["first"]["author_email_address"], Topic.find(1).author_email_address)
+ assert_equal(topics(:first).author_email_address, Topic.find(1).author_email_address)
end
def test_integers_as_nil
@@ -110,7 +110,7 @@ def test_update_array_content
end
def test_attributes_hash
- assert_equal @projects['active_record'].to_hash, Project.find_first.attributes
+ assert_equal @loaded_fixtures['projects']['active_record'].to_hash, Project.find_first.attributes
end
def test_create
@@ -229,14 +229,14 @@ def test_initialize_with_invalid_attribute
def test_load
topics = Topic.find_all nil, "id"
assert_equal(2, topics.size)
- assert_equal(@topics["first"]["title"], topics.first.title)
+ assert_equal(topics(:first).title, topics.first.title)
end
def test_load_with_condition
topics = Topic.find_all "author_name = 'Mary'"
assert_equal(1, topics.size)
- assert_equal(@topics["second"]["title"], topics.first.title)
+ assert_equal(topics(:second).title, topics.first.title)
end
def test_table_name_guesses
@@ -721,35 +721,35 @@ def test_class_level_delete
end
def test_increment_attribute
- assert_equal 0, @topics["first"].find.replies_count
- @topics["first"].find.increment! :replies_count
- assert_equal 1, @topics["first"].find.replies_count
+ assert_equal 0, topics(:first).replies_count
+ topics(:first).increment! :replies_count
+ assert_equal 1, topics(:first, :reload).replies_count
- @topics["first"].find.increment(:replies_count).increment!(:replies_count)
- assert_equal 3, @topics["first"].find.replies_count
+ topics(:first).increment(:replies_count).increment!(:replies_count)
+ assert_equal 3, topics(:first, :reload).replies_count
end
def test_increment_nil_attribute
- assert_nil @topics["first"].find.parent_id
- @topics["first"].find.increment! :parent_id
- assert_equal 1, @topics["first"].find.parent_id
+ assert_nil topics(:first).parent_id
+ topics(:first).increment! :parent_id
+ assert_equal 1, topics(:first).parent_id
end
def test_decrement_attribute
- @topics["first"].find.increment(:replies_count).increment!(:replies_count)
- assert_equal 2, @topics["first"].find.replies_count
+ topics(:first).increment(:replies_count).increment!(:replies_count)
+ assert_equal 2, topics(:first).replies_count
- @topics["first"].find.decrement!(:replies_count)
- assert_equal 1, @topics["first"].find.replies_count
+ topics(:first).decrement!(:replies_count)
+ assert_equal 1, topics(:first, :reload).replies_count
- @topics["first"].find.decrement(:replies_count).decrement!(:replies_count)
- assert_equal -1, @topics["first"].find.replies_count
+ topics(:first).decrement(:replies_count).decrement!(:replies_count)
+ assert_equal -1, topics(:first, :reload).replies_count
end
def test_toggle_attribute
- assert !@topics["first"].find.approved?
- @topics["first"].find.toggle!(:approved)
- assert @topics["first"].find.approved?
+ assert !topics(:first).approved?
+ topics(:first).toggle!(:approved)
+ assert topics(:first).approved?
end
def test_reload
@@ -51,9 +51,7 @@ def after_find
class CallbacksTest < Test::Unit::TestCase
- def setup
- @developers = create_fixtures('developers')
- end
+ fixtures :developers
def test_initialize
david = CallbackDeveloper.new
@@ -16,10 +16,8 @@ class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
class DeprecatedAssociationsTest < Test::Unit::TestCase
- def setup
- create_fixtures "accounts", "companies", "developers", "projects", "developers_projects", "topics"
- @signals37 = Firm.find(1)
- end
+ fixtures :accounts, :companies, :developers, :projects, :topics,
+ :developers_projects
def test_has_many_find
assert_equal 2, Firm.find_first.clients.length
@@ -67,6 +65,7 @@ def test_has_many_dependence
assert_equal 0, Client.find_all.length
end
+ uses_transaction :test_has_many_dependence_with_transaction_support_on_failure
def test_has_many_dependence_with_transaction_support_on_failure
assert_equal 2, Client.find_all.length
@@ -94,7 +93,7 @@ def test_has_one_dependence_with_missing_association
end
def test_belongs_to
- assert_equal @signals37.name, Client.find(3).firm.name
+ assert_equal companies(:first_firm).name, Client.find(3).firm.name
assert Client.find(3).has_firm?, "Microsoft should have a firm"
# assert !Company.find(1).has_firm?, "37signals shouldn't have a firm"
end
@@ -115,25 +114,25 @@ def test_belongs_to_equality
end
def test_has_one
- assert @signals37.account?(Account.find(1))
- assert_equal Account.find(1).credit_limit, @signals37.account.credit_limit
- assert @signals37.has_account?, "37signals should have an account"
- assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
+ assert companies(:first_firm).account?(Account.find(1))
+ assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
+ assert companies(:first_firm).has_account?, "37signals should have an account"
+ assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack"
assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
assert !Account.find(2).has_firm?, "Unknown isn't linked"
- assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
+ assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
end
def test_has_many_dependence_on_account
assert_equal 2, Account.find_all.length
- @signals37.destroy
+ companies(:first_firm).destroy
assert_equal 1, Account.find_all.length
end
def test_find_in
- assert_equal Client.find(2).name, @signals37.find_in_clients(2).name
- assert_raises(ActiveRecord::RecordNotFound) { @signals37.find_in_clients(6) }
+ assert_equal Client.find(2).name, companies(:first_firm).find_in_clients(2).name
+ assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).find_in_clients(6) }
end
def test_force_reload
@@ -157,21 +156,21 @@ def test_force_reload
end
def test_included_in_collection
- assert @signals37.clients.include?(Client.find(2))
+ assert companies(:first_firm).clients.include?(Client.find(2))
end
def test_build_to_collection
- assert_equal 1, @signals37.clients_of_firm_count
- new_client = @signals37.build_to_clients_of_firm("name" => "Another Client")
+ assert_equal 1, companies(:first_firm).clients_of_firm_count
+ new_client = companies(:first_firm).build_to_clients_of_firm("name" => "Another Client")
assert_equal "Another Client", new_client.name
assert new_client.save
- assert new_client.firm?(@signals37)
- assert_equal 2, @signals37.clients_of_firm_count(true)
+ assert new_client.firm?(companies(:first_firm))
+ assert_equal 2, companies(:first_firm).clients_of_firm_count(true)
end
def test_create_in_collection
- assert_equal @signals37.create_in_clients_of_firm("name" => "Another Client"), @signals37.clients_of_firm(true).last
+ assert_equal companies(:first_firm).create_in_clients_of_firm("name" => "Another Client"), companies(:first_firm).clients_of_firm(true).last
end
def test_has_and_belongs_to_many
@@ -317,13 +316,13 @@ def test_has_many_find_all
end
def test_has_one
- assert @signals37.account?(Account.find(1))
- assert @signals37.has_account?, "37signals should have an account"
- assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
+ assert companies(:first_firm).account?(Account.find(1))
+ assert companies(:first_firm).has_account?, "37signals should have an account"
+ assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack"
assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
assert !Account.find(2).has_firm?, "Unknown isn't linked"
- assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
+ assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
end
def test_has_one_build
Oops, something went wrong.

0 comments on commit 2c0fa32

Please sign in to comment.