[Groonga-commit] ranguba/groonga-client at 5b507a4 [master] load: support command_version=3 output

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 6 16:17:58 JST 2016


Kouhei Sutou	2016-12-06 16:17:58 +0900 (Tue, 06 Dec 2016)

  New Revision: 5b507a44ab1fadee741bff01ce890b7ad418d19f
  https://github.com/ranguba/groonga-client/commit/5b507a44ab1fadee741bff01ce890b7ad418d19f

  Message:
    load: support command_version=3 output

  Added files:
    test/response/test-load.rb
  Modified files:
    lib/groonga/client/response/load.rb

  Modified: lib/groonga/client/response/load.rb (+27 -3)
===================================================================
--- lib/groonga/client/response/load.rb    2016-10-11 17:59:47 +0900 (87a59ed)
+++ lib/groonga/client/response/load.rb    2016-12-06 16:17:58 +0900 (786e05c)
@@ -1,6 +1,5 @@
-# -*- coding: utf-8 -*-
-#
 # Copyright (C) 2013  Haruka Yoshihara <yoshihara �� clear-code.com>
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -23,8 +22,33 @@ module Groonga
     module Response
       class Load < Base
         Response.register("load", self)
+
+        # @return [Integer] The number of loaded records.
+        attr_accessor :n_loaded_records
+
+        # @return [::Array<Integer>] The IDs of loaded records. ID is
+        #   `0` if the corresponding record is failed to add.
+        #
+        #   If you don't specify `yes` to `output_ids` `load`
+        #   parameter, this is always an empty array.
+        attr_accessor :ids
+
+        def body=(body)
+          super(body)
+          parse_body(body)
+        end
+
+        private
+        def parse_body(body)
+          if body.is_a?(::Hash)
+            @n_loaded_records = body["n_loaded_records"]
+            @ids = body["ids"] || []
+          else
+            @n_loaded_records = body
+            @ids = []
+          end
+        end
       end
     end
   end
 end
-

  Added: test/response/test-load.rb (+78 -0) 100644
===================================================================
--- /dev/null
+++ test/response/test-load.rb    2016-12-06 16:17:58 +0900 (eefb9da)
@@ -0,0 +1,78 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "response/helper"
+
+class TestResponseLoad < Test::Unit::TestCase
+  private
+  def create_response(command, body)
+    header = {
+      "return_code"  => 0,
+      "start_time"   => 1372430096.70991,
+      "elapsed_time" => 0.000522851943969727,
+    }
+    Groonga::Client::Response::Load.new(command, header, body)
+  end
+
+  sub_test_case("#n_loaded_records") do
+    test("command_version=1") do
+      command = Groonga::Command::Load.new("load",
+                                           {"command_version" => "1"})
+      response = create_response(command, 29)
+      assert_equal(29, response.n_loaded_records)
+    end
+
+    test("command_version=3") do
+      command = Groonga::Command::Load.new("load",
+                                           {"command_version" => "3"})
+      response = create_response(command, {"n_loaded_records" => 29})
+      assert_equal(29, response.n_loaded_records)
+    end
+  end
+
+  sub_test_case("#ids") do
+    test("command_version=1") do
+      command = Groonga::Command::Load.new("load",
+                                           {"command_version" => "1"})
+      response = create_response(command, 29)
+      assert_equal([], response.ids)
+    end
+
+    sub_test_case("command_version=3") do
+      test("no output_ids") do
+        command = Groonga::Command::Load.new("load",
+                                             {"command_version" => "3"})
+        response = create_response(command, {"n_loaded_records" => 29})
+        assert_equal(29, response.n_loaded_records)
+      end
+
+      test("output_ids=yes") do
+        command = Groonga::Command::Load.new("load",
+                                             {
+                                               "command_version" => "3",
+                                               "output_ids" => "yes",
+                                             })
+        ids = [1, 2, 0, 4, 3]
+        response = create_response(command,
+                                   {
+                                     "n_loaded_records" => 4,
+                                     "ids" => ids,
+                                   })
+        assert_equal(ids, response.ids)
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
下载 



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