Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-accessor.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-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 <groonga.h>
20 
21 #include <gcutter.h>
22 #include <glib/gstdio.h>
23 
24 #include "../lib/grn-assertions.h"
25 
26 #define get(name) grn_ctx_get(context, name, strlen(name))
27 
28 void data_column_name(void);
29 void test_column_name(gconstpointer data);
30 
31 static gchar *tmp_directory;
32 
33 static grn_ctx *context;
34 static grn_obj *database;
35 
36 void
38 {
39  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
40  "accessor",
41  NULL);
42 }
43 
44 void
46 {
47  g_free(tmp_directory);
48 }
49 
50 static void
51 remove_tmp_directory(void)
52 {
53  cut_remove_path(tmp_directory, NULL);
54 }
55 
56 void
57 cut_setup(void)
58 {
59  const gchar *database_path;
60 
61  remove_tmp_directory();
62  g_mkdir_with_parents(tmp_directory, 0700);
63 
64  context = g_new0(grn_ctx, 1);
65  grn_ctx_init(context, 0);
66 
67  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
68  database = grn_db_create(context, database_path, NULL);
69 }
70 
71 void
73 {
74  grn_obj_close(context, database);
75  grn_ctx_fin(context);
76  g_free(context);
77 
78  remove_tmp_directory();
79 }
80 
81 void
83 {
84 #define ADD_DATA(accessor_name) \
85  gcut_add_datum(accessor_name, \
86  "accessor_name", G_TYPE_STRING, accessor_name, \
87  NULL)
88 
89  ADD_DATA("_id");
90  ADD_DATA("_key");
91  ADD_DATA("_value");
92  ADD_DATA("_score");
93 
94 #undef ADD_DATA
95 }
96 
97 void
98 test_column_name(gconstpointer data)
99 {
100  const gchar *table_name = "Bookmarks";
101  const gchar *accessor_name;
102  grn_obj *bookmarks, *accessor;
103  gchar name[256];
104  gint length;
105 
106  bookmarks = grn_table_create(context, table_name, strlen(table_name),
107  NULL,
111  get("ShortText"), get("Int32"));
112  accessor_name = gcut_data_get_string(data, "accessor_name");
113  accessor = grn_obj_column(context, bookmarks,
114  accessor_name, strlen(accessor_name));
115  length = grn_column_name(context, accessor, name, sizeof(name));
116  name[length] = '\0';
117  grn_obj_unlink(context, accessor);
118  cut_assert_equal_string(accessor_name, name);
119 }