Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-column.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2009-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 <groonga.h>
20 
21 #include <gcutter.h>
22 #include <glib/gstdio.h>
23 
24 #include "../lib/grn-assertions.h"
25 
29 
30 static grn_logger_info *logger;
31 static grn_ctx *context;
32 static grn_obj *database;
33 static grn_obj *bookmarks;
34 static grn_obj *count_column;
35 static grn_id groonga_bookmark_id;
36 
37 static void
38 create_bookmarks_table(void)
39 {
40  const gchar bookmarks_table_name[] = "bookmarks";
41 
42  bookmarks = grn_table_create(context,
43  bookmarks_table_name,
44  strlen(bookmarks_table_name),
45  NULL,
47  get_object("ShortText"),
48  NULL);
49  grn_test_assert_context(context);
50  cut_assert_not_null(
51  bookmarks,
52  cut_message("%s", grn_collect_logger_to_string(logger)));
53 }
54 
55 static void
56 add_count_column_to_bookmarks_table (void)
57 {
58  const gchar count_column_name[] = "count";
59 
60  count_column = grn_column_create(context,
61  bookmarks,
62  count_column_name,
63  strlen(count_column_name),
64  NULL, 0,
65  get_object("Int32"));
66  grn_test_assert_context(context);
67  cut_assert_not_null(
68  count_column,
69  cut_message("%s", grn_collect_logger_to_string(logger)));
70 }
71 
72 static void
73 add_groonga_bookmark(void)
74 {
75  gchar key[] = "groonga";
76  groonga_bookmark_id = grn_table_add(context, bookmarks,
77  &key, strlen(key), NULL);
78  grn_test_assert_context(context);
80  groonga_bookmark_id,
81  cut_message("%s", grn_collect_logger_to_string(logger)));
82 }
83 
84 void
85 cut_setup(void)
86 {
87  logger = setup_grn_logger();
88  context = g_new0(grn_ctx, 1);
89  grn_ctx_init(context, 0);
90  database = grn_db_create(context, NULL, NULL);
91 
92  create_bookmarks_table();
93  add_count_column_to_bookmarks_table();
94  add_groonga_bookmark();
95 }
96 
97 void
99 {
100  grn_obj_close(context, database);
101  grn_ctx_fin(context);
102  g_free(context);
103  teardown_grn_logger(logger);
104 }
105 
106 void
108 {
109  gint32 count = 29;
110  gint32 retrieved_count;
111  grn_obj record_value;
112  grn_obj retrieved_record_value;
113 
114  GRN_INT32_INIT(&record_value, 0);
115  GRN_INT32_SET(context, &record_value, count);
116  grn_test_assert(grn_obj_set_value(context, count_column, groonga_bookmark_id,
117  &record_value, GRN_OBJ_SET));
118 
119  GRN_INT32_INIT(&retrieved_record_value, 0);
120  grn_obj_get_value(context, count_column, groonga_bookmark_id, &retrieved_record_value);
121  retrieved_count = GRN_INT32_VALUE(&retrieved_record_value);
122  cut_assert_equal_int(count, retrieved_count);
123  GRN_OBJ_FIN(context, &record_value);
124  GRN_OBJ_FIN(context, &retrieved_record_value);
125 }
126 
127 void
129 {
130  gint32 count = 29;
131  gint32 increment_count = 5;
132  gint32 retrieved_count;
133  grn_obj *record_value;
134  grn_obj *retrieved_record_value;
135 
136  record_value = grn_obj_open(context, GRN_BULK, 0, GRN_DB_INT32);
137  grn_bulk_write(context, record_value, (const char *)&count, sizeof(count));
138  grn_test_assert(grn_obj_set_value(context, count_column, groonga_bookmark_id,
139  record_value, GRN_OBJ_SET));
140  grn_obj_close(context, record_value);
141 
142  record_value = grn_obj_open(context, GRN_BULK, 0, GRN_DB_INT32);
143  grn_bulk_write(context, record_value,
144  (const char *)&increment_count, sizeof(increment_count));
145  grn_test_assert(grn_obj_set_value(context, count_column, groonga_bookmark_id,
146  record_value, GRN_OBJ_INCR));
147  grn_obj_close(context, record_value);
148 
149  retrieved_record_value = grn_obj_get_value(context, count_column,
150  groonga_bookmark_id, NULL);
151  memcpy(&retrieved_count,
152  GRN_BULK_HEAD(retrieved_record_value),
153  GRN_BULK_VSIZE(retrieved_record_value));
154  cut_assert_equal_int(count + increment_count, retrieved_count);
155  grn_obj_close(context, retrieved_record_value);
156 }
157 
158 void
160 {
161  grn_obj *table;
162  grn_obj *column;
163  const gchar *column_name = "count";
164 
165  table = grn_table_create(context, NULL, 0, NULL,
167  NULL,
168  NULL);
169  grn_test_assert_context(context);
170  column = grn_column_create(context,
171  table,
172  column_name,
173  strlen(column_name),
174  NULL, 0,
175  get_object("Int32"));
177  "[column][create] "
178  "temporary table doesn't support column: <count>",
179  context);
180 }