From 6009107caf9b0b570519b17aa0745fdb5a8c3b1f Mon Sep 17 00:00:00 2001
From: Yosuke Kabuto <bluewhale1982@gmail.com>
Date: Sun, 11 Sep 2016 15:29:00 +0900
Subject: Add tests for ActiveRecord::Enum#enum when suffix specified

Make name of attribute medium instead of normal
---
 activerecord/test/cases/enum_test.rb | 40 ++++++++++++++++++++++++++++++++++++
 activerecord/test/fixtures/books.yml |  1 +
 activerecord/test/models/book.rb     |  1 +
 activerecord/test/schema/schema.rb   |  1 +
 4 files changed, 43 insertions(+)

(limited to 'activerecord/test')

diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index c2009843f0..b7641fcf32 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -18,6 +18,7 @@ class EnumTest < ActiveRecord::TestCase
     assert @book.author_visibility_visible?
     assert @book.illustrator_visibility_visible?
     assert @book.with_medium_font_size?
+    assert @book.medium_to_read?
   end
 
   test "query state with strings" do
@@ -26,6 +27,7 @@ class EnumTest < ActiveRecord::TestCase
     assert_equal "english", @book.language
     assert_equal "visible", @book.author_visibility
     assert_equal "visible", @book.illustrator_visibility
+    assert_equal "medium", @book.difficulty
   end
 
   test "find via scope" do
@@ -34,6 +36,7 @@ class EnumTest < ActiveRecord::TestCase
     assert_equal @book, Book.in_english.first
     assert_equal @book, Book.author_visibility_visible.first
     assert_equal @book, Book.illustrator_visibility_visible.first
+    assert_equal @book, Book.medium_to_read.first
   end
 
   test "find via where with values" do
@@ -422,6 +425,43 @@ class EnumTest < ActiveRecord::TestCase
     assert_not @book.in_french?
   end
 
+  test "query state by predicate with custom suffix" do
+    assert     @book.medium_to_read?
+    assert_not @book.easy_to_read?
+    assert_not @book.hard_to_read?
+  end
+
+  test "enum methods with custom suffix defined" do
+    assert @book.class.respond_to?(:easy_to_read)
+    assert @book.class.respond_to?(:medium_to_read)
+    assert @book.class.respond_to?(:hard_to_read)
+
+    assert @book.respond_to?(:easy_to_read?)
+    assert @book.respond_to?(:medium_to_read?)
+    assert @book.respond_to?(:hard_to_read?)
+
+    assert @book.respond_to?(:easy_to_read!)
+    assert @book.respond_to?(:medium_to_read!)
+    assert @book.respond_to?(:hard_to_read!)
+  end
+
+  test "update enum attributes with custom suffix" do
+    @book.medium_to_read!
+    assert_not @book.easy_to_read?
+    assert     @book.medium_to_read?
+    assert_not @book.hard_to_read?
+
+    @book.easy_to_read!
+    assert     @book.easy_to_read?
+    assert_not @book.medium_to_read?
+    assert_not @book.hard_to_read?
+
+    @book.hard_to_read!
+    assert_not @book.easy_to_read?
+    assert_not @book.medium_to_read?
+    assert     @book.hard_to_read?
+  end
+
   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"
diff --git a/activerecord/test/fixtures/books.yml b/activerecord/test/fixtures/books.yml
index a304fba399..b3625ee72e 100644
--- a/activerecord/test/fixtures/books.yml
+++ b/activerecord/test/fixtures/books.yml
@@ -9,6 +9,7 @@ awdr:
   author_visibility: :visible
   illustrator_visibility: :visible
   font_size: :medium
+  difficulty: :medium
 
 rfr:
   author_id: 1
diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb
index 4c275fdebb..17bf3fbcb4 100644
--- a/activerecord/test/models/book.rb
+++ b/activerecord/test/models/book.rb
@@ -14,6 +14,7 @@ class Book < ActiveRecord::Base
   enum author_visibility: [:visible, :invisible], _prefix: true
   enum illustrator_visibility: [:visible, :invisible], _prefix: true
   enum font_size: [:small, :medium, :large], _prefix: :with, _suffix: true
+  enum difficulty: [:easy, :medium, :hard], _suffix: :to_read
   enum cover: { hard: "hard", soft: "soft" }
 
   def published!
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 9997ddee77..a4756ec75a 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -98,6 +98,7 @@ ActiveRecord::Schema.define do
     t.column :author_visibility, :integer, default: 0
     t.column :illustrator_visibility, :integer, default: 0
     t.column :font_size, :integer, default: 0
+    t.column :difficulty, :integer, default: 0
     t.column :cover, :string, default: "hard"
   end
 
-- 
cgit v1.2.3