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

Verify the testing database before running tests. #1174

Merged
merged 1 commit into from
Mar 15, 2021

Conversation

junaruga
Copy link
Contributor

@junaruga junaruga commented Mar 4, 2021

Motivation

Right now when the testing database is not running or has an error to the testing database, we see a lot of test failures on the testing result. By verifying the testing database connection before running the tests, we can guide users by the simple and proper message.

New error message

The actual error message will be like this by this PR when there is an error while connecting to the testing database.
For example, this message is when the database server is not running.

$ bundle exec rake
...
Randomized with seed 54589

An error occurred while connecting to the testing database server.
Make sure that the database server is running.
Make sure that `mysql -u root [options] test` succeeds by the root user config in spec/configuration.yml.
Make sure that the testing database 'test' exists. If it does not exist, create it.

An error occurred in a `before(:suite)` hook.
Failure/Error: connect user, pass, host, port, database, socket, flags, conn_attrs

Mysql2::Error::ConnectionError:
  Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
# ./lib/mysql2/client.rb:90:in `connect'
# ./lib/mysql2/client.rb:90:in `initialize'
# ./spec/spec_helper.rb:31:in `new'
# ./spec/spec_helper.rb:31:in `new_client'
# ./spec/spec_helper.rb:62:in `block (2 levels) in <top (required)>'

Finished in 0.03324 seconds (files took 0.17416 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

I was inspired for the message content by Bundler error message guiding the actual command line gem install ... to make user verify it.

$ bundle install
...
An error occurred while installing mysql (2.9.1), and Bundler cannot continue.
Make sure that `gem install mysql -v '2.9.1' --source 'https://rubygems.org/'` succeeds before bundling.
...

Here are also other cases I tested on CI.

  • Mysql2::Error::ConnectionError: / Access denied for user 'root'@'localhost' (using password: NO): the log.
  • Mysql2::Error: / Unknown database 'test': the log

Showing the message.

I wanted to use the following heredoc to show the message. But I faced the rubocop error on it, I am using %( ... ) syntax to avoid it.

      message = <<-MESSAGE
foo
MESSAGE

Sorting the before after blocks

I also sorted the before after blocks in the following actually called order.

before suite
before all
before each
after  each
after  all
after  suite

You can see the called order on the rspec 2.99 document. As the used keywords :all, :each is old unlike the :context and :example on the rspec 3 document. I will send another PR to replace the keywords later.

To avoid seeing a lot of test failures on the database connection error,
and guide the user how to fix.

Sort the before after blocks in the following actually called order.
See https://relishapp.com/rspec/rspec-core/v/2-99/docs/hooks/before-and-after-hooks
@junaruga
Copy link
Contributor Author

@sodabrew ping.

@sodabrew sodabrew merged commit 706a43b into brianmario:master Mar 15, 2021
@junaruga junaruga deleted the wip/verify-db branch March 15, 2021 21:08
@junaruga
Copy link
Contributor Author

Thanks!

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.

2 participants