2013-11-02 15:01:31 -04:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/book'
|
|
|
|
|
2013-11-04 13:22:53 -05:00
|
|
|
class EnumTest < ActiveRecord::TestCase
|
2013-11-02 15:01:31 -04:00
|
|
|
fixtures :books
|
|
|
|
|
|
|
|
setup do
|
2013-11-02 16:13:02 -04:00
|
|
|
@book = books(:awdr)
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "query state by predicate" do
|
|
|
|
assert @book.proposed?
|
|
|
|
assert_not @book.written?
|
|
|
|
assert_not @book.published?
|
2013-11-02 20:48:16 -04:00
|
|
|
|
|
|
|
assert @book.unread?
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|
2013-11-02 16:08:36 -04:00
|
|
|
|
2013-12-30 10:56:13 -05:00
|
|
|
test "query state with strings" do
|
2013-11-05 11:29:52 -05:00
|
|
|
assert_equal "proposed", @book.status
|
|
|
|
assert_equal "unread", @book.read_status
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|
|
|
|
|
2013-11-02 19:18:31 -04:00
|
|
|
test "find via scope" do
|
|
|
|
assert_equal @book, Book.proposed.first
|
2013-11-02 20:48:16 -04:00
|
|
|
assert_equal @book, Book.unread.first
|
2013-11-02 19:18:31 -04:00
|
|
|
end
|
|
|
|
|
2013-11-02 15:01:31 -04:00
|
|
|
test "update by declaration" do
|
|
|
|
@book.written!
|
|
|
|
assert @book.written?
|
|
|
|
end
|
2013-11-02 16:08:36 -04:00
|
|
|
|
2013-11-02 15:01:31 -04:00
|
|
|
test "update by setter" do
|
|
|
|
@book.update! status: :written
|
|
|
|
assert @book.written?
|
|
|
|
end
|
2013-11-04 13:36:22 -05:00
|
|
|
|
|
|
|
test "enum methods are overwritable" do
|
|
|
|
assert_equal "do publish work...", @book.published!
|
|
|
|
assert @book.published?
|
|
|
|
end
|
2013-11-04 13:54:46 -05:00
|
|
|
|
|
|
|
test "direct assignment" do
|
|
|
|
@book.status = :written
|
|
|
|
assert @book.written?
|
|
|
|
end
|
|
|
|
|
2013-11-05 11:29:52 -05:00
|
|
|
test "assign string value" do
|
|
|
|
@book.status = "written"
|
|
|
|
assert @book.written?
|
|
|
|
end
|
|
|
|
|
2014-01-20 18:59:20 -05:00
|
|
|
test "enum changed attributes" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = :published
|
|
|
|
assert_equal old_status, @book.changed_attributes[:status]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum changes" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = :published
|
|
|
|
assert_equal [old_status, 'published'], @book.changes[:status]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute was" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = :published
|
|
|
|
assert_equal old_status, @book.attribute_was(:status)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed" do
|
|
|
|
@book.status = :published
|
|
|
|
assert @book.attribute_changed?(:status)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed to" do
|
|
|
|
@book.status = :published
|
|
|
|
assert @book.attribute_changed?(:status, to: 'published')
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed from" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = :published
|
|
|
|
assert @book.attribute_changed?(:status, from: old_status)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed from old status to new status" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = :published
|
|
|
|
assert @book.attribute_changed?(:status, from: old_status, to: 'published')
|
|
|
|
end
|
|
|
|
|
2014-01-20 20:10:48 -05:00
|
|
|
test "enum didn't change" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = old_status
|
|
|
|
assert_not @book.attribute_changed?(:status)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "persist changes that are dirty" do
|
|
|
|
@book.status = :published
|
|
|
|
assert @book.attribute_changed?(:status)
|
|
|
|
@book.status = :written
|
|
|
|
assert @book.attribute_changed?(:status)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reverted changes that are not dirty" do
|
|
|
|
old_status = @book.status
|
|
|
|
@book.status = :published
|
|
|
|
assert @book.attribute_changed?(:status)
|
|
|
|
@book.status = old_status
|
|
|
|
assert_not @book.attribute_changed?(:status)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reverted changes are not dirty going from nil to value and back" do
|
|
|
|
book = Book.create!(nullable_status: nil)
|
|
|
|
|
|
|
|
book.nullable_status = :married
|
|
|
|
assert book.attribute_changed?(:nullable_status)
|
|
|
|
|
|
|
|
book.nullable_status = nil
|
|
|
|
assert_not book.attribute_changed?(:nullable_status)
|
|
|
|
end
|
|
|
|
|
2013-11-04 13:54:46 -05:00
|
|
|
test "assign non existing value raises an error" do
|
|
|
|
e = assert_raises(ArgumentError) do
|
|
|
|
@book.status = :unknown
|
|
|
|
end
|
|
|
|
assert_equal "'unknown' is not a valid status", e.message
|
|
|
|
end
|
2013-11-06 09:25:04 -05:00
|
|
|
|
2013-12-20 07:12:59 -05:00
|
|
|
test "assign nil value" do
|
|
|
|
@book.status = nil
|
|
|
|
assert @book.status.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assign empty string value" do
|
|
|
|
@book.status = ''
|
|
|
|
assert @book.status.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assign long empty string value" do
|
|
|
|
@book.status = ' '
|
|
|
|
assert @book.status.nil?
|
|
|
|
end
|
|
|
|
|
2013-11-06 09:25:04 -05:00
|
|
|
test "constant to access the mapping" do
|
2014-01-14 06:58:22 -05:00
|
|
|
assert_equal 0, Book.statuses[:proposed]
|
|
|
|
assert_equal 1, Book.statuses["written"]
|
|
|
|
assert_equal 2, Book.statuses[:published]
|
2013-11-06 09:25:04 -05:00
|
|
|
end
|
2013-12-30 10:56:13 -05:00
|
|
|
|
2014-01-03 12:31:01 -05:00
|
|
|
test "building new objects with enum scopes" do
|
|
|
|
assert Book.written.build.written?
|
|
|
|
assert Book.read.build.read?
|
|
|
|
end
|
2013-12-30 10:56:13 -05:00
|
|
|
|
2014-01-03 12:31:01 -05:00
|
|
|
test "creating new objects with enum scopes" do
|
|
|
|
assert Book.written.create.written?
|
|
|
|
assert Book.read.create.read?
|
2013-12-30 10:56:13 -05:00
|
|
|
end
|
2014-01-11 06:57:09 -05:00
|
|
|
|
|
|
|
test "_before_type_cast returns the enum label (required for form fields)" do
|
|
|
|
assert_equal "proposed", @book.status_before_type_cast
|
|
|
|
end
|
2014-01-27 04:39:52 -05:00
|
|
|
|
|
|
|
test "reserved enum names" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [:proposed, :written, :published]
|
|
|
|
end
|
|
|
|
|
|
|
|
conflicts = [
|
|
|
|
:column, # generates class method .columns, which conflicts with an AR method
|
|
|
|
:logger, # generates #logger, which conflicts with an AR method
|
|
|
|
:attributes, # generates #attributes=, which conflicts with an AR method
|
|
|
|
]
|
|
|
|
|
|
|
|
conflicts.each_with_index do |name, i|
|
|
|
|
assert_raises(ArgumentError, "enum name `#{name}` should not be allowed") do
|
|
|
|
klass.class_eval { enum name => ["value_#{i}"] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reserved enum values" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [:proposed, :written, :published]
|
|
|
|
end
|
|
|
|
|
|
|
|
conflicts = [
|
|
|
|
:new, # generates a scope that conflicts with an AR class method
|
|
|
|
:valid, # generates #valid?, which conflicts with an AR method
|
|
|
|
:save, # generates #save!, which conflicts with an AR method
|
|
|
|
:proposed, # same value as an existing enum
|
2014-04-03 10:39:05 -04:00
|
|
|
:public, :private, :protected, # generates a method that conflict with ruby words
|
2014-01-27 04:39:52 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
conflicts.each_with_index do |value, i|
|
|
|
|
assert_raises(ArgumentError, "enum value `#{value}` should not be allowed") do
|
|
|
|
klass.class_eval { enum "status_#{i}" => [value] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "overriding enum method should not raise" do
|
|
|
|
assert_nothing_raised do
|
2014-01-31 00:50:09 -05:00
|
|
|
Class.new(ActiveRecord::Base) do
|
2014-01-27 04:39:52 -05:00
|
|
|
self.table_name = "books"
|
|
|
|
|
|
|
|
def published!
|
|
|
|
super
|
|
|
|
"do publish work..."
|
|
|
|
end
|
|
|
|
|
|
|
|
enum status: [:proposed, :written, :published]
|
|
|
|
|
|
|
|
def written!
|
|
|
|
super
|
|
|
|
"do written work..."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-02-25 20:24:44 -05:00
|
|
|
|
|
|
|
test "validate uniqueness" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Book'; end
|
|
|
|
enum status: [:proposed, :written]
|
|
|
|
validates_uniqueness_of :status
|
|
|
|
end
|
|
|
|
klass.delete_all
|
|
|
|
klass.create!(status: "proposed")
|
|
|
|
book = klass.new(status: "written")
|
|
|
|
assert book.valid?
|
|
|
|
book.status = "proposed"
|
|
|
|
assert_not book.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "validate inclusion of value in array" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name; 'Book'; end
|
|
|
|
enum status: [:proposed, :written]
|
|
|
|
validates_inclusion_of :status, in: ["written"]
|
|
|
|
end
|
|
|
|
klass.delete_all
|
|
|
|
invalid_book = klass.new(status: "proposed")
|
|
|
|
assert_not invalid_book.valid?
|
|
|
|
valid_book = klass.new(status: "written")
|
|
|
|
assert valid_book.valid?
|
|
|
|
end
|
2014-04-07 10:01:03 -04:00
|
|
|
|
|
|
|
test "enums are distinct per class" do
|
2014-04-07 21:52:21 -04:00
|
|
|
klass1 = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [:proposed, :written]
|
|
|
|
end
|
|
|
|
|
|
|
|
klass2 = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [:drafted, :uploaded]
|
2014-04-07 10:01:03 -04:00
|
|
|
end
|
2014-04-07 21:52:21 -04:00
|
|
|
|
|
|
|
book1 = klass1.proposed.create!
|
|
|
|
book1.status = :written
|
|
|
|
assert_equal ['proposed', 'written'], book1.status_change
|
|
|
|
|
|
|
|
book2 = klass2.drafted.create!
|
|
|
|
book2.status = :uploaded
|
|
|
|
assert_equal ['drafted', 'uploaded'], book2.status_change
|
2014-04-07 10:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enums are inheritable" do
|
2014-04-07 21:52:21 -04:00
|
|
|
subklass1 = Class.new(Book)
|
|
|
|
|
|
|
|
subklass2 = Class.new(Book) do
|
|
|
|
enum status: [:drafted, :uploaded]
|
2014-04-07 10:01:03 -04:00
|
|
|
end
|
2014-04-07 21:52:21 -04:00
|
|
|
|
|
|
|
book1 = subklass1.proposed.create!
|
|
|
|
book1.status = :written
|
|
|
|
assert_equal ['proposed', 'written'], book1.status_change
|
|
|
|
|
|
|
|
book2 = subklass2.drafted.create!
|
|
|
|
book2.status = :uploaded
|
|
|
|
assert_equal ['drafted', 'uploaded'], book2.status_change
|
2014-04-07 10:01:03 -04:00
|
|
|
end
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|