Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-cast-table.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-2012 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <db.h>
20 
21 #include <gcutter.h>
22 #include <glib/gstdio.h>
23 
24 #include "../lib/grn-assertions.h"
25 
26 void data_text_to_table(void);
27 void test_text_to_table(gconstpointer data);
28 
29 static grn_logger_info *logger;
30 static grn_ctx context;
31 static grn_obj *database;
32 static grn_obj src, dest;
33 
34 static gchar *tmp_directory;
35 static grn_id users, daijiro;
36 
37 static void
38 setup_database(void)
39 {
40  gchar *database_path;
41 
42  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
43  NULL);
44  database_path = g_build_filename(tmp_directory,
45  "cast-table.db",
46  NULL);
47 
48  g_mkdir_with_parents(tmp_directory, 0700);
49  database = grn_db_create(&context, database_path, NULL);
50  g_free(database_path);
51 }
52 
53 static void
54 teardown_database(void)
55 {
56  grn_obj_close(&context, database);
57  cut_remove_path(tmp_directory, NULL);
58  g_free(tmp_directory);
59 }
60 
61 static grn_id
62 create_table(const gchar *name, grn_obj_flags flags, const gchar *key_type_name)
63 {
64  grn_obj *key_type = NULL;
65  grn_obj *table;
66 
67  if (key_type_name) {
68  key_type = grn_ctx_get(&context, key_type_name, strlen(key_type_name));
69  }
70  table = grn_table_create(&context, name, strlen(name),
71  NULL, GRN_OBJ_PERSISTENT | flags,
72  key_type, NULL);
73  if (key_type) {
74  grn_obj_unlink(&context, key_type);
75  }
76 
77  return grn_obj_id(&context, table);
78 }
79 
80 static grn_id
81 add_record(const gchar *table_name, const gchar *key)
82 {
83  grn_obj *table;
84  grn_id record_id;
85 
86  table = grn_ctx_get(&context, table_name, strlen(table_name));
87  record_id = grn_table_add(&context, table, key, key ? strlen(key) : 0, NULL);
88  grn_obj_unlink(&context, table);
89 
90  grn_test_assert_not_nil(record_id);
91  return record_id;
92 }
93 
94 static void
95 setup_tables(void)
96 {
97  users = create_table("Users", GRN_OBJ_TABLE_HASH_KEY, "ShortText");
98  daijiro = add_record("Users", "daijiro");
99 }
100 
101 void
103 {
104  logger = setup_grn_logger();
105  grn_ctx_init(&context, 0);
106  GRN_VOID_INIT(&src);
107  GRN_VOID_INIT(&dest);
108 
109  setup_database();
110  setup_tables();
111 }
112 
113 void
115 {
116  grn_obj_remove(&context, &src);
117  grn_obj_remove(&context, &dest);
118  teardown_database();
119 
120  grn_ctx_fin(&context);
121  teardown_grn_logger(logger);
122 }
123 
124 static void
125 cast_text(const gchar *text)
126 {
127  grn_obj_reinit(&context, &src, GRN_DB_TEXT, 0);
128  if (text) {
129  GRN_TEXT_PUTS(&context, &src, text);
130  }
131  grn_test_assert(grn_obj_cast(&context, &src, &dest, GRN_FALSE));
132 }
133 
134 void
136 {
137 #define ADD_DATA(label, expected, expected_size, text) \
138  gcut_add_datum(label, \
139  "expected", G_TYPE_UINT, expected, \
140  "expected-size", GCUT_TYPE_SIZE, expected_size, \
141  "text", G_TYPE_STRING, text, \
142  NULL)
143 
144  ADD_DATA("existence", 1, sizeof(grn_id), "daijiro");
145  ADD_DATA("nonexistence", GRN_ID_NIL, 0, "yu");
146  ADD_DATA("empty key", GRN_ID_NIL, sizeof(grn_id), "");
147 
148 #undef ADD_DATA
149 }
150 
151 void
152 test_text_to_table(gconstpointer data)
153 {
154  gsize expected_size;
155 
156  grn_obj_reinit(&context, &dest, users, 0);
157  cast_text(gcut_data_get_string(data, "text"));
158  expected_size = gcut_data_get_size(data, "expected-size");
159  if (expected_size == 0) {
160  cut_assert_equal_uint(0, GRN_BULK_VSIZE(&dest));
161  } else {
163  grn_ctx_at(&context, users),
164  gcut_data_get_uint(data, "expected"),
165  GRN_RECORD_VALUE(&dest));
166  cut_assert_equal_uint(expected_size, GRN_BULK_VSIZE(&dest));
167  }
168 }