Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-command-select-prefix-search.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010 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_query(void);
27 void test_filter(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-select-prefix-search",
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 static void
55 setup_ddl(void)
56 {
57  assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"
58  "column_create Sites url COLUMN_SCALAR ShortText");
59 
60  assert_send_commands("table_create Bookmarks TABLE_PAT_KEY ShortText\n"
61  "column_create Bookmarks site COLUMN_SCALAR Sites");
62 
63  assert_send_commands("column_create Sites bookmarks_site "
64  "COLUMN_INDEX Bookmarks site");
65 }
66 
67 static void
68 setup_data(void)
69 {
70  assert_send_commands("load --table Sites\n"
71  "[\n"
72  "[\"_key\", \"url\"],\n"
73  "[\"groonga\", \"http://groonga.org\"],\n"
74  "[\"Senna\", \"http://qwik.jp/senna/FrontPageJ.html\"],\n"
75  "[\"rroonga\", \"http://groonga.rubyforge.org/\"],\n"
76  "[\"ranguba\", \"http://groonga.rubyforge.org/\"]\n"
77  "]");
78 
79  assert_send_commands("load --table Bookmarks\n"
80  "[\n"
81  "[\"_key\", \"site\"],\n"
82  "[\"search engine1\", \"Senna\"],\n"
83  "[\"search engine2\", \"groonga\"],\n"
84  "[\"groonga + Ruby\", \"rroonga\"],\n"
85  "[\"search system\", \"ranguba\"]\n"
86  "]");
87 }
88 
89 void
90 cut_setup(void)
91 {
92  const gchar *database_path;
93 
94  remove_tmp_directory();
95  g_mkdir_with_parents(tmp_directory, 0700);
96 
97  context = g_new0(grn_ctx, 1);
98  grn_ctx_init(context, 0);
99 
100  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
101  database = grn_db_create(context, database_path, NULL);
102 
103  setup_ddl();
104  setup_data();
105 }
106 
107 void
109 {
110  if (context) {
111  grn_obj_unlink(context, database);
112  grn_ctx_fin(context);
113  g_free(context);
114  }
115 
116  remove_tmp_directory();
117 }
118 
119 void
121 {
122  cut_assert_equal_string(
123  "[[[2],"
124  "[[\"_id\",\"UInt32\"],"
125  "[\"_key\",\"ShortText\"],"
126  "[\"site\",\"Sites\"]],"
127  "[3,\"groonga + Ruby\",\"rroonga\"],"
128  "[4,\"search system\",\"ranguba\"]]]",
129  send_command("select Bookmarks "
130  "--query \"site:^r\""));
131 }
132 
133 void
134 test_filter(void)
135 {
136  cut_assert_equal_string(
137  "[[[2],"
138  "[[\"_id\",\"UInt32\"],"
139  "[\"_key\",\"ShortText\"],"
140  "[\"site\",\"Sites\"]],"
141  "[3,\"groonga + Ruby\",\"rroonga\"],"
142  "[4,\"search system\",\"ranguba\"]]]",
143  send_command("select Bookmarks "
144  "--filter 'site @^ \"r\"'"));
145 }