[Groonga-commit] groonga/groonga [master] [test][truncate] add tests for truncating named tables. refs #892

Back to archive index

null+****@clear***** null+****@clear*****
2011年 11月 7日 (月) 15:51:55 JST


Kouhei Sutou	2011-11-07 06:51:55 +0000 (Mon, 07 Nov 2011)

  New Revision: 3f2a3faae7a0292d3b82d0d25dd3845c8537f2f4

  Log:
    [test][truncate] add tests for truncating named tables. refs #892

  Modified files:
    test/unit/core/test-table.c

  Modified: test/unit/core/test-table.c (+92 -1)
===================================================================
--- test/unit/core/test-table.c    2011-11-07 06:19:17 +0000 (7ce86c0)
+++ test/unit/core/test-table.c    2011-11-07 06:51:55 +0000 (41e5e37)
@@ -39,12 +39,14 @@ void data_create_with_invalid_name(void);
 void test_create_with_invalid_name(gpointer data);
 void data_truncate_anonymous(void);
 void test_truncate_anonymous(gconstpointer data);
+void data_truncate_named(void);
+void test_truncate_named(gconstpointer data);
 
 static gchar *tmp_directory;
 
 static grn_logger_info *logger;
 static grn_ctx *context;
-static grn_obj *database, *table;
+static grn_obj *database, *table, *column, buffer;
 
 void
 cut_startup(void)
@@ -83,11 +85,17 @@ cut_setup(void)
                            NULL);
 
   table = NULL;
+  column = NULL;
+  GRN_VOID_INIT(&buffer);
 }
 
 void
 cut_teardown(void)
 {
+  grn_obj_unlink(context, &buffer);
+  if (column) {
+    grn_obj_unlink(context, column);
+  }
   if (table) {
     grn_obj_unlink(context, table);
   }
@@ -518,3 +526,86 @@ test_truncate_anonymous(gconstpointer data)
   grn_test_assert(grn_table_truncate(context, table));
   cut_assert_equal_uint(0, grn_table_size(context, table));
 }
+
+void
+data_truncate_named(void)
+{
+#define ADD_DATA(label, flags)			\
+  gcut_add_datum(label,				\
+		 "flags", G_TYPE_INT, flags,	\
+		 NULL)
+
+  ADD_DATA("array", GRN_OBJ_TABLE_NO_KEY);
+  ADD_DATA("hash", GRN_OBJ_TABLE_HASH_KEY);
+  ADD_DATA("patricia trie", GRN_OBJ_TABLE_PAT_KEY);
+
+#undef ADD_DATA
+}
+
+void
+test_truncate_named(gconstpointer data)
+{
+  grn_obj_flags flags;
+  const gchar *table_name = "SearchEngines";
+  const gchar *key;
+  grn_obj *key_type;
+  unsigned key_size;
+  const gchar *column_name = "description";
+  grn_obj *column_type;
+  const gchar *column_value = "An open-source fulltext search engine";
+  grn_bool array_p;
+  grn_id record_id;
+  int added;
+
+  flags = gcut_data_get_int(data, "flags");
+  array_p = ((flags & GRN_OBJ_TABLE_TYPE_MASK) == GRN_OBJ_TABLE_NO_KEY);
+
+  if (array_p) {
+    key = NULL;
+    key_size = 0;
+    key_type = NULL;
+  } else {
+    key = "groonga";
+    key_size = strlen(key);
+    key_type = grn_ctx_at(context, GRN_DB_SHORT_TEXT);
+  }
+  table = grn_table_create(context,
+			   table_name, strlen(table_name), NULL,
+                           flags | GRN_OBJ_PERSISTENT,
+                           key_type, NULL);
+  if (key_type) {
+    grn_obj_unlink(context, key_type);
+  }
+  grn_test_assert_context(context);
+
+  column_type = grn_ctx_at(context, GRN_DB_SHORT_TEXT);
+  column = grn_column_create(context, table, column_name, strlen(column_name),
+			     NULL,
+			     GRN_OBJ_COLUMN_SCALAR | GRN_OBJ_PERSISTENT,
+			     column_type);
+  grn_obj_unlink(context, column_type);
+  grn_test_assert_context(context);
+
+  record_id = grn_table_add(context, table, key, key_size, &added);
+  grn_test_assert_not_nil(record_id);
+  cut_assert_true(added);
+
+  grn_obj_reinit(context, &buffer, GRN_DB_SHORT_TEXT, 0);
+  GRN_TEXT_PUTS(context, &buffer, column_value);
+  grn_test_assert(grn_obj_set_value(context, column, record_id,
+				    &buffer, GRN_OBJ_SET));
+
+  GRN_BULK_REWIND(&buffer);
+  grn_obj_get_value(context, column, record_id, &buffer);
+  GRN_TEXT_PUTC(context, &buffer, '\0');
+  cut_assert_equal_string(column_value, GRN_TEXT_VALUE(&buffer));
+  cut_assert_equal_uint(1, grn_table_size(context, table));
+
+  grn_test_assert(grn_table_truncate(context, table));
+
+  GRN_BULK_REWIND(&buffer);
+  grn_obj_get_value(context, column, record_id, &buffer);
+  GRN_TEXT_PUTC(context, &buffer, '\0');
+  cut_assert_equal_string("", GRN_TEXT_VALUE(&buffer));
+  cut_assert_equal_uint(0, grn_table_size(context, table));
+}




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