|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::ActiveRecordCalculation, :config do |
| 4 | + it 'registers an offense and corrects when using `pluck.max`' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + Model.pluck(:column).max |
| 7 | + ^^^^^^^^^^^^^^^^^^ Use `maximum` instead of `pluck.max`. |
| 8 | + RUBY |
| 9 | + |
| 10 | + expect_correction(<<~RUBY) |
| 11 | + Model.maximum(:column) |
| 12 | + RUBY |
| 13 | + end |
| 14 | + |
| 15 | + it 'registers an offense and corrects when using `pluck.min`' do |
| 16 | + expect_offense(<<~RUBY) |
| 17 | + Model.pluck(:column).min |
| 18 | + ^^^^^^^^^^^^^^^^^^ Use `minimum` instead of `pluck.min`. |
| 19 | + RUBY |
| 20 | + |
| 21 | + expect_correction(<<~RUBY) |
| 22 | + Model.minimum(:column) |
| 23 | + RUBY |
| 24 | + end |
| 25 | + |
| 26 | + it 'registers an offense and corrects when using `pluck.sum`' do |
| 27 | + expect_offense(<<~RUBY) |
| 28 | + Model.pluck(:column).sum |
| 29 | + ^^^^^^^^^^^^^^^^^^ Use `sum` instead of `pluck.sum`. |
| 30 | + RUBY |
| 31 | + |
| 32 | + expect_correction(<<~RUBY) |
| 33 | + Model.sum(:column) |
| 34 | + RUBY |
| 35 | + end |
| 36 | + |
| 37 | + it 'registers an offense and corrects when using `pluck.max` without receiver' do |
| 38 | + expect_offense(<<~RUBY) |
| 39 | + pluck(:column).max |
| 40 | + ^^^^^^^^^^^^^^^^^^ Use `maximum` instead of `pluck.max`. |
| 41 | + RUBY |
| 42 | + |
| 43 | + expect_correction(<<~RUBY) |
| 44 | + maximum(:column) |
| 45 | + RUBY |
| 46 | + end |
| 47 | + |
| 48 | + it 'registers an offense and corrects when using `pluck.max` with non-literal column' do |
| 49 | + expect_offense(<<~RUBY) |
| 50 | + Model.pluck(column).max |
| 51 | + ^^^^^^^^^^^^^^^^^ Use `maximum` instead of `pluck.max`. |
| 52 | + RUBY |
| 53 | + |
| 54 | + expect_correction(<<~RUBY) |
| 55 | + Model.maximum(column) |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'does not register an offense when using `pluck.max` with multiple arguments' do |
| 60 | + expect_no_offenses(<<~RUBY) |
| 61 | + Model.pluck(:column1, :column2).max |
| 62 | + RUBY |
| 63 | + end |
| 64 | + |
| 65 | + it 'does not register an offense when using `pluck.max` with block' do |
| 66 | + expect_no_offenses(<<~RUBY) |
| 67 | + Model.pluck(:column).max { |e| e } |
| 68 | + RUBY |
| 69 | + end |
| 70 | + |
| 71 | + it 'does not register an offense when using `pluck.max` and `max` has argument' do |
| 72 | + expect_no_offenses(<<~RUBY) |
| 73 | + Model.pluck(:column).max(1) |
| 74 | + RUBY |
| 75 | + end |
| 76 | + |
| 77 | + it 'does not register an offense when using `maximum`' do |
| 78 | + expect_no_offenses(<<~RUBY) |
| 79 | + Model.maximum(:column) |
| 80 | + RUBY |
| 81 | + end |
| 82 | +end |
0 commit comments