2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
2017-05-02 15:37:06 -04:00
|
|
|
require "models/author"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/book"
|
2019-06-02 17:01:01 -04:00
|
|
|
require "active_support/log_subscriber/test_helper"
|
2013-11-02 15:01:31 -04:00
|
|
|
|
2013-11-04 13:22:53 -05:00
|
|
|
class EnumTest < ActiveRecord::TestCase
|
2017-08-22 14:49:50 -04:00
|
|
|
fixtures :books, :authors, :author_addresses
|
2013-11-02 15:01:31 -04:00
|
|
|
|
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :published?
|
|
|
|
assert_not_predicate @book, :written?
|
|
|
|
assert_not_predicate @book, :proposed?
|
2013-11-02 20:48:16 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :read?
|
|
|
|
assert_predicate @book, :in_english?
|
|
|
|
assert_predicate @book, :author_visibility_visible?
|
|
|
|
assert_predicate @book, :illustrator_visibility_visible?
|
|
|
|
assert_predicate @book, :with_medium_font_size?
|
|
|
|
assert_predicate @book, :medium_to_read?
|
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
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal "published", @book.status
|
2020-05-13 06:58:12 -04:00
|
|
|
assert_equal "read", @book.last_read
|
2015-04-19 05:25:09 -04:00
|
|
|
assert_equal "english", @book.language
|
|
|
|
assert_equal "visible", @book.author_visibility
|
|
|
|
assert_equal "visible", @book.illustrator_visibility
|
2016-09-11 02:29:00 -04:00
|
|
|
assert_equal "medium", @book.difficulty
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|
|
|
|
|
2013-11-02 19:18:31 -04:00
|
|
|
test "find via scope" do
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.published.first
|
|
|
|
assert_equal @book, Book.read.first
|
2015-04-19 05:25:09 -04:00
|
|
|
assert_equal @book, Book.in_english.first
|
|
|
|
assert_equal @book, Book.author_visibility_visible.first
|
|
|
|
assert_equal @book, Book.illustrator_visibility_visible.first
|
2016-09-11 02:29:00 -04:00
|
|
|
assert_equal @book, Book.medium_to_read.first
|
2017-05-03 05:35:45 -04:00
|
|
|
assert_equal books(:ddd), Book.forgotten.first
|
|
|
|
assert_equal books(:rfr), authors(:david).unpublished_books.first
|
2013-11-02 19:18:31 -04:00
|
|
|
end
|
2019-02-26 15:54:02 -05:00
|
|
|
|
2019-02-26 15:47:27 -05:00
|
|
|
test "find via negative scope" do
|
|
|
|
assert Book.not_published.exclude?(@book)
|
|
|
|
assert Book.not_proposed.include?(@book)
|
|
|
|
end
|
2013-11-02 19:18:31 -04:00
|
|
|
|
2015-02-13 19:46:52 -05:00
|
|
|
test "find via where with values" do
|
2015-05-15 23:27:41 -04:00
|
|
|
published, written = Book.statuses[:published], Book.statuses[:written]
|
2015-02-13 19:46:52 -05:00
|
|
|
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.where(status: published).first
|
2015-08-13 03:20:39 -04:00
|
|
|
assert_not_equal @book, Book.where(status: written).first
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.where(status: [published]).first
|
2015-08-13 03:20:39 -04:00
|
|
|
assert_not_equal @book, Book.where(status: [written]).first
|
|
|
|
assert_not_equal @book, Book.where("status <> ?", published).first
|
2015-02-13 19:46:52 -05:00
|
|
|
assert_equal @book, Book.where("status <> ?", written).first
|
2015-02-11 16:56:26 -05:00
|
|
|
end
|
|
|
|
|
2015-02-13 19:18:22 -05:00
|
|
|
test "find via where with symbols" do
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.where(status: :published).first
|
2015-08-13 03:20:39 -04:00
|
|
|
assert_not_equal @book, Book.where(status: :written).first
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.where(status: [:published]).first
|
2015-08-13 03:20:39 -04:00
|
|
|
assert_not_equal @book, Book.where(status: [:written]).first
|
|
|
|
assert_not_equal @book, Book.where.not(status: :published).first
|
2015-02-14 03:04:16 -05:00
|
|
|
assert_equal @book, Book.where.not(status: :written).first
|
2020-05-13 06:58:12 -04:00
|
|
|
assert_equal books(:ddd), Book.where(last_read: :forgotten).first
|
2015-02-13 19:18:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "find via where with strings" do
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.where(status: "published").first
|
2015-08-13 03:20:39 -04:00
|
|
|
assert_not_equal @book, Book.where(status: "written").first
|
2015-05-15 23:27:41 -04:00
|
|
|
assert_equal @book, Book.where(status: ["published"]).first
|
2015-08-13 03:20:39 -04:00
|
|
|
assert_not_equal @book, Book.where(status: ["written"]).first
|
|
|
|
assert_not_equal @book, Book.where.not(status: "published").first
|
2015-02-14 03:04:16 -05:00
|
|
|
assert_equal @book, Book.where.not(status: "written").first
|
2020-05-13 06:58:12 -04:00
|
|
|
assert_equal books(:ddd), Book.where(last_read: "forgotten").first
|
2015-02-11 16:56:26 -05:00
|
|
|
end
|
|
|
|
|
2015-02-13 19:46:52 -05:00
|
|
|
test "build from scope" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Book.written.build, :written?
|
|
|
|
assert_not_predicate Book.written.build, :proposed?
|
2015-02-13 19:46:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "build from where" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Book.where(status: Book.statuses[:written]).build, :written?
|
|
|
|
assert_not_predicate Book.where(status: Book.statuses[:written]).build, :proposed?
|
|
|
|
assert_predicate Book.where(status: :written).build, :written?
|
|
|
|
assert_not_predicate Book.where(status: :written).build, :proposed?
|
|
|
|
assert_predicate Book.where(status: "written").build, :written?
|
|
|
|
assert_not_predicate Book.where(status: "written").build, :proposed?
|
2015-02-13 19:46:52 -05:00
|
|
|
end
|
|
|
|
|
2013-11-02 15:01:31 -04:00
|
|
|
test "update by declaration" do
|
|
|
|
@book.written!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :written?
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.in_english!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :in_english?
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.author_visibility_visible!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :author_visibility_visible?
|
2013-11-02 15:01:31 -04:00
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :written?
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|
2013-11-04 13:36:22 -05:00
|
|
|
|
|
|
|
test "enum methods are overwritable" do
|
|
|
|
assert_equal "do publish work...", @book.published!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :published?
|
2013-11-04 13:36:22 -05:00
|
|
|
end
|
2013-11-04 13:54:46 -05:00
|
|
|
|
|
|
|
test "direct assignment" do
|
|
|
|
@book.status = :written
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :written?
|
2013-11-04 13:54:46 -05:00
|
|
|
end
|
|
|
|
|
2013-11-05 11:29:52 -05:00
|
|
|
test "assign string value" do
|
|
|
|
@book.status = "written"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :written?
|
2013-11-05 11:29:52 -05:00
|
|
|
end
|
|
|
|
|
2014-01-20 18:59:20 -05:00
|
|
|
test "enum changed attributes" do
|
|
|
|
old_status = @book.status
|
2015-04-19 05:25:09 -04:00
|
|
|
old_language = @book.language
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :spanish
|
2014-01-20 18:59:20 -05:00
|
|
|
assert_equal old_status, @book.changed_attributes[:status]
|
2015-04-19 05:25:09 -04:00
|
|
|
assert_equal old_language, @book.changed_attributes[:language]
|
2014-01-20 18:59:20 -05:00
|
|
|
end
|
|
|
|
|
2020-04-03 14:33:14 -04:00
|
|
|
test "enum value after write symbol" do
|
|
|
|
@book.status = :proposed
|
|
|
|
assert_equal "proposed", @book.status
|
|
|
|
end
|
|
|
|
|
|
|
|
test "enum value after write string" do
|
|
|
|
@book.status = "proposed"
|
|
|
|
assert_equal "proposed", @book.status
|
|
|
|
end
|
|
|
|
|
2014-01-20 18:59:20 -05:00
|
|
|
test "enum changes" do
|
|
|
|
old_status = @book.status
|
2015-04-19 05:25:09 -04:00
|
|
|
old_language = @book.language
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :spanish
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal [old_status, "proposed"], @book.changes[:status]
|
|
|
|
assert_equal [old_language, "spanish"], @book.changes[:language]
|
2014-01-20 18:59:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute was" do
|
|
|
|
old_status = @book.status
|
2015-04-19 05:25:09 -04:00
|
|
|
old_language = @book.language
|
2014-01-20 18:59:20 -05:00
|
|
|
@book.status = :published
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :spanish
|
2014-01-20 18:59:20 -05:00
|
|
|
assert_equal old_status, @book.attribute_was(:status)
|
2015-04-19 05:25:09 -04:00
|
|
|
assert_equal old_language, @book.attribute_was(:language)
|
2014-01-20 18:59:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed" do
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :french
|
2014-01-20 18:59:20 -05:00
|
|
|
assert @book.attribute_changed?(:status)
|
2015-04-19 05:25:09 -04:00
|
|
|
assert @book.attribute_changed?(:language)
|
2014-01-20 18:59:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed to" do
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :french
|
2016-08-06 12:26:20 -04:00
|
|
|
assert @book.attribute_changed?(:status, to: "proposed")
|
|
|
|
assert @book.attribute_changed?(:language, to: "french")
|
2014-01-20 18:59:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed from" do
|
|
|
|
old_status = @book.status
|
2015-04-19 05:25:09 -04:00
|
|
|
old_language = @book.language
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :french
|
2014-01-20 18:59:20 -05:00
|
|
|
assert @book.attribute_changed?(:status, from: old_status)
|
2015-04-19 05:25:09 -04:00
|
|
|
assert @book.attribute_changed?(:language, from: old_language)
|
2014-01-20 18:59:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enum attribute changed from old status to new status" do
|
|
|
|
old_status = @book.status
|
2015-04-19 05:25:09 -04:00
|
|
|
old_language = @book.language
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2015-04-19 05:25:09 -04:00
|
|
|
@book.language = :french
|
2016-08-06 12:26:20 -04:00
|
|
|
assert @book.attribute_changed?(:status, from: old_status, to: "proposed")
|
|
|
|
assert @book.attribute_changed?(:language, from: old_language, to: "french")
|
2014-01-20 18:59:20 -05:00
|
|
|
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
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2014-01-20 20:10:48 -05:00
|
|
|
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
|
2015-05-15 23:27:41 -04:00
|
|
|
@book.status = :proposed
|
2014-01-20 20:10:48 -05:00
|
|
|
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
|
|
|
|
2015-02-14 02:51:43 -05:00
|
|
|
test "NULL values from database should be casted to nil" do
|
|
|
|
Book.where(id: @book.id).update_all("status = NULL")
|
|
|
|
assert_nil @book.reload.status
|
|
|
|
end
|
|
|
|
|
2013-12-20 07:12:59 -05:00
|
|
|
test "assign nil value" do
|
|
|
|
@book.status = nil
|
2015-02-14 02:51:43 -05:00
|
|
|
assert_nil @book.status
|
2013-12-20 07:12:59 -05:00
|
|
|
end
|
|
|
|
|
2019-12-26 23:58:54 -05:00
|
|
|
test "assign nil value to enum which defines nil value to hash" do
|
2020-05-13 06:58:12 -04:00
|
|
|
@book.last_read = nil
|
|
|
|
assert_equal "forgotten", @book.last_read
|
2019-12-26 23:58:54 -05:00
|
|
|
end
|
|
|
|
|
2013-12-20 07:12:59 -05:00
|
|
|
test "assign empty string value" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@book.status = ""
|
2015-02-14 02:51:43 -05:00
|
|
|
assert_nil @book.status
|
2013-12-20 07:12:59 -05:00
|
|
|
end
|
|
|
|
|
2019-12-24 02:11:07 -05:00
|
|
|
test "assign false value to a field defined as not boolean" do
|
|
|
|
@book.status = false
|
|
|
|
assert_nil @book.status
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assign false value to a field defined as boolean" do
|
|
|
|
@book.boolean_status = false
|
|
|
|
assert_equal "disabled", @book.boolean_status
|
|
|
|
end
|
|
|
|
|
2013-12-20 07:12:59 -05:00
|
|
|
test "assign long empty string value" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@book.status = " "
|
2015-02-14 02:51:43 -05:00
|
|
|
assert_nil @book.status
|
2013-12-20 07:12:59 -05:00
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Book.written.build, :written?
|
|
|
|
assert_predicate Book.read.build, :read?
|
|
|
|
assert_predicate Book.in_spanish.build, :in_spanish?
|
|
|
|
assert_predicate Book.illustrator_visibility_invisible.build, :illustrator_visibility_invisible?
|
2014-01-03 12:31:01 -05:00
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Book.written.create, :written?
|
|
|
|
assert_predicate Book.read.create, :read?
|
|
|
|
assert_predicate Book.in_spanish.create, :in_spanish?
|
|
|
|
assert_predicate Book.illustrator_visibility_invisible.create, :illustrator_visibility_invisible?
|
2013-12-30 10:56:13 -05:00
|
|
|
end
|
2014-01-11 06:57:09 -05:00
|
|
|
|
2017-08-16 04:48:44 -04:00
|
|
|
test "_before_type_cast" do
|
|
|
|
assert_equal 2, @book.status_before_type_cast
|
|
|
|
assert_equal "published", @book.status
|
|
|
|
|
|
|
|
@book.status = "published"
|
|
|
|
|
|
|
|
assert_equal "published", @book.status_before_type_cast
|
|
|
|
assert_equal "published", @book.status
|
2014-01-11 06:57:09 -05:00
|
|
|
end
|
2014-01-27 04:39:52 -05:00
|
|
|
|
2018-10-06 12:08:45 -04:00
|
|
|
test "invalid definition values raise an ArgumentError" do
|
|
|
|
e = assert_raises(ArgumentError) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [proposed: 1, written: 2, published: 3]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/must be either a hash, an array of symbols, or an array of strings./, e.message)
|
2018-11-07 17:38:41 -05:00
|
|
|
|
|
|
|
e = assert_raises(ArgumentError) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: { "" => 1, "active" => 2 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/Enum label name must not be blank/, e.message)
|
|
|
|
|
|
|
|
e = assert_raises(ArgumentError) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: ["active", ""]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/Enum label name must not be blank/, e.message)
|
2018-10-06 12:08:45 -04:00
|
|
|
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|
|
2014-11-07 21:27:18 -05:00
|
|
|
e = assert_raises(ArgumentError) do
|
2014-01-27 04:39:52 -05:00
|
|
|
klass.class_eval { enum name => ["value_#{i}"] }
|
|
|
|
end
|
2014-11-07 21:27:18 -05:00
|
|
|
assert_match(/You tried to define an enum named \"#{name}\" on the model/, e.message)
|
2014-01-27 04:39:52 -05:00
|
|
|
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-09-04 03:26:27 -04:00
|
|
|
:public, :private, :protected, # some important methods on Module and Class
|
|
|
|
:name, :parent, :superclass
|
2014-01-27 04:39:52 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
conflicts.each_with_index do |value, i|
|
2014-11-07 21:27:18 -05:00
|
|
|
e = assert_raises(ArgumentError, "enum value `#{value}` should not be allowed") do
|
2014-01-27 04:39:52 -05:00
|
|
|
klass.class_eval { enum "status_#{i}" => [value] }
|
|
|
|
end
|
2014-11-07 21:27:18 -05:00
|
|
|
assert_match(/You tried to define an enum named .* on the model/, e.message)
|
2014-01-27 04:39:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-18 14:22:00 -05:00
|
|
|
test "reserved enum values for relation" do
|
|
|
|
relation_method_samples = [
|
|
|
|
:records,
|
|
|
|
:to_ary,
|
|
|
|
:scope_for_create
|
|
|
|
]
|
|
|
|
|
|
|
|
relation_method_samples.each do |value|
|
|
|
|
e = assert_raises(ArgumentError, "enum value `#{value}` should not be allowed") do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum category: [:other, value]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_match(/You tried to define an enum named .* on the model/, e.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-27 04:39:52 -05:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
def self.name; "Book"; end
|
2014-02-25 20:24:44 -05:00
|
|
|
enum status: [:proposed, :written]
|
|
|
|
validates_uniqueness_of :status
|
|
|
|
end
|
|
|
|
klass.delete_all
|
|
|
|
klass.create!(status: "proposed")
|
|
|
|
book = klass.new(status: "written")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book, :valid?
|
2014-02-25 20:24:44 -05:00
|
|
|
book.status = "proposed"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate book, :valid?
|
2014-02-25 20:24:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "validate inclusion of value in array" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
2016-08-06 12:26:20 -04:00
|
|
|
def self.name; "Book"; end
|
2014-02-25 20:24:44 -05:00
|
|
|
enum status: [:proposed, :written]
|
|
|
|
validates_inclusion_of :status, in: ["written"]
|
|
|
|
end
|
|
|
|
klass.delete_all
|
|
|
|
invalid_book = klass.new(status: "proposed")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate invalid_book, :valid?
|
2014-02-25 20:24:44 -05:00
|
|
|
valid_book = klass.new(status: "written")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate valid_book, :valid?
|
2014-02-25 20:24:44 -05:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["proposed", "written"], book1.status_change
|
2014-04-07 21:52:21 -04:00
|
|
|
|
|
|
|
book2 = klass2.drafted.create!
|
|
|
|
book2.status = :uploaded
|
2016-08-06 12:26:20 -04:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["proposed", "written"], book1.status_change
|
2014-04-07 21:52:21 -04:00
|
|
|
|
|
|
|
book2 = subklass2.drafted.create!
|
|
|
|
book2.status = :uploaded
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["drafted", "uploaded"], book2.status_change
|
2014-04-07 10:01:03 -04:00
|
|
|
end
|
2015-03-05 07:39:03 -05:00
|
|
|
|
2018-11-23 18:55:31 -05:00
|
|
|
test "attempting to modify enum raises error" do
|
|
|
|
e = assert_raises(RuntimeError) do
|
|
|
|
Book.statuses["bad_enum"] = 40
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/can't modify frozen/, e.message)
|
|
|
|
|
|
|
|
e = assert_raises(RuntimeError) do
|
|
|
|
Book.statuses.delete("published")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/can't modify frozen/, e.message)
|
|
|
|
end
|
|
|
|
|
2015-03-05 07:39:03 -05:00
|
|
|
test "declare multiple enums at a time" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [:proposed, :written, :published],
|
|
|
|
nullable_status: [:single, :married]
|
|
|
|
end
|
|
|
|
|
|
|
|
book1 = klass.proposed.create!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book1, :proposed?
|
2015-03-05 07:39:03 -05:00
|
|
|
|
|
|
|
book2 = klass.single.create!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book2, :single?
|
2015-03-05 07:39:03 -05:00
|
|
|
end
|
2015-04-19 05:25:09 -04:00
|
|
|
|
2016-07-30 16:08:15 -04:00
|
|
|
test "enum with alias_attribute" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
alias_attribute :aliased_status, :status
|
|
|
|
enum aliased_status: [:proposed, :written, :published]
|
|
|
|
end
|
|
|
|
|
|
|
|
book = klass.proposed.create!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book, :proposed?
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "proposed", book.aliased_status
|
2016-07-30 16:08:15 -04:00
|
|
|
|
|
|
|
book = klass.find(book.id)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book, :proposed?
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "proposed", book.aliased_status
|
2016-07-30 16:08:15 -04:00
|
|
|
end
|
|
|
|
|
2015-04-19 05:25:09 -04:00
|
|
|
test "query state by predicate with prefix" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :author_visibility_visible?
|
|
|
|
assert_not_predicate @book, :author_visibility_invisible?
|
|
|
|
assert_predicate @book, :illustrator_visibility_visible?
|
|
|
|
assert_not_predicate @book, :illustrator_visibility_invisible?
|
2015-04-19 05:25:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "query state by predicate with custom prefix" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @book, :in_english?
|
|
|
|
assert_not_predicate @book, :in_spanish?
|
|
|
|
assert_not_predicate @book, :in_french?
|
2015-04-19 05:25:09 -04:00
|
|
|
end
|
2015-07-02 15:42:52 -04:00
|
|
|
|
2016-09-11 02:29:00 -04:00
|
|
|
test "query state by predicate with custom suffix" do
|
2018-01-25 18:57:25 -05:00
|
|
|
assert_predicate @book, :medium_to_read?
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @book, :easy_to_read?
|
|
|
|
assert_not_predicate @book, :hard_to_read?
|
2016-09-11 02:29:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enum methods with custom suffix defined" do
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_respond_to @book.class, :easy_to_read
|
|
|
|
assert_respond_to @book.class, :medium_to_read
|
|
|
|
assert_respond_to @book.class, :hard_to_read
|
2016-09-11 02:29:00 -04:00
|
|
|
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_respond_to @book, :easy_to_read?
|
|
|
|
assert_respond_to @book, :medium_to_read?
|
|
|
|
assert_respond_to @book, :hard_to_read?
|
2016-09-11 02:29:00 -04:00
|
|
|
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_respond_to @book, :easy_to_read!
|
|
|
|
assert_respond_to @book, :medium_to_read!
|
|
|
|
assert_respond_to @book, :hard_to_read!
|
2016-09-11 02:29:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "update enum attributes with custom suffix" do
|
|
|
|
@book.medium_to_read!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @book, :easy_to_read?
|
2018-01-25 18:57:25 -05:00
|
|
|
assert_predicate @book, :medium_to_read?
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @book, :hard_to_read?
|
2016-09-11 02:29:00 -04:00
|
|
|
|
|
|
|
@book.easy_to_read!
|
2018-01-25 18:57:25 -05:00
|
|
|
assert_predicate @book, :easy_to_read?
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @book, :medium_to_read?
|
|
|
|
assert_not_predicate @book, :hard_to_read?
|
2016-09-11 02:29:00 -04:00
|
|
|
|
|
|
|
@book.hard_to_read!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @book, :easy_to_read?
|
|
|
|
assert_not_predicate @book, :medium_to_read?
|
2018-01-25 18:57:25 -05:00
|
|
|
assert_predicate @book, :hard_to_read?
|
2016-09-11 02:29:00 -04:00
|
|
|
end
|
|
|
|
|
2015-07-02 15:42:52 -04:00
|
|
|
test "uses default status when no status is provided in fixtures" do
|
|
|
|
book = books(:tlg)
|
|
|
|
assert book.proposed?, "expected fixture to default to proposed status"
|
|
|
|
assert book.in_english?, "expected fixture to default to english language"
|
|
|
|
end
|
2016-01-23 10:42:40 -05:00
|
|
|
|
|
|
|
test "uses default value from database on initialization" do
|
|
|
|
book = Book.new
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book, :proposed?
|
2016-01-23 10:42:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "uses default value from database on initialization when using custom mapping" do
|
|
|
|
book = Book.new
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate book, :hard?
|
2016-01-23 10:42:40 -05:00
|
|
|
end
|
2016-06-24 04:43:25 -04:00
|
|
|
|
|
|
|
test "data type of Enum type" do
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal :integer, Book.type_for_attribute("status").type
|
2016-06-24 04:43:25 -04:00
|
|
|
end
|
2018-12-03 04:52:59 -05:00
|
|
|
|
|
|
|
test "scopes can be disabled" do
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "books"
|
|
|
|
enum status: [:proposed, :written], _scopes: false
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(NoMethodError) { klass.proposed }
|
|
|
|
end
|
2019-06-02 17:01:01 -04:00
|
|
|
|
|
|
|
test "enums with a negative condition log a warning" do
|
|
|
|
old_logger = ActiveRecord::Base.logger
|
|
|
|
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
|
|
|
|
|
|
|
|
ActiveRecord::Base.logger = logger
|
|
|
|
|
|
|
|
expected_message = "An enum element in Book uses the prefix 'not_'."\
|
|
|
|
" This will cause a conflict with auto generated negative scopes."
|
|
|
|
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
def self.name
|
|
|
|
"Book"
|
|
|
|
end
|
2019-07-29 15:49:20 -04:00
|
|
|
silence_warnings do
|
|
|
|
enum status: [:sent, :not_sent]
|
|
|
|
end
|
2019-06-02 17:01:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(expected_message, logger.logged(:warn).first)
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.logger = old_logger
|
|
|
|
end
|
2013-11-02 15:01:31 -04:00
|
|
|
end
|