[Groonga-commit] ranguba/groonga-client at 3d6bfb2 [master] Add tests to groonga-client-index-recreate

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Wed Oct 25 17:59:24 JST 2017


Kouhei Sutou	2017-10-25 17:59:24 +0900 (Wed, 25 Oct 2017)

  New Revision: 3d6bfb237177de9d6825df52a70407c430f0609e
  https://github.com/ranguba/groonga-client/commit/3d6bfb237177de9d6825df52a70407c430f0609e

  Message:
    Add tests to groonga-client-index-recreate

  Modified files:
    lib/groonga/client/command-line/groonga-client-index-recreate.rb
    test/command-line/test-index-recreate.rb

  Modified: lib/groonga/client/command-line/groonga-client-index-recreate.rb (+52 -13)
===================================================================
--- lib/groonga/client/command-line/groonga-client-index-recreate.rb    2017-10-25 17:03:22 +0900 (e29a3df)
+++ lib/groonga/client/command-line/groonga-client-index-recreate.rb    2017-10-25 17:59:24 +0900 (6e437d0)
@@ -31,6 +31,8 @@ module Groonga
 
           @read_timeout = -1
 
+          @interval = :day
+
           @n_workers = 0
         end
 
@@ -43,7 +45,7 @@ module Groonga
                       :port     => @port,
                       :read_timeout => @read_timeout,
                       :backend  => :synchronous) do |client|
-            runner = Runner.new(client, target_indexes)
+            runner = Runner.new(client, @interval, target_indexes)
             runner.run do
               @n_workers.times do
                 client.database_unmap
@@ -59,7 +61,6 @@ module Groonga
           parser.banner += " LEXICON1.INDEX1 LEXICON2.INDEX2 ..."
 
           parser.separator("")
-
           parser.separator("Connection:")
 
           parser.on("--url=URL",
@@ -96,6 +97,20 @@ module Groonga
             @read_timeout = timeout
           end
 
+          parser.separator("")
+          parser.separator("Configuration:")
+
+          available_intervals = [:day]
+          parser.on("--interval=INTERVAL", available_intervals,
+                    "Index create interval.",
+                    "[#{available_intervals.join(", ")}]",
+                    "(#{@interval})") do |interval|
+            @interval = interval
+          end
+
+          parser.separator("")
+          parser.separator("groonga-httpd:")
+
           parser.on("--n-workers=N", Integer,
                     "The number of groonga-httpd workers.",
                     "This options is meaningless for groonga -s.",
@@ -120,9 +135,11 @@ module Groonga
         end
 
         class Runner
-          def initialize(client, target_indexes)
+          def initialize(client, interval, target_indexes)
             @client = client
+            @interval = interval
             @target_indexes = target_indexes
+            @now = Time.now
           end
 
           def run
@@ -139,11 +156,15 @@ module Groonga
           end
 
           private
+          def abort_run(message)
+            $stderr.puts(message)
+            throw(@abort_tag, false)
+          end
+
           def execute_command(name, arguments={})
             response =****@clien*****(name, arguments)
             unless response.success?
-              puts("failed to run #{name}: #{response.inspect}")
-              throw(@abort_tag, false)
+              abort_run("Failed to run #{name}: #{response.inspect}")
             end
             response
           end
@@ -191,10 +212,18 @@ module Groonga
                 column => real_name,
               },
             ]
-            # TODO: check return value
-            execute_command(:load,
-                            :table => table,
-                            :values => JSON.generate(values))
+            response = execute_command(:load,
+                                       :table => table,
+                                       :values => JSON.generate(values),
+                                       :command_version => "3",
+                                       :output_errors => "yes")
+            response.errors.each do |error|
+              unless error.return_code.zero?
+                abort_run("Failed to set alias: " +
+                          "<#{alias_name}> -> <#{real_name}>: " +
+                          "#{error.message}(#{error.return_code})")
+              end
+            end
           end
 
           def resolve_alias(alias_column, key)
@@ -233,7 +262,7 @@ module Groonga
           end
 
           def recreate_index(full_index_name, alias_column)
-            revision = Time.now.strftime("%Y%m%d")
+            revision = generate_revision
             table_name, index_name = full_index_name.split(".", 2)
             real_index_name = "#{index_name}_#{revision}"
             real_full_index_name = "#{table_name}.#{real_index_name}"
@@ -249,11 +278,12 @@ module Groonga
               current_table_name, current_index_name =
                 full_current_index_name.split(".", 2)
               if current_table_name != table_name
-                puts("!!!")
+                abort_run("Different lexicon isn't supported: " +
+                          "<#{full_index_name}> -> <#{full_current_index_name}>")
               end
               if current_index_name == real_index_name
-                puts("Same")
-                return nil
+                abort_run("Alias doesn't specify real index column: " +
+                          "<#{full_current_index_name}>")
               end
               column_create_similar(table_name,
                                     real_index_name,
@@ -279,6 +309,15 @@ module Groonga
               column_remove(table_name, index_name)
             end
           end
+
+          def generate_revision
+            case @interval
+            when :day
+              @now.strftime("%Y%m%d")
+            else
+              abort_run("Unsupported revision: #{@interval}")
+            end
+          end
         end
       end
     end

  Modified: test/command-line/test-index-recreate.rb (+322 -3)
===================================================================
--- test/command-line/test-index-recreate.rb    2017-10-25 17:03:22 +0900 (ac130f0)
+++ test/command-line/test-index-recreate.rb    2017-10-25 17:59:24 +0900 (c8503fd)
@@ -14,6 +14,8 @@
 # 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 "time"
+
 require "groonga/command/parser"
 
 require "groonga/client"
@@ -23,22 +25,77 @@ require "groonga/client/command-line/groonga-client-index-recreate"
 class TestCommandLineIndexRecreate < Test::Unit::TestCase
   include Groonga::Client::TestHelper
 
+  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 dump
+  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
-    command_line.run(["--url", groonga_url, *arguments])
+    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
+    end
   end
 
-  def test_no_alias
+  def test_no_alias_column
     index_recreate
     assert_equal(<<-DUMP.chomp, dump)
 config_set alias.column Aliases.real_name
@@ -47,4 +104,266 @@ table_create Aliases TABLE_HASH_KEY ShortText
 column_create Aliases real_name COLUMN_SCALAR ShortText
     DUMP
   end
+
+  def test_real_index
+    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
+    COMMANDS
+
+    assert_equal([true, "", ""],
+                 index_recreate("Terms.memos_content"))
+
+    assert_equal(<<-DUMP.chomp, dump)
+config_set alias.column Aliases.real_name
+
+table_create Aliases TABLE_HASH_KEY ShortText
+column_create Aliases real_name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos content COLUMN_SCALAR Text
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+
+load --table Aliases
+[
+["_key","real_name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+
+column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
+    DUMP
+  end
+
+  def test_old_index
+    restore(<<-COMMANDS)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+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_20171024 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171024"]
+]
+    COMMANDS
+
+    assert_equal([true, "", ""],
+                 index_recreate("Terms.memos_content"))
+
+    assert_equal(<<-DUMP.chomp, dump)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos content COLUMN_SCALAR Text
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+
+column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
+    DUMP
+  end
+
+  def test_old_indexes
+    restore(<<-COMMANDS)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+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_20171022 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171023 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171024 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171024"]
+]
+    COMMANDS
+
+    assert_equal([true, "", ""],
+                 index_recreate("Terms.memos_content"))
+
+    assert_equal(<<-DUMP.chomp, dump)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos content COLUMN_SCALAR Text
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+
+column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
+    DUMP
+  end
+
+  def test_already_latest
+    restore(<<-COMMANDS)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+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_20171022 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171023 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171024 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171025 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+    COMMANDS
+
+    assert_equal([true, "", ""],
+                 index_recreate("Terms.memos_content"))
+
+    assert_equal(<<-DUMP.chomp, dump)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos content COLUMN_SCALAR Text
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+
+column_create Terms memos_content_20171022 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171023 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
+    DUMP
+  end
+
+  def test_latest_alias_but_not_exist
+    restore(<<-COMMANDS)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+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_20171022 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171023 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+column_create Terms memos_content_20171024 \
+  COLUMN_INDEX|WITH_POSITION \
+  Memos content
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+    COMMANDS
+
+    assert_equal([
+                   false,
+                   "",
+                   "Alias doesn't specify real index column: " +
+                   "<Terms.memos_content_20171025>\n",
+                 ],
+                 index_recreate("Terms.memos_content"))
+
+    assert_equal(<<-DUMP.chomp, dump)
+config_set alias.column CustomAliases.name
+
+table_create CustomAliases TABLE_HASH_KEY ShortText
+column_create CustomAliases name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos content COLUMN_SCALAR Text
+
+table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
+
+load --table CustomAliases
+[
+["_key","name"],
+["Terms.memos_content","Terms.memos_content_20171025"]
+]
+
+column_create Terms memos_content_20171022 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171023 COLUMN_INDEX|WITH_POSITION Memos content
+column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
+    DUMP
+  end
 end
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171025/17643fb7/attachment-0001.htm 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index