Skip to content
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

Merged
merged 1 commit into from
Jul 15, 2018
Merged

Conversation

utilum
Copy link
Contributor

@utilum utilum commented Jul 10, 2018

Step 4 in #33162

@rails-bot
Copy link

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" }
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️

Copy link
Contributor Author

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

Copy link
Member

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

Copy link
Member

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

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️

@bogdanvlviv
Copy link
Contributor

Also remove two stub return values following comments on #33326

@utilum Those stubbed returns were removed in 7fc499d.

@utilum utilum force-pushed the stub_with_minitest_not_mocha branch 2 times, most recently from 6172fca to e3295b3 Compare July 10, 2018 22:56
@utilum
Copy link
Contributor Author

utilum commented Jul 10, 2018

Thank you @bogdanvlviv ... so quick I did not notice ;)

@utilum utilum mentioned this pull request Jul 10, 2018
8 tasks
assert_raise(ActiveRecord::ProtectedEnvironmentError) do
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
end

Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this stub seems extra

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️ empty line

Copy link
Contributor Author

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

Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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!?

Copy link
Contributor Author

@utilum utilum Jul 14, 2018

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
Copy link
Contributor

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!?

Copy link
Contributor Author

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
Copy link
Contributor

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!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't! Removing.

@utilum utilum force-pushed the stub_with_minitest_not_mocha branch 3 times, most recently from 898b684 to ddec0cb Compare July 12, 2018 07:03
@bogdanvlviv
Copy link
Contributor

@utilum Please let me know when it is ready for review.

kamipo added a commit to kamipo/rails that referenced this pull request Jul 12, 2018
…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.
@utilum utilum force-pushed the stub_with_minitest_not_mocha branch 3 times, most recently from 092b0b7 to de4bfdd Compare July 12, 2018 22:05
@utilum utilum force-pushed the stub_with_minitest_not_mocha branch from de4bfdd to 837d603 Compare July 14, 2018 22:33
@utilum
Copy link
Contributor Author

utilum commented Jul 14, 2018

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.

@kamipo kamipo merged commit 5a71a77 into rails:master Jul 15, 2018
bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Jul 15, 2018
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.
@bogdanvlviv bogdanvlviv mentioned this pull request Jul 15, 2018
@bogdanvlviv
Copy link
Contributor

@utilum Excellent 😍! Thank you.
I just have few corrections of this PR, but since this is merged I opened new PR #33370.

@utilum utilum deleted the stub_with_minitest_not_mocha branch July 15, 2018 23:14
utilum added a commit to utilum/rails that referenced this pull request Jul 16, 2018
utilum added a commit to utilum/rails that referenced this pull request Jul 22, 2018
utilum added a commit to utilum/rails that referenced this pull request Jul 22, 2018
A correct, but not obvious use of `ActiveSupport::Testing::MethodCallAssertions`, which might also have been part of rails#33337 or rails#33391.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants