[Groonga-commit] ranguba/groonga-client at 11c765d [master] Unify FilterEqualParameter into FilterExpressionParameter

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Apr 27 10:19:26 JST 2017


Kouhei Sutou	2017-04-27 10:19:26 +0900 (Thu, 27 Apr 2017)

  New Revision: 11c765d552cbf34127ce26da1068e6eb95e5f927
  https://github.com/ranguba/groonga-client/commit/11c765d552cbf34127ce26da1068e6eb95e5f927

  Message:
    Unify FilterEqualParameter into FilterExpressionParameter

  Added files:
    test/request/select/test-filter.rb
  Removed files:
    test/request/select/test-filter-equal-parameter.rb
    test/request/select/test-filter-expression-parameter.rb
  Modified files:
    lib/groonga/client/request/select.rb
    test/request/test-select.rb

  Modified: lib/groonga/client/request/select.rb (+25 -29)
===================================================================
--- lib/groonga/client/request/select.rb    2017-04-27 09:46:17 +0900 (69188ff)
+++ lib/groonga/client/request/select.rb    2017-04-27 10:19:26 +0900 (6a2895e)
@@ -107,22 +107,33 @@ module Groonga
         #     The new request object for setting a filter condition.
         #
         #   @since 0.4.3
-        def filter(expression_or_column_name=nil, values_or_value=nil)
-          if expression_or_column_name.nil? and values_or_value.nil?
-            return Filter.new(self)
-          end
-
-          if expression_or_column_name.is_a?(Symbol)
-            parameter = FilterEqualParameter.new(expression_or_column_name,
-                                                 values_or_value)
-          elsif values_or_value.nil? or values_or_value.is_a?(::Hash)
-            parameter = FilterExpressionParameter.new(expression_or_column_name,
-                                                      values_or_value)
+        def filter(*args)
+          n_args = args.size
+          case n_args
+          when 0
+            Filter.new(self)
+          when 1, 2
+            expression_or_column_name, values_or_value = *args
+
+            if values_or_value.nil? or values_or_value.is_a?(::Hash)
+              expression = expression_or_column_name
+              values = values_or_value
+            else
+              expression = "%{column} == %{value}"
+              column_name = expression_or_column_name
+              column_name = column_name.to_sym if column_name.is_a?(String)
+              values = {
+                column: column_name,
+                value: values_or_value,
+              }
+            end
+            parameter = FilterExpressionParameter.new(expression, values)
+            add_parameter(FilterMerger, parameter)
           else
-            parameter = FilterEqualParameter.new(expression_or_column_name,
-                                                 values_or_value)
+            message =
+              "wrong number of arguments (given #{n_args}, expected 0..2)"
+            raise ArgumentError, message
           end
-          add_parameter(FilterMerger, parameter)
         end
 
         def output_columns(value)
@@ -631,21 +642,6 @@ module Groonga
           end
         end
 
-        class FilterEqualParameter
-          include ScriptSyntaxValueEscapable
-
-          def initialize(column_name, value)
-            @column_name = column_name
-            @value = value
-          end
-
-          def to_parameters
-            {
-              filter: "#{@column_name} == #{escape_script_syntax_value(@value)}",
-            }
-          end
-        end
-
         # @private
         class OutputColumnsParameter < ValuesParameter
           def initialize(prefix, output_columns)

  Deleted: test/request/select/test-filter-equal-parameter.rb (+0 -91) 100644
===================================================================
--- test/request/select/test-filter-equal-parameter.rb    2017-04-27 09:46:17 +0900 (ca397df)
+++ /dev/null
@@ -1,91 +0,0 @@
-# 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 TestRequestSelectFilterEqualParmater < Test::Unit::TestCase
-  def filter_parameter(column_name, value)
-    Groonga::Client::Request::Select::FilterEqualParameter.new(column_name,
-                                                               value)
-  end
-
-  def to_parameters(column_name, value)
-    filter_parameter(column_name, value).to_parameters
-  end
-
-  sub_test_case("column name") do
-    def test_string
-      assert_equal({
-                     :filter => "_key == 29",
-                   },
-                   to_parameters("_key", 29))
-    end
-
-    def test_symbol
-      assert_equal({
-                     :filter => "_key == 29",
-                   },
-                   to_parameters(:_key, 29))
-    end
-  end
-
-  sub_test_case("value") do
-    def test_string
-      filter = <<-'FILTER'.strip
-title == "[\"He\\ llo\"]"
-      FILTER
-      assert_equal({
-                     :filter => filter,
-                   },
-                   to_parameters("title", "[\"He\\ llo\"]"))
-    end
-
-    sub_test_case("Symbol") do
-      def test_id
-        assert_equal({
-                       :filter => "title == normalized_title",
-                     },
-                     to_parameters("title", :normalized_title))
-      end
-
-      def test_not_id
-        assert_equal({
-                       :filter => "title == \"Hello World\"",
-                     },
-                     to_parameters("title", :"Hello World"))
-      end
-    end
-
-    def test_number
-      assert_equal({
-                     :filter => "age == 29",
-                   },
-                   to_parameters("age", 29))
-    end
-
-    def test_true
-      assert_equal({
-                     :filter => "published == true",
-                   },
-                   to_parameters("published", true))
-    end
-
-    def test_false
-      assert_equal({
-                     :filter => "published == false",
-                   },
-                   to_parameters("published", false))
-    end
-  end
-end

  Deleted: test/request/select/test-filter-expression-parameter.rb (+0 -109) 100644
===================================================================
--- test/request/select/test-filter-expression-parameter.rb    2017-04-27 09:46:17 +0900 (4bf146e)
+++ /dev/null
@@ -1,109 +0,0 @@
-# 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 TestRequestSelectFilterExpressionParmater < Test::Unit::TestCase
-  def filter_parameter(expression, values=nil)
-    Groonga::Client::Request::Select::FilterExpressionParameter.new(expression,
-                                                                    values)
-  end
-
-  def to_parameters(expression, values=nil)
-    filter_parameter(expression, values).to_parameters
-  end
-
-  sub_test_case("expression") do
-    def test_nil
-      assert_equal({},
-                   to_parameters(nil))
-    end
-
-    def test_string
-      assert_equal({
-                     :filter => "age <= 20",
-                   },
-                   to_parameters("age <= 20"))
-    end
-
-    def test_empty_string
-      assert_equal({},
-                   to_parameters(""))
-    end
-  end
-
-  sub_test_case("values") do
-    def test_string
-      filter = <<-'FILTER'.strip
-title == "[\"He\\ llo\"]"
-      FILTER
-      assert_equal({
-                     :filter => filter,
-                   },
-                   to_parameters("title == %{value}",
-                                 :value => "[\"He\\ llo\"]"))
-    end
-
-    sub_test_case("Symbol") do
-      def test_valid_id
-        assert_equal({
-                       :filter => "title == \"Hello\"",
-                     },
-                     to_parameters("%{column} == %{value}",
-                                   :column => :title,
-                                   :value => "Hello"))
-      end
-
-      def test_invalid_id
-        assert_equal({
-                       :filter => "title == \"Hello World\"",
-                     },
-                     to_parameters("title == %{value}",
-                                   :value => :"Hello World"))
-      end
-    end
-
-    def test_number
-      assert_equal({
-                     :filter => "age <= 29",
-                   },
-                   to_parameters("age <= %{value}",
-                                 :value => 29))
-    end
-
-    def test_true
-      assert_equal({
-                     :filter => "published == true",
-                   },
-                   to_parameters("published == %{value}",
-                                 :value => true))
-    end
-
-    def test_false
-      assert_equal({
-                     :filter => "published == false",
-                   },
-                   to_parameters("published == %{value}",
-                                 :value => false))
-    end
-
-    def test_nil
-      assert_equal({
-                     :filter => "function(null)",
-                   },
-                   to_parameters("function(%{value})",
-                                 :value => nil))
-    end
-  end
-end

  Added: test/request/select/test-filter.rb (+280 -0) 100644
===================================================================
--- /dev/null
+++ test/request/select/test-filter.rb    2017-04-27 10:19:26 +0900 (86e0fc9)
@@ -0,0 +1,280 @@
+# Copyright (C) 2016-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
+# 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 TestRequestSelectFilter < Test::Unit::TestCase
+  setup do
+    @request = Groonga::Client::Request::Select.new("posts")
+  end
+
+  def filter(*args)
+    @request.filter(*args).to_parameters
+  end
+
+  sub_test_case("expression") do
+    def test_nil
+      assert_equal({
+                     :table => "posts",
+                   },
+                   filter(nil))
+    end
+
+    def test_string
+      assert_equal({
+                     :table => "posts",
+                     :filter => "age <= 20",
+                   },
+                   filter("age <= 20"))
+    end
+
+    def test_empty_string
+      assert_equal({
+                     :table => "posts",
+                   },
+                   filter(""))
+    end
+  end
+
+  sub_test_case("values") do
+    test("String") do
+      filter = <<-'FILTER'.strip
+title == "[\"He\\ llo\"]"
+      FILTER
+      assert_equal({
+                     :table => "posts",
+                     :filter => filter,
+                   },
+                   filter("title == %{value}",
+                          :value => "[\"He\\ llo\"]"))
+    end
+
+    sub_test_case("Symbol") do
+      test("valid ID") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "title == \"Hello\"",
+                     },
+                     filter("%{column} == %{value}",
+                            :column => :title,
+                            :value => "Hello"))
+      end
+
+      test("invalid ID") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "title == \"Hello World\"",
+                     },
+                     filter("title == %{value}",
+                            :value => :"Hello World"))
+      end
+    end
+
+    test("Numeric") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "age <= 29",
+                   },
+                   filter("age <= %{value}",
+                          :value => 29))
+    end
+
+    test("true") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "published == true",
+                   },
+                   filter("published == %{value}",
+                          :value => true))
+    end
+
+    test("false") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "published == false",
+                   },
+                   filter("published == %{value}",
+                          :value => false))
+    end
+
+    test("nil") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "function(null)",
+                   },
+                   filter("function(%{value})",
+                          :value => nil))
+    end
+
+    test("Array") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "function([\"a\", 29])",
+                   },
+                   filter("function(%{arg})", :arg => ["a", 29]))
+    end
+
+    test("Hash") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "function({\"string\": \"value\", \"number\": 29})",
+                   },
+                   filter("function(%{options})",
+                          :options => {
+                            "string" => "value",
+                            "number" => 29
+                          }))
+    end
+  end
+
+  sub_test_case("column name") do
+    test("String") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "_key == 29",
+                   },
+                   filter("_key", 29))
+    end
+
+    test("Symbol") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "_key == 29",
+                   },
+                   filter(:_key, 29))
+    end
+  end
+
+  sub_test_case("value") do
+    test("String") do
+      filter = <<-'FILTER'.strip
+title == "[\"He\\ llo\"]"
+      FILTER
+      assert_equal({
+                     :table => "posts",
+                     :filter => filter,
+                   },
+                   filter("title", "[\"He\\ llo\"]"))
+    end
+
+    sub_test_case("Symbol") do
+      test("valid ID") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "title == normalized_title",
+                     },
+                     filter("title", :normalized_title))
+      end
+
+      test("invalid ID") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "title == \"Hello World\"",
+                     },
+                     filter("title", :"Hello World"))
+      end
+    end
+
+    test("Numeric") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "age == 29",
+                   },
+                   filter("age", 29))
+    end
+
+    test("true") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "published == true",
+                   },
+                   filter("published", true))
+    end
+
+    test("false") do
+      assert_equal({
+                     :table => "posts",
+                     :filter => "published == false",
+                   },
+                   filter("published", false))
+    end
+  end
+
+  sub_test_case("Filter") do
+    sub_test_case("#geo_in_circle") do
+      def geo_in_circle(point,
+                        center, radious_or_point,
+                        approximate_type="rectangle")
+        @request.filter.geo_in_circle(point,
+                                      center, radious_or_point,
+                                      approximate_type).to_parameters
+      end
+
+      test("approximate type") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "geo_in_circle(\"100x100\", \"140x250\", 300, \"rectangle\")",
+                     },
+                     geo_in_circle("100x100", "140x250", 300, "rectangle"))
+      end
+    end
+
+    sub_test_case("#between") do
+      def between(column_name,
+                  min, min_border,
+                  max, max_border)
+        @request.filter.between(column_name,
+                                min, min_border,
+                                max, max_border).to_parameters
+      end
+
+      test("border") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "between(ages, 2, \"include\", 29, \"exclude\")",
+                     },
+                     between("ages", 2, "include", 29, "exclude"))
+      end
+    end
+
+    sub_test_case("#in_values") do
+      def in_values(column_name, *values)
+        @request.filter.in_values(column_name, *values).to_parameters
+      end
+
+      test("numbers") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "in_values(ages, 2, 29)",
+                     },
+                     in_values("ages", 2, 29))
+      end
+
+      test("strings") do
+        assert_equal({
+                       :table => "posts",
+                       :filter => "in_values(tags, \"groonga\", \"have \\\"double\\\" quote\")",
+                     },
+                     in_values("tags", "groonga", "have \"double\" quote"))
+      end
+
+      test("no values") do
+        assert_equal({
+                       :table => "posts",
+                     },
+                     in_values("tags"))
+      end
+    end
+  end
+end

  Modified: test/request/test-select.rb (+0 -141)
===================================================================
--- test/request/test-select.rb    2017-04-27 09:46:17 +0900 (11c2bfe)
+++ test/request/test-select.rb    2017-04-27 10:19:26 +0900 (c6d40c2)
@@ -19,147 +19,6 @@ class TestRequestSelect < Test::Unit::TestCase
     @request = Groonga::Client::Request::Select.new("posts")
   end
 
-  sub_test_case("#filter") do
-    def filter(*args)
-      @request.filter(*args).to_parameters
-    end
-
-    test("Numeric") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "rate == 5",
-                   },
-                   filter("rate == %{rate}", :rate => 5))
-    end
-
-    test("true") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "published == true",
-                   },
-                   filter("published == %{published}", :published => true))
-    end
-
-    test("false") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "published == false",
-                   },
-                   filter("published == %{published}", :published => false))
-    end
-
-    test("nil") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "function(null)",
-                   },
-                   filter("function(%{arg})", :arg => nil))
-    end
-
-    test("String") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "title == \"Hello\"",
-                   },
-                   filter("title == %{title}", :title => "Hello"))
-    end
-
-    test("Symbol") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "title == \"Hello\"",
-                   },
-                   filter("%{column} == %{value}",
-                          :column => :title,
-                          :value => "Hello"))
-    end
-
-    test("Array") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "function([\"a\", 29])",
-                   },
-                   filter("function(%{arg})", :arg => ["a", 29]))
-    end
-
-    test("Hash") do
-      assert_equal({
-                     :table => "posts",
-                     :filter => "function({\"string\": \"value\", \"number\": 29})",
-                   },
-                   filter("function(%{options})",
-                          :options => {
-                            "string" => "value",
-                            "number" => 29
-                          }))
-    end
-
-    sub_test_case("#geo_in_circle") do
-      def geo_in_circle(point,
-                        center, radious_or_point,
-                        approximate_type="rectangle")
-        @request.filter.geo_in_circle(point,
-                                      center, radious_or_point,
-                                      approximate_type).to_parameters
-      end
-
-      test("approximate type") do
-        assert_equal({
-                       :table => "posts",
-                       :filter => "geo_in_circle(\"100x100\", \"140x250\", 300, \"rectangle\")",
-                     },
-                     geo_in_circle("100x100", "140x250", 300, "rectangle"))
-      end
-    end
-
-    sub_test_case("#between") do
-      def between(column_name,
-                  min, min_border,
-                  max, max_border)
-        @request.filter.between(column_name,
-                                min, min_border,
-                                max, max_border).to_parameters
-      end
-
-      test("border") do
-        assert_equal({
-                       :table => "posts",
-                       :filter => "between(ages, 2, \"include\", 29, \"exclude\")",
-                     },
-                     between("ages", 2, "include", 29, "exclude"))
-      end
-    end
-
-    sub_test_case("#in_values") do
-      def in_values(column_name, *values)
-        @request.filter.in_values(column_name, *values).to_parameters
-      end
-
-      test("numbers") do
-        assert_equal({
-                       :table => "posts",
-                       :filter => "in_values(ages, 2, 29)",
-                     },
-                     in_values("ages", 2, 29))
-      end
-
-      test("strings") do
-        assert_equal({
-                       :table => "posts",
-                       :filter => "in_values(tags, \"groonga\", \"have \\\"double\\\" quote\")",
-                     },
-                     in_values("tags", "groonga", "have \"double\" quote"))
-      end
-
-      test("no values") do
-        assert_equal({
-                       :table => "posts",
-                     },
-                     in_values("tags"))
-      end
-    end
-  end
-
   sub_test_case("#drilldowns") do
     def drilldown
       @request.drilldowns("label")
-------------- next part --------------
HTML����������������������������...
下载 



More information about the Groonga-commit mailing list
Back to archive index