[Groonga-commit] ranguba/groonga-client at 0547987 [master] test: extract common codes

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Oct 27 17:38:31 JST 2017


Kouhei Sutou	2017-10-27 17:38:31 +0900 (Fri, 27 Oct 2017)

  New Revision: 05479876bb6f0f688e3560958b87ad650b936729
  https://github.com/ranguba/groonga-client/commit/05479876bb6f0f688e3560958b87ad650b936729

  Message:
    test: extract common codes

  Copied files:
    test/command-line/helper.rb
      (from test/command-line/test-index-check.rb)
  Modified files:
    test/command-line/test-index-check.rb
    test/command-line/test-index-recreate.rb

  Copied: test/command-line/helper.rb (+10 -59) 57%
===================================================================
--- test/command-line/test-index-check.rb    2017-10-27 17:34:17 +0900 (2a581ea)
+++ test/command-line/helper.rb    2017-10-27 17:38:31 +0900 (030983f)
@@ -1,5 +1,4 @@
 # Copyright (C) 2017  Kouhei Sutou <kou �� clear-code.com>
-# Copyright (C) 2017  Kentaro Hayashi <hayashi �� 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
@@ -19,11 +18,8 @@ require "groonga/command/parser"
 
 require "groonga/client"
 require "groonga/client/test-helper"
-require "groonga/client/command-line/groonga-client-index-check"
-
-class TestCommandLineIndexCheck < Test::Unit::TestCase
-  include Groonga::Client::TestHelper
 
+module CommandLineTestHelper
   def groonga_url
     @groonga_server_runner.url.to_s
   end
@@ -68,14 +64,19 @@ class TestCommandLineIndexCheck < Test::Unit::TestCase
     end
   end
 
-  def run_client_index_check(*arguments)
-    command_line = Groonga::Client::CommandLine::GroongaClientIndexCheck.new
+  def dump
+    open_client do |client|
+      client.dump.body
+    end
+  end
+
+  def capture_outputs
     begin
       stdout, $stdout = $stdout, StringIO.new
       stderr, $stderr = $stderr, StringIO.new
+      result = yield
       [
-        command_line.run(["--url", groonga_url,
-                          *arguments]),
+        result,
         $stdout.string,
         $stderr.string,
       ]
@@ -83,54 +84,4 @@ class TestCommandLineIndexCheck < Test::Unit::TestCase
       $stdout, $stderr = stdout, stderr
     end
   end
-
-  def test_source
-    restore(<<-COMMANDS)
-table_create Memos TABLE_HASH_KEY ShortText
-column_create Memos content COLUMN_SCALAR Text
-
-table_create Terms TABLE_PAT_KEY ShortText \
-  --normalizer NormalizerAuto \
-  --default_tokenizer TokenBigram
-column_create Terms memos_content \
-  COLUMN_INDEX|WITH_POSITION \
-  Memos
-    COMMANDS
-
-    expected = <<CLIENT_OUTPUT
-index column:<Terms.memos_content> is missing source.
-CLIENT_OUTPUT
-
-    assert_equal([false, expected, ""],
-                 run_client_index_check("--method=source",
-                                        "Terms.memos_content"))
-  end
-
-  def test_content
-    restore(<<-COMMANDS)
-table_create Memos TABLE_HASH_KEY ShortText
-column_create Memos content COLUMN_SCALAR Text
-
-table_create Terms TABLE_PAT_KEY ShortText \
-  --normalizer NormalizerAuto \
-  --default_tokenizer TokenBigram
-column_create Terms memos_content \
-  COLUMN_INDEX|WITH_POSITION \
-  Memos content
-
-load --table Memos
-[
-["_key","content"],
-["groonga","Groonga is fast"]
-]
-    COMMANDS
-
-    expected = <<CLIENT_OUTPUT
-check 3 tokens against <Terms.memos_content>.
-CLIENT_OUTPUT
-
-    assert_equal([true, expected, ""],
-                 run_client_index_check("--method=content",
-                                        "Terms.memos_content"))
-  end
 end

  Modified: test/command-line/test-index-check.rb (+4 -58)
===================================================================
--- test/command-line/test-index-check.rb    2017-10-27 17:34:17 +0900 (2a581ea)
+++ test/command-line/test-index-check.rb    2017-10-27 17:38:31 +0900 (2d9cb35)
@@ -15,72 +15,18 @@
 # 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 "groonga/command/parser"
+require_relative "helper"
 
-require "groonga/client"
-require "groonga/client/test-helper"
 require "groonga/client/command-line/groonga-client-index-check"
 
 class TestCommandLineIndexCheck < Test::Unit::TestCase
   include Groonga::Client::TestHelper
-
-  def groonga_url
-    @groonga_server_runner.url.to_s
-  end
-
-  def open_client
-    Groonga::Client.open(:url => groonga_url) do |client|
-      yield(client)
-    end
-  end
-
-  def restore(commands)
-    open_client do |client|
-      values = nil
-      Groonga::Command::Parser.parse(commands) do |event, *args|
-        case event
-        when :on_command
-          command, = args
-          response = client.execute(command)
-          unless response.success?
-            raise Groonga::Client::Request::ErrorResponse.new(response)
-          end
-        when :on_load_start
-          command, = args
-          values = []
-        when :on_load_columns
-          command, columns = args
-          command[:columns] ||= columns.join(",")
-        when :on_load_value
-          command, value = args
-          values << value
-        when :on_load_complete
-          command, = args
-          command[:values] ||= JSON.generate(values)
-          response = client.execute(command)
-          unless response.success?
-            raise Groonga::Client::Request::ErrorResponse.new(response)
-          end
-        else
-          p [:unhandled_event, event, *args]
-        end
-      end
-    end
-  end
+  include CommandLineTestHelper
 
   def run_client_index_check(*arguments)
     command_line = Groonga::Client::CommandLine::GroongaClientIndexCheck.new
-    begin
-      stdout, $stdout = $stdout, StringIO.new
-      stderr, $stderr = $stderr, StringIO.new
-      [
-        command_line.run(["--url", groonga_url,
-                          *arguments]),
-        $stdout.string,
-        $stderr.string,
-      ]
-    ensure
-      $stdout, $stderr = stdout, stderr
+    capture_outputs do
+      command_line.run(["--url", groonga_url, *arguments])
     end
   end
 

  Modified: test/command-line/test-index-recreate.rb (+4 -63)
===================================================================
--- test/command-line/test-index-recreate.rb    2017-10-27 17:34:17 +0900 (c8503fd)
+++ test/command-line/test-index-recreate.rb    2017-10-27 17:38:31 +0900 (12f5fea)
@@ -16,82 +16,23 @@
 
 require "time"
 
-require "groonga/command/parser"
+require_relative "helper"
 
-require "groonga/client"
-require "groonga/client/test-helper"
 require "groonga/client/command-line/groonga-client-index-recreate"
 
 class TestCommandLineIndexRecreate < Test::Unit::TestCase
   include Groonga::Client::TestHelper
+  include CommandLineTestHelper
 
   def setup
     @now = Time.parse("2017-10-25T17:22:00+0900")
     stub(Time).now {@now}
   end
 
-  def groonga_url
-    @groonga_server_runner.url.to_s
-  end
-
-  def open_client
-    Groonga::Client.open(:url => groonga_url) do |client|
-      yield(client)
-    end
-  end
-
-  def restore(commands)
-    open_client do |client|
-      values = nil
-      Groonga::Command::Parser.parse(commands) do |event, *args|
-        case event
-        when :on_command
-          command, = args
-          response = client.execute(command)
-          unless response.success?
-            raise Groonga::Client::Request::ErrorResponse.new(response)
-          end
-        when :on_load_start
-          command, = args
-          values = []
-        when :on_load_columns
-          command, columns = args
-          command[:columns] ||= columns.join(",")
-        when :on_load_value
-          command, value = args
-          values << value
-        when :on_load_complete
-          command, = args
-          command[:values] ||= JSON.generate(values)
-          response = client.execute(command)
-          unless response.success?
-            raise Groonga::Client::Request::ErrorResponse.new(response)
-          end
-        else
-          p [:unhandled_event, event, *args]
-        end
-      end
-    end
-  end
-
-  def dump
-    open_client do |client|
-      client.dump.body
-    end
-  end
-
   def index_recreate(*arguments)
     command_line = Groonga::Client::CommandLine::GroongaClientIndexRecreate.new
-    begin
-      stdout, $stdout = $stdout, StringIO.new
-      stderr, $stderr = $stderr, StringIO.new
-      [
-        command_line.run(["--url", groonga_url, *arguments]),
-        $stdout.string,
-        $stderr.string,
-      ]
-    ensure
-      $stdout, $stderr = stdout, stderr
+    capture_outputs do
+      command_line.run(["--url", groonga_url, *arguments])
     end
   end
 
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171027/1aa3501a/attachment-0001.htm 



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