Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-select.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright(C) 2009-2010 Brazil
4  Copyright(C) 2011 Kouhei Sutou <kou@clear-code.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License version 2.1 as published by the Free Software Foundation.
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 "str.h"
21 #include <stdio.h>
22 
23 #include <gcutter.h>
24 
25 #include "../lib/grn-assertions.h"
26 
28 void test_vector_int32(void);
29 void test_vector_text(void);
30 void test_vector_reference_id(void);
32 void test_nonexistent_id(void);
33 void test_tautology(void);
34 void test_contradiction(void);
35 void test_filter_null(void);
37 void test_nonexistent_table(void);
38 void test_boolean(void);
39 void test_equal_index(void);
40 void data_less_than(void);
41 void test_less_than(gconstpointer data);
42 void data_less_than_or_equal(void);
43 void test_less_than_or_equal(gconstpointer data);
44 void data_equal_numeric(void);
45 void test_equal_numeric(gconstpointer data);
47 void test_not_tokenize_by_full_width_space(gconstpointer data);
48 
49 static gchar *tmp_directory;
50 
51 static grn_ctx *context;
52 static grn_obj *database;
53 
54 void
56 {
57  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
58  "command-select",
59  NULL);
60 }
61 
62 void
64 {
65  g_free(tmp_directory);
66 }
67 
68 static void
69 remove_tmp_directory(void)
70 {
71  cut_remove_path(tmp_directory, NULL);
72 }
73 
74 void
75 cut_setup(void)
76 {
77  const gchar *database_path;
78 
79  remove_tmp_directory();
80  g_mkdir_with_parents(tmp_directory, 0700);
81 
82  context = g_new0(grn_ctx, 1);
83  grn_ctx_init(context, 0);
84 
85  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
86  database = grn_db_create(context, database_path, NULL);
87 }
88 
89 void
91 {
92  if (context) {
93  grn_obj_unlink(context, database);
94  grn_ctx_fin(context);
95  g_free(context);
96  }
97 
98  remove_tmp_directory();
99 }
100 
101 
102 void
104 {
105  assert_send_command("table_create Answer TABLE_HASH_KEY ShortText");
106  assert_send_command("column_create Answer value COLUMN_SCALAR UInt32");
107  assert_send_command("table_create Question TABLE_HASH_KEY ShortText");
108  assert_send_command("column_create Question num COLUMN_SCALAR UInt32");
109  assert_send_command("column_create Question answer COLUMN_SCALAR Answer");
110  assert_send_command("load '"
111  "["
112  "[\"_key\",\"value\"],"
113  "[\"ultimate\",42]"
114  "]' "
115  "Answer");
116  assert_send_command("load '"
117  "["
118  "[\"_key\",\"answer\"],"
119  "[\"universe\",\"ultimate\"]"
120  "[\"mankind\",\"\"],"
121  "]' "
122  "Question");
123  cut_assert_equal_string("[["
124  "[2],"
125  "["
126  "[\"_key\",\"ShortText\"],"
127  "[\"num\",\"UInt32\"],"
128  "[\"answer.value\",\"UInt32\"]"
129  "],"
130  "[\"universe\",0,42],"
131  "[\"mankind\",0,0]"
132  "]]",
133  send_command("select Question"
134  " --output_columns"
135  " \"_key, num, answer.value\""));
136 }
137 
138 void
140 {
141  assert_send_command("table_create Students TABLE_HASH_KEY ShortText");
142  assert_send_command("column_create Students scores COLUMN_VECTOR Int32");
143 
144  cut_assert_equal_string("1",
145  send_command("load --table Students\n"
146  "[{\"_key\": \"Daijiro MORI\", "
147  "\"scores\": [5, 5, 5]}]"));
148  cut_assert_equal_string("[[[1],"
149  "[[\"_id\",\"UInt32\"],"
150  "[\"_key\",\"ShortText\"],"
151  "[\"scores\",\"Int32\"]],"
152  "[1,\"Daijiro MORI\",[5,5,5]]]]",
153  send_command("select Students"));
154 }
155 
156 void
158 {
159  assert_send_command("table_create Blogs TABLE_HASH_KEY ShortText");
160  assert_send_command("column_create Blogs articles COLUMN_VECTOR Text");
161  assert_send_command("table_create Terms "
162  "TABLE_PAT_KEY|KEY_NORMALIZE ShortText "
163  "--default_tokenizer TokenBigram");
164  assert_send_command("column_create Terms Blogs_articles "
165  "COLUMN_INDEX|WITH_POSITION Blogs articles");
166 
167  assert_send_command("load --table Blogs --columns '_key, articles' \n"
168  "[\n"
169  " [\"gunya-gunya\", "
170  " [\"hello all!\", "
171  " \"hello groonga!\", "
172  " \"hello Senna!\"]],\n"
173  " [\"groonga\", "
174  " [\"Release!\", "
175  " \"My name is groonga!\"]],\n"
176  " [\"Senna\", "
177  " [\"Release!\", "
178  " \"My name is Senna!\"]]\n"
179  "]");
180  cut_assert_equal_string("[[[2],"
181  "[[\"_key\",\"ShortText\"],"
182  "[\"articles\",\"Text\"]],"
183  "[\"gunya-gunya\","
184  "[\"hello all!\","
185  "\"hello groonga!\","
186  "\"hello Senna!\"]],"
187  "[\"groonga\","
188  "[\"Release!\","
189  "\"My name is groonga!\"]]]]",
190  send_command("select Blogs "
191  "--output_columns _key,articles "
192  "--match_columns articles "
193  "--query groonga"));
194 }
195 
196 void
198 {
199  assert_send_command("table_create Users TABLE_NO_KEY");
200  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
201  assert_send_command("table_create Comments TABLE_PAT_KEY ShortText");
202  assert_send_command("column_create Comments text COLUMN_SCALAR ShortText");
203  assert_send_command("column_create Comments authors COLUMN_VECTOR Users");
204 
205  cut_assert_equal_string("2",
206  send_command("load --table Users\n"
207  "[\n"
208  " [\"_id\", \"name\"],\n"
209  " [1, \"ryoqun\"],\n"
210  " [2, \"hayamiz\"]\n"
211  "]"));
212  cut_assert_equal_string("1",
213  send_command("load --table Comments\n"
214  "[\n"
215  " [\"_key\", \"text\", \"authors\"],\n"
216  " [\"groonga\", \"it is fast\", [1, 2]]\n"
217  "]"));
218  cut_assert_equal_string("[[[1],"
219  "[[\"_id\",\"UInt32\"],"
220  "[\"_key\",\"ShortText\"],"
221  "[\"authors\",\"Users\"],"
222  "[\"text\",\"ShortText\"]],"
223  "[1,\"groonga\",[1,2],\"it is fast\"]]]",
224  send_command("select Comments"));
225 }
226 
227 void
229 {
230  assert_send_command("table_create Users TABLE_HASH_KEY Int32");
231  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
232  assert_send_command("table_create Comments TABLE_PAT_KEY ShortText");
233  assert_send_command("column_create Comments text COLUMN_SCALAR ShortText");
234  assert_send_command("column_create Comments authors COLUMN_VECTOR Users");
235 
236  cut_assert_equal_string("2",
237  send_command("load --table Users\n"
238  "[\n"
239  "[\"_key\",\"name\"],\n"
240  "[1000,\"ryoqun\"],\n"
241  "[1001,\"hayamiz\"]\n"
242  "]"));
243 
244  cut_assert_equal_string(
245  "1",
246  send_command("load --table Comments\n"
247  "[\n"
248  "[\"_key\",\"text\",\"authors\"],\n"
249  "[\"groonga\",\"it is fast\",[1000,1001]]\n"
250  "]"));
251 
252  cut_assert_equal_string("[[[1],"
253  "[[\"_id\",\"UInt32\"],"
254  "[\"_key\",\"ShortText\"],"
255  "[\"authors\",\"Users\"],"
256  "[\"text\",\"ShortText\"]],"
257  "[1,\"groonga\",[1000,1001],\"it is fast\"]]]",
258  send_command("select Comments"));
259 }
260 
261 void
263 {
264  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
265  "column_create Sites link COLUMN_SCALAR Sites\n"
266  "load --table Sites\n"
267  "[\n"
268  "[\"_key\"],\n"
269  "[\"groonga.org\"],\n"
270  "[\"razil.jp\"]\n"
271  "]");
272  cut_assert_equal_string("[[[0],"
273  "[[\"_id\",\"UInt32\"],"
274  "[\"_key\",\"ShortText\"]]]]",
275  send_command("select Sites "
276  "--output_columns '_id _key' "
277  "--filter '_id == 100'"));
278 }
279 
280 void
282 {
283  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
284  "column_create Sites link COLUMN_SCALAR Sites\n"
285  "load --table Sites\n"
286  "[\n"
287  "[\"_key\"],\n"
288  "[\"groonga.org\"],\n"
289  "[\"razil.jp\"]\n"
290  "]");
291  cut_assert_equal_string("[[[2],"
292  "[[\"_id\",\"UInt32\"],"
293  "[\"_key\",\"ShortText\"]],"
294  "[1,\"groonga.org\"],[2,\"razil.jp\"]]]",
295  send_command("select Sites "
296  "--output_columns '_id _key' "
297  "--filter true"));
298 }
299 
300 void
302 {
303  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
304  "column_create Sites link COLUMN_SCALAR Sites\n"
305  "load --table Sites\n"
306  "[\n"
307  "[\"_key\"],\n"
308  "[\"groonga.org\"],\n"
309  "[\"razil.jp\"]\n"
310  "]");
311  cut_assert_equal_string("[[[0],"
312  "[[\"_id\",\"UInt32\"],"
313  "[\"_key\",\"ShortText\"]]]]",
314  send_command("select Sites "
315  "--output_columns '_id _key' "
316  "--filter false"));
317 }
318 
319 void
321 {
322  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
323  "column_create Sites link COLUMN_SCALAR Sites\n"
324  "load --table Sites\n"
325  "[\n"
326  "[\"_key\"],\n"
327  "[\"groonga.org\"],\n"
328  "[\"razil.jp\"]\n"
329  "]");
330  cut_assert_equal_string("[[[0],"
331  "[[\"_id\",\"UInt32\"],"
332  "[\"_key\",\"ShortText\"]]]]",
333  send_command("select Sites "
334  "--output_columns '_id _key' "
335  "--filter null"));
336 }
337 
338 void
340 {
341  assert_send_commands("table_create Softwares TABLE_HASH_KEY ShortText\n"
342  "column_create Softwares comment "
343  "COLUMN_SCALAR ShortText\n"
344  "table_create Terms TABLE_PAT_KEY ShortText "
345  "--default_tokenizer TokenBigramSplitSymbol\n"
346  "column_create Terms Softwares_comment "
347  "COLUMN_INDEX|WITH_POSITION Softwares comment\n"
348  "load --table Softwares\n"
349  "[\n"
350  "[\"_key\", \"comment\"],\n"
351  "[\"groonga\", \"うむ上々な仕上がりだね。\"],\n"
352  "[\"Cutter\", \"使いやすいじゃないか。\"]\n"
353  "[\"Senna\", \"悪くないね。上々だよ。\"]\n"
354  "]");
355  cut_assert_equal_string("[[[2],"
356  "[[\"_key\",\"ShortText\"]],"
357  "[\"groonga\"],"
358  "[\"Senna\"]]]",
359  send_command("select Softwares "
360  "--output_columns '_key' "
361  "--match_columns comment "
362  "--query 上々"));
363 }
364 
365 void
367 {
368  const gchar *command = "select nonexistent";
369 
370  grn_ctx_send(context, command, strlen(command), 0);
372  "invalid table name: <nonexistent>",
373  context);
374 }
375 
376 void
378 {
379  assert_send_command("table_create Blogs TABLE_HASH_KEY ShortText");
380  assert_send_command("column_create Blogs public COLUMN_SCALAR Bool");
381 
382  assert_send_command("load --table Blogs --columns '_key, public' \n"
383  "[\n"
384  " [\"groonga 1.0\", true],\n"
385  " [\"groonga 2.0\", false]\n"
386  "]");
387  cut_assert_equal_string("[[[1],"
388  "[[\"_key\",\"ShortText\"],"
389  "[\"public\",\"Bool\"]],"
390  "[\"groonga 1.0\",true]]]",
391  send_command("select Blogs "
392  "--output_columns _key,public "
393  "--filter public==true"));
394 }
395 
396 void
398 {
399  assert_send_command("table_create Blogs TABLE_HASH_KEY ShortText");
400  assert_send_command("table_create Titles TABLE_HASH_KEY ShortText");
401  assert_send_command("column_create Blogs title COLUMN_SCALAR Titles");
402  assert_send_command("column_create Titles Blogs_title "
403  "COLUMN_INDEX Blogs title");
404 
405  assert_send_command("load --table Blogs --columns '_key, title' \n"
406  "[\n"
407  " [\"groonga-1-0\", \"groonga 1.0 release!\"],\n"
408  " [\"groonga-2-0\", \"groonga 2.0 release!\"]\n"
409  "]");
410  cut_assert_equal_string(
411  "[[[1],"
412  "[[\"_key\",\"ShortText\"],"
413  "[\"title\",\"Titles\"]],"
414  "[\"groonga-1-0\",\"groonga 1.0 release!\"]]]",
415  send_command("select Blogs "
416  "--output_columns _key,title "
417  "--filter 'title == \"groonga 1.0 release!\"'"));
418 }
419 
420 void
422 {
423 #define ADD_DATA(type) \
424  gcut_add_datum(type, \
425  "type", G_TYPE_STRING, type, \
426  NULL)
427 
428  ADD_DATA("Int8");
429  ADD_DATA("UInt8");
430  ADD_DATA("Int16");
431  ADD_DATA("UInt16");
432  ADD_DATA("Int32");
433  ADD_DATA("UInt32");
434  ADD_DATA("Int64");
435  ADD_DATA("UInt64");
436 
437 #undef ADD_DATA
438 }
439 
440 void
441 test_less_than(gconstpointer data)
442 {
443  const gchar *type;
444 
445  type = gcut_data_get_string(data, "type");
446 
447  assert_send_command("table_create Sites TABLE_HASH_KEY ShortText");
449  cut_take_printf("column_create Sites score COLUMN_SCALAR %s", type));
450 
451  cut_assert_equal_string(
452  "3",
453  send_command("load --table Sites --columns '_key, score' \n"
454  "[\n"
455  " [\"groonga.org\", 5],\n"
456  " [\"ruby-lang.org\", 4],\n"
457  " [\"qwik.jp/senna/\", 3]\n"
458  "]"));
459  cut_assert_equal_string(
460  cut_take_printf("[[[1],"
461  "[[\"_key\",\"ShortText\"],"
462  "[\"score\",\"%s\"]],"
463  "[\"groonga.org\",5]]]",
464  type),
465  send_command("select Sites "
466  "--sortby -score "
467  "--output_columns _key,score "
468  "--filter 'score > 4'"));
469 }
470 
471 void
473 {
474 #define ADD_DATA(type) \
475  gcut_add_datum(type, \
476  "type", G_TYPE_STRING, type, \
477  NULL)
478 
479  ADD_DATA("Int8");
480  ADD_DATA("UInt8");
481  ADD_DATA("Int16");
482  ADD_DATA("UInt16");
483  ADD_DATA("Int32");
484  ADD_DATA("UInt32");
485  ADD_DATA("Int64");
486  ADD_DATA("UInt64");
487 
488 #undef ADD_DATA
489 }
490 
491 void
492 test_less_than_or_equal(gconstpointer data)
493 {
494  const gchar *type;
495 
496  type = gcut_data_get_string(data, "type");
497 
498  assert_send_command("table_create Sites TABLE_HASH_KEY ShortText");
500  cut_take_printf("column_create Sites score COLUMN_SCALAR %s", type));
501 
502  cut_assert_equal_string(
503  "3",
504  send_command("load --table Sites --columns '_key, score' \n"
505  "[\n"
506  " [\"groonga.org\", 5],\n"
507  " [\"ruby-lang.org\", 4],\n"
508  " [\"qwik.jp/senna/\", 3]\n"
509  "]"));
510  cut_assert_equal_string(
511  cut_take_printf("[[[2],"
512  "[[\"_key\",\"ShortText\"],"
513  "[\"score\",\"%s\"]],"
514  "[\"groonga.org\",5],"
515  "[\"ruby-lang.org\",4]]]",
516  type),
517  send_command("select Sites "
518  "--sortby -score "
519  "--output_columns _key,score "
520  "--filter 'score >= 4'"));
521 }
522 
523 void
525 {
526 #define ADD_DATA(type) \
527  gcut_add_datum(type, \
528  "type", G_TYPE_STRING, type, \
529  NULL)
530 
531  ADD_DATA("Int8");
532  ADD_DATA("UInt8");
533  ADD_DATA("Int16");
534  ADD_DATA("UInt16");
535  ADD_DATA("Int32");
536  ADD_DATA("UInt32");
537  ADD_DATA("Int64");
538  ADD_DATA("UInt64");
539 
540 #undef ADD_DATA
541 }
542 
543 void
544 test_equal_numeric(gconstpointer data)
545 {
546  const gchar *type;
547 
548  type = gcut_data_get_string(data, "type");
549 
550  assert_send_command("table_create Sites TABLE_HASH_KEY ShortText");
552  cut_take_printf("column_create Sites score COLUMN_SCALAR %s", type));
553 
554  cut_assert_equal_string(
555  "3",
556  send_command("load --table Sites --columns '_key, score' \n"
557  "[\n"
558  " [\"groonga.org\", 5],\n"
559  " [\"ruby-lang.org\", 4],\n"
560  " [\"qwik.jp/senna/\", 3]\n"
561  "]"));
562  cut_assert_equal_string(
563  cut_take_printf("[[[1],"
564  "[[\"_key\",\"ShortText\"],"
565  "[\"score\",\"%s\"]],"
566  "[\"ruby-lang.org\",4]]]",
567  type),
568  send_command("select Sites "
569  "--sortby -score "
570  "--output_columns _key,score "
571  "--filter 'score == 4'"));
572 }
573 
574 void
576 {
577 #define ADD_DATA(label, error_message, command) \
578  gcut_add_datum(label, \
579  "error-message", G_TYPE_STRING, error_message, \
580  "command", G_TYPE_STRING, command, \
581  NULL)
582 
583  ADD_DATA("separator",
584  "invalid table name: <Sites --output_columns>",
585  "select Sites"
586  " "
587  "--output_columns _key");
588 
589  ADD_DATA("prepend",
590  "invalid table name: < Sites>",
591  "select "
592  "  Sites"
593  " "
594  "--output_columns _key");
595 
596 #undef ADD_DATA
597 }
598 
599 void
601 {
602  const gchar *error_message;
603  const gchar *command;
604 
605  error_message = gcut_data_get_string(data, "error-message");
606  command = gcut_data_get_string(data, "command");
607 
608  assert_send_command("table_create Sites TABLE_HASH_KEY ShortText");
609  cut_assert_equal_string(
610  "3",
611  send_command("load --table Sites --columns '_key' \n"
612  "[\n"
613  " [\"groonga.org\"],\n"
614  " [\"ruby-lang.org\"],\n"
615  " [\"qwik.jp/senna/\"]\n"
616  "]"));
617 
619  error_message,
620  command);
621 }