Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-select-query.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright(C) 2010-2011 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 "str.h"
20 #include <stdio.h>
21 
22 #include <gcutter.h>
23 
24 #include "../lib/grn-assertions.h"
25 
29 void test_prefix_search(void);
30 void test_full_width_space(void);
31 void test_leading_plus(void);
33 
34 static gchar *tmp_directory;
35 
36 static grn_ctx *context;
37 static grn_obj *database;
38 
39 void
41 {
42  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
43  "command-select-query",
44  NULL);
45 }
46 
47 void
49 {
50  g_free(tmp_directory);
51 }
52 
53 static void
54 remove_tmp_directory(void)
55 {
56  cut_remove_path(tmp_directory, NULL);
57 }
58 
59 void
60 cut_setup(void)
61 {
62  const gchar *database_path;
63 
64  remove_tmp_directory();
65  g_mkdir_with_parents(tmp_directory, 0700);
66 
67  context = g_new0(grn_ctx, 1);
68  grn_ctx_init(context, 0);
69 
70  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
71  database = grn_db_create(context, database_path, NULL);
72 }
73 
74 void
76 {
77  if (context) {
78  grn_obj_unlink(context, database);
79  grn_ctx_fin(context);
80  g_free(context);
81  }
82 
83  remove_tmp_directory();
84 }
85 
86 void
88 {
89  assert_send_command("table_create Integers TABLE_NO_KEY");
90  assert_send_command("column_create Integers int64 COLUMN_SCALAR Int64");
91  assert_send_command("load --table Integers\n"
92  "[\n"
93  "{\"int64\":344494643000000}\n"
94  "]");
95  cut_assert_equal_string(
96  "[[[1],"
97  "[[\"_id\",\"UInt32\"],[\"int64\",\"Int64\"]],"
98  "[1,344494643000000]]]",
99  send_command("select Integers --query int64:<=344494643000000"));
100 }
101 
102 void
104 {
105  assert_send_command("table_create Integers TABLE_NO_KEY");
106  assert_send_command("column_create Integers int64 COLUMN_SCALAR Int64");
107  assert_send_command("load --table Integers\n"
108  "[\n"
109  "{\"int64\":344494643000000}\n"
110  "]");
111  cut_assert_equal_string(
112  "[[[1],"
113  "[[\"_id\",\"UInt32\"],[\"int64\",\"Int64\"]],"
114  "[1,344494643000000]]]",
115  send_command("select Integers --query int64:<=3.44494643e14"));
116 }
117 
118 void
120 {
121  assert_send_command("table_create Users TABLE_NO_KEY");
122  assert_send_command("table_create IDs TABLE_HASH_KEY Int32");
123  assert_send_command("column_create Users id COLUMN_SCALAR IDs");
124  assert_send_command("load --table Users\n"
125  "[\n"
126  "{\"id\":29}\n"
127  "]");
128  cut_assert_equal_string(
129  "[[[1],"
130  "[[\"_id\",\"UInt32\"],[\"id\",\"IDs\"]],"
131  "[1,29]]]",
132  send_command("select Users --query id:<30"));
133 }
134 
135 void
136 test_prefix_search(void)
137 {
138  assert_send_command("table_create Users TABLE_PAT_KEY ShortText");
139  assert_send_command("load --table Users\n"
140  "[\n"
141  "{\"_key\":\"mori\"},\n"
142  "{\"_key\":\"morita\"},\n"
143  "{\"_key\":\"mona\"}\n"
144  "]");
145  cut_assert_equal_string(
146  "[[[2],"
147  "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]],"
148  "[2,\"morita\"],"
149  "[1,\"mori\"]]]",
150  send_command("select Users --match_columns _key --query mor*"));
151 }
152 
153 void
155 {
156  assert_send_command("table_create Users TABLE_PAT_KEY ShortText");
157  assert_send_command("table_create Terms TABLE_PAT_KEY ShortText "
158  "--default_tokenizer TokenBigram");
159  assert_send_command("column_create Terms users COLUMN_INDEX|WITH_POSITION "
160  "Users _key");
161  assert_send_command("load --table Users\n"
162  "[\n"
163  "{\"_key\":\"森 大二郎\"}\n"
164  "]");
165  cut_assert_equal_string(
166  "[[[1],"
167  "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]],"
168  "[1,\"森 大二郎\"]]]",
169  send_command("select Users --match_columns _key --query 森 二郎"));
170 }
171 
172 void
174 {
175  assert_send_command("table_create Users TABLE_PAT_KEY ShortText");
176  assert_send_command("table_create Terms TABLE_PAT_KEY ShortText "
177  "--default_tokenizer TokenBigram");
178  assert_send_command("column_create Terms users COLUMN_INDEX|WITH_POSITION "
179  "Users _key");
180  assert_send_command("load --table Users\n"
181  "[\n"
182  "{\"_key\":\"森 大二郎\"}\n"
183  "]");
184  cut_assert_equal_string(
185  "[[[1],"
186  "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]],"
187  "[1,\"森 大二郎\"]]]",
188  send_command("select Users --match_columns _key --query '+森 +二郎'"));
189 }
190 
191 void
193 {
194  assert_send_command("table_create Users TABLE_PAT_KEY ShortText");
195  assert_send_command("table_create Terms TABLE_PAT_KEY ShortText "
196  "--default_tokenizer TokenBigram");
197  assert_send_command("column_create Terms users COLUMN_INDEX|WITH_POSITION "
198  "Users _key");
199  assert_send_command("load --table Users\n"
200  "[\n"
201  "{\"_key\":\"森 大二郎\"}\n"
202  "]");
203  cut_assert_equal_string(
204  "[[[1],"
205  "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]],"
206  "[1,\"森 大二郎\"]]]",
207  send_command("select Users --match_columns _key --query '(+森 +二郎)'"));
208 }