Skip to content

Commit

Permalink
table_create: return appropriate values
Browse files Browse the repository at this point in the history
  • Loading branch information
Kosuke Asami committed Jul 11, 2013
1 parent d2dd673 commit f6b0285
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/droonga/plugin/groonga/table_create.rb
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/droonga/plugin/handler_groonga.rb
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions test/plugin/groonga/test_table_create.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/plugin/test_handler_groonga.rb
Expand Up @@ -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

0 comments on commit f6b0285

Please sign in to comment.