Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-load.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-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_columns(void);
27 void attributes_bool(void);
28 void data_bool(void);
29 void test_bool(gconstpointer data);
30 void test_int32_key(void);
31 void test_time_float(void);
32 void data_null(void);
33 void test_null(gconstpointer data);
34 void test_index_geo_point(void);
35 void test_no_key_table(void);
40 void test_invalid_table_name(void);
41 void data_each(void);
42 void test_each(gconstpointer data);
44 void test_vector_domain(void);
45 
46 static gchar *tmp_directory;
47 static const gchar *database_path;
48 
49 static grn_ctx *context;
50 static grn_obj *database;
51 
52 void
54 {
55  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
56  "load",
57  NULL);
58 }
59 
60 void
62 {
63  g_free(tmp_directory);
64 }
65 
66 static void
67 remove_tmp_directory(void)
68 {
69  cut_remove_path(tmp_directory, NULL);
70 }
71 
72 void
73 cut_setup(void)
74 {
75  remove_tmp_directory();
76  g_mkdir_with_parents(tmp_directory, 0700);
77 
78  context = g_new0(grn_ctx, 1);
79  grn_ctx_init(context, 0);
80 
81  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
82  database = grn_db_create(context, database_path, NULL);
83 }
84 
85 void
87 {
88  if (context) {
89  grn_obj_close(context, database);
90  grn_ctx_fin(context);
91  g_free(context);
92  }
93 
94  remove_tmp_directory();
95 }
96 
97 void
99 {
100  assert_send_command("table_create Users TABLE_HASH_KEY ShortText");
101  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
102  assert_send_command("column_create Users desc COLUMN_SCALAR ShortText");
103  cut_assert_equal_string(
104  "2",
105  send_command("load --table Users --columns '_key,name,desc'\n"
106  "[\n"
107  " [\"mori\", \"モリ\", \"タポ\"],\n"
108  " [\"tapo\", \"タポ\", \"モリモリモリタポ\"]\n"
109  "]"));
110  cut_assert_equal_string("[[[2],"
111  "["
112  "[\"_id\",\"UInt32\"],"
113  "[\"_key\",\"ShortText\"],"
114  "[\"desc\",\"ShortText\"],"
115  "[\"name\",\"ShortText\"]"
116  "],"
117  "[1,\"mori\",\"タポ\",\"モリ\"],"
118  "[2,\"tapo\",\"モリモリモリタポ\",\"タポ\"]"
119  "]]",
120  send_command("select Users"));
121 }
122 
123 void
125 {
126  cut_set_attributes("bug", "123, 304",
127  NULL);
128 }
129 
130 void
132 {
133 #define ADD_DATUM(label, load_command) \
134  gcut_add_datum(label, \
135  "load-command", G_TYPE_STRING, load_command, \
136  NULL)
137 
138  ADD_DATUM("symbol",
139  "load --table Users --columns '_key,enabled'\n"
140  "[\n"
141  " [\"mori\",true],\n"
142  " [\"tapo\",false]\n"
143  "]");
144  ADD_DATUM("number",
145  "load --table Users --columns '_key,enabled'\n"
146  "[\n"
147  " [\"mori\",1],\n"
148  " [\"tapo\",0]\n"
149  "]");
150  ADD_DATUM("string",
151  "load --table Users --columns '_key,enabled'\n"
152  "[\n"
153  " [\"mori\",\"1\"],\n"
154  " [\"tapo\",\"\"]\n"
155  "]");
156  ADD_DATUM("string (null)",
157  "load --table Users --columns '_key,enabled'\n"
158  "[\n"
159  " [\"mori\",\"0\"],\n"
160  " [\"tapo\",null]\n"
161  "]");
162 
163 #undef ADD_DATUM
164 }
165 
166 void
167 test_bool(gconstpointer data)
168 {
169  assert_send_command("table_create Users TABLE_HASH_KEY ShortText");
170  assert_send_command("column_create Users enabled COLUMN_SCALAR Bool");
171  cut_assert_equal_string("2",
172  send_command(gcut_data_get_string(data,
173  "load-command")));
174  cut_assert_equal_string("[[[2],"
175  "["
176  "[\"_id\",\"UInt32\"],"
177  "[\"_key\",\"ShortText\"],"
178  "[\"enabled\",\"Bool\"]"
179  "],"
180  "[1,\"mori\",true],"
181  "[2,\"tapo\",false]"
182  "]]",
183  send_command("select Users"));
184 }
185 
186 void
188 {
189  assert_send_command("table_create Students TABLE_HASH_KEY Int32");
190  assert_send_command("column_create Students name COLUMN_SCALAR ShortText");
191  cut_assert_equal_string(
192  "1",
193  send_command("load --table Students\n"
194  "[{\"_key\": 1, \"name\": \"morita\"}]"));
195  cut_assert_equal_string("[[[1],"
196  "["
197  "[\"_id\",\"UInt32\"],"
198  "[\"_key\",\"Int32\"],"
199  "[\"name\",\"ShortText\"]"
200  "],"
201  "[1,1,\"morita\"]"
202  "]]",
203  send_command("select Students"));
204 }
205 
206 void
208 {
209  assert_send_command("table_create Logs TABLE_NO_KEY");
210  assert_send_command("column_create Logs time_stamp COLUMN_SCALAR Time");
211  cut_assert_equal_string("1",
212  send_command("load --table Logs\n"
213  "[{\"time_stamp\": 1295851581.41798}]"));
214  cut_assert_equal_string("[[[1],"
215  "["
216  "[\"_id\",\"UInt32\"],"
217  "[\"time_stamp\",\"Time\"]"
218  "],"
219  "[1,1295851581.41798]"
220  "]]",
221  send_command("select Logs"));
222 }
223 
224 void
226 {
227 #define ADD_DATUM(label, expected, load) \
228  gcut_add_datum(label, \
229  "expected", G_TYPE_STRING, expected, \
230  "load", G_TYPE_STRING, load, \
231  NULL)
232 
233  ADD_DATUM("string - null",
234  "[[[1],"
235  "[[\"_id\",\"UInt32\"],"
236  "[\"_key\",\"ShortText\"],"
237  "[\"nick\",\"ShortText\"],"
238  "[\"scores\",\"Int32\"]],"
239  "[1,\"Daijiro MORI\",\"\",[5,5,5]]]]",
240  "load --table Students --columns '_key, nick'\n"
241  "[\n"
242  " [\"Daijiro MORI\", null]\n"
243  "]");
244  ADD_DATUM("string - empty string",
245  "[[[1],"
246  "[[\"_id\",\"UInt32\"],"
247  "[\"_key\",\"ShortText\"],"
248  "[\"nick\",\"ShortText\"],"
249  "[\"scores\",\"Int32\"]],"
250  "[1,\"Daijiro MORI\",\"\",[5,5,5]]]]",
251  "load --table Students --columns '_key, nick'\n"
252  "[\n"
253  " [\"Daijiro MORI\", \"\"]\n"
254  "]");
255 
256  ADD_DATUM("vector - empty null",
257  "[[[1],"
258  "[[\"_id\",\"UInt32\"],"
259  "[\"_key\",\"ShortText\"],"
260  "[\"nick\",\"ShortText\"],"
261  "[\"scores\",\"Int32\"]],"
262  "[1,\"Daijiro MORI\",\"morita\",[]]]]",
263  "load --table Students --columns '_key, scores'\n"
264  "[\n"
265  " [\"Daijiro MORI\", null]\n"
266  "]");
267  ADD_DATUM("vector - empty string",
268  "[[[1],"
269  "[[\"_id\",\"UInt32\"],"
270  "[\"_key\",\"ShortText\"],"
271  "[\"nick\",\"ShortText\"],"
272  "[\"scores\",\"Int32\"]],"
273  "[1,\"Daijiro MORI\",\"morita\",[]]]]",
274  "load --table Students --columns '_key, scores'\n"
275  "[\n"
276  " [\"Daijiro MORI\", \"\"]\n"
277  "]");
278  ADD_DATUM("vector - empty array",
279  "[[[1],"
280  "[[\"_id\",\"UInt32\"],"
281  "[\"_key\",\"ShortText\"],"
282  "[\"nick\",\"ShortText\"],"
283  "[\"scores\",\"Int32\"]],"
284  "[1,\"Daijiro MORI\",\"morita\",[]]]]",
285  "load --table Students --columns '_key, scores'\n"
286  "[\n"
287  " [\"Daijiro MORI\", []]\n"
288  "]");
289 
290 #undef ADD_DATUM
291 }
292 
293 void
294 test_null(gconstpointer data)
295 {
296  assert_send_command("table_create Students TABLE_HASH_KEY ShortText");
297  assert_send_command("column_create Students nick COLUMN_SCALAR ShortText");
298  assert_send_command("column_create Students scores COLUMN_VECTOR Int32");
299 
300  cut_assert_equal_string("1",
301  send_command("load --table Students\n"
302  "[{\"_key\": \"Daijiro MORI\", "
303  "\"nick\": \"morita\", "
304  "\"scores\": [5, 5, 5]}]"));
305  cut_assert_equal_string("1",
306  send_command(gcut_data_get_string(data, "load")));
307  cut_assert_equal_string(gcut_data_get_string(data, "expected"),
308  send_command("select Students"));
309 }
310 
311 void
313 {
314  assert_send_command("table_create Users TABLE_NO_KEY");
315  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
316  assert_send_command("column_create Users desc COLUMN_SCALAR ShortText");
317  cut_assert_equal_string("2",
318  send_command("load "
319  "--table Users "
320  "--columns 'name, desc' "
321  "--values "
322  "'[[\"mori\", \"the author of groonga\"],"
323  "[\"gunyara-kun\", \"co-author\"]]'"));
324 }
325 
326 void
328 {
329  cut_omit("crashed!!!");
330  assert_send_command("table_create Books TABLE_PAT_KEY ShortText");
331  assert_send_command("table_create Authors TABLE_PAT_KEY ShortText");
332  assert_send_command("table_create Bigram "
333  "TABLE_PAT_KEY|KEY_NORMALIZE ShortText "
334  "--default_tokenizer TokenBigram");
335  assert_send_command("table_create BigramLoose "
336  "TABLE_PAT_KEY|KEY_NORMALIZE ShortText "
337  "--default_tokenizer TokenBigramIgnoreBlankSplitSymbolAlphaDigit");
338  assert_send_command("column_create Books author COLUMN_SCALAR Authors");
339  assert_send_command("column_create Bigram author "
340  "COLUMN_INDEX|WITH_POSITION Authors _key");
341  assert_send_command("column_create BigramLoose author "
342  "COLUMN_INDEX|WITH_POSITION Authors _key");
343  cut_assert_equal_string(
344  "2",
345  send_command("load "
346  "--table Books "
347  "--columns '_key, author' "
348  "--values "
349  "'["
350  "[\"The groonga book\", \"mori\"],"
351  "[\"The first groonga\", \"morita\"]"
352  "]'"));
353  cut_assert_equal_string(
354  "[[[2],"
355  "[[\"_key\",\"ShortText\"],"
356  "[\"author\",\"Authors\"],"
357  "[\"_score\",\"Int32\"]],"
358  "[\"The groonga book\",\"mori\",11],"
359  "[\"The first groonga\",\"morita\",1]]]",
360  send_command("select Books "
361  "--match_columns "
362  "'Bigram.author * 10 || BigramLoose.author * 1' "
363  "--query 'mori' "
364  "--output_columns '_key, author, _score'"));
365 }
366 
367 void
369 {
370  const gchar *table_list_result;
371 
372  table_list_result =
373  cut_take_printf("["
374  "[[\"id\",\"UInt32\"],"
375  "[\"name\",\"ShortText\"],"
376  "[\"path\",\"ShortText\"],"
377  "[\"flags\",\"ShortText\"],"
378  "[\"domain\",\"ShortText\"],"
379  "[\"range\",\"ShortText\"],"
380  "[\"default_tokenizer\",\"ShortText\"],"
381  "[\"normalizer\",\"ShortText\"]],"
382  "[256,"
383  "\"Authors\","
384  "\"%s.0000100\","
385  "\"TABLE_PAT_KEY|PERSISTENT\","
386  "\"ShortText\","
387  "null,"
388  "null,"
389  "null]]",
390  database_path);
391 
392  assert_send_command("table_create Authors TABLE_PAT_KEY ShortText");
393  cut_assert_equal_string(table_list_result, send_command("table_list"));
396  "JSON must start with '[' or '{': <invalid>",
397  "load "
398  "--table Authors "
399  "--columns '_key' "
400  "--values 'invalid'");
401  cut_assert_equal_string(table_list_result, send_command("table_list"));
402 }
403 
404 void
406 {
407  assert_send_command("table_create Numbers TABLE_NO_KEY");
410  "column name must be string: <1>",
411  "load --table Numbers\n"
412  "[\n"
413  "[1],\n"
414  "[2],\n"
415  "[3]\n"
416  "]");
417 }
418 
419 void
421 {
424  "nonexistent table: <Users>",
425  "load --table Users\n"
426  "[\n"
427  "[\"_key\": \"alice\"]\n"
428  "]");
429 }
430 
431 void
433 {
435  context,
437  "[table][load] name can't start with '_' "
438  "and contains only 0-9, A-Z, a-z, #, @, - or _: <_Users>",
439  "load --table _Users\n"
440  "[\n"
441  "{\"_key\": \"alice\"}\n"
442  "]");
443 }
444 
445 
446 void
448 {
449  assert_send_command("table_create Tags TABLE_HASH_KEY ShortText "
450  "--default_tokenizer TokenDelimit");
451  assert_send_command("table_create Users TABLE_HASH_KEY ShortText");
452  assert_send_command("column_create Users tags COLUMN_VECTOR Tags");
453  cut_assert_equal_string(
454  "1",
455  send_command(
456  "load "
457  "--table Users "
458  "--values '[{\"_key\": \"mori\", \"tags\": \"groonga search engine\"}]'"));
459  cut_assert_equal_string(
460  "[[[1],"
461  "[[\"_id\",\"UInt32\"],"
462  "[\"_key\",\"ShortText\"],"
463  "[\"tags\",\"Tags\"]],"
464  "[1,\"mori\",[\"groonga\",\"search\",\"engine\"]]]]",
465  send_command("select Users"));
466 }
467 
468 void
470 {
471  assert_send_command("table_create Posts TABLE_NO_KEY");
472  assert_send_command("column_create Posts tags COLUMN_VECTOR ShortText");
473  cut_assert_equal_string(
474  "1",
475  send_command(
476  "load "
477  "--table Posts "
478  "--values '[{\"tags\": [\"groonga\", \"search engine\"]}]'"));
479 
480  {
481  grn_obj *tags_column;
482  grn_obj tags;
483  const char *tag;
484  grn_id domain;
485 
486  tags_column = grn_ctx_get(context, "Posts.tags", strlen("Posts.tags"));
488  grn_obj_get_value(context, tags_column, 1, &tags);
489  cut_assert_equal_int(2, grn_vector_size(context, &tags));
490  grn_vector_get_element(context, &tags, 0, &tag, NULL, &domain);
491  GRN_OBJ_FIN(context, &tags);
492  grn_obj_unlink(context, tags_column);
493 
495  }
496 }