Navigation Menu

Skip to content

Commit

Permalink
test: extract common codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 27, 2017
1 parent d2f00ee commit 0547987
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 121 deletions.
87 changes: 87 additions & 0 deletions test/command-line/helper.rb
@@ -0,0 +1,87 @@
# Copyright (C) 2017 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 "groonga/command/parser"

require "groonga/client"
require "groonga/client/test-helper"

module CommandLineTestHelper
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 capture_outputs
begin
stdout, $stdout = $stdout, StringIO.new
stderr, $stderr = $stderr, StringIO.new
result = yield
[
result,
$stdout.string,
$stderr.string,
]
ensure
$stdout, $stderr = stdout, stderr
end
end
end
62 changes: 4 additions & 58 deletions test/command-line/test-index-check.rb
Expand Up @@ -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

Expand Down
67 changes: 4 additions & 63 deletions test/command-line/test-index-recreate.rb
Expand Up @@ -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

Expand Down

0 comments on commit 0547987

Please sign in to comment.