Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-rename.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 <stdio.h>
20 
21 #include <gcutter.h>
22 
23 #include "../lib/grn-assertions.h"
24 
25 void test_table(void);
26 void test_table_with_no_column(void);
27 void test_column(void);
28 
29 static gchar *tmp_directory;
30 
31 static grn_ctx *context;
32 static grn_obj *database, *table, *column;
33 
34 void
36 {
37  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
38  "rename",
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  table = NULL;
69  column = NULL;
70 }
71 
72 void
74 {
75  if (context) {
76  grn_obj_unlink(context, column);
77  grn_obj_unlink(context, table);
78  grn_obj_unlink(context, database);
79  grn_ctx_fin(context);
80  g_free(context);
81  }
82 
83  remove_tmp_directory();
84 }
85 
86 void
87 test_table(void)
88 {
89  const gchar *actual;
90 
91  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
92  "column_create Sites link COLUMN_SCALAR Sites\n"
93  "load --table Sites\n"
94  "[\n"
95  "[\"_key\",\"link\"],\n"
96  "[\"groonga.org\",\"razil.jp\"],\n"
97  "[\"razil.jp\",\"qwik.jp/senna/\"]\n"
98  "]");
99  actual = send_command("select Sites");
100  cut_assert_equal_string("[[[3],"
101  "[[\"_id\",\"UInt32\"],"
102  "[\"_key\",\"ShortText\"],"
103  "[\"link\",\"Sites\"]],"
104  "[1,\"groonga.org\",\"razil.jp\"],"
105  "[3,\"qwik.jp/senna/\",\"\"],"
106  "[2,\"razil.jp\",\"qwik.jp/senna/\"]]]",
107  actual);
108 
109  table = grn_ctx_get(context, "Sites", strlen("Sites"));
110  grn_test_assert(grn_table_rename(context, table, "URLs", strlen("URLs")));
111  grn_test_assert_context(context);
112 
113  actual = send_command("select URLs");
114  cut_assert_equal_string("[[[3],"
115  "[[\"_id\",\"UInt32\"],"
116  "[\"_key\",\"ShortText\"],"
117  "[\"link\",\"URLs\"]],"
118  "[1,\"groonga.org\",\"razil.jp\"],"
119  "[3,\"qwik.jp/senna/\",\"\"],"
120  "[2,\"razil.jp\",\"qwik.jp/senna/\"]]]",
121  actual);
122 }
123 
124 void
126 {
127  const gchar *actual;
128 
129  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
130  "load --table Sites\n"
131  "[\n"
132  "[\"_key\"],\n"
133  "[\"groonga.org\"],\n"
134  "[\"razil.jp\"]\n"
135  "]");
136  actual = send_command("select Sites");
137  cut_assert_equal_string("[[[2],"
138  "[[\"_id\",\"UInt32\"],"
139  "[\"_key\",\"ShortText\"]],"
140  "[1,\"groonga.org\"],"
141  "[2,\"razil.jp\"]]]",
142  actual);
143 
144  table = grn_ctx_get(context, "Sites", strlen("Sites"));
145  grn_test_assert(grn_table_rename(context, table, "URLs", strlen("URLs")));
146  grn_test_assert_context(context);
147 
148  actual = send_command("select URLs");
149  cut_assert_equal_string("[[[2],"
150  "[[\"_id\",\"UInt32\"],"
151  "[\"_key\",\"ShortText\"]],"
152  "[1,\"groonga.org\"],"
153  "[2,\"razil.jp\"]]]",
154  actual);
155 }
156 
157 void
159 {
160  const gchar *actual;
161 
162  assert_send_commands("table_create Users TABLE_PAT_KEY ShortText\n"
163  "column_create Users nick COLUMN_SCALAR ShortText\n"
164  "load --table Users\n"
165  "[\n"
166  "[\"_key\",\"nick\"],\n"
167  "[\"Daijiro MORI\",\"daijiro\"],\n"
168  "[\"Kouhei Sutou\",\"kou\"]\n"
169  "]");
170  actual = send_command("select Users");
171  cut_assert_equal_string("[[[2],"
172  "[[\"_id\",\"UInt32\"],"
173  "[\"_key\",\"ShortText\"],"
174  "[\"nick\",\"ShortText\"]],"
175  "[1,\"Daijiro MORI\",\"daijiro\"],"
176  "[2,\"Kouhei Sutou\",\"kou\"]]]",
177  actual);
178 
179  column = grn_ctx_get(context, "Users.nick", strlen("Users.nick"));
180  grn_test_assert(grn_column_rename(context, column,
181  "account", strlen("account")));
182  grn_test_assert_context(context);
183 
184  actual = send_command("select Users");
185  cut_assert_equal_string("[[[2],"
186  "[[\"_id\",\"UInt32\"],"
187  "[\"_key\",\"ShortText\"],"
188  "[\"account\",\"ShortText\"]],"
189  "[1,\"Daijiro MORI\",\"daijiro\"],"
190  "[2,\"Kouhei Sutou\",\"kou\"]]]",
191  actual);
192 }