From f6b0285c58425803b2432b7cd577247ba2fd0376 Mon Sep 17 00:00:00 2001 From: Kosuke Asami Date: Thu, 11 Jul 2013 16:32:11 +0900 Subject: [PATCH] table_create: return appropriate values --- lib/droonga/plugin/groonga/table_create.rb | 13 +++++++++++-- lib/droonga/plugin/handler_groonga.rb | 5 +++++ test/plugin/groonga/test_table_create.rb | 10 ++++++++-- test/plugin/test_handler_groonga.rb | 8 ++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/droonga/plugin/groonga/table_create.rb b/lib/droonga/plugin/groonga/table_create.rb index 9033bf1..6c66b1e 100644 --- a/lib/droonga/plugin/groonga/table_create.rb +++ b/lib/droonga/plugin/groonga/table_create.rb @@ -25,18 +25,27 @@ def initialize(context) @context = context end + def header(return_code, error_message = "") + elapsed_time = Time.now.to_f - @start_time + header = [return_code, @start_time, elapsed_time] + header.push(error_message) unless error_message.empty? + header + end + def execute(request) + @start_time = Time.now.to_f + command_class = Groonga::Command.find("table_create") command = command_class.new("table_create", request) name = command["name"] - return [false] unless name + return [header(Status::INVALID_ARGUMENT, "Should not create anonymous table"), false] unless name options = parse_command(command) Groonga::Schema.define(:context => @context) do |schema| schema.create_table(name, options) end - [true] + [header(Status::SUCCESS), true] end private diff --git a/lib/droonga/plugin/handler_groonga.rb b/lib/droonga/plugin/handler_groonga.rb index 0fb4294..fb9a160 100644 --- a/lib/droonga/plugin/handler_groonga.rb +++ b/lib/droonga/plugin/handler_groonga.rb @@ -29,6 +29,11 @@ def table_create(request) outputs = command.execute(request) post(outputs) end + + module Status + SUCCESS = 0 + INVALID_ARGUMENT = 65514 + end end end diff --git a/test/plugin/groonga/test_table_create.rb b/test/plugin/groonga/test_table_create.rb index 980ad94..5acd10d 100644 --- a/test/plugin/groonga/test_table_create.rb +++ b/test/plugin/groonga/test_table_create.rb @@ -16,12 +16,18 @@ class TableCreateTest < GroongaHandlerTest def test_success @handler.table_create({"name" => "Books"}) - assert_equal([true], @worker.body) + assert_equal( + [[Droonga::GroongaHandler::Status::SUCCESS, NORMALIZED_START_TIME, NORMALIZED_ELAPSED_TIME], true], + [normalize_header(@worker.body.first), @worker.body.last] + ) end def test_failure @handler.table_create({}) - assert_equal([false], @worker.body) + assert_equal( + [[Droonga::GroongaHandler::Status::INVALID_ARGUMENT, NORMALIZED_START_TIME, NORMALIZED_ELAPSED_TIME], false], + [normalize_header(@worker.body.first), @worker.body.last] + ) end def test_name diff --git a/test/plugin/test_handler_groonga.rb b/test/plugin/test_handler_groonga.rb index 70b46ee..6dc4f6c 100644 --- a/test/plugin/test_handler_groonga.rb +++ b/test/plugin/test_handler_groonga.rb @@ -65,4 +65,12 @@ def dump database_dumper = Groonga::DatabaseDumper.new(:database => @database) database_dumper.dump end + + NORMALIZED_START_TIME = Time.parse("2013-07-11T16:04:28+0900").to_i + NORMALIZED_ELAPSED_TIME = 1 + def normalize_header(header) + start_time = NORMALIZED_START_TIME + elapsed_time = NORMALIZED_ELAPSED_TIME + [header[0], start_time, elapsed_time] + end end