Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array option support for have db column matcher #1465

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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