Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-database.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2009-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 <gcutter.h>
20 #include <glib/gstdio.h>
21 
22 #include "../lib/grn-assertions.h"
23 
24 #include <str.h>
25 
26 void test_domain(void);
27 void test_range(void);
28 void test_cursor(void);
31 void test_size(void);
34 void test_get(void);
35 void test_at(void);
37 
38 static gchar *tmp_directory;
39 
40 static grn_ctx *context, *context2;
41 static grn_obj *database, *database2;
42 
43 void
45 {
46  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
47  "test-database",
48  NULL);
49 }
50 
51 void
53 {
54  g_free(tmp_directory);
55 }
56 
57 static void
58 remove_tmp_directory(void)
59 {
60  cut_remove_path(tmp_directory, NULL);
61 }
62 
63 void
64 cut_setup(void)
65 {
66  remove_tmp_directory();
67  g_mkdir_with_parents(tmp_directory, 0700);
68 
69  context = g_new0(grn_ctx, 1);
70  grn_ctx_init(context, 0);
71  database = NULL;
72 
73  context2 = g_new0(grn_ctx, 1);
74  grn_ctx_init(context2, 0);
75  database2 = NULL;
76 }
77 
78 void
80 {
81  if (context) {
82  if (database) {
83  grn_obj_close(context, database);
84  }
85  grn_ctx_fin(context);
86  g_free(context);
87  }
88 
89  if (context2) {
90  if (database2) {
91  grn_obj_close(context2, database2);
92  }
93  grn_ctx_fin(context2);
94  g_free(context2);
95  }
96 
97  remove_tmp_directory();
98 }
99 
100 void
102 {
103  database = grn_db_create(context, NULL, NULL);
104  grn_test_assert_nil(database->header.domain);
105 }
106 
107 void
109 {
110  database = grn_db_create(context, NULL, NULL);
111  grn_test_assert_nil(grn_obj_get_range(context, database));
112 }
113 
114 void
116 {
117  grn_table_cursor *c;
118  database = grn_db_create(context, NULL, NULL);
119  c = grn_table_cursor_open(context, database, NULL, 0, NULL, 0, 0, -1, 0);
120  cut_assert_true(grn_table_cursor_next(context, c));
121  grn_table_cursor_close(context, c);
122 }
123 
124 void
126 {
127  const gchar table_name[] = "Users";
128  const gchar *path;
129 
130  path = cut_build_path(tmp_directory, "database.groonga", NULL);
131  database = grn_db_create(context, path, NULL);
132  grn_test_assert_not_null(context, database);
133  grn_test_assert_not_null(context,
134  grn_table_create(context,
135  table_name,
136  strlen(table_name),
137  NULL,
140  grn_ctx_at(context, GRN_DB_UINT32),
141  NULL));
142 
143  database2 = grn_db_open(context2, path);
144  grn_test_assert_not_null(context2, database2);
145  grn_test_assert_not_null(context2,
146  grn_ctx_get(context2,
147  table_name,
148  strlen(table_name)));
149 }
150 
151 void
153 {
154  const gchar table_name[] = "<users>";
155  const gchar *path;
156 
157  path = cut_build_path(tmp_directory, "database.groonga", NULL);
158  database = grn_db_create(context, path, NULL);
159  grn_test_assert_not_null(context, database);
160  grn_test_assert_null(context,
161  grn_table_create(context,
162  table_name,
163  strlen(table_name),
164  NULL,
166  grn_ctx_at(context, GRN_DB_UINT32),
167  NULL));
168 
169  database2 = grn_db_open(context2, path);
170  grn_test_assert_not_null(context2, database2);
171  grn_test_assert_null(context2,
172  grn_ctx_get(context2,
173  table_name,
174  strlen(table_name)));
175  grn_test_assert_null(context2,
176  grn_table_create(context,
177  table_name,
178  strlen(table_name),
179  NULL,
181  grn_ctx_at(context, GRN_DB_UINT32),
182  NULL));
183 }
184 
185 void
187 {
188  guint n_builtin_objects = 255;
189  const gchar table_name[] = "bookmarks";
190 
191  database = grn_db_create(context, NULL, NULL);
192 
193  cut_assert_equal_uint(n_builtin_objects, grn_table_size(context, database));
194  grn_test_assert_context(context);
195 
196  grn_table_create(context,
197  table_name,
198  strlen(table_name),
199  NULL,
201  grn_ctx_at(context, GRN_DB_UINT32),
202  NULL);
203  cut_assert_equal_uint(n_builtin_objects + 1,
204  grn_table_size(context, database));
205  grn_test_assert_context(context);
206 }
207 
208 void
210 {
211  const gchar *path;
212 
213  path = cut_build_path(tmp_directory, "database.groonga", NULL);
214  database = grn_db_create(context, path, NULL);
215  assert_send_command("table_create Sites 0 ShortText");
216  assert_send_command("load '[[\"_key\"],[\"groonga.org\"]]' Sites");
217  cut_assert_equal_string("[[[1],[[\"_key\",\"ShortText\"]],[\"groonga.org\"]]]",
218  send_command("select Sites --output_columns _key"));
219  assert_send_command("table_remove Sites");
220  grn_obj_close(context, database);
221 
222  database = grn_db_open(context, path);
223  assert_send_command("table_create Sites 0 ShortText");
224  cut_assert_equal_string("[[[0],[[\"_key\",\"ShortText\"]]]]",
225  send_command("select Sites --output_columns _key"));
226 }
227 
228 void
230 {
231  const gchar *path;
232  gint i, n_tries = 100;
233  grn_obj *expression;
234 
235  cut_omit("will be SEGVed.");
236  path = cut_build_path(tmp_directory, "database.groonga", NULL);
237  for (i = 0; i < n_tries; i++) {
238  gint j, n_records = 100;
239  const gchar *query;
240  grn_obj *table, *variable;
241  grn_obj default_column;
242 
243  database = grn_db_create(context, path, NULL);
244  grn_test_assert_context(context);
245 
246  assert_send_command("table_create Sites 0 ShortText");
247  assert_send_command("column_create Sites point COLUMN_SCALAR Int32");
248  for (j = 0; j < n_records; j++) {
249  gchar *command;
250 
251  command = g_strdup_printf("load '"
252  "[[\"_key\", \"point\"],"
253  "[\"http://groonga.org/version/%d\",%d]]' "
254  "Sites",
255  j, j);
256  assert_send_command(command);
257  g_free(command);
258  }
259 
260  table = get_object("Sites");
261  GRN_EXPR_CREATE_FOR_QUERY(context, table, expression, variable);
262  grn_obj_unlink(context, table);
263 
264  GRN_TEXT_INIT(&default_column, 0);
265  GRN_TEXT_PUTS(context, &default_column, "point");
266  query = "point:50";
267  grn_expr_parse(context, expression,
268  query, strlen(query),
269  &default_column,
272  grn_test_assert_context(context);
273  grn_obj_unlink(context, &default_column);
274  grn_expr_compile(context, expression);
275 
276  grn_obj_close(context, database);
277  database = NULL;
278 
279  remove_tmp_directory();
280  g_mkdir_with_parents(tmp_directory, 0700);
281  }
282 
283  grn_ctx_fin(context);
284  g_free(context);
285  context = NULL;
286 }
287 
288 void
289 test_get(void)
290 {
291  const gchar *short_text_type_name = "ShortText";
292 
293  database = grn_db_create(context, NULL, NULL);
294  grn_test_assert_equal_id(context,
296  grn_table_get(context, database,
297  short_text_type_name,
298  strlen(short_text_type_name)));
299 }
300 
301 void
302 test_at(void)
303 {
304  database = grn_db_create(context, NULL, NULL);
305  grn_test_assert_equal_id(context,
307  grn_table_at(context, database, GRN_DB_SHORT_TEXT));
308 }
309 
310 void
312 {
313  const gchar *path;
314 
315  path = cut_build_path(tmp_directory, "database.groonga", NULL);
316  database = grn_db_create(context, path, NULL);
317  grn_obj_lock(context, database, GRN_ID_NIL, 0);
318  cut_assert_true(grn_obj_is_locked(context, database));
319 
320  database2 = grn_db_open(context2, path);
321  grn_obj_unlock(context2, database2, GRN_ID_NIL);
322  cut_assert_false(grn_obj_is_locked(context, database));
323 }