[Groonga-commit] groonga/grntest at d62b4de [master] Add generate-series directive

Back to archive index

Kouhei Sutou null+****@clear*****
Wed May 25 15:46:25 JST 2016


Kouhei Sutou	2016-05-25 15:46:25 +0900 (Wed, 25 May 2016)

  New Revision: d62b4def5b903cd8e1513ee04b2099926675d77d
  https://github.com/groonga/grntest/commit/d62b4def5b903cd8e1513ee04b2099926675d77d

  Message:
    Add generate-series directive
    
    It's useful to create many data.

  Added files:
    lib/grntest/template-evaluator.rb
  Modified files:
    lib/grntest/executors/base-executor.rb

  Modified: lib/grntest/executors/base-executor.rb (+25 -2)
===================================================================
--- lib/grntest/executors/base-executor.rb    2016-05-25 15:46:05 +0900 (c572c30)
+++ lib/grntest/executors/base-executor.rb    2016-05-25 15:46:25 +0900 (07293e3)
@@ -24,6 +24,7 @@ require "grntest/log-parser"
 require "grntest/query-log-parser"
 require "grntest/execution-context"
 require "grntest/response-parser"
+require "grntest/template-evaluator"
 
 module Grntest
   module Executors
@@ -123,7 +124,7 @@ module Grntest
         parser.on_comment do |comment|
           if /\A@/ =~ comment
             directive_content = $POSTMATCH
-            execute_directive("\##{comment}", directive_content)
+            execute_directive(parser, "\##{comment}", directive_content)
           end
         end
         parser
@@ -287,7 +288,27 @@ module Grntest
         @context.collect_query_log = (options[0] == "true")
       end
 
-      def execute_directive(line, content)
+      def execute_directive_generate_series(parser, line, content, options)
+        start, stop, table, template, = options
+        evaluator = TemplateEvaluator.new(template)
+        parser << "load --table #{table}\n"
+        parser << "["
+        first_record = true
+        Integer(start).step(Integer(stop)) do |i|
+          record = ""
+          if first_record
+            first_record = false
+          else
+            record << ","
+          end
+          record << "\n"
+          record << evaluator.evaluate(i).to_json
+          parser << record
+        end
+        parser << "\n]\n"
+      end
+
+      def execute_directive(parser, line, content)
         command, *options = Shellwords.split(content)
         case command
         when "disable-logging"
@@ -316,6 +337,8 @@ module Grntest
           execute_directive_sleep(line, content, options)
         when "collect-query-log"
           execute_directive_collect_query_log(line, content, options)
+        when "generate-series"
+          execute_directive_generate_series(parser, line, content, options)
         else
           log_input(line)
           log_error("#|e| unknown directive: <#{command}>")

  Added: lib/grntest/template-evaluator.rb (+26 -0) 100644
===================================================================
--- /dev/null
+++ lib/grntest/template-evaluator.rb    2016-05-25 15:46:25 +0900 (373a0ba)
@@ -0,0 +1,26 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+module Grntest
+  class TemplateEvaluator
+    def initialize(template)
+      @template = template
+    end
+
+    def evaluate(i)
+      eval(@template)
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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