Haruka Yoshihara
null+****@clear*****
Mon Dec 17 15:12:59 JST 2012
Haruka Yoshihara 2012-12-17 15:12:59 +0900 (Mon, 17 Dec 2012) New Revision: 1a78b9fe627cbfcb483b83892d4c00bd10e83622 https://github.com/groonga/groonga-query-log/commit/1a78b9fe627cbfcb483b83892d4c00bd10e83622 Log: extractor: implement #run from bin/groonga-query-log-extract Added files: test/fixtures/other-query.log Modified files: lib/groonga/query-log/extractor.rb test/test-extractor.rb Modified: lib/groonga/query-log/extractor.rb (+25 -0) =================================================================== --- lib/groonga/query-log/extractor.rb 2012-12-17 14:28:12 +0900 (7e9d8c4) +++ lib/groonga/query-log/extractor.rb 2012-12-17 15:12:59 +0900 (b5e5c88) @@ -41,6 +41,31 @@ module Groonga setup_options end + def run(*arguments) + begin + log_paths = @option_parser.parse!(arguments) + rescue OptionParser::ParseError + raise(ArgumentError, $!.message) + end + + if log_paths.empty? + raise(NoInputError, "Error: Please specify input log files.") + end + + log = "" + log_paths.each do |log_path| + log << File.read(log_path) + end + + if****@optio*****_path + File.open(@options.output_path, "w") do |output| + extract(log, output) + end + else + extract(log, $stdout) + end + end + private def setup_options @options = OpenStruct.new Added: test/fixtures/other-query.log (+5 -0) 100644 =================================================================== --- /dev/null +++ test/fixtures/other-query.log 2012-12-17 15:12:59 +0900 (45a3edf) @@ -0,0 +1,5 @@ +2012-12-17 15:00:10.165344|0x7fff8f4c8ca0|>table_create --name Comments --flags TABLE_HASH_KEY --key_type UInt32 +2012-12-17 15:00:10.217643|0x7fff8f4c8ca0|<000000052316820 rc=0 +2012-12-17 15:00:10.217818|0x7fff8f4c8ca0|>column_create --table Comments --name title --flags COLUMN_SCALAR --type ShortText +2012-12-17 15:00:10.262427|0x7fff8f4c8ca0|<000000044635779 rc=0 +2012-12-17 15:00:10.476747|0x60c8e0| query log will be closed: </tmp/query.log> Modified: test/test-extractor.rb (+82 -0) =================================================================== --- test/test-extractor.rb 2012-12-17 14:28:12 +0900 (85e042e) +++ test/test-extractor.rb 2012-12-17 15:12:59 +0900 (f36e22e) @@ -21,10 +21,92 @@ require "groonga/command" require "groonga/query-log/extractor" class TestExtractor < Test::Unit::TestCase + setup + def setup_fixtures + @fixtures_path = File.join(File.dirname(__FILE__), "fixtures") + @query_log_path = File.join(@fixtures_path, "query.log") + end + def setup @extractor = Groonga::QueryLog::Extractor.new end + class TestInputFile < self + def test_multi + other_query_log_path = File.join(@fixtures_path, "other-query.log") + actual_commands = run_extractor(@query_log_path, other_query_log_path) + expected_commands = <<-EOC +load --table Video +select --table Users --query follower:@groonga --output_columns _key,name +table_create --name Comments --flags TABLE_HASH_KEY --key_type UInt32 +column_create --table Comments --name title --flags COLUMN_SCALAR --type ShortText +EOC + assert_equal(expected_commands, actual_commands) + end + + def test_no_specified + assert_raise(Groonga::QueryLog::Extractor::NoInputError) do + run_extractor + end + end + end + + class TestUnifyFormat < self + def test_commands + actual_commands = run_extractor(@query_log_path, + "--unify-format", "command") + + expected_commands = <<-EOC +load --table "Video" +select --output_columns "_key,name" --query "follower:@groonga" --table "Users" +EOC + assert_equal(expected_commands, actual_commands) + end + + def test_uri + actual_commands = run_extractor(@query_log_path, + "--unify-format", "uri") + expected_commands = <<-EOC +/d/load?table=Video +/d/select?output_columns=_key%2Cname&query=follower%3A%40groonga&table=Users +EOC + assert_equal(expected_commands, actual_commands) + end + + def test_not_unify + actual_commands = run_extractor(@query_log_path) + expected_commands = <<-EOC +load --table Video +select --table Users --query follower:@groonga --output_columns _key,name +EOC + assert_equal(expected_commands, actual_commands) + end + end + + def test_command + actual_command = run_extractor(@query_log_path, "--command", "load") + expected_command = "load --table Video\n" + + assert_equal(expected_command, actual_command) + end + + def test_exclude_command + actual_command = run_extractor(@query_log_path, "--exclude-command", "load") + expected_command = "select --table Users --query follower:@groonga" + + " --output_columns _key,name\n" + + assert_equal(expected_command, actual_command) + end + + private + def run_extractor(*arguments) + Tempfile.open("extract.actual") do |output| + arguments << "--output" << output.path + @extractor.run(*arguments) + File.read(output.path) + end + end + class TestExtract < self def setup super -------------- next part -------------- HTML����������������������������...Download