|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::MigrationTimestamp, :config do |
| 4 | + it 'registers no offenses if timestamp is valid' do |
| 5 | + expect_no_offenses(<<~RUBY, 'db/migrate/20170101000000_good.rb') |
| 6 | + # ... |
| 7 | + RUBY |
| 8 | + end |
| 9 | + |
| 10 | + it 'registers an offense if timestamp is impossible' do |
| 11 | + expect_offense(<<~RUBY, 'db/migrate/20002222222222_bad.rb') |
| 12 | + # ... |
| 13 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 14 | + RUBY |
| 15 | + end |
| 16 | + |
| 17 | + it 'registers an offense if timestamp swaps month and day' do |
| 18 | + expect_offense(<<~RUBY, 'db/migrate/20003112000000_bad.rb') |
| 19 | + # ... |
| 20 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 21 | + RUBY |
| 22 | + end |
| 23 | + |
| 24 | + it 'registers an offense if timestamp day is wrong' do |
| 25 | + expect_offense(<<~RUBY, 'db/migrate/20000231000000_bad.rb') |
| 26 | + # ... |
| 27 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 28 | + RUBY |
| 29 | + end |
| 30 | + |
| 31 | + it 'registers an offense if timestamp hours are invalid' do |
| 32 | + expect_offense(<<~RUBY, 'db/migrate/20000101240000_bad.rb') |
| 33 | + # ... |
| 34 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 35 | + RUBY |
| 36 | + end |
| 37 | + |
| 38 | + it 'registers an offense if timestamp minutes are invalid' do |
| 39 | + expect_offense(<<~RUBY, 'db/migrate/20000101006000_bad.rb') |
| 40 | + # ... |
| 41 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 42 | + RUBY |
| 43 | + end |
| 44 | + |
| 45 | + it 'registers an offense if timestamp seconds are invalid' do |
| 46 | + expect_offense(<<~RUBY, 'db/migrate/20000101000060_bad.rb') |
| 47 | + # ... |
| 48 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 49 | + RUBY |
| 50 | + end |
| 51 | + |
| 52 | + it 'registers an offense if timestamp is invalid' do |
| 53 | + expect_offense(<<~RUBY, 'db/migrate/123_bad.rb') |
| 54 | + # ... |
| 55 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'registers an offense if no timestamp at all' do |
| 60 | + expect_offense(<<~RUBY, 'db/migrate/bad.rb') |
| 61 | + # ... |
| 62 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 63 | + RUBY |
| 64 | + end |
| 65 | + |
| 66 | + it 'registers an offense if the timestamp is in the future' do |
| 67 | + timestamp = (Time.now.utc + 5).strftime('%Y%m%d%H%M%S') |
| 68 | + expect_offense(<<~RUBY, "db/migrate/#{timestamp}_bad.rb") |
| 69 | + # ... |
| 70 | + ^ Migration file name must start with a valid `YYYYmmddHHMMSS_` timestamp in the past. |
| 71 | + RUBY |
| 72 | + end |
| 73 | + |
| 74 | + it 'registers no offense if the timestamp is in the past' do |
| 75 | + timestamp = (Time.now.utc - 5).strftime('%Y%m%d%H%M%S') |
| 76 | + expect_no_offenses(<<~RUBY, "db/migrate/#{timestamp}_good.rb") |
| 77 | + # ... |
| 78 | + RUBY |
| 79 | + end |
| 80 | +end |
0 commit comments