[Groonga-commit] ranguba/groonga-client-model at 365e039 [master] Add _key presence validation as the default validation

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Dec 22 16:46:26 JST 2016


Kouhei Sutou	2016-12-22 16:46:26 +0900 (Thu, 22 Dec 2016)

  New Revision: 365e03973e530b33a084515fedd1b9a2ecaef046
  https://github.com/ranguba/groonga-client-model/commit/365e03973e530b33a084515fedd1b9a2ecaef046

  Message:
    Add _key presence validation as the default validation

  Modified files:
    lib/groonga_client_model/record.rb
    test/unit/test_record.rb

  Modified: lib/groonga_client_model/record.rb (+6 -0)
===================================================================
--- lib/groonga_client_model/record.rb    2016-12-22 16:39:44 +0900 (8d9888f)
+++ lib/groonga_client_model/record.rb    2016-12-22 16:46:26 +0900 (4d71e23)
@@ -41,6 +41,10 @@ module GroongaClientModel
         schema.tables[table_name].columns
       end
 
+      def have_key?
+        columns.exist?("_key")
+      end
+
       def define_attributes
         return if defined?(@defined)
         @defined = true
@@ -118,6 +122,8 @@ module GroongaClientModel
 
     attr_reader :attributes
 
+    validates :_key, presence: true, if: ->(record) {record.class.have_key?}
+
     def initialize(attributes=nil)
       @attributes = {}
       self.class.define_attributes

  Modified: test/unit/test_record.rb (+37 -0)
===================================================================
--- test/unit/test_record.rb    2016-12-22 16:39:44 +0900 (18fff3c)
+++ test/unit/test_record.rb    2016-12-22 16:46:26 +0900 (be41450)
@@ -49,4 +49,41 @@ class TestRecord < Test::Unit::TestCase
       assert_equal(29, @memo.id)
     end
   end
+
+  sub_test_case("validations") do
+    sub_test_case("_key presence") do
+      class NoKey < GroongaClientModel::Record
+        class << self
+          def columns
+            GroongaClientModel::Schema::Columns.new(nil, "_id" => {})
+          end
+        end
+      end
+
+      class HaveKey < GroongaClientModel::Record
+        class << self
+          def columns
+            GroongaClientModel::Schema::Columns.new(nil,
+                                                    "_id" => {},
+                                                    "_key" => {})
+          end
+        end
+      end
+
+      test "no key" do
+        record = NoKey.new
+        record.validate
+        assert_equal({}, record.errors.messages)
+      end
+
+      test "have key" do
+        record = HaveKey.new
+        record.validate
+        assert_equal({
+                       :_key => [record.errors.generate_message(:_key, :blank)],
+                     },
+                     record.errors.messages)
+      end
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



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