[Groonga-commit] groonga/groonga [master] [shcema][table-create] improve message on variable size value type error.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 12月 19日 (月) 17:41:07 JST


Kouhei Sutou	2011-12-19 08:41:07 +0000 (Mon, 19 Dec 2011)

  New Revision: aca41f7fb4ad06e0194eeeda11934f13b4b28c0d

  Log:
    [shcema][table-create] improve message on variable size value type error.
    
    refs #915
    refs #1210

  Modified files:
    lib/db.c
    test/unit/http/test-http-schema.rb

  Modified: lib/db.c (+19 -7)
===================================================================
--- lib/db.c    2011-12-19 08:24:53 +0000 (58719ed)
+++ lib/db.c    2011-12-19 08:41:07 +0000 (c7c0b85)
@@ -747,7 +747,7 @@ grn_table_create(grn_ctx *ctx, const char *name, unsigned name_size,
         key_name_size = grn_obj_name(ctx, key_type, key_name,
                                      GRN_TABLE_MAX_KEY_SIZE);
         ERR(GRN_INVALID_ARGUMENT,
-            "[table][create] key must by type or table: <%.*s> (%.*s)",
+            "[table][create] key type must be type or table: <%.*s> (%.*s)",
             name_size, name, key_name_size, key_name);
         GRN_API_RETURN(NULL);
       }
@@ -763,7 +763,13 @@ grn_table_create(grn_ctx *ctx, const char *name, unsigned name_size,
       {
         grn_db_obj *t = (grn_db_obj *)value_type;
         if (t->header.flags & GRN_OBJ_KEY_VAR_SIZE) {
-          ERR(GRN_INVALID_ARGUMENT, "value_type must be fixed size");
+          int type_name_size;
+          char type_name[GRN_TABLE_MAX_KEY_SIZE];
+          type_name_size = grn_obj_name(ctx, value_type, type_name,
+                                        GRN_TABLE_MAX_KEY_SIZE);
+          ERR(GRN_INVALID_ARGUMENT,
+              "[table][create] value type must be fixed size: <%.*s> (%.*s)",
+              name_size, name, type_name_size, type_name);
           GRN_API_RETURN(NULL);
         }
         value_size = GRN_TYPE_SIZE(t);
@@ -773,15 +779,21 @@ grn_table_create(grn_ctx *ctx, const char *name, unsigned name_size,
     case GRN_TABLE_PAT_KEY :
     case GRN_TABLE_DAT_KEY :
     case GRN_TABLE_NO_KEY :
+    case GRN_TABLE_VIEW :
       value_size = sizeof(grn_id);
       break;
     default :
-      /*
-      if (value_type == grn_type_any) {
-        value_size = sizeof(grn_id) + sizeof(grn_id);
+      {
+        int value_name_size;
+        char value_name[GRN_TABLE_MAX_KEY_SIZE];
+        value_name_size = grn_obj_name(ctx, value_type, value_name,
+                                       GRN_TABLE_MAX_KEY_SIZE);
+        ERR(GRN_INVALID_ARGUMENT,
+            "[table][create] value type must be type or table: <%.*s> (%.*s)",
+            name_size, name, value_name_size, value_name);
+        GRN_API_RETURN(NULL);
       }
-      */
-      value_size = sizeof(grn_id);
+      break;
     }
   } else {
     value_size = 0;

  Modified: test/unit/http/test-http-schema.rb (+60 -2)
===================================================================
--- test/unit/http/test-http-schema.rb    2011-12-19 08:24:53 +0000 (d82e55f)
+++ test/unit/http/test-http-schema.rb    2011-12-19 08:41:07 +0000 (3aedaa2)
@@ -369,7 +369,7 @@ class HTTPSchemaTest < Test::Unit::TestCase
                                   :key_type => "table_create"))
       assert_error_response(Result::INVALID_ARGUMENT,
                             "[table][create] " +
-                              "key must by type or table: " +
+                              "key type must be type or table: " +
                               "<users> (table_create)",
                             response,
                             :content_type => "application/json")
@@ -399,6 +399,34 @@ class HTTPSchemaTest < Test::Unit::TestCase
 
       assert_table_list([])
     end
+
+    def test_invalid_value_type
+      response = get(command_path(:table_create,
+                                  :name => "users",
+                                  :value_type => "table_create"))
+      assert_error_response(Result::INVALID_ARGUMENT,
+                            "[table][create] " +
+                              "value type must be type or table: " +
+                              "<users> (table_create)",
+                            response,
+                            :content_type => "application/json")
+
+      assert_table_list([])
+    end
+
+    def test_variable_size_value_type
+      response = get(command_path(:table_create,
+                                  :name => "users",
+                                  :value_type => "ShortText"))
+      assert_error_response(Result::INVALID_ARGUMENT,
+                            "[table][create] " +
+                              "value type must be fixed size: " +
+                              "<users> (ShortText)",
+                            response,
+                            :content_type => "application/json")
+
+      assert_table_list([])
+    end
   end
 
   class PatriciaTrieCreateTest < Test::Unit::TestCase
@@ -499,7 +527,7 @@ class HTTPSchemaTest < Test::Unit::TestCase
                                   :key_type => "table_create"))
       assert_error_response(Result::INVALID_ARGUMENT,
                             "[table][create] " +
-                              "key must by type or table: " +
+                              "key type must be type or table: " +
                               "<users> (table_create)",
                             response,
                             :content_type => "application/json")
@@ -532,6 +560,36 @@ class HTTPSchemaTest < Test::Unit::TestCase
 
       assert_table_list([])
     end
+
+    def test_invalid_value_type
+      response = get(command_path(:table_create,
+                                  :name => "users",
+                                  :flags => Table::PAT_KEY,
+                                  :value_type => "table_create"))
+      assert_error_response(Result::INVALID_ARGUMENT,
+                            "[table][create] " +
+                              "value type must be type or table: " +
+                              "<users> (table_create)",
+                            response,
+                            :content_type => "application/json")
+
+      assert_table_list([])
+    end
+
+    def test_variable_size_value_type
+      response = get(command_path(:table_create,
+                                  :name => "users",
+                                  :flags => Table::PAT_KEY,
+                                  :value_type => "ShortText"))
+      assert_error_response(Result::INVALID_ARGUMENT,
+                            "[table][create] " +
+                              "value type must be fixed size: " +
+                              "<users> (ShortText)",
+                            response,
+                            :content_type => "application/json")
+
+      assert_table_list([])
+    end
   end
 
   class ArrayCreateTest < Test::Unit::TestCase




Groonga-commit メーリングリストの案内
Back to archive index