Groonga 3.0.9 Source Code Document
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-table-select-normalize.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010 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 as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #include "../lib/grn-assertions.h"
21 
22 void test_japanese_parenthesis(void);
23 
24 static gchar *tmp_directory;
25 static gchar *database_path;
26 
27 static grn_logger_info *logger;
28 static grn_ctx *context;
29 static grn_obj *database, *comments, *content, *expression, *variable, *result;
30 
31 void
33 {
34  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
35  "test-table-select-normalize",
36  NULL);
37  database_path = g_build_filename(tmp_directory, "database.groonga", NULL);
38 }
39 
40 void
42 {
43  g_free(database_path);
44  g_free(tmp_directory);
45 }
46 
47 static void
48 reset_variables(void)
49 {
50  context = NULL;
51  database = NULL;
52  comments = NULL;
53  content = NULL;
54  expression = NULL;
55  variable = NULL;
56  result = NULL;
57 }
58 
59 static void
60 remove_tmp_directory(void)
61 {
62  cut_remove_path(tmp_directory, NULL);
63 }
64 
65 static void
66 setup_ddl(void)
67 {
68  assert_send_command("table_create Comments TABLE_HASH_KEY ShortText");
69  comments = get_object("Comments");
70  assert_send_command("column_create Comments content COLUMN_SCALAR ShortText");
71  content = get_object("Comments.content");
72 
73  assert_send_command("table_create Terms "
74  "--flags TABLE_PAT_KEY|KEY_NORMALIZE "
75  "--key_type ShortText "
76  "--default_tokenizer TokenBigramSplitSymbol");
77  assert_send_command("column_create Terms comment_content "
78  "COLUMN_INDEX|WITH_POSITION "
79  "Comments "
80  "--source content");
81 }
82 
83 static void
84 setup_data(void)
85 {
86  assert_send_command("load "
87  "'[[\"_key\",\"content\"],"
88  "[\"ボロ\",\"うちのボロTV(アナログ...)はまだ現役です\"],"
89  "[\"ロボ\",\"ロボット 鉄\"]]'"
90  "Comments");
91 }
92 
93 static void
94 setup_database(void)
95 {
96  remove_tmp_directory();
97  g_mkdir_with_parents(tmp_directory, 0700);
98 
99  database = grn_db_create(context, database_path, NULL);
100  grn_test_assert_context(context);
101 
102  setup_ddl();
103  setup_data();
104 }
105 
106 void
108 {
109  reset_variables();
110 
111  logger = setup_grn_logger();
112 
113  context = g_new(grn_ctx, 1);
114  grn_ctx_init(context, 0);
115 
116  setup_database();
117 
118  result = grn_table_create(context, NULL, 0, NULL,
120  comments, NULL);
121  grn_test_assert_context(context);
122 }
123 
124 static void
125 teardown_database(void)
126 {
127  if (variable) {
128  grn_obj_unlink(context, variable);
129  }
130 
131  if (expression) {
132  grn_obj_unlink(context, expression);
133  }
134 
135  if (result) {
136  grn_obj_unlink(context, result);
137  }
138 
139  if (content) {
140  grn_obj_unlink(context, content);
141  }
142 
143  if (comments) {
144  grn_obj_unlink(context, comments);
145  }
146 
147  if (database) {
148  grn_obj_unlink(context, database);
149  }
150 }
151 
152 void
154 {
155  teardown_database();
156 
157  if (context) {
158  grn_ctx_fin(context);
159  g_free(context);
160  }
161 
162  teardown_grn_logger(logger);
163 
164  remove_tmp_directory();
165 }
166 
167 static grn_obj *
168 query(const gchar *string)
169 {
170  GRN_EXPR_CREATE_FOR_QUERY(context, comments, expression, variable);
171  grn_test_assert(grn_expr_parse(context, expression,
172  string, strlen(string),
173  content, GRN_OP_MATCH, GRN_OP_AND,
177  grn_test_assert_context(context);
178  return expression;
179 }
180 
181 void
183 {
184  cut_assert_not_null(grn_table_select(context, comments,
185  query("content:@)は"),
186  result, GRN_OP_OR));
187  grn_test_assert_select(context,
188  gcut_take_new_list_string("ボロ", NULL),
189  result,
190  "_key");
191 }
192 
193 void
195 {
196  cut_assert_not_null(grn_table_select(context, comments,
197  query("content:@...)は"),
198  result, GRN_OP_OR));
199  grn_test_assert_select(context,
200  gcut_take_new_list_string("ボロ", NULL),
201  result,
202  "_key");
203 }
204 
205 void
207 {
208  cut_assert_not_null(grn_table_select(context, comments,
209  query("content:@\"ロボット 鉄\""),
210  result, GRN_OP_OR));
211  grn_test_assert_select(context,
212  gcut_take_new_list_string("ロボ", NULL),
213  result,
214  "_key");
215 }
216 
217 void
219 {
220  cut_assert_not_null(grn_table_select(context, comments,
221  query("content:@ロボット"),
222  result, GRN_OP_OR));
223  grn_test_assert_select(context,
224  gcut_take_new_list_string("ロボ", NULL),
225  result,
226  "_key");
227 }