-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stub with minitest not mocha #33337
Stub with minitest not mocha #33337
Conversation
r? @schneems (@rails-bot has picked a reviewer for you, use r? to override) |
end | ||
|
||
class DatabaseTasksCreateCurrentTest < ActiveRecord::TestCase | ||
def setup | ||
@configurations = { | ||
"development" => { "database" => "dev-db" }, | ||
"test" => { "database" => "test-db" }, | ||
"production" => { "url" => "prod-db-url" } | ||
"production" => { "url" => "prod-db-url" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need to align this. It is better to not do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
ActiveSupport::StringInquirer.new("development") | ||
) | ||
with_stubbed_configurations_establish_connection do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. Fixed. Also found and fixed some more here and in the postgresql task.
ActiveRecord::Tasks::DatabaseTasks.expects(:create). | ||
with("database" => "secondary-test-db") | ||
with_stubbed_configurations_establish_connection do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
ActiveRecord::Tasks::DatabaseTasks.expects(:create). | ||
with("url" => "secondary-prod-db-url") | ||
with_stubbed_configurations_establish_connection do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
ActiveSupport::StringInquirer.new("development") | ||
) | ||
with_stubbed_configurations_establish_connection do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
) | ||
|
||
with_stubbed_configurations_establish_connection do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
6172fca
to
e3295b3
Compare
Thank you @bogdanvlviv ... so quick I did not notice ;) |
assert_raise(ActiveRecord::ProtectedEnvironmentError) do | ||
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments! | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there reason adding this? Why do we need to assert it twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason whatsoever. It's a mix-up. Removing.
|
||
private | ||
|
||
def with_stubbed_configuration_establish_connection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is unused in this PR. Can we remove this changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Removing.
["database" => "dev-db"], | ||
["database" => "test-db"] | ||
], | ||
returns: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return value seems redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe we should add this for all the kind of tests in order to ensure that dbs were created?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am playing with it and wonder why changing this to returns: false
doesn't cause the test failure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By looking at the implementation of the create
method looks like we shouldn't expect returning any value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
["database" => "dev-db"], | ||
["database" => "test-db"] | ||
], | ||
returns: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seme here. Or maybe we should add this for all the kind of tests in order to ensure that dbs were created?
ensure | ||
ENV["RAILS_ENV"] = old_env | ||
end | ||
|
||
def test_establishes_connection_for_the_given_environments | ||
ActiveRecord::Tasks::DatabaseTasks.stubs(:create) | ||
ActiveRecord::Tasks::DatabaseTasks.stub(:create, true) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think there should be nil
instead of true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
ActiveRecord::Tasks::DatabaseTasks.create_current( | ||
ActiveSupport::StringInquirer.new("test") | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
ActiveRecord::Tasks::DatabaseTasks.create_current( | ||
ActiveSupport::StringInquirer.new("production") | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
ActiveSupport::StringInquirer.new("development") | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
ActiveSupport::StringInquirer.new("development") | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
ActiveRecord::Tasks::DatabaseTasks.stubs(:create) | ||
|
||
ActiveRecord::Base.expects(:establish_connection).with(:development) | ||
ActiveRecord::Tasks::DatabaseTasks.stub(:create, true) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be nil
instead of true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do | ||
ActiveRecord::Tasks::DatabaseTasks.drop_all | ||
ActiveRecord::Base.stub(:configurations, @configurations) do | ||
$stderr.stub(:puts, nil) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this stub seems extra
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
ActiveRecord::Tasks::DatabaseTasks.expects(:drop). | ||
with("database" => "test-db") | ||
ActiveRecord::Base.stub(:configurations, @configurations) do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
ActiveRecord::Tasks::DatabaseTasks.drop_current( | ||
ActiveSupport::StringInquirer.new("development") | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
ActiveSupport::StringInquirer.new("development") | ||
) | ||
ActiveRecord::Base.stub(:configurations, @configurations) do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, removing.
) | ||
ActiveRecord::Base.stubs(:establish_connection).returns(true) | ||
ActiveRecord::Base.stub(:connection, @connection) do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ extra empty line
) | ||
|
||
ActiveRecord::Tasks::DatabaseTasks.purge @configuration | ||
ActiveRecord::Base.stubs(:establish_connection).returns(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get rid of ActiveRecord::Base.stubs(:establish_connection).returns(true)
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get rid of the return value. Still need ActiveRecord::Base.respond_to?(:establish_connection) == true. Fixing.
|
||
def with_stubbed_connection_clear_active_connections | ||
ActiveRecord::Base.stub(:connection, @connection) do | ||
ActiveRecord::Base.stub(:clear_active_connections!, true) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to stub clear_active_connections!
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't! Removing.
@connection.expects(:encoding) | ||
ActiveRecord::Tasks::DatabaseTasks.charset @configuration | ||
ActiveRecord::Base.stub(:connection, @connection) do | ||
ActiveRecord::Base.stub(:clear_active_connections!, true) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to stub clear_active_connections!
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't! Removing.
@connection.expects(:collation) | ||
ActiveRecord::Tasks::DatabaseTasks.collation @configuration | ||
ActiveRecord::Base.stub(:connection, @connection) do | ||
ActiveRecord::Base.stub(:clear_active_connections!, true) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to stub clear_active_connections!
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't! Removing.
898b684
to
ddec0cb
Compare
@utilum Please let me know when it is ready for review. |
…e future We sometimes ask "✂️ extra blank lines" to a contributor in reviews like rails#33337 (comment). It is preferable to deal automatically without depending on manpower.
092b0b7
to
de4bfdd
Compare
de4bfdd
to
837d603
Compare
Thank you for the generous detailed comments. I believe I have responded to all, following suggestions in most cases, and offering an explanations where only partly followed, or not at all. |
Remove extra stub of `ActiveRecord::Base::connection` in `activerecord/test/cases/tasks/mysql_rake_test.rb`. Remove extra stub of `File::exist?` in `activerecord/test/cases/tasks/sqlite_rake_test.rb`. `ActiveRecord::Base::establish_connection` shouldn't return `true` in test cases. Related to rails#33337.
Missed these in preparing rails#33337
A correct, but not obvious use of `ActiveSupport::Testing::MethodCallAssertions`, which might also have been part of rails#33337 or rails#33391.
Step 4 in #33162