Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-delete.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_id(void);
27 void test_filter(void);
28 void test_key(void);
29 void test_uint64(void);
30 void test_last_token(void);
31 void test_no_key_twice(void);
32 void test_no_key_by_id(void);
33 void test_corrupt_jagged_array(void);
34 void test_no_table_name(void);
35 void test_nonexistent_table(void);
36 void test_no_selector(void);
37 void test_all_selector(void);
38 void test_key_and_id(void);
39 void test_key_and_filter(void);
40 void test_id_and_filter(void);
41 void test_invalid_id(void);
42 void test_invalid_filter(void);
43 
44 static gchar *tmp_directory;
45 
46 static grn_ctx *context;
47 static grn_obj *database;
48 
49 void
51 {
52  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
53  "command-delete",
54  NULL);
55 }
56 
57 void
59 {
60  g_free(tmp_directory);
61 }
62 
63 static void
64 remove_tmp_directory(void)
65 {
66  cut_remove_path(tmp_directory, NULL);
67 }
68 
69 static void
70 setup_data(void)
71 {
72  assert_send_command("table_create Users TABLE_HASH_KEY ShortText");
73  assert_send_command("table_create Bookmarks TABLE_HASH_KEY ShortText");
74  assert_send_command("table_create Bigram TABLE_PAT_KEY ShortText "
75  "--default_tokenizer TokenBigram");
76 
77  assert_send_command("column_create Bookmarks user COLUMN_SCALAR Users");
78  assert_send_command("column_create Users bookmarks COLUMN_INDEX "
79  "Bookmarks user");
80  assert_send_command("column_create Bigram user COLUMN_INDEX|WITH_POSITION "
81  "Users _key");
82 
83  assert_send_command("load --table Users --columns '_key'\n"
84  "[\n"
85  " [\"mori\"],\n"
86  " [\"tapo\"]\n"
87  "]");
88  assert_send_command("load --table Bookmarks --columns '_key, user'\n"
89  "[\n"
90  " [\"http://groonga.org/\", \"yu\"],\n"
91  " [\"http://cutter.sourceforge.net/\", \"tasukuchan\"]\n"
92  "]");
93 }
94 
95 void
96 cut_setup(void)
97 {
98  const gchar *database_path;
99 
100  remove_tmp_directory();
101  g_mkdir_with_parents(tmp_directory, 0700);
102 
103  context = g_new0(grn_ctx, 1);
104  grn_ctx_init(context, 0);
105 
106  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
107  database = grn_db_create(context, database_path, NULL);
108 
109  setup_data();
110 }
111 
112 void
114 {
115  if (context) {
116  grn_obj_close(context, database);
117  grn_ctx_fin(context);
118  g_free(context);
119  }
120 
121  remove_tmp_directory();
122 }
123 
124 void
125 test_id(void)
126 {
127  assert_send_command("delete Users --id 1");
128  cut_assert_equal_string("[[[3],"
129  "[[\"_key\",\"ShortText\"]],"
130  "[\"tapo\"],"
131  "[\"yu\"],"
132  "[\"tasukuchan\"]]]",
133  send_command("select Users "
134  "--output_columns _key"));
135 }
136 
137 void
139 {
140  assert_send_command("delete Users --filter "
141  "\"_key == \\\"tapo\\\"\"");
142  cut_assert_equal_string("[[[3],"
143  "[[\"_key\",\"ShortText\"]],"
144  "[\"mori\"],"
145  "[\"yu\"],"
146  "[\"tasukuchan\"]]]",
147  send_command("select Users "
148  "--output_columns _key"));
149 }
150 
151 void
152 test_key(void)
153 {
154  assert_send_command("delete Users tapo");
155  cut_assert_equal_string("[[[3],"
156  "[[\"_key\",\"ShortText\"]],"
157  "[\"mori\"],"
158  "[\"yu\"],"
159  "[\"tasukuchan\"]]]",
160  send_command("select Users "
161  "--output_columns _key"));
162 }
163 
164 void
166 {
167  assert_send_command("table_create Students TABLE_HASH_KEY UInt64");
168  assert_send_command("load --table Students --columns '_key'\n"
169  "[\n"
170  " [29],\n"
171  " [2929]\n"
172  "]");
173  assert_send_command("delete Students 2929");
174  cut_assert_equal_string("[[[1],"
175  "[[\"_key\",\"UInt64\"]],"
176  "[29]]]",
177  send_command("select Students "
178  "--output_columns _key"));
179 }
180 
181 void
183 {
184  cut_assert_equal_string("[[[2],"
185  "[[\"_key\",\"ShortText\"]],"
186  "[\"mori\"],"
187  "[\"tapo\"]]]",
188  send_command("select Users "
189  "--match_columns _key "
190  "--query o "
191  "--output_columns _key"));
192  assert_send_command("delete Users tapo");
193  cut_assert_equal_string("[[[1],"
194  "[[\"_key\",\"ShortText\"]],"
195  "[\"mori\"]]]",
196  send_command("select Users "
197  "--match_columns _key "
198  "--query o "
199  "--output_columns _key"));
200 }
201 
202 void
204 {
205  assert_send_command("table_create Sites TABLE_NO_KEY");
206  assert_send_command("column_create Sites title COLUMN_SCALAR ShortText");
207  assert_send_command("delete Sites --id 999");
208  assert_send_command("delete Sites --id 999");
209  cut_assert_equal_string("3",
210  send_command("load --table Sites\n"
211  "[\n"
212  "{\"title\": \"groonga\"},\n"
213  "{\"title\": \"Ruby\"},\n"
214  "{\"title\": \"Cutter\"}\n"
215  "]"));
216  cut_assert_equal_string("[[[3],"
217  "[[\"_id\",\"UInt32\"],"
218  "[\"title\",\"ShortText\"]],"
219  "[1,\"groonga\"],"
220  "[2,\"Ruby\"],"
221  "[3,\"Cutter\"]]]",
222  send_command("select Sites"));
223 }
224 
225 void
227 {
228  assert_send_command("table_create Sites TABLE_NO_KEY");
229  assert_send_command("column_create Sites title COLUMN_SCALAR ShortText");
230  cut_assert_equal_string("3",
231  send_command("load --table Sites\n"
232  "[\n"
233  "{\"title\": \"groonga\"},\n"
234  "{\"title\": \"Ruby\"},\n"
235  "{\"title\": \"Cutter\"}\n"
236  "]"));
237  assert_send_command("delete Sites --id 2");
238  cut_assert_equal_string("[[[2],"
239  "[[\"_id\",\"UInt32\"],"
240  "[\"title\",\"ShortText\"]],"
241  "[1,\"groonga\"],"
242  "[3,\"Cutter\"]]]",
243  send_command("select Sites"));
244 }
245 
246 void
248 {
249  const gchar *text_65bytes =
250  "65bytes text "
251  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
252  const gchar *text_129bytes =
253  "129bytes text "
254  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
255  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
256 
257  assert_send_command("table_create Sites TABLE_NO_KEY");
258  assert_send_command("column_create Sites description COLUMN_SCALAR ShortText");
259  cut_assert_equal_string(
260  "1",
261  send_command(cut_take_printf("load --table Sites\n"
262  "[[\"description\"],\n"
263  "[\"%s\"]\n"
264  "]",
265  text_129bytes)));
266  assert_send_command("delete Sites --id 1");
267 
268  cut_assert_equal_string(
269  "3",
270  send_command(cut_take_printf("load --table Sites\n"
271  "[[\"description\"],\n"
272  "[\"%s\"],\n"
273  "[\"%s\"],\n"
274  "[\"%s\"]"
275  "]",
276  text_65bytes,
277  text_65bytes,
278  text_129bytes)));
279  cut_assert_equal_string(
280  cut_take_printf("[[[3],"
281  "[[\"_id\",\"UInt32\"],"
282  "[\"description\",\"ShortText\"]],"
283  "[2,\"%s\"],"
284  "[3,\"%s\"],"
285  "[4,\"%s\"]]]",
286  text_65bytes,
287  text_65bytes,
288  text_129bytes),
289  send_command("select Sites"));
290 }
291 
292 void
294 {
296  context,
298  "[table][record][delete] table name isn't specified",
299  "delete");
300 }
301 
302 void
304 {
306  context,
308  "[table][record][delete] table doesn't exist: <nonexistent>",
309  "delete nonexistent");
310 }
311 
312 void
314 {
316  context,
318  "[table][record][delete] either key, id or filter must be specified: "
319  "table: <Users>",
320  "delete Users");
321 }
322 
323 void
325 {
327  context,
329  "[table][record][delete] "
330  "record selector must be one of key, id and filter: "
331  "table: <Users>, key: <mori>, id: <1>, filter: <true>",
332  "delete Users --key mori --id 1 --filter \"true\"");
333 }
334 
335 void
337 {
339  context,
341  "[table][record][delete] can't use both key and id: "
342  "table: <Users>, key: <mori>, id: <1>",
343  "delete Users --key mori --id 1");
344 }
345 
346 void
348 {
350  context,
352  "[table][record][delete] can't use both key and filter: "
353  "table: <Users>, key: <mori>, filter: <true>",
354  "delete Users --key mori --filter true");
355 }
356 
357 void
359 {
361  context,
363  "[table][record][delete] can't use both id and filter: "
364  "table: <Users>, id: <1>, filter: <true>",
365  "delete Users --id 1 --filter true");
366 }
367 
368 void
370 {
372  context,
374  "[table][record][delete] id should be number: "
375  "table: <Users>, id: <1x2>, detail: <1|x|2>",
376  "delete Users --id \"1x2\"");
377 }
378 
379 void
381 {
383  context,
385  "[table][record][delete] failed to parse filter: "
386  "table: <Users>, filter: <$>, detail: <Syntax error! ($)>",
387  "delete Users --filter \"$\"");
388 }