[Groonga-commit] ranguba/groonga-client-model at 67c209a [master] Support all UInt family validation

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Feb 6 12:49:21 JST 2017


Kouhei Sutou	2017-02-06 12:49:21 +0900 (Mon, 06 Feb 2017)

  New Revision: 67c209ab724e5a6b6a501c8cee933fd1e1fe52b1
  https://github.com/ranguba/groonga-client-model/commit/67c209ab724e5a6b6a501c8cee933fd1e1fe52b1

  Message:
    Support all UInt family validation

  Modified files:
    lib/groonga_client_model/locale/en.yml
    lib/groonga_client_model/validations/type_validator.rb
    test/unit/test_record.rb

  Modified: lib/groonga_client_model/locale/en.yml (+6 -0)
===================================================================
--- lib/groonga_client_model/locale/en.yml    2017-02-06 12:45:12 +0900 (fdf65fe)
+++ lib/groonga_client_model/locale/en.yml    2017-02-06 12:49:21 +0900 (ad78066)
@@ -3,5 +3,11 @@ en:
     messages:
       uint:
         "must be positive integer: %{inspected_value}"
+      uint8:
+        "must be less than 2 ** 8: %{inspected_value}"
+      uint16:
+        "must be less than 2 ** 16: %{inspected_value}"
       uint32:
         "must be less than 2 ** 32: %{inspected_value}"
+      uint64:
+        "must be less than 2 ** 64: %{inspected_value}"

  Modified: lib/groonga_client_model/validations/type_validator.rb (+7 -5)
===================================================================
--- lib/groonga_client_model/validations/type_validator.rb    2017-02-06 12:45:12 +0900 (30d7289)
+++ lib/groonga_client_model/validations/type_validator.rb    2017-02-06 12:49:21 +0900 (d45b47b)
@@ -24,8 +24,14 @@ module GroongaClientModel
         return if value_type.nil?
 
         case value_type["name"]
+        when "UInt8"
+          validate_uint(record, attribute, value, 8)
+        when "UInt16"
+          validate_uint(record, attribute, value, 16)
         when "UInt32"
-          validate_uint32(record, attribute, value)
+          validate_uint(record, attribute, value, 32)
+        when "UInt64"
+          validate_uint(record, attribute, value, 64)
         end
       end
 
@@ -58,10 +64,6 @@ module GroongaClientModel
                             options.merge(inspected_value: value.inspect))
         end
       end
-
-      def validate_uint32(record, attribute, value)
-        validate_uint(record, attribute, value, 32)
-      end
     end
   end
 end

  Modified: test/unit/test_record.rb (+71 -26)
===================================================================
--- test/unit/test_record.rb    2017-02-06 12:45:12 +0900 (02d1b4f)
+++ test/unit/test_record.rb    2017-02-06 12:49:21 +0900 (78bfa77)
@@ -93,6 +93,22 @@ class TestRecord < Test::Unit::TestCase
         end
       end
 
+      class UInt8Key < Key
+        class << self
+          def key_type
+            "UInt8"
+          end
+        end
+      end
+
+      class UInt16Key < Key
+        class << self
+          def key_type
+            "UInt16"
+          end
+        end
+      end
+
       class UInt32Key < Key
         class << self
           def key_type
@@ -101,6 +117,14 @@ class TestRecord < Test::Unit::TestCase
         end
       end
 
+      class UInt64Key < Key
+        class << self
+          def key_type
+            "UInt64"
+          end
+        end
+      end
+
       sub_test_case("presence") do
         test "no key" do
           record = NoKey.new
@@ -145,37 +169,58 @@ class TestRecord < Test::Unit::TestCase
       end
 
       sub_test_case("type") do
+        def assert_invalid(klass, key, message_key)
+          record = klass.new(_key: key)
+          assert do
+            not record.valid?
+          end
+          options = {
+            inspected_value: key.inspect
+          }
+          message = record.errors.generate_message(:_key, message_key, options)
+          assert_equal({
+                         :_key => [message],
+                       },
+                       record.errors.messages)
+        end
+
+        sub_test_case("UInt8") do
+          test("invalid") do
+            assert_invalid(UInt8Key, "String", :uint)
+          end
+
+          test("too large") do
+            assert_invalid(UInt8Key, 2 ** 8, :uint8)
+          end
+        end
+
+        sub_test_case("UInt16") do
+          test("invalid") do
+            assert_invalid(UInt16Key, "String", :uint)
+          end
+
+          test("too large") do
+            assert_invalid(UInt16Key, 2 ** 16, :uint16)
+          end
+        end
+
         sub_test_case("UInt32") do
           test("invalid") do
-            key = "String"
-            record = UInt32Key.new(_key: key)
-            assert do
-              not record.valid?
-            end
-            options = {
-              inspected_value: key.inspect
-            }
-            message = record.errors.generate_message(:_key, :uint, options)
-            assert_equal({
-                           :_key => [message],
-                         },
-                         record.errors.messages)
+            assert_invalid(UInt32Key, "String", :uint)
           end
 
           test("too large") do
-            key = 2 ** 32
-            record = UInt32Key.new(_key: key)
-            assert do
-              not record.valid?
-            end
-            options = {
-              inspected_value: key.inspect
-            }
-            message = record.errors.generate_message(:_key, :uint32, options)
-            assert_equal({
-                           :_key => [message],
-                         },
-                         record.errors.messages)
+            assert_invalid(UInt32Key, 2 ** 32, :uint32)
+          end
+        end
+
+        sub_test_case("UInt64") do
+          test("invalid") do
+            assert_invalid(UInt64Key, "String", :uint)
+          end
+
+          test("too large") do
+            assert_invalid(UInt64Key, 2 ** 64, :uint64)
           end
         end
       end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index