Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-truncate.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright(C) 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_no_columns(void);
27 void test_have_columns(void);
28 
29 static gchar *tmp_directory;
30 
31 static grn_ctx *context;
32 static grn_obj *database;
33 
34 void
36 {
37  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
38  "command-truncate",
39  NULL);
40 }
41 
42 void
44 {
45  g_free(tmp_directory);
46 }
47 
48 static void
49 remove_tmp_directory(void)
50 {
51  cut_remove_path(tmp_directory, NULL);
52 }
53 
54 void
55 cut_setup(void)
56 {
57  const gchar *database_path;
58 
59  remove_tmp_directory();
60  g_mkdir_with_parents(tmp_directory, 0700);
61 
62  context = g_new0(grn_ctx, 1);
63  grn_ctx_init(context, 0);
64 
65  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
66  database = grn_db_create(context, database_path, NULL);
67 }
68 
69 void
71 {
72  if (context) {
73  grn_obj_unlink(context, database);
74  grn_ctx_fin(context);
75  g_free(context);
76  }
77 
78  remove_tmp_directory();
79 }
80 
81 void
83 {
84  assert_send_command("table_create Users TABLE_HASH_KEY ShortText");
85  assert_send_command("load --table Users\n"
86  "[\n"
87  "{\"_key\":\"mori\"}\n"
88  "{\"_key\":\"gunyara-kun\"}\n"
89  "{\"_key\":\"yu\"}\n"
90  "]");
91  cut_assert_equal_string(
92  "[[[3],"
93  "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]],"
94  "[1,\"mori\"],"
95  "[2,\"gunyara-kun\"],"
96  "[3,\"yu\"]]]",
97  send_command("select Users"));
98  cut_assert_equal_string("true",
99  send_command("truncate Users"));
100  cut_assert_equal_string(
101  "[[[0],"
102  "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]]"
103  "]]",
104  send_command("select Users"));
105 }
106 
107 void
109 {
110  assert_send_command("table_create Users TABLE_PAT_KEY ShortText");
111  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
112  assert_send_command("load --table Users\n"
113  "[\n"
114  "{\"_key\":\"mori\", \"name\":\"Daijiro MORI\"},\n"
115  "{\"_key\":\"gunyara-kun\", \"name\":\"Tasuku SUENAGA\"},\n"
116  "{\"_key\":\"yu\", \"name\":\"Yutaro Shimamura\"}\n"
117  "]");
118  cut_assert_equal_string(
119  "[[[3],"
120  "[[\"_id\",\"UInt32\"],"
121  "[\"_key\",\"ShortText\"],"
122  "[\"name\",\"ShortText\"]],"
123  "[1,\"mori\",\"Daijiro MORI\"],"
124  "[2,\"gunyara-kun\",\"Tasuku SUENAGA\"],"
125  "[3,\"yu\",\"Yutaro Shimamura\"]]]",
126  send_command("select Users --sortby _id"));
127  cut_assert_equal_string("true",
128  send_command("truncate Users"));
129  cut_assert_equal_string(
130  "[[[0],"
131  "[[\"_id\",\"UInt32\"],"
132  "[\"_key\",\"ShortText\"],"
133  "[\"name\",\"ShortText\"]]"
134  "]]",
135  send_command("select Users"));
136 }