Navigation Menu

Skip to content

Commit

Permalink
column_list response: add convenient readers
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 25, 2017
1 parent cafa443 commit 51a3fe0
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 110 deletions.
35 changes: 32 additions & 3 deletions lib/groonga/client/response/column-list.rb
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2013-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
Expand Down Expand Up @@ -66,6 +64,37 @@ class Column < Struct.new(:id,
:domain,
:range,
:source)
# @return [::Array<String>]
# The flag names of the column.
#
# @since 0.5.3
def flags
(super || "").split("|")
end

# @return [Boolean]
# `true` if the column is a scalar column, `false` otherwise.
#
# @since 0.5.3
def scalar?
flags.include?("COLUMN_SCALAR")
end

# @return [Boolean]
# `true` if the column is a vector column, `false` otherwise.
#
# @since 0.5.3
def vector?
flags.include?("COLUMN_VECTOR")
end

# @return [Boolean]
# `true` if the column is an index column, `false` otherwise.
#
# @since 0.5.3
def index?
flags.include?("COLUMN_INDEX")
end
end
end
end
Expand Down
157 changes: 114 additions & 43 deletions test/response/test-column-list.rb
Expand Up @@ -18,54 +18,125 @@
require "response/helper"

class TestResponseColumnList < Test::Unit::TestCase
include TestResponseHelper
class TestParse < self
include TestResponseHelper

def column(attributes)
c = Groonga::Client::Response::ColumnList::Column.new
attributes.each do |name, value|
c[name] = value
def column(attributes)
c = Groonga::Client::Response::ColumnList::Column.new
attributes.each do |name, value|
c[name] = value
end
c
end

def test_parse
header = [0, 1372430096.70991, 0.000522851943969727]
body = [
[
["id", "UInt32"],
["name", "ShortText"],
["path", "ShortText"],
["type", "ShortText"],
["flags", "ShortText"],
["domain", "ShortText"],
["range", "ShortText"],
["source", "ShortText"],
],
[
256,
"content",
"/tmp/test.db.0000100",
"var",
"COLUMN_SCALAR|PERSISTENT",
"TestTable",
"ShortText",
[],
],
]
raw_response = [header, body].to_json

response = parse_raw_response("column_list", raw_response)
assert_equal([
column(:id => 256,
:name => "content",
:path => "/tmp/test.db.0000100",
:type => "var",
:flags => "COLUMN_SCALAR|PERSISTENT",
:domain => "TestTable",
:range => "ShortText",
:source => []),
],
response.to_a)
end
c
end

def test_parse
header = [0, 1372430096.70991, 0.000522851943969727]
body = [
[
["id", "UInt32"],
["name", "ShortText"],
["path", "ShortText"],
["type", "ShortText"],
["flags", "ShortText"],
["domain", "ShortText"],
["range", "ShortText"],
["source", "ShortText"],
],
[
256,
"Text",
"/tmp/test.db.0000100",
"var",
"COLUMN_SCALAR|PERSISTENT",
"TestTable",
"ShortText",
[],
],
]
raw_response = [header, body].to_json
class TestBody < self
def setup
@command = Groonga::Command::Base.new("column_list", "table" => "Memos")
end

def create_response(columns)
header = [0, 1372430096.70991, 0.000522851943969727]
body = [
[
["id", "UInt32"],
["name", "ShortText"],
["path", "ShortText"],
["type", "ShortText"],
["flags", "ShortText"],
["domain", "ShortText"],
["range", "ShortText"],
["source", "ShortText"],
],
*columns,
]
Groonga::Client::Response::ColumnList.new(@command, header, body)
end

response = parse_raw_response("column_list", raw_response)
assert_equal([
column(:id => 256,
:name => "Text",
:path => "/tmp/test.db.0000100",
:type => "var",
:flags => "COLUMN_SCALAR|PERSISTENT",
:domain => "TestTable",
:range => "ShortText",
:source => []),
],
response.to_a)
class TestFlags < self
def create_response(flags)
columns = [
[
256,
"content",
"/tmp/test.db.0000100",
"var",
flags,
"Memos",
"ShortText",
[],
]
]
super(columns)
end

def test_multiple
response = create_response("COLUMN_SCALAR|PERSISTENT")
assert_equal(["COLUMN_SCALAR", "PERSISTENT"],
response[0].flags)
end

def test_scalar?
response = create_response("COLUMN_SCALAR|PERSISTENT")
assert do
response[0].scalar?
end
end

def test_vector?
response = create_response("COLUMN_VECTOR|PERSISTENT")
assert do
response[0].vector?
end
end

def test_index?
response = create_response("COLUMN_INDEX|WITH_POSITION|PERSISTENT")
assert do
response[0].index?
end
end
end
end
end

64 changes: 0 additions & 64 deletions test/results/test-column-list.rb

This file was deleted.

0 comments on commit 51a3fe0

Please sign in to comment.