summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration/safer_boolean_column_spec.rb
blob: aa7bb58ab45ad61f0ce53cd20841a02308396f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/migration/safer_boolean_column'

RSpec.describe RuboCop::Cop::Migration::SaferBooleanColumn do
  include CopHelper

  subject(:cop) { described_class.new }

  context 'in migration' do
    before do
      allow(cop).to receive(:in_migration?).and_return(true)
    end

    described_class::SMALL_TABLES.each do |table|
      context "for the #{table} table" do
        sources_and_offense = [
          ["add_column :#{table}, :column, :boolean, default: true", 'should disallow nulls'],
          ["add_column :#{table}, :column, :boolean, default: false", 'should disallow nulls'],
          ["add_column :#{table}, :column, :boolean, default: nil", 'should have a default and should disallow nulls'],
          ["add_column :#{table}, :column, :boolean, null: false", 'should have a default'],
          ["add_column :#{table}, :column, :boolean, null: true", 'should have a default and should disallow nulls'],
          ["add_column :#{table}, :column, :boolean", 'should have a default and should disallow nulls'],
          ["add_column :#{table}, :column, :boolean, default: nil, null: false", 'should have a default'],
          ["add_column :#{table}, :column, :boolean, default: nil, null: true", 'should have a default and should disallow nulls'],
          ["add_column :#{table}, :column, :boolean, default: false, null: true", 'should disallow nulls']
        ]

        sources_and_offense.each do |source, offense|
          context "given the source \"#{source}\"" do
            it "registers the offense matching \"#{offense}\"" do
              inspect_source(source)

              aggregate_failures do
                expect(cop.offenses.first.message).to match(offense)
              end
            end
          end
        end

        inoffensive_sources = [
          "add_column :#{table}, :column, :boolean, default: true, null: false",
          "add_column :#{table}, :column, :boolean, default: false, null: false"
        ]

        inoffensive_sources.each do |source|
          context "given the source \"#{source}\"" do
            it "registers no offense" do
              inspect_source(source)

              aggregate_failures do
                expect(cop.offenses).to be_empty
              end
            end
          end
        end
      end
    end

    it 'registers no offense for tables not listed in SMALL_TABLES' do
      inspect_source("add_column :large_table, :column, :boolean")

      expect(cop.offenses).to be_empty
    end

    it 'registers no offense for non-boolean columns' do
      table = described_class::SMALL_TABLES.sample
      inspect_source("add_column :#{table}, :column, :string")

      expect(cop.offenses).to be_empty
    end
  end

  context 'outside of migration' do
    it 'registers no offense' do
      table = described_class::SMALL_TABLES.sample
      inspect_source("add_column :#{table}, :column, :boolean")

      expect(cop.offenses).to be_empty
    end
  end
end