1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/enum_test.rb
Robin Dupret 7aebcb67b0 Fix the enums writer methods
Previously, the writer methods would simply check whether the passed
argument was the symbol representing the integer value of an enum field.
Therefore, it was not possible to specify the numeric value itself but
the dynamically defined scopes generate where clauses relying on this
kind of values so a chained call to a method like `find_or_initialize_by`
would trigger an `ArgumentError`.

Reference #13530
2014-01-01 17:57:34 +01:00

90 lines
1.9 KiB
Ruby

require 'cases/helper'
require 'models/book'
class EnumTest < ActiveRecord::TestCase
fixtures :books
setup do
@book = books(:awdr)
end
test "query state by predicate" do
assert @book.proposed?
assert_not @book.written?
assert_not @book.published?
assert @book.unread?
end
test "query state with strings" do
assert_equal "proposed", @book.status
assert_equal "unread", @book.read_status
end
test "find via scope" do
assert_equal @book, Book.proposed.first
assert_equal @book, Book.unread.first
end
test "update by declaration" do
@book.written!
assert @book.written?
end
test "update by setter" do
@book.update! status: :written
assert @book.written?
end
test "enum methods are overwritable" do
assert_equal "do publish work...", @book.published!
assert @book.published?
end
test "direct assignment" do
@book.status = :written
assert @book.written?
end
test "assign string value" do
@book.status = "written"
assert @book.written?
end
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
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
test "constant to access the mapping" do
assert_equal 0, Book::STATUS[:proposed]
assert_equal 1, Book::STATUS["written"]
assert_equal 2, Book::STATUS[:published]
end
test "first_or_initialize with enums' scopes" do
class Issue < ActiveRecord::Base
enum status: [:open, :closed]
end
assert Issue.open.empty?
assert Issue.open.first_or_initialize
end
end