Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-plugin.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 <gcutter.h>
20 #include <glib/gstdio.h>
21 
22 #include "../lib/grn-assertions.h"
23 
24 #include <str.h>
25 
26 void test_register_function(void);
29 
30 static gchar *tmp_directory, *plugins_dir, *plugins_dir_env;
31 
32 static grn_ctx *context;
33 static grn_obj *database;
34 
35 void
37 {
38  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
39  "plugin",
40  NULL);
41 }
42 
43 void
45 {
46  g_free(tmp_directory);
47 }
48 
49 static void
50 remove_tmp_directory(void)
51 {
52  cut_remove_path(tmp_directory, NULL);
53 }
54 
55 static void
56 setup_plugins_dir(void)
57 {
58  plugins_dir = g_build_filename(grn_test_get_build_dir(),
59  "fixtures",
60  "plugins",
61  ".libs",
62  NULL);
63  plugins_dir_env = g_strdup(g_getenv("GRN_PLUGINS_DIR"));
64  g_setenv("GRN_PLUGINS_DIR", plugins_dir, TRUE);
65 }
66 
67 void
68 cut_setup(void)
69 {
70  const gchar *database_path;
71 
72  remove_tmp_directory();
73  g_mkdir_with_parents(tmp_directory, 0700);
74 
75  context = g_new0(grn_ctx, 1);
76  grn_ctx_init(context, 0);
77 
78  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
79  database = grn_db_create(context, database_path, NULL);
80 
81  setup_plugins_dir();
82 }
83 
84 static void
85 teardown_plugins_dir(void)
86 {
87  if (plugins_dir_env) {
88  g_setenv("GRN_PLUGINS_DIR", plugins_dir_env, TRUE);
89  } else {
90  g_unsetenv("GRN_PLUGINS_DIR");
91  }
92  g_free(plugins_dir_env);
93  g_free(plugins_dir);
94 }
95 
96 void
98 {
99  teardown_plugins_dir();
100 
101  grn_obj_close(context, database);
102  grn_ctx_fin(context);
103  g_free(context);
104 
105  remove_tmp_directory();
106 }
107 
108 void
110 {
111  assert_send_command("register string");
112  assert_send_command("table_create Sites TABLE_HASH_KEY ShortText");
113  assert_send_command("load '[[\"_key\"],[\"groonga.org\"]]' Sites");
114  cut_assert_equal_string("[[[1],[[\"_score\",\"Int32\"]],[11]]]",
115  send_command("select Sites "
116  "--output_columns _score "
117  "--filter true "
118  "--scorer '_score=str_len(_key)'"));
119 }
120 
121 void
123 {
124  GString *long_name;
125  const gchar *full_path;
126  const gchar *error_message_without_path;
127  const gchar *error_message_without_name;
128  gint i, max_name_length;
129 
130  long_name = gcut_take_new_string(NULL);
131  max_name_length = (PATH_MAX - strlen(plugins_dir) - 1) - 1;
132  for (i = 0; i < max_name_length; i++) {
133  g_string_append_c(long_name, 'x');
134  }
135  full_path = cut_take_string(g_build_filename(plugins_dir,
136  long_name->str,
137  NULL));
138  error_message_without_path = "too long plugin path: <";
140  context,
142  cut_take_printf("%s%.*s",
143  error_message_without_path,
144  (int)(GRN_CTX_MSGSIZE -
145  strlen(error_message_without_path) -
146  1),
147  full_path),
148  cut_take_printf("register %s", long_name->str));
149 
150  g_string_append_c(long_name, 'x');
151  full_path = cut_take_string(g_build_filename(plugins_dir,
152  long_name->str,
153  NULL));
154  error_message_without_name =
155  cut_take_printf("plugin name is too long: %d (max: %d) <",
156  (int)(long_name->len),
157  max_name_length);
159  context,
161  cut_take_printf("%s%.*s",
162  error_message_without_name,
163  (int)(GRN_CTX_MSGSIZE -
164  strlen(error_message_without_name) -
165  1),
166  full_path),
167  cut_take_printf("register %s", long_name->str));
168 }
169 
170 void
172 {
173  assert_send_command(cut_take_printf("register %s/string", plugins_dir));
174  assert_send_command("table_create Sites TABLE_HASH_KEY ShortText");
175  assert_send_command("load '[[\"_key\"],[\"groonga.org\"]]' Sites");
176  cut_assert_equal_string("[[[1],[[\"_score\",\"Int32\"]],[11]]]",
177  send_command("select Sites "
178  "--output_columns _score "
179  "--filter true "
180  "--scorer '_score=str_len(_key)'"));
181 }