rails icon indicating copy to clipboard operation
rails copied to clipboard

Add explicit regression tests for ActiveRecord::Migration's #say & #say_with_time

Open nfedyashev opened this issue 1 year ago • 2 comments

related tests are in activerecord/test/cases/migrator_test.rb, but it checks puts(message_count) quantity only.

Summary

I noticed that Migration's #say and #say_with_time methods didn't have explicit tests so this should increase test coverage a little bit.

Other than that(in the 2nd commit), after this update it fails with an explicit error in case #say_with_time was called without a block. I wasn't sure whether to put it in this PR or probably a future one. LMKWYT

nfedyashev avatar Jul 30 '22 16:07 nfedyashev

Hello @nfedyashev, I have isolated the problem. Other test is not cleaning properly ActiveRecord::Migration.verbose. I'll open PR to fix this.

simi avatar Jul 31 '22 12:07 simi

@nfedyashev Problem fixed at https://github.com/rails/rails/pull/45714.

simi avatar Jul 31 '22 14:07 simi

I think the current implementation is fine and the error message "no block given (yield) " is straighforward. This error is generated by Ruby and users are easy to find why this error occurs.

  • Migration file
$ cat db/migrate/20230131141426_foo.rb
class Foo < ActiveRecord::Migration[7.1]
  def change
    say_with_time 'Without block'
  end
end
$
  • With this pull request
$ bin/rails db:migrate
== 20230131141426 Foo: migrating ==============================================
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Missing block
/home/yahonda/src/github.com/yahonda/sample45705/db/migrate/20230131141426_foo.rb:3:in `change'

Caused by:
ArgumentError: Missing block
/home/yahonda/src/github.com/yahonda/sample45705/db/migrate/20230131141426_foo.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
$
  • With the current main branch
$ bin/rails db:migrate
== 20230131141426 Foo: migrating ==============================================
-- Without block
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

no block given (yield)
/home/yahonda/src/github.com/yahonda/sample45705/db/migrate/20230131141426_foo.rb:3:in `change'

Caused by:
LocalJumpError: no block given (yield)
/home/yahonda/src/github.com/yahonda/sample45705/db/migrate/20230131141426_foo.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
$

yahonda avatar Feb 07 '23 12:02 yahonda