Kouhei Sutou
null+****@clear*****
Tue Dec 13 01:06:29 JST 2016
Kouhei Sutou 2016-12-13 01:06:29 +0900 (Tue, 13 Dec 2016) New Revision: 42e3fa7a72e3d42bb1041b56456ff979a89cf276 https://github.com/ranguba/groonga-client/commit/42e3fa7a72e3d42bb1041b56456ff979a89cf276 Message: select: support labeled dilldown Added files: test/request/test-select.rb Modified files: lib/groonga/client/request/base.rb lib/groonga/client/request/select.rb test/request/select/test-output-columns-parameter.rb test/request/select/test-sort-keys-columns-parameter.rb Renamed files: test/request/select/test-values-parameter.rb (from test/request/select/test-match-columns-parameter.rb) Modified: lib/groonga/client/request/base.rb (+70 -1) =================================================================== --- lib/groonga/client/request/base.rb 2016-12-13 00:09:49 +0900 (56bdff7) +++ lib/groonga/client/request/base.rb 2016-12-13 01:06:29 +0900 (6df029e) @@ -82,12 +82,81 @@ module Groonga end def to_parameters + case @value + when Symbol + value =****@value*****_s + when String + return {} if****@value*****? + value = @value + when NilClass + return {} + else + value = @value + end { - @name => @value, + @name => value, } end end + class ValuesParameter + def initialize(names, values) + @names = names + @values = values + end + + def to_parameters + case @values + when ::Array + return {} if****@value*****? + values =****@value*****(&:to_s).join(", ") + when Symbol + values =****@value*****_s + when String + return {} if /\A\s*\z/ === @values + values = @values + when NilClass + return {} + else + values = @values + end + parameters = {} + @names.each do |name| + parameters[name] = values + end + parameters + end + end + + class FlagsParameter + def initialize(names, flags) + @names = names + @flags = flags + end + + def to_parameters + case @flags + when ::Array + return {} if****@flags*****? + flags =****@flags*****(&:to_s).join("|") + when Symbol + flags =****@flags*****_s + when String + return {} if /\A\s*\z/ === @flags + flags = @flags + when NilClass + return {} + else + flags = @flags + end + parameters = {} + @names.each do |name| + parameters[name] = flags + end + parameters + end + end + class ParameterMerger def initialize(parameters1, parameters2) @parameters1 = parameters1 Modified: lib/groonga/client/request/select.rb (+74 -77) =================================================================== --- lib/groonga/client/request/select.rb 2016-12-13 00:09:49 +0900 (f950511) +++ lib/groonga/client/request/select.rb 2016-12-13 01:06:29 +0900 (e308e88) @@ -38,7 +38,7 @@ module Groonga def match_columns(value) add_parameter(OverwriteMerger, - MatchColumnsParameter.new(value)) + ValuesParameter.new([:match_columns], value)) end def query(value) @@ -53,12 +53,12 @@ module Groonga def output_columns(value) add_parameter(OverwriteMerger, - OutputColumnsParameter.new(value)) + OutputColumnsParameter.new("", value)) end def sort_keys(value) add_parameter(OverwriteMerger, - SortKeysParameter.new(value)) + SortKeysParameter.new("", value)) end alias_method :sortby, :sort_keys alias_method :sort, :sort_keys @@ -82,6 +82,10 @@ module Groonga offset(offset).limit(per_page) end + def drilldowns(label) + LabeledDrilldown.new(self, label) + end + def each(&block) response.records.each(&block) end @@ -101,6 +105,56 @@ module Groonga parameters.key?(:offset) and parameters.key?(:limit) end + class LabeledDrilldown + def initialize(request, label) + @request = request + @label = label + end + + def keys(value) + add_parameter(OverwriteMerger, + ValuesParameter.new([:"#{prefix}keys"], value)) + end + + def sort_keys(value) + add_parameter(OverwriteMerger, + SortKeysParameter.new(prefix, value)) + end + alias_method :sortby, :sort_keys + alias_method :sort, :sort_keys + + def output_columns(value) + add_parameter(OverwriteMerger, + OutputColumnsParameter.new(prefix, value)) + end + + def offset(value) + @request.parameter(:"#{prefix}offset", value) + end + + def limit(value) + @request.parameter(:"#{prefix}limit", value) + end + + def calc_types(value) + add_parameter(OverwriteMerger, + FlagsParameter.new([:"#{prefix}calc_types"], value)) + end + + def calc_target(value) + @request.parameter(:"#{prefix}calc_target", value) + end + + private + def prefix + "drilldowns[#{@label}]." + end + + def add_parameter(merger, parameter) + @request.__send__(:add_parameter, merger, parameter) + end + end + # @private class QueryMerger < ParameterMerger def to_parameters @@ -136,33 +190,6 @@ module Groonga end # @private - class MatchColumnsParameter - def initialize(match_columns) - @match_columns = match_columns - end - - def to_parameters - case @match_columns - when ::Array - return {} if @match_columns.empty? - match_columns = @match_columns.join(", ") - when Symbol - match_columns = @match_columns.to_s - when String - return {} if /\A\s*\z/ === @match_columns - match_columns = @match_columns - when NilClass - return {} - else - match_columns = @match_columns - end - { - match_columns: match_columns, - } - end - end - - # @private class FilterParameter def initialize(expression, values) @expression = expression @@ -231,62 +258,32 @@ module Groonga end # @private - class OutputColumnsParameter - def initialize(output_columns) - @output_columns = output_columns + class OutputColumnsParameter < ValuesParameter + def initialize(prefix, output_columns) + super([:"#{prefix}output_columns"], output_columns) end def to_parameters - case @output_columns - when ::Array - return {} if @output_columns.empty? - output_columns = @output_columns.join(", ") - when Symbol - output_columns = @output_columns.to_s - when String - return {} if /\A\s*\z/ === @output_columns - output_columns = @output_columns - when NilClass - return {} - else - output_columns = @output_columns - end - - parameters = { - output_columns: output_columns, - } - if output_columns.include?("(") - parameters[:command_version] = "2" + parameters = super + @names.each do |name| + output_columns = parameters[name] + if output_columns and output_columns.include?("(") + parameters[:command_version] = "2" + break + end end parameters end end # @private - class SortKeysParameter - def initialize(keys) - @keys = keys - end - - def to_parameters - case @keys - when ::Array - return {} if****@keys*****? - keys =****@keys*****(&:to_s).join(", ") - when Symbol - keys =****@keys*****_s - when String - return {} if /\A\s*\z/ === @keys - keys = @keys - when NilClass - return {} - else - keys = @keys - end - { - sort_keys: keys, - sortby: keys, # For backward compatibility - } + class SortKeysParameter < ValuesParameter + def initialize(prefix, output_columns) + names = [ + :"#{prefix}sort_keys", + :"#{prefix}sortby", # for backward compatibility + ] + super(names, output_columns) end end end Modified: test/request/select/test-output-columns-parameter.rb (+18 -9) =================================================================== --- test/request/select/test-output-columns-parameter.rb 2016-12-13 00:09:49 +0900 (cdf5db8) +++ test/request/select/test-output-columns-parameter.rb 2016-12-13 01:06:29 +0900 (16ff730) @@ -15,52 +15,61 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class TestRequestSelectOutputColumnsParmater < Test::Unit::TestCase - def output_columns_parameter(output_columns) - Groonga::Client::Request::Select::OutputColumnsParameter.new(output_columns) + def output_columns_parameter(prefix, output_columns) + Groonga::Client::Request::Select::OutputColumnsParameter.new(prefix, + output_columns) end def test_nil assert_equal({}, - output_columns_parameter(nil).to_parameters) + output_columns_parameter("", nil).to_parameters) end def test_string assert_equal({ :output_columns => "title", }, - output_columns_parameter("title").to_parameters) + output_columns_parameter("", "title").to_parameters) end def test_empty_string assert_equal({}, - output_columns_parameter("").to_parameters) + output_columns_parameter("", "").to_parameters) end def test_symbol assert_equal({ :output_columns => "title", }, - output_columns_parameter(:title).to_parameters) + output_columns_parameter("", :title).to_parameters) end def test_array assert_equal({ :output_columns => "title, body", }, - output_columns_parameter(["title", "body"]).to_parameters) + output_columns_parameter("", ["title", "body"]).to_parameters) end def test_empty_array assert_equal({}, - output_columns_parameter([]).to_parameters) + output_columns_parameter("", []).to_parameters) end def test_function - parameter = output_columns_parameter(["title", "snippet_html(body)"]) + parameter = output_columns_parameter("", ["title", "snippet_html(body)"]) assert_equal({ :output_columns => "title, snippet_html(body)", :command_version => "2", }, parameter.to_parameters) end + + def test_prefix + parameter = output_columns_parameter("drilldowns[title].", "title") + assert_equal({ + :"drilldowns[title].output_columns" => "title", + }, + parameter.to_parameters) + end end Modified: test/request/select/test-sort-keys-columns-parameter.rb (+13 -3) =================================================================== --- test/request/select/test-sort-keys-columns-parameter.rb 2016-12-13 00:09:49 +0900 (5810e11) +++ test/request/select/test-sort-keys-columns-parameter.rb 2016-12-13 01:06:29 +0900 (2771a1e) @@ -15,12 +15,12 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class TestRequestSelectSortKeysParmater < Test::Unit::TestCase - def sort_keys_parameter(sort_keys) - Groonga::Client::Request::Select::SortKeysParameter.new(sort_keys) + def sort_keys_parameter(prefix, sort_keys) + Groonga::Client::Request::Select::SortKeysParameter.new(prefix, sort_keys) end def to_parameters(sort_keys) - sort_keys_parameter(sort_keys).to_parameters + sort_keys_parameter("", sort_keys).to_parameters end def test_nil @@ -61,4 +61,14 @@ class TestRequestSelectSortKeysParmater < Test::Unit::TestCase assert_equal({}, to_parameters([])) end + + def test_prefix + parameter = sort_keys_parameter("slices[tag].", "-_score, _id") + + assert_equal({ + :"slices[tag].sort_keys" => "-_score, _id", + :"slices[tag].sortby" => "-_score, _id", + }, + parameter.to_parameters) + end end Renamed: test/request/select/test-values-parameter.rb (+10 -9) 68% =================================================================== --- test/request/select/test-match-columns-parameter.rb 2016-12-13 00:09:49 +0900 (6aa7c3e) +++ test/request/select/test-values-parameter.rb 2016-12-13 01:06:29 +0900 (e5928d0) @@ -14,44 +14,45 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -class TestRequestSelectMatchColumnsParmater < Test::Unit::TestCase - def match_columns_parameter(match_columns) - Groonga::Client::Request::Select::MatchColumnsParameter.new(match_columns) +class TestRequestSelectValuesParmater < Test::Unit::TestCase + def values_parameter(values) + names = [:match_columns] + Groonga::Client::Request::ValuesParameter.new(names, values) end def test_nil assert_equal({}, - match_columns_parameter(nil).to_parameters) + values_parameter(nil).to_parameters) end def test_string assert_equal({ :match_columns => "title", }, - match_columns_parameter("title").to_parameters) + values_parameter("title").to_parameters) end def test_empty_string assert_equal({}, - match_columns_parameter("").to_parameters) + values_parameter("").to_parameters) end def test_symbol assert_equal({ :match_columns => "title", }, - match_columns_parameter(:title).to_parameters) + values_parameter(:title).to_parameters) end def test_array assert_equal({ :match_columns => "title, body", }, - match_columns_parameter(["title", "body"]).to_parameters) + values_parameter(["title", "body"]).to_parameters) end def test_empty_array assert_equal({}, - match_columns_parameter([]).to_parameters) + values_parameter([]).to_parameters) end end Added: test/request/test-select.rb (+131 -0) 100644 =================================================================== --- /dev/null +++ test/request/test-select.rb 2016-12-13 01:06:29 +0900 (68e56a4) @@ -0,0 +1,131 @@ +# Copyright (C) 2016 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 + +class TestRequestSelect < Test::Unit::TestCase + setup do + @request = Groonga::Client::Request::Select.new("posts") + end + + sub_test_case("#drilldowns") do + def drilldown + @request.drilldowns("label") + end + + sub_test_case("#keys") do + test "String" do + assert_equal({ + :table => "posts", + :"drilldowns[label].keys" => "tag", + }, + drilldown.keys("tag").to_parameters) + end + + test "Array" do + assert_equal({ + :table => "posts", + :"drilldowns[label].keys" => "start, end", + }, + drilldown.keys(["start", "end"]).to_parameters) + end + end + + sub_test_case("#sort_keys") do + test "String" do + assert_equal({ + :table => "posts", + :"drilldowns[label].sort_keys" => "-_nsubrecs", + :"drilldowns[label].sortby" => "-_nsubrecs", + }, + drilldown.sort_keys("-_nsubrecs").to_parameters) + end + + test "Array" do + assert_equal({ + :table => "posts", + :"drilldowns[label].sort_keys" => "-_nsubrecs, name", + :"drilldowns[label].sortby" => "-_nsubrecs, name", + }, + drilldown.sort_keys(["-_nsubrecs", "name"]).to_parameters) + end + end + + sub_test_case("#output_columns") do + test "String" do + assert_equal({ + :table => "posts", + :"drilldowns[label].output_columns" => "_key, -_nsubrecs", + }, + drilldown.output_columns("_key, -_nsubrecs").to_parameters) + end + + test "Array" do + assert_equal({ + :table => "posts", + :"drilldowns[label].output_columns" => "_key, -_nsubrecs", + }, + drilldown.output_columns(["_key, -_nsubrecs"]).to_parameters) + end + end + + sub_test_case("#offset") do + test "Integer" do + assert_equal({ + :table => "posts", + :"drilldowns[label].offset" => 29, + }, + drilldown.offset(29).to_parameters) + end + end + + sub_test_case("#limit") do + test "Integer" do + assert_equal({ + :table => "posts", + :"drilldowns[label].limit" => 29, + }, + drilldown.limit(29).to_parameters) + end + end + + sub_test_case("#calc_types") do + test "String" do + assert_equal({ + :table => "posts", + :"drilldowns[label].calc_types" => "COUNT|AVG", + }, + drilldown.calc_types("COUNT|AVG").to_parameters) + end + + test "Array" do + assert_equal({ + :table => "posts", + :"drilldowns[label].calc_types" => "COUNT|AVG", + }, + drilldown.calc_types(["COUNT", "AVG"]).to_parameters) + end + end + + sub_test_case("#calc_target") do + test "Symbol" do + assert_equal({ + :table => "posts", + :"drilldowns[label].calc_target" => "rank", + }, + drilldown.calc_target(:rank).to_parameters) + end + end + end +end -------------- next part -------------- HTML����������������������������... ダウンロード