Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-select-sort.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 
26 void test_int(void);
27 void test_drilldown(void);
28 void test_score_without_query(void);
30 void test_nonexistent(void);
34 
35 static gchar *tmp_directory;
36 
37 static grn_ctx *context;
38 static grn_obj *database;
39 
40 void
42 {
43  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
44  "command-select-sort",
45  NULL);
46 }
47 
48 void
50 {
51  g_free(tmp_directory);
52 }
53 
54 static void
55 remove_tmp_directory(void)
56 {
57  cut_remove_path(tmp_directory, NULL);
58 }
59 
60 static void
61 setup_ddl(void)
62 {
63  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
64  "column_create Sites score COLUMN_SCALAR Int32\n"
65  "column_create Sites age COLUMN_SCALAR Int32\n"
66  "column_create Sites description COLUMN_SCALAR ShortText");
67 
68  assert_send_commands("table_create Users TABLE_PAT_KEY ShortText\n"
69  "column_create Users name COLUMN_SCALAR ShortText");
70 
71  assert_send_commands("table_create Bookmarks TABLE_NO_KEY\n"
72  "column_create Bookmarks site COLUMN_SCALAR Sites\n"
73  "column_create Bookmarks user COLUMN_SCALAR Users\n"
74  "column_create Bookmarks rank COLUMN_SCALAR Int32");
75 
76  assert_send_commands("column_create Users bookmarks COLUMN_VECTOR Bookmarks "
77  "--source Bookmarks.user");
78 
79  assert_send_commands("table_create Bigram TABLE_PAT_KEY ShortText "
80  "--default_tokenizer TokenBigram\n"
81  "column_create Bigram description "
82  "COLUMN_INDEX|WITH_POSITION Sites description");
83 
84 }
85 
86 static void
87 setup_data(void)
88 {
89  assert_send_commands("load --table Sites\n"
90  "[\n"
91  "[\"_key\", \"score\", \"age\", \"description\"],\n"
92  "[\"groonga.org\", 100, 2, "
93  "\"fulltext search engine and column store\"],\n"
94  "[\"qwik.jp/senna/FrontPageJ.html\", 100, 5, "
95  "\"embeddable fulltext search engine\"],\n"
96  "[\"2ch.net\", 10, 11, \"BBS\"]\n"
97  "]");
98 
99  assert_send_commands("load --table Users\n"
100  "[\n"
101  "[\"_key\", \"name\"],\n"
102  "[\"morita\", \"Daijiro MORI\"],\n"
103  "[\"gunyara-kun\", \"Tasuku SUENAGA\"],\n"
104  "[\"yu\", \"Yutaro Shimamura\"]\n"
105  "]");
106 
107  assert_send_commands("load --table Bookmarks\n"
108  "[\n"
109  "[\"site\", \"user\", \"rank\"],\n"
110  "[\"groonga.org\", \"morita\", 100],\n"
111  "[\"groonga.org\", \"gunyara-kun\", 100],\n"
112  "[\"groonga.org\", \"yu\", 50],\n"
113  "[\"2ch.net\", \"gunyara-kun\", null],\n"
114  "[\"2ch.net\", \"yu\", 10]\n"
115  "]");
116 }
117 
118 void
120 {
121  const gchar *database_path;
122 
123  remove_tmp_directory();
124  g_mkdir_with_parents(tmp_directory, 0700);
125 
126  context = g_new0(grn_ctx, 1);
127  grn_ctx_init(context, 0);
128 
129  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
130  database = grn_db_create(context, database_path, NULL);
131 
132  setup_ddl();
133  setup_data();
134 }
135 
136 void
138 {
139  if (context) {
140  grn_obj_unlink(context, database);
141  grn_ctx_fin(context);
142  g_free(context);
143  }
144 
145  remove_tmp_directory();
146 }
147 
148 void
149 test_int(void)
150 {
151  cut_assert_equal_string(
152  "[[[3],"
153  "[[\"_key\",\"ShortText\"],"
154  "[\"score\",\"Int32\"],"
155  "[\"age\",\"Int32\"]],"
156  "[\"qwik.jp/senna/FrontPageJ.html\",100,5],"
157  "[\"groonga.org\",100,2],"
158  "[\"2ch.net\",10,11]]]",
159  send_command("select Sites "
160  "--sortby \"-score, -age\" "
161  "--output_columns \"_key, score, age\""));
162 }
163 
164 void
166 {
167  cut_assert_equal_string(
168  "[[[5],"
169  "[[\"site._key\",\"ShortText\"],"
170  "[\"user._key\",\"ShortText\"],"
171  "[\"rank\",\"Int32\"]],"
172  "[\"groonga.org\",\"morita\",100],"
173  "[\"groonga.org\",\"gunyara-kun\",100],"
174  "[\"groonga.org\",\"yu\",50],"
175  "[\"2ch.net\",\"gunyara-kun\",0],"
176  "[\"2ch.net\",\"yu\",10]],"
177  "[[2],"
178  "[[\"_key\",\"ShortText\"],"
179  "[\"_nsubrecs\",\"Int32\"]],"
180  "[\"2ch.net\",2],"
181  "[\"groonga.org\",3]],"
182  "[[3],"
183  "[[\"_key\",\"ShortText\"],"
184  "[\"_nsubrecs\",\"Int32\"]],"
185  "[\"gunyara-kun\",2],"
186  "[\"morita\",1],"
187  "[\"yu\",2]],"
188  "[[4],"
189  "[[\"_key\",\"Int32\"],"
190  "[\"_nsubrecs\",\"Int32\"]],"
191  "[0,1],"
192  "[10,1],"
193  "[50,1],"
194  "[100,2]]]",
195  send_command("select Bookmarks "
196  "--output_columns \"site._key, user._key, rank\" "
197  "--sortby \"-site.score, -site.age\" "
198  "--drilldown \"site, user, rank\" "
199  "--drilldown_output_columns \"_key, _nsubrecs\" "
200  "--drilldown_sortby \"_key\""));
201 }
202 
203 void
205 {
206  cut_assert_equal_string(
207  "[[[3],"
208  "[[\"_key\",\"ShortText\"]],"
209  "[\"2ch.net\"],"
210  "[\"groonga.org\"],"
211  "[\"qwik.jp/senna/FrontPageJ.html\"]]]",
212  send_command("select Sites "
213  "--sortby \"_score\" "
214  "--output_columns \"_key\""));
215 }
216 
217 void
219 {
220  cut_assert_equal_string(
221  "[[[5],"
222  "[[\"_id\",\"UInt32\"],"
223  "[\"site._key\",\"ShortText\"],"
224  "[\"user._key\",\"ShortText\"]],"
225  "[5,\"2ch.net\",\"yu\"],"
226  "[4,\"2ch.net\",\"gunyara-kun\"],"
227  "[3,\"groonga.org\",\"yu\"],"
228  "[2,\"groonga.org\",\"gunyara-kun\"],"
229  "[1,\"groonga.org\",\"morita\"]],"
230  "[[2],"
231  "[[\"_key\",\"ShortText\"],"
232  "[\"_nsubrecs\",\"Int32\"]],"
233  "[\"2ch.net\",2],"
234  "[\"groonga.org\",3]],"
235  "[[3],"
236  "[[\"_key\",\"ShortText\"],"
237  "[\"_nsubrecs\",\"Int32\"]],"
238  "[\"gunyara-kun\",2],"
239  "[\"morita\",1],"
240  "[\"yu\",2]],"
241  "[[4],"
242  "[[\"_key\",\"Int32\"],"
243  "[\"_nsubrecs\",\"Int32\"]],"
244  "[0,1],"
245  "[10,1],"
246  "[50,1],"
247  "[100,2]]]",
248  send_command("select Bookmarks "
249  "--sortby \"_score,-_id\" "
250  "--output_columns \"_id, site._key, user._key\" "
251  "--drilldown \"site user rank\" "
252  "--drilldown_output_columns \"_key, _nsubrecs\" "
253  "--drilldown_sortby \"_key\""));
254 }
255 
256 void
258 {
260  context,
262  "invalid sort key: <nonexistent>(<_score,nonexistent>)",
263  "select Sites "
264  "--sortby \"_score,nonexistent\" "
265  "--output_columns \"_key\"");
266 }
267 
268 void
270 {
271  cut_assert_equal_string(
272  "[[[2],"
273  "[[\"_key\",\"ShortText\"],[\"description\",\"ShortText\"]],"
274  "[\"qwik.jp/senna/FrontPageJ.html\","
275  "\"embeddable fulltext search engine\"],"
276  "[\"groonga.org\","
277  "\"fulltext search engine and column store\"]]]",
278  send_command("select Sites "
279  "--sortby \"description\" "
280  "--output_columns \"_key, description\" "
281  "--match_columns \"description\" "
282  "--query \"fulltext\""));
283 }
284 
285 void
287 {
288  assert_send_commands("table_create Ages TABLE_PAT_KEY Int32\n"
289  "column_create Ages site_index COLUMN_INDEX Sites age");
290  assert_send_commands("load --table Sites\n"
291  "[\n"
292  "[\"_key\", \"score\", \"age\", \"description\"],\n"
293  "[\"mroonga.github.com\", 100, 2, "
294  "\"fast fulltext search on MySQL\"],\n"
295  "[\"groonga.rubyforge.org\", 100, 1, "
296  "\"Ruby bindings for groonga\"]\n"
297  "]");
298 
299  cut_assert_equal_string(
300  "[[[5],"
301  "[[\"age\",\"Int32\"],[\"_key\",\"ShortText\"]],"
302  "[1,\"groonga.rubyforge.org\"],"
303  "[2,\"groonga.org\"],"
304  "[2,\"mroonga.github.com\"],"
305  "[5,\"qwik.jp/senna/FrontPageJ.html\"],"
306  "[11,\"2ch.net\"]]]",
307  send_command("select Sites "
308  "--sortby \"age\" "
309  "--output_columns \"age, _key\" "
310  "--match_columns \"description\" "
311  "--query \"fulltext OR BBS OR groonga\""));
312 }
313 
314 void
316 {
317  assert_send_commands("table_create Ages TABLE_PAT_KEY Int32\n"
318  "column_create Ages site_index COLUMN_INDEX Sites age");
319  assert_send_commands("load --table Sites\n"
320  "[\n"
321  "[\"_key\", \"score\", \"age\", \"description\"],\n"
322  "[\"mroonga.github.com\", 100, 2, "
323  "\"fast fulltext search on MySQL\"],\n"
324  "[\"groonga.rubyforge.org\", 100, 1, "
325  "\"Ruby bindings for groonga\"]\n"
326  "]");
327 
328  cut_assert_equal_string(
329  "[[[5],"
330  "[[\"age\",\"Int32\"],[\"_key\",\"ShortText\"]],"
331  "[1,\"groonga.rubyforge.org\"],"
332  "[2,\"groonga.org\"],"
333  "[2,\"mroonga.github.com\"],"
334  "[5,\"qwik.jp/senna/FrontPageJ.html\"],"
335  "[11,\"2ch.net\"]]]",
336  send_command("select Sites "
337  "--sortby \"age\" "
338  "--output_columns \"age, _key\""));
339 }