mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #21566 from ronakjangir47/active_record2
Removed mocha from Active Record Part 2
This commit is contained in:
commit
79b201e4e3
8 changed files with 85 additions and 58 deletions
|
@ -1299,9 +1299,10 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_compute_type_no_method_error
|
||||
ActiveSupport::Dependencies.stubs(:safe_constantize).raises(NoMethodError)
|
||||
assert_raises NoMethodError do
|
||||
ActiveRecord::Base.send :compute_type, 'InvalidModel'
|
||||
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise NoMethodError }) do
|
||||
assert_raises NoMethodError do
|
||||
ActiveRecord::Base.send :compute_type, 'InvalidModel'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1315,18 +1316,20 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
error = e
|
||||
end
|
||||
|
||||
ActiveSupport::Dependencies.stubs(:safe_constantize).raises(e)
|
||||
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise e }) do
|
||||
|
||||
exception = assert_raises NameError do
|
||||
ActiveRecord::Base.send :compute_type, 'InvalidModel'
|
||||
exception = assert_raises NameError do
|
||||
ActiveRecord::Base.send :compute_type, 'InvalidModel'
|
||||
end
|
||||
assert_equal error.message, exception.message
|
||||
end
|
||||
assert_equal error.message, exception.message
|
||||
end
|
||||
|
||||
def test_compute_type_argument_error
|
||||
ActiveSupport::Dependencies.stubs(:safe_constantize).raises(ArgumentError)
|
||||
assert_raises ArgumentError do
|
||||
ActiveRecord::Base.send :compute_type, 'InvalidModel'
|
||||
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise ArgumentError }) do
|
||||
assert_raises ArgumentError do
|
||||
ActiveRecord::Base.send :compute_type, 'InvalidModel'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -39,38 +39,49 @@ if ActiveRecord::Base.connection.supports_explain?
|
|||
binds = [[], []]
|
||||
queries = sqls.zip(binds)
|
||||
|
||||
connection.stubs(:explain).returns('query plan foo', 'query plan bar')
|
||||
expected = sqls.map {|sql| "EXPLAIN for: #{sql}\nquery plan #{sql}"}.join("\n")
|
||||
assert_equal expected, base.exec_explain(queries)
|
||||
stub_explain_for_query_plans do
|
||||
expected = sqls.map {|sql| "EXPLAIN for: #{sql}\nquery plan #{sql}"}.join("\n")
|
||||
assert_equal expected, base.exec_explain(queries)
|
||||
end
|
||||
end
|
||||
|
||||
def test_exec_explain_with_binds
|
||||
cols = [Object.new, Object.new]
|
||||
cols[0].expects(:name).returns('wadus')
|
||||
cols[1].expects(:name).returns('chaflan')
|
||||
object = Struct.new(:name)
|
||||
cols = [object.new('wadus'), object.new('chaflan')]
|
||||
|
||||
sqls = %w(foo bar)
|
||||
binds = [[[cols[0], 1]], [[cols[1], 2]]]
|
||||
queries = sqls.zip(binds)
|
||||
|
||||
connection.stubs(:explain).returns("query plan foo\n", "query plan bar\n")
|
||||
expected = <<-SQL.strip_heredoc
|
||||
EXPLAIN for: #{sqls[0]} [["wadus", 1]]
|
||||
query plan foo
|
||||
stub_explain_for_query_plans(["query plan foo\n", "query plan bar\n"]) do
|
||||
expected = <<-SQL.strip_heredoc
|
||||
EXPLAIN for: #{sqls[0]} [["wadus", 1]]
|
||||
query plan foo
|
||||
|
||||
EXPLAIN for: #{sqls[1]} [["chaflan", 2]]
|
||||
query plan bar
|
||||
SQL
|
||||
assert_equal expected, base.exec_explain(queries)
|
||||
EXPLAIN for: #{sqls[1]} [["chaflan", 2]]
|
||||
query plan bar
|
||||
SQL
|
||||
assert_equal expected, base.exec_explain(queries)
|
||||
end
|
||||
end
|
||||
|
||||
def test_unsupported_connection_adapter
|
||||
connection.stubs(:supports_explain?).returns(false)
|
||||
|
||||
base.logger.expects(:warn).never
|
||||
|
||||
Car.where(:name => 'honda').to_a
|
||||
connection.stub(:supports_explain?, false) do
|
||||
assert_not_called(base.logger, :warn) do
|
||||
Car.where(:name => 'honda').to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def stub_explain_for_query_plans(query_plans = ['query plan foo', 'query plan bar'])
|
||||
explain_called = 0
|
||||
|
||||
connection.stub(:explain, proc{ explain_called += 1; query_plans[explain_called - 1] }) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -178,8 +178,9 @@ class FinderTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_exists_does_not_instantiate_records
|
||||
Developer.expects(:instantiate).never
|
||||
Developer.exists?
|
||||
assert_not_called(Developer, :instantiate) do
|
||||
Developer.exists?
|
||||
end
|
||||
end
|
||||
|
||||
def test_find_by_array_of_one_id
|
||||
|
|
|
@ -408,9 +408,11 @@ class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_reloading_fixtures_through_accessor_methods
|
||||
topic = Struct.new(:title)
|
||||
assert_equal "The First Topic", topics(:first).title
|
||||
@loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
|
||||
assert_equal "Fresh Topic!", topics(:first, true).title
|
||||
assert_called(@loaded_fixtures['topics']['first'], :find, returns: topic.new("Fresh Topic!")) do
|
||||
assert_equal "Fresh Topic!", topics(:first, true).title
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -31,7 +31,8 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_unknown_commands_delegate
|
||||
recorder = CommandRecorder.new(stub(:foo => 'bar'))
|
||||
recorder = Struct.new(:foo)
|
||||
recorder = CommandRecorder.new(recorder.new('bar'))
|
||||
assert_equal 'bar', recorder.foo
|
||||
end
|
||||
|
||||
|
|
|
@ -273,10 +273,11 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
|
||||
@ship.stubs(:id).returns('ABC1X')
|
||||
@pirate.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
|
||||
@ship.stub(:id, 'ABC1X') do
|
||||
@pirate.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
|
||||
|
||||
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
|
||||
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
|
||||
|
@ -457,10 +458,11 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
|
||||
@pirate.stubs(:id).returns('ABC1X')
|
||||
@ship.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
|
||||
@pirate.stub(:id, 'ABC1X') do
|
||||
@ship.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
|
||||
|
||||
assert_equal 'Arr', @ship.pirate.catchphrase
|
||||
assert_equal 'Arr', @ship.pirate.catchphrase
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
|
||||
|
@ -638,17 +640,19 @@ module NestedAttributesOnACollectionAssociationTests
|
|||
end
|
||||
|
||||
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
|
||||
@child_1.stubs(:id).returns('ABC1X')
|
||||
@child_2.stubs(:id).returns('ABC2X')
|
||||
@child_1.stub(:id, 'ABC1X') do
|
||||
@child_2.stub(:id, 'ABC2X') do
|
||||
|
||||
@pirate.attributes = {
|
||||
association_getter => [
|
||||
{ :id => @child_1.id, :name => 'Grace OMalley' },
|
||||
{ :id => @child_2.id, :name => 'Privateers Greed' }
|
||||
]
|
||||
}
|
||||
@pirate.attributes = {
|
||||
association_getter => [
|
||||
{ :id => @child_1.id, :name => 'Grace OMalley' },
|
||||
{ :id => @child_2.id, :name => 'Privateers Greed' }
|
||||
]
|
||||
}
|
||||
|
||||
assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
|
||||
assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
|
||||
|
|
|
@ -55,9 +55,10 @@ module ActiveRecord
|
|||
|
||||
test '#order! on non-string does not attempt regexp match for references' do
|
||||
obj = Object.new
|
||||
obj.expects(:=~).never
|
||||
assert relation.order!(obj)
|
||||
assert_equal [obj], relation.order_values
|
||||
assert_not_called(obj, :=~) do
|
||||
assert relation.order!(obj)
|
||||
assert_equal [obj], relation.order_values
|
||||
end
|
||||
end
|
||||
|
||||
test '#references!' do
|
||||
|
|
|
@ -487,13 +487,17 @@ class TransactionTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_rollback_when_commit_raises
|
||||
Topic.connection.expects(:begin_db_transaction)
|
||||
Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
|
||||
Topic.connection.expects(:rollback_db_transaction)
|
||||
assert_called(Topic.connection, :begin_db_transaction) do
|
||||
Topic.connection.stub(:commit_db_transaction, ->{ raise('OH NOES') }) do
|
||||
assert_called(Topic.connection, :rollback_db_transaction) do
|
||||
|
||||
assert_raise RuntimeError do
|
||||
Topic.transaction do
|
||||
# do nothing
|
||||
e = assert_raise RuntimeError do
|
||||
Topic.transaction do
|
||||
# do nothing
|
||||
end
|
||||
end
|
||||
assert_equal 'OH NOES', e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue