Fix tests for array columns

None of the tests around array columns were working because they weren't
actually creating models with array columns. This fixes that.
This commit is contained in:
Elliot Winkler 2019-07-09 23:04:02 -06:00
parent b202f2cb27
commit fb6ea7afd4
6 changed files with 100 additions and 18 deletions

View File

@ -312,7 +312,7 @@ validation for you? Instead of using `validate_presence_of`, try
end
def attribute_accepts_string_values?
if association? || attachment?
if association?
false
elsif attribute_serializer
attribute_serializer.object_class == String
@ -333,7 +333,7 @@ validation for you? Instead of using `validate_presence_of`, try
def attachment?
model_has_associations?(
["#{@attribute}_attachment", "#{@attribute}_attachments"]
["#{@attribute}_attachment", "#{@attribute}_attachments"],
)
end

View File

@ -1,3 +1,6 @@
require_relative '../helpers/active_record_versions'
require_relative '../helpers/database_helpers'
module UnitTests
module ActiveRecord
class CreateTable
@ -51,6 +54,18 @@ module UnitTests
attr_reader :table_name, :columns, :connection, :customizer
delegate(
:active_record_supports_array_columns?,
:active_record_version,
to: UnitTests::ActiveRecordVersions,
)
delegate(
:database_supports_array_columns?,
:database_adapter,
to: UnitTests::DatabaseHelpers,
)
def add_columns_to_table(table)
columns.each do |column_name, column_specification|
add_column_to_table(table, column_name, column_specification)
@ -61,8 +76,35 @@ module UnitTests
def add_column_to_table(table, column_name, column_specification)
if column_specification.is_a?(Hash)
column_type = column_specification.fetch(:type)
column_options = column_specification.fetch(:options, {})
column_specification = column_specification.dup
column_type = column_specification.delete(:type)
column_options = column_specification.delete(:options) { {} }
if column_options[:array]
if !active_record_supports_array_columns?
raise ArgumentError.new(
'An array column is being added to a table, but this version ' +
"of ActiveRecord (#{active_record_version}) " +
'does not support array columns.',
)
end
if !database_supports_array_columns?
raise ArgumentError.new(
'An array column is being added to a table, but this ' +
"database adapter (#{database_adapter}) " +
'does not support array columns.',
)
end
end
if column_specification.any?
raise ArgumentError.new(
"Invalid column specification.\nYou need to put " +
"#{column_specification.keys.map(&:inspect).to_sentence} " +
'inside an :options key!',
)
end
else
column_type = column_specification
column_options = {}

View File

@ -19,9 +19,10 @@ module UnitTests
end
def column_options
DEFAULT_COLUMN_OPTIONS.
merge(args.fetch(:column_options, {})).
merge(type: column_type)
{
type: column_type,
options: DEFAULT_COLUMN_OPTIONS.merge(args.fetch(:column_options, {}))
}
end
def array?

View File

@ -5,6 +5,8 @@ module UnitTests
example_group.extend(self)
end
extend self
def active_record_version
Tests::Version.new(::ActiveRecord::VERSION::STRING)
end

View File

@ -5,6 +5,8 @@ module UnitTests
example_group.extend(self)
end
extend self
def database_adapter
Tests::Database.instance.adapter_name
end
@ -13,9 +15,9 @@ module UnitTests
database_adapter == :postgresql
end
alias_method :database_supports_array_columns?, :postgresql?
alias_method :database_supports_uuid_columns?, :postgresql?
alias_method :database_supports_money_columns?, :postgresql?
alias_method :database_supports_expression_indexes?, :postgresql?
alias :database_supports_array_columns? :postgresql?
alias :database_supports_uuid_columns? :postgresql?
alias :database_supports_money_columns? :postgresql?
alias :database_supports_expression_indexes? :postgresql?
end
end

View File

@ -89,14 +89,49 @@ this could not be proved.
end
end
context 'when the column backing the attribute is an array' do
it 'still works' do
record = record_validating_presence_of(
:possible_meeting_dates,
column_options: { type: :date, array: true },
)
if database_supports_array_columns? && active_record_supports_array_columns?
context 'when the column backing the attribute is an array' do
context 'of varchar' do
it 'still works' do
record = record_validating_presence_of(
:filters,
column_options: {
type: :varchar,
options: { array: true, default: [], null: false },
},
)
expect(record).to validate_presence_of(:possible_meeting_dates)
expect(record).to validate_presence_of(:filters)
end
end
context 'of string' do
it 'still works' do
record = record_validating_presence_of(
:filters,
column_options: {
type: :string,
options: { array: true, default: [] },
},
)
expect(record).to validate_presence_of(:filters)
end
end
context 'of a type other than string' do
it 'still works' do
record = record_validating_presence_of(
:possible_meeting_dates,
column_options: {
type: :date,
options: { array: true, default: [] },
},
)
expect(record).to validate_presence_of(:possible_meeting_dates)
end
end
end
end
end