Remove active_record_supports_enum? helper
The `enum` macro was introduced in Rails 4.1, which shoulda-matchers no longer supports, so we can always assume that enum is available.
This commit is contained in:
parent
6bd3fcf24a
commit
b1e11fb69a
|
@ -9,10 +9,6 @@ module UnitTests
|
|||
Tests::Version.new(::ActiveRecord::VERSION::STRING)
|
||||
end
|
||||
|
||||
def active_record_supports_enum?
|
||||
defined?(::ActiveRecord::Enum)
|
||||
end
|
||||
|
||||
def active_record_supports_has_secure_password?
|
||||
active_record_version >= 3.1
|
||||
end
|
||||
|
|
|
@ -815,16 +815,14 @@ value", but that attribute does not exist.
|
|||
end
|
||||
end
|
||||
|
||||
if active_record_supports_enum?
|
||||
context 'given an ActiveRecord model' do
|
||||
context 'where the attribute under test is an enum and the given value is a value in that enum' do
|
||||
it 'accepts' do
|
||||
model = define_model('Shipment', status: :integer) do
|
||||
enum status: { pending: 1, shipped: 2, delivered: 3 }
|
||||
end
|
||||
|
||||
expect(model.new).to allow_value(1).for(:status)
|
||||
context 'given an ActiveRecord model' do
|
||||
context 'where the attribute under test is an enum and the given value is a value in that enum' do
|
||||
it 'accepts' do
|
||||
model = define_model('Shipment', status: :integer) do
|
||||
enum status: { pending: 1, shipped: 2, delivered: 3 }
|
||||
end
|
||||
|
||||
expect(model.new).to allow_value(1).for(:status)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,47 +1,116 @@
|
|||
require 'unit_spec_helper'
|
||||
|
||||
describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do
|
||||
if active_record_supports_enum?
|
||||
context 'if the attribute is given in plural form accidentally' do
|
||||
context 'if the attribute is given in plural form accidentally' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :integer,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attrs as an enum and store the value in a
|
||||
column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to define_enum_for(:attrs)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if a method to hold enum values exists on the model but was not created via the enum macro' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
model = define_model 'Example' do
|
||||
def self.statuses; end
|
||||
end
|
||||
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :statuses as an enum and store the value in a
|
||||
column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(model.new).to define_enum_for(:statuses)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with only the attribute name specified' do
|
||||
context 'if the attribute is not defined as an enum' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_non_enum_attribute(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum and store the value in a
|
||||
column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the column storing the attribute is not an integer type' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :integer,
|
||||
column_type: :string,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attrs as an enum and store the value in a
|
||||
Expected Example to define :attr as an enum and store the value in a
|
||||
column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to define_enum_for(:attrs)
|
||||
expect(record).to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if a method to hold enum values exists on the model but was not created via the enum macro' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
model = define_model 'Example' do
|
||||
def self.statuses; end
|
||||
context 'if the attribute is defined as an enum' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_array_values(attribute_name: :attr)
|
||||
|
||||
expect(record).to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
context 'and the matcher is negated' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :integer,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Did not expect Example to define :attr as an enum and store the
|
||||
value in a column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).not_to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :statuses as an enum and store the value in a
|
||||
column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(model.new).to define_enum_for(:statuses)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with only the attribute name specified' do
|
||||
describe 'with both attribute name and enum values specified' do
|
||||
context 'when the actual enum values are an array' do
|
||||
context 'if the attribute is not defined as an enum' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_non_enum_attribute(
|
||||
|
@ -49,74 +118,25 @@ describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do
|
|||
attribute_name: :attr,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum and store the value in a
|
||||
column of type integer
|
||||
Expected Example to define :attr as an enum with ["open", "close"]
|
||||
and store the value in a column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to define_enum_for(:attr)
|
||||
expect(record).to define_enum_for(:attr).with(['open', 'close'])
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the column storing the attribute is not an integer type' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :string,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum and store the value in a
|
||||
column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the attribute is defined as an enum' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_array_values(attribute_name: :attr)
|
||||
|
||||
expect(record).to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
context 'and the matcher is negated' do
|
||||
context 'if the attribute is defined as an enum and the enum values match' do
|
||||
context 'but the enum values do not match' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :integer,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Did not expect Example to define :attr as an enum and store the
|
||||
value in a column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).not_to define_enum_for(:attr)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with both attribute name and enum values specified' do
|
||||
context 'when the actual enum values are an array' do
|
||||
context 'if the attribute is not defined as an enum' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_non_enum_attribute(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
values: ['published', 'unpublished', 'draft'],
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum with ["open", "close"]
|
||||
|
@ -131,47 +151,49 @@ describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
context 'if the attribute is defined as an enum and the enum values match' do
|
||||
context 'but the enum values do not match' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
values: ['published', 'unpublished', 'draft'],
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum with ["open", "close"]
|
||||
and store the value in a column of type integer
|
||||
MESSAGE
|
||||
context 'and the enum values match' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_array_values(
|
||||
attribute_name: :attr,
|
||||
values: ['published', 'unpublished', 'draft'],
|
||||
)
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to define_enum_for(:attr).with(['open', 'close'])
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and the enum values match' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_array_values(
|
||||
attribute_name: :attr,
|
||||
values: ['published', 'unpublished', 'draft'],
|
||||
)
|
||||
|
||||
expect(record).to define_enum_for(:attr).
|
||||
with(['published', 'unpublished', 'draft'])
|
||||
end
|
||||
expect(record).to define_enum_for(:attr).
|
||||
with(['published', 'unpublished', 'draft'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the actual enum values are a hash' do
|
||||
context 'if the attribute is not defined as an enum' do
|
||||
context 'when the actual enum values are a hash' do
|
||||
context 'if the attribute is not defined as an enum' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_non_enum_attribute(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum with {:active=>5,
|
||||
:archived=>10} and store the value in a column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
with(active: 5, archived: 10)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the attribute is defined as an enum' do
|
||||
context 'but the enum values do not match' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_non_enum_attribute(
|
||||
record = build_record_with_hash_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
values: { active: 0, archived: 1 },
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum with {:active=>5,
|
||||
|
@ -188,94 +210,70 @@ describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
context 'if the attribute is defined as an enum' do
|
||||
context 'but the enum values do not match' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
context 'and the enum values match' do
|
||||
context 'when expected enum values are a hash' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_hash_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
values: { active: 0, archived: 1 },
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum with {:active=>5,
|
||||
:archived=>10} and store the value in a column of type integer
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
with(active: 5, archived: 10)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
with(active: 0, archived: 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and the enum values match' do
|
||||
context 'when expected enum values are a hash' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_hash_values(
|
||||
attribute_name: :attr,
|
||||
values: { active: 0, archived: 1 },
|
||||
)
|
||||
context 'when expected enum values are an array' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_hash_values(
|
||||
attribute_name: :attr,
|
||||
values: { active: 0, archived: 1 },
|
||||
)
|
||||
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
with(active: 0, archived: 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when expected enum values are an array' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_hash_values(
|
||||
attribute_name: :attr,
|
||||
values: { active: 0, archived: 1 },
|
||||
)
|
||||
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
with(['active', 'archived'])
|
||||
end
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
with(['active', 'archived'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with the backing column specified to be of some type' do
|
||||
context 'if the column storing the attribute is of a different type' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :integer,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum and store the value in a
|
||||
column of type string
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
backed_by_column_of_type(:string)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the column storing the attribute is of the same type' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_array_values(
|
||||
attribute_name: :attr,
|
||||
column_type: :string,
|
||||
)
|
||||
describe 'with the backing column specified to be of some type' do
|
||||
context 'if the column storing the attribute is of a different type' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_with_array_values(
|
||||
model_name: 'Example',
|
||||
attribute_name: :attr,
|
||||
column_type: :integer,
|
||||
)
|
||||
message = as_one_line(<<~MESSAGE)
|
||||
Expected Example to define :attr as an enum and store the value in a
|
||||
column of type string
|
||||
MESSAGE
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
backed_by_column_of_type(:string)
|
||||
end
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the column storing the attribute is of the same type' do
|
||||
it 'accepts' do
|
||||
record = build_record_with_array_values(
|
||||
attribute_name: :attr,
|
||||
column_type: :string,
|
||||
)
|
||||
|
||||
expect(record).
|
||||
to define_enum_for(:attr).
|
||||
backed_by_column_of_type(:string)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -721,60 +721,58 @@ Example did not properly validate that :attr is case-sensitively unique.
|
|||
include_context 'it supports scoped attributes of a certain type',
|
||||
column_type: :integer
|
||||
|
||||
if active_record_supports_enum?
|
||||
context 'when one of the scoped attributes is an enum' do
|
||||
it 'accepts' do
|
||||
context 'when one of the scoped attributes is an enum' do
|
||||
it 'accepts' do
|
||||
record = build_record_validating_scoped_uniqueness_with_enum(
|
||||
enum_scope: :scope
|
||||
)
|
||||
expect(record).to validate_uniqueness.scoped_to(:scope)
|
||||
end
|
||||
|
||||
context 'when too narrow of a scope is specified' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_validating_scoped_uniqueness_with_enum(
|
||||
enum_scope: :scope
|
||||
enum_scope: :scope1,
|
||||
additional_scopes: [:scope2],
|
||||
additional_attributes: [:other]
|
||||
)
|
||||
expect(record).to validate_uniqueness.scoped_to(:scope)
|
||||
end
|
||||
|
||||
context 'when too narrow of a scope is specified' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_validating_scoped_uniqueness_with_enum(
|
||||
enum_scope: :scope1,
|
||||
additional_scopes: [:scope2],
|
||||
additional_attributes: [:other]
|
||||
)
|
||||
assertion = lambda do
|
||||
expect(record).
|
||||
to validate_uniqueness.
|
||||
scoped_to(:scope1, :scope2, :other)
|
||||
end
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).
|
||||
to validate_uniqueness.
|
||||
scoped_to(:scope1, :scope2, :other)
|
||||
end
|
||||
|
||||
message = <<-MESSAGE
|
||||
message = <<-MESSAGE
|
||||
Example did not properly validate that :attr is case-sensitively unique
|
||||
within the scope of :scope1, :scope2, and :other.
|
||||
Expected the validation to be scoped to :scope1, :scope2, and :other,
|
||||
but it was scoped to :scope1 and :scope2 instead.
|
||||
MESSAGE
|
||||
MESSAGE
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when too broad of a scope is specified' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_validating_scoped_uniqueness_with_enum(
|
||||
enum_scope: :scope1,
|
||||
additional_scopes: [:scope2]
|
||||
)
|
||||
context 'when too broad of a scope is specified' do
|
||||
it 'rejects with an appropriate failure message' do
|
||||
record = build_record_validating_scoped_uniqueness_with_enum(
|
||||
enum_scope: :scope1,
|
||||
additional_scopes: [:scope2]
|
||||
)
|
||||
|
||||
assertion = lambda do
|
||||
expect(record).to validate_uniqueness.scoped_to(:scope1)
|
||||
end
|
||||
assertion = lambda do
|
||||
expect(record).to validate_uniqueness.scoped_to(:scope1)
|
||||
end
|
||||
|
||||
message = <<-MESSAGE
|
||||
message = <<-MESSAGE
|
||||
Example did not properly validate that :attr is case-sensitively unique
|
||||
within the scope of :scope1.
|
||||
Expected the validation to be scoped to :scope1, but it was scoped to
|
||||
:scope1 and :scope2 instead.
|
||||
MESSAGE
|
||||
MESSAGE
|
||||
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
expect(&assertion).to fail_with_message(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue