[Groonga-commit] droonga/grn2drn at 1bcf3f3 [master] Add error handling for unknown table referenced case

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Mar 15 22:05:56 JST 2014


Kouhei Sutou	2014-03-15 22:05:56 +0900 (Sat, 15 Mar 2014)

  New Revision: 1bcf3f3527d502026b453132d885f394abf74e88
  https://github.com/droonga/grn2drn/commit/1bcf3f3527d502026b453132d885f394abf74e88

  Message:
    Add error handling for unknown table referenced case

  Added files:
    test/test-grn2drn-schema.rb
  Copied files:
    lib/grn2drn/error.rb
      (from bin/grn2drn-schema)
  Modified files:
    bin/grn2drn-schema
    lib/grn2drn/schema-converter.rb
    test/test-schema-converter.rb

  Modified: bin/grn2drn-schema (+6 -1)
===================================================================
--- bin/grn2drn-schema    2014-03-15 21:47:25 +0900 (747df8a)
+++ bin/grn2drn-schema    2014-03-15 22:05:56 +0900 (b9f9a7b)
@@ -21,5 +21,10 @@ require "json"
 require "grn2drn/schema-converter"
 
 converter = Grn2Drn::SchemaConverter.new
-schema = converter.convert(ARGF)
+begin
+  schema = converter.convert(ARGF)
+rescue Grn2Drn::Error
+  puts($!.message)
+  exit(false)
+end
 puts(JSON.pretty_generate(schema))

  Copied: lib/grn2drn/error.rb (+4 -10) 77%
  Mode: 100755 -> 100644
===================================================================
--- bin/grn2drn-schema    2014-03-15 21:47:25 +0900 (747df8a)
+++ lib/grn2drn/error.rb    2014-03-15 22:05:56 +0900 (2c45333)
@@ -1,6 +1,3 @@
-#!/usr/bin/env ruby
-# -*- coding: utf-8 -*-
-#
 # Copyright (C) 2014 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
@@ -16,10 +13,7 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-require "json"
-
-require "grn2drn/schema-converter"
-
-converter = Grn2Drn::SchemaConverter.new
-schema = converter.convert(ARGF)
-puts(JSON.pretty_generate(schema))
+module Grn2Drn
+  class Error < StandardError
+  end
+end

  Modified: lib/grn2drn/schema-converter.rb (+15 -2)
===================================================================
--- lib/grn2drn/schema-converter.rb    2014-03-15 21:47:25 +0900 (3eb462d)
+++ lib/grn2drn/schema-converter.rb    2014-03-15 22:05:56 +0900 (75dc688)
@@ -15,8 +15,18 @@
 
 require "groonga/command/parser"
 
+require "grn2drn/error"
+
 module Grn2Drn
   class SchemaConverter
+    class UnknownTable < Error
+      attr_reader :table
+      def initialize(table)
+        @table = table
+        super("Unknown table: <#{@table}>")
+      end
+    end
+
     def initialize(options={})
       @options = options
     end
@@ -52,8 +62,11 @@ module Grn2Drn
       end
 
       def on_column_create_command(command)
-        @tables[command.table].add_column(command[:name],
-                                          Column.new(command))
+        table_name = command.table
+        table = @tables[table_name]
+        raise UnknownTable.new(table_name) if table.nil?
+        table.add_column(command[:name],
+                         Column.new(command))
       end
 
       def to_droonga_schema

  Added: test/test-grn2drn-schema.rb (+67 -0) 100644
===================================================================
--- /dev/null
+++ test/test-grn2drn-schema.rb    2014-03-15 22:05:56 +0900 (ce3585e)
@@ -0,0 +1,67 @@
+# Copyright (C) 2014 Droonga Project
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+require "rbconfig"
+require "tempfile"
+require "stringio"
+
+class Grn2DrnSchemaTest < Test::Unit::TestCase
+  def test_convert
+    assert_equal({
+                   "Users" => {
+                     "type" => "Hash",
+                     "keyType" => "ShortText",
+                     "columns" => {
+                       "name" => {
+                         "type" => "Scalar",
+                         "valueType" => "ShortText",
+                       },
+                     },
+                   },
+                 },
+                 run_grn2drn_schema(<<-GROONGA_SCHEMA))
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users name COLUMN_SCALAR ShortText
+    GROONGA_SCHEMA
+  end
+
+  private
+  def grn2drn_schema
+    File.join(File.dirname(__FILE__), "..", "bin", "grn2drn-schema")
+  end
+
+  def run_grn2drn_schema(groonga_command, *arguments)
+    input = Tempfile.new("grn2drn-schema-input")
+    input.puts(groonga_command)
+    input.flush
+    output = Tempfile.new("grn2drn-schema-output")
+    error = Tempfile.new("grn2drn-schema-error")
+    env = {}
+    options = {
+      :in  => input.path,
+      :out => output.path,
+      :err => error.path,
+    }
+    spawn_arguments = [env, RbConfig.ruby, grn2drn_schema, *arguments]
+    spawn_arguments << options
+    pid = spawn(*spawn_arguments)
+    _, status = Process.waitpid2(pid)
+    if status.success?
+      JSON.parse(output.read)
+    else
+      error.read
+    end
+  end
+end

  Modified: test/test-schema-converter.rb (+10 -0)
===================================================================
--- test/test-schema-converter.rb    2014-03-15 21:47:25 +0900 (99d1785)
+++ test/test-schema-converter.rb    2014-03-15 22:05:56 +0900 (d917e5e)
@@ -351,5 +351,15 @@ column_create Terms index COLUMN_INDEX|WITH_SECTION|WITH_POSITION \
                      index_options(command))
       end
     end
+
+    class UnknownTableTest < self
+      def test_no_table_definition
+        assert_raise(Grn2Drn::SchemaConverter::UnknownTable.new("Logs")) do
+          convert(<<-COMMAND)
+column_create Logs date COLUMN_SCALAR Time
+          COMMAND
+        end
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



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