Skip to content

Commit

Permalink
Add array option support for have db column matcher (#1465)
Browse files Browse the repository at this point in the history
* Added array option support for have_db_column matcher

* Added specs for array option support for have_db_column matcher

* Emtty commit
  • Loading branch information
shaggyone authored Dec 15, 2021
1 parent 05a6c37 commit 629a0f0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/shoulda/matchers/active_record/have_db_column_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module ActiveRecord
#
# Use `with_options` to assert that a column has been defined with
# certain options (`:precision`, `:limit`, `:default`, `:null`, `:scale`,
# or `:primary`).
# `:primary` or `:array`).
#
# class CreatePhones < ActiveRecord::Migration
# def change
Expand Down Expand Up @@ -84,7 +84,7 @@ def have_db_column(column)

# @private
class HaveDbColumnMatcher
OPTIONS = %i(precision limit default null scale primary).freeze
OPTIONS = %i(precision limit default null scale primary array).freeze

def initialize(column)
@column = column
Expand Down Expand Up @@ -115,7 +115,8 @@ def matches?(subject)
correct_default? &&
correct_null? &&
correct_scale? &&
correct_primary?
correct_primary? &&
correct_array?
end

def failure_message
Expand Down Expand Up @@ -258,6 +259,23 @@ def correct_primary?
end
end

def correct_array?
return true unless @options.key?(:array)

if matched_column.array? == @options[:array]
true
else
@missing = "#{model_class} has a db column named #{@column} "
@missing <<
if @options[:primary]
'that is not array, but should be'
else
'that is array, but should not be'
end
false
end
end

def matched_column
@_matched_column ||= begin
column = model_class.columns.detect do |each|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
end
end

if database_supports_array_columns?
context 'with array option' do
it 'accepts a column that is array' do
expect(with_table(:tags, :string, array: true)).
to have_db_column(:tags).with_options(array: true)
end

it 'rejects a column that is not array' do
expect(with_table(:whatever, :string, array: false)).
not_to have_db_column(:whatever).with_options(array: true)
end
end
end

context 'with invalid argument option' do
it 'raises an error with the unknown options' do
expect {
Expand Down

0 comments on commit 629a0f0

Please sign in to comment.