Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-taiyaki.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-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 <gcutter.h>
20 #include <glib/gstdio.h>
21 
22 #include "../lib/grn-assertions.h"
23 
24 void test_in_circle(void);
33 void test_sort(void);
35 void test_in_circle_and_tag(void);
36 void test_but_white(void);
37 void test_drilldown(void);
39 void test_weight_match(void);
40 void test_query_expansion(void);
41 
42 static gchar *tmp_directory;
43 
44 static grn_ctx *context;
45 static grn_obj *database;
46 
47 void
49 {
50  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
51  "taiyaki",
52  NULL);
53 }
54 
55 void
57 {
58  g_free(tmp_directory);
59 }
60 
61 static void
62 remove_tmp_directory(void)
63 {
64  cut_remove_path(tmp_directory, NULL);
65 }
66 
67 void
68 cut_setup(void)
69 {
70  const gchar *database_path;
71 
72  cut_set_fixture_data_dir(grn_test_get_base_dir(),
73  "fixtures",
74  "story",
75  "taiyaki",
76  NULL);
77 
78  remove_tmp_directory();
79  g_mkdir_with_parents(tmp_directory, 0700);
80 
81  context = g_new0(grn_ctx, 1);
82  grn_ctx_init(context, 0);
83 
84  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
85  database = grn_db_create(context, database_path, NULL);
86 
87  assert_send_commands(cut_get_fixture_data_string("ddl.grn", NULL));
88  assert_send_command(cut_get_fixture_data_string("areas.grn", NULL));
89  assert_send_command(cut_get_fixture_data_string("categories.grn", NULL));
90  assert_send_command(cut_get_fixture_data_string("shops.grn", NULL));
91  assert_send_command(cut_get_fixture_data_string("synonyms.grn", NULL));
92 }
93 
94 void
96 {
97  if (context) {
98  grn_obj_unlink(context, database);
99  grn_ctx_fin(context);
100  g_free(context);
101  }
102 
103  remove_tmp_directory();
104 }
105 
106 void
107 test_in_circle(void)
108 {
109  gdouble yurakucho_latitude = 35.67487;
110  gdouble yurakucho_longitude = 139.76352;
111  gint distance = 3 * 1000;
112 
113  cut_assert_equal_string(
114  "[[[7],"
115  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"],"
116  "[\"location\",\"WGS84GeoPoint\"]],"
117  "[\"銀座 かずや\",280,\"128424629x503139222\"],"
118  "[\"たい焼き鉄次 大丸東京店\",810,\"128451283x503166852\"],"
119  "[\"たいやき神田達磨 八重洲店\",970,\"128453260x503174156\"],"
120  "[\"にしみや 甘味処\",1056,\"128418570x503188661\"],"
121  "[\"築地 さのきや\",1186,\"128397312x503174596\"],"
122  "[\"しげ田\",1530,\"128421454x503208983\"],"
123  "[\"柳屋 たい焼き\",2179,\"128467228x503222332\"]"
124  "]]",
125  send_command(
126  cut_take_printf(
127  "select Shops "
128  "--sortby '+_score, +name' "
129  "--output_columns 'name, _score, location' "
130  "--filter 'geo_in_circle(location, \"%s\", %d)' "
131  "--scorer '_score=geo_distance(location, \"%s\")'",
132  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
133  distance,
134  grn_test_location_string(yurakucho_latitude, yurakucho_longitude))));
135 }
136 
137 void
139 {
140  gdouble sugamo_latitude = 35.73360;
141  gdouble sugamo_longitude = 139.7394;
142  gdouble daiba_latitude = 35.62614;
143  gdouble daiba_longitude = 139.7714;
144  gdouble budoukan_latitude = 35.69328;
145  gdouble budoukan_longitude = 139.74968;
146 
147  cut_assert_equal_string(
148  "[[[5],"
149  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"],"
150  "[\"location\",\"WGS84GeoPoint\"]],"
151  "[\"たい焼き鉄次 大丸東京店\",2186,\"128451283x503166852\"],"
152  "[\"たいやき神田達磨 八重洲店\",2296,\"128453260x503174156\"],"
153  "[\"銀座 かずや\",2415,\"128424629x503139222\"],"
154  "[\"根津のたいやき\",3210,\"128592911x503145263\"],"
155  "[\"築地 さのきや\",3579,\"128397312x503174596\"]"
156  "]]",
157  send_command(
158  cut_take_printf(
159  "select Shops "
160  "--sortby '+_score, +name' "
161  "--output_columns 'name, _score, location' "
162  "--filter 'geo_in_rectangle(location, \"%s\", \"%s\")' "
163  "--scorer '_score=geo_distance(location, \"%s\")'",
164  grn_test_location_string(sugamo_latitude, sugamo_longitude),
165  grn_test_location_string(daiba_latitude, daiba_longitude),
166  grn_test_location_string(budoukan_latitude, budoukan_longitude))));
167 }
168 
169 void
171 {
172  gdouble takada_no_baba_latitude = 35.7121;
173  gdouble takada_no_baba_longitude = 139.7038;
174  gdouble tsukiji_latitude = 35.6684;
175  gdouble tsukiji_longitude = 139.7727;
176  gdouble budoukan_latitude = 35.69328;
177  gdouble budoukan_longitude = 139.74968;
178 
179  cut_assert_equal_string(
180  "[[[3],"
181  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"],"
182  "[\"location\",\"WGS84GeoPoint\"]],"
183  "[\"たい焼き鉄次 大丸東京店\",2186,\"128451283x503166852\"],"
184  "[\"たいやき神田達磨 八重洲店\",2296,\"128453260x503174156\"],"
185  "[\"銀座 かずや\",2415,\"128424629x503139222\"]"
186  "]]",
187  send_command(
188  cut_take_printf(
189  "select Shops "
190  "--sortby '+_score, +name' "
191  "--output_columns 'name, _score, location' "
192  "--filter 'geo_in_rectangle(location, \"%s\", \"%s\")' "
193  "--scorer '_score=geo_distance(location, \"%s\")'",
194  grn_test_location_string(takada_no_baba_latitude,
195  takada_no_baba_longitude),
196  grn_test_location_string(tsukiji_latitude, tsukiji_longitude),
197  grn_test_location_string(budoukan_latitude, budoukan_longitude))));
198 }
199 
200 void
202 {
203  gdouble takada_no_baba_latitude = 35.7121;
204  gdouble takada_no_baba_longitude = 139.7038;
205  gdouble budoukan_latitude = 35.69328;
206  gdouble budoukan_longitude = 139.74968;
207 
208  cut_assert_equal_string(
209  "[[[0],"
210  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"],"
211  "[\"location\",\"WGS84GeoPoint\"]]"
212  "]]",
213  send_command(
214  cut_take_printf(
215  "select Shops "
216  "--sortby '+_score, +name' "
217  "--output_columns 'name, _score, location' "
218  "--filter 'geo_in_rectangle(location, \"%s\", \"%s\")' "
219  "--scorer '_score=geo_distance(location, \"%s\")'",
220  grn_test_location_string(takada_no_baba_latitude,
221  takada_no_baba_longitude),
222  grn_test_location_string(takada_no_baba_latitude,
223  takada_no_baba_longitude),
224  grn_test_location_string(budoukan_latitude, budoukan_longitude))));
225 }
226 
227 void
229 {
231  context,
233  "geo_in_rectangle(): negative coordinate is not implemented.",
234  "select Shops "
235  "--sortby '+name' "
236  "--output_columns 'name, location' "
237  "--filter 'geo_in_rectangle(location, "
238  "\"35.73360x-139.7394\", \"-35.62614x139.7714\")'");
239 }
240 
241 void
243 {
245  context,
247  "geo_in_rectangle(): top left point's latitude is too big: "
248  "<324000000>(max:324000000): "
249  "(324000000,503061840) (128254104,50317704",
250  "select Shops "
251  "--sortby '+name' "
252  "--output_columns 'name, location' "
253  "--filter "
254  "'geo_in_rectangle(location, \"90.0x139.7394\", \"35.62614x139.7714\")'");
255 }
256 
257 void
259 {
261  context,
263  "geo_in_rectangle(): top left point's longitude is too big: "
264  "<648000000>(max:648000000): "
265  "(128640960,648000000) (128254104,5031770",
266  "select Shops "
267  "--sortby '+name' "
268  "--output_columns 'name, location' "
269  "--filter "
270  "'geo_in_rectangle(location, \"35.73360x180.0\", \"35.62614x139.7714\")'");
271 }
272 
273 void
275 {
277  context,
279  "geo_in_rectangle(): bottom right point's latitude is too big: "
280  "<324000000>(max:324000000): "
281  "(128640960,503061840) (324000000,5031",
282  "select Shops "
283  "--sortby '+name' "
284  "--output_columns 'name, location' "
285  "--filter "
286  "'geo_in_rectangle(location, \"35.73360x139.7394\", \"90.0x139.7714\")'");
287 }
288 
289 void
291 {
293  context,
295  "geo_in_rectangle(): bottom right point's longitude is too big: "
296  "<648000000>(max:648000000): "
297  "(128640960,503061840) (128254104,648",
298  "select Shops "
299  "--sortby '+name' "
300  "--output_columns 'name, location' "
301  "--filter "
302  "'geo_in_rectangle(location, \"35.73360x139.7394\", \"35.62614x180.0\")'");
303 }
304 
305 void
307 {
308  gdouble yurakucho_latitude = 35.67487;
309  gdouble yurakucho_longitude = 139.76352;
310  gint distance = 3 * 1000;
311 
312  cut_assert_equal_string(
313  "[[[7],"
314  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
315  "[\"銀座 かずや\",280],"
316  "[\"たい焼き鉄次 大丸東京店\",810],"
317  "[\"たいやき神田達磨 八重洲店\",970],"
318  "[\"にしみや 甘味処\",1056],"
319  "[\"築地 さのきや\",1186],"
320  "[\"しげ田\",1530],"
321  "[\"柳屋 たい焼き\",2179]"
322  "]]",
323  send_command(
324  cut_take_printf(
325  "select Shops "
326  "--sortby 'geo_distance(location, \"%s\")' "
327  "--output_columns 'name, _score' "
328  "--filter 'geo_in_circle(location, \"%s\", %d)' "
329  "--scorer '_score=geo_distance(location, \"%s\")'",
330  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
331  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
332  distance,
333  grn_test_location_string(yurakucho_latitude, yurakucho_longitude))));
334 }
335 
336 void
338 {
339  gdouble tokyo_tocho_latitude = 35.689444;
340  gdouble tokyo_tocho_longitude = 139.69166701;
341 
342  cut_assert_equal_string(
343  "[[[31],"
344  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
345  "[\"さざれ\",2860],"
346  "[\"広瀬屋\",2870],"
347  "[\"そばたいやき空\",3004],"
348  "[\"たい焼 カタオカ\",3347],"
349  "[\"車\",3792],"
350  "[\"吾妻屋\",4166],"
351  "[\"代官山たい焼き黒鯛\",4497],"
352  "[\"たいやきひいらぎ\",4965],"
353  "[\"たいやき工房白家 阿佐ヶ谷店\",5102],"
354  "[\"たいやき本舗 藤家 阿佐ヶ谷店\",5172]"
355  "]]",
356  send_command(
357  cut_take_printf(
358  "select Shops "
359  "--sortby '+_score, +name' "
360  "--output_columns 'name, _score' "
361  "--filter 'tags @ \"たいやき\"' "
362  "--scorer '_score=geo_distance2(location, \"%s\")'",
363  grn_test_location_string(tokyo_tocho_latitude, tokyo_tocho_longitude))));
364 }
365 
366 void
368 {
369  gdouble tokyo_tocho_latitude = 35.689444;
370  gdouble tokyo_tocho_longitude = 139.691667;
371  gint distance = 4 * 1000;
372 
373  cut_assert_equal_string(
374  "[[[5],"
375  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
376  "[\"さざれ\",2860],"
377  "[\"広瀬屋\",2871],"
378  "[\"そばたいやき空\",3016],"
379  "[\"たい焼 カタオカ\",3353],"
380  "[\"車\",3794]"
381  "]]",
382  send_command(
383  cut_take_printf(
384  "select Shops "
385  "--sortby '+_score, +name' "
386  "--output_columns 'name, _score' "
387  "--filter 'geo_in_circle(location, \"%s\", %d) && tags @ \"たいやき\"' "
388  "--scorer '_score=geo_distance3(location, \"%s\")'",
389  grn_test_location_string(tokyo_tocho_latitude, tokyo_tocho_longitude),
390  distance,
391  grn_test_location_string(tokyo_tocho_latitude, tokyo_tocho_longitude))));
392 }
393 
394 void
396 {
397  gdouble asagaya_latitude = 35.70452;
398  gdouble asagaya_longitude = 139.6351;
399  gint distance = 5 * 1000;
400 
401  cut_assert_equal_string(
402  "[[[5],"
403  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
404  "[\"吾妻屋\",1198],"
405  "[\"そばたいやき空\",3162],"
406  "[\"たい焼き / たつみや\",4341],"
407  "[\"さざれ\",4637],"
408  "[\"広瀬屋\",4692]"
409  "]]",
410  send_command(
411  cut_take_printf(
412  "select Shops "
413  "--sortby '+_score, +name' "
414  "--output_columns 'name, _score' "
415  "--filter '" \
416  "geo_in_circle(location, \"%s\", %d) && " \
417  "tags @ \"たいやき\" &! tags @ \"白\"' "
418  "--scorer '_score=geo_distance(location, \"%s\")'",
419  grn_test_location_string(asagaya_latitude, asagaya_longitude),
420  distance,
421  grn_test_location_string(asagaya_latitude, asagaya_longitude))));
422 }
423 
424 void
425 test_drilldown(void)
426 {
427  gdouble yurakucho_latitude = 35.67487;
428  gdouble yurakucho_longitude = 139.76352;
429  gint distance = 10 * 1000;
430 
431  cut_assert_equal_string(
432  "[[[23],"
433  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
434  "[\"たい焼き鉄次 大丸東京店\",810],"
435  "[\"たいやき神田達磨 八重洲店\",970],"
436  "[\"にしみや 甘味処\",1056],"
437  "[\"築地 さのきや\",1186],"
438  "[\"しげ田\",1530],"
439  "[\"柳屋 たい焼き\",2179],"
440  "[\"尾長屋 錦糸町店\",5007],"
441  "[\"根津のたいやき\",5036],"
442  "[\"横浜 くりこ庵 浅草店\",5098],"
443  "[\"たい焼き写楽\",5457]],"
444  "[[7],"
445  "[[\"_key\",\"ShortText\"],"
446  "[\"name\",\"ShortText\"],"
447  "[\"_nsubrecs\",\"Int32\"]],"
448  "[\"おでん\",\"\",1],"
449  "[\"たいやき\",\"\",23],"
450  "[\"カレー\",\"\",1],"
451  "[\"マグロ\",\"\",1],"
452  "[\"和菓子\",\"\",1],"
453  "[\"天然\",\"\",4],"
454  "[\"白\",\"\",1]],"
455  "[[2],"
456  "[[\"_key\",\"ShortText\"],"
457  "[\"name\",\"ShortText\"],"
458  "[\"_nsubrecs\",\"Int32\"]],"
459  "[\"category0001\",\"和食\",1],"
460  "[\"category0003\",\"おやつ\",1]],"
461  "[[3],"
462  "[[\"_key\",\"ShortText\"],"
463  "[\"name\",\"ShortText\"],"
464  "[\"_nsubrecs\",\"Int32\"]],"
465  "[\"area0002\",\"東京都中央区\",3],"
466  "[\"area0005\",\"東京都文京区\",1],"
467  "[\"area0013\",\"東京都渋谷区\",1]]"
468  "]",
469  send_command(
470  cut_take_printf(
471  "select Shops "
472  "--sortby '+_score, +name' "
473  "--output_columns 'name, _score' "
474  "--filter 'geo_in_circle(location, \"%s\", %d) && tags @ \"たいやき\"' "
475  "--scorer '_score=geo_distance2(location, \"%s\")' "
476  "--drilldown 'tags categories area' "
477  "--drilldown_output_columns '_key, name, _nsubrecs' "
478  "--drilldown_sortby '_key'",
479  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
480  distance,
481  grn_test_location_string(yurakucho_latitude, yurakucho_longitude))));
482 }
483 
484 void
486 {
487  gdouble yurakucho_latitude = 35.67487;
488  gdouble yurakucho_longitude = 139.76352;
489  gint distance = 10 * 1000;
490 
491  assert_send_commands("delete Areas area0002");
492  assert_send_commands("delete Areas area0005");
493  cut_assert_equal_string(
494  "[[[23],"
495  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
496  "[\"たい焼き鉄次 大丸東京店\",811],"
497  "[\"たいやき神田達磨 八重洲店\",972],"
498  "[\"にしみや 甘味処\",1060],"
499  "[\"築地 さのきや\",1187],"
500  "[\"しげ田\",1537],"
501  "[\"柳屋 たい焼き\",2186],"
502  "[\"尾長屋 錦糸町店\",5024],"
503  "[\"根津のたいやき\",5036],"
504  "[\"横浜 くりこ庵 浅草店\",5106],"
505  "[\"たい焼き写楽\",5464]],"
506  "[[1],"
507  "[[\"_key\",\"ShortText\"],"
508  "[\"name\",\"ShortText\"],"
509  "[\"_nsubrecs\",\"Int32\"]],"
510  "[\"area0013\",\"東京都渋谷区\",1]]"
511  "]",
512  send_command(
513  cut_take_printf(
514  "select Shops "
515  "--sortby '+_score, +name' "
516  "--output_columns 'name, _score' "
517  "--filter 'geo_in_circle(location, \"%s\", %d) && tags @ \"たいやき\"' "
518  "--scorer '_score=geo_distance3(location, \"%s\")' "
519  "--drilldown 'area' "
520  "--drilldown_output_columns '_key, name, _nsubrecs' "
521  "--drilldown_sortby '_key'",
522  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
523  distance,
524  grn_test_location_string(yurakucho_latitude, yurakucho_longitude))));
525 }
526 
527 void
529 {
530  gdouble yurakucho_latitude = 35.67487;
531  gdouble yurakucho_longitude = 139.76352;
532  gint distance = 10 * 1000;
533 
534  cut_assert_equal_string(
535  "[[[23],"
536  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
537  "[\"たいやき神田達磨 八重洲店\",10031],"
538  "[\"たい焼き鉄次 大丸東京店\",9191],"
539  "[\"にしみや 甘味処\",8945],"
540  "[\"築地 さのきや\",8815],"
541  "[\"しげ田\",8471],"
542  "[\"柳屋 たい焼き\",7822],"
543  "[\"根津のたいやき\",5965],"
544  "[\"たいやきひいらぎ\",5428],"
545  "[\"尾長屋 錦糸町店\",4994],"
546  "[\"横浜 くりこ庵 浅草店\",4903]],"
547  "[[7],"
548  "[[\"_key\",\"ShortText\"],[\"_nsubrecs\",\"Int32\"]],"
549  "[\"たいやき\",23],"
550  "[\"天然\",4],"
551  "[\"カレー\",1],"
552  "[\"白\",1],"
553  "[\"マグロ\",1],"
554  "[\"和菓子\",1],"
555  "[\"おでん\",1]"
556  "]]",
557  send_command(
558  cut_take_printf(
559  "select Shops "
560  "--sortby '-_score, +name' "
561  "--output_columns 'name, _score' "
562  "--match_columns 'name * 1000 || tags * 10000' "
563  "--query たいやき "
564  "--filter 'geo_in_circle(location, \"%s\", %d)' "
565  "--scorer '_score -= geo_distance(location, \"%s\")' "
566  "--drilldown_output_columns '_key, _nsubrecs' "
567  "--drilldown_sortby '-_nsubrecs' "
568  "--drilldown 'tags' ",
569  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
570  distance,
571  grn_test_location_string(yurakucho_latitude, yurakucho_longitude))));
572 }
573 
574 void
576 {
577  gdouble yurakucho_latitude = 35.67487;
578  gdouble yurakucho_longitude = 139.76352;
579  gint distance = 3 * 1000;
580 
581  cut_assert_equal_string(
582  "[[[1],"
583  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
584  "[\"たいやき神田達磨 八重洲店\",10032]"
585  "]]",
586  send_command(
587  cut_take_printf(
588  "select Shops "
589  "--sortby '-_score, +name' "
590  "--output_columns 'name, _score' "
591  "--match_columns 'name * 1000 || tags * 10000' "
592  "--query たいやき "
593  "--filter '"
594  " (geo_in_circle(location1, \"%s\", %d) ||"
595  " geo_in_circle(location2, \"%s\", %d) ||"
596  " geo_in_circle(location3, \"%s\", %d))' "
597  "--scorer '_score = _score - geo_distance(location, \"%s\")'",
598  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
599  distance,
600  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
601  distance,
602  grn_test_location_string(yurakucho_latitude, yurakucho_longitude),
603  distance,
604  grn_test_location_string(yurakucho_latitude, yurakucho_longitude))));
605 }
606 
607 void
609 {
610  cut_assert_equal_string(
611  "[[[16],"
612  "[[\"name\",\"ShortText\"]],"
613  "[\"おめで鯛焼き本舗錦糸町東急店\"],"
614  "[\"そばたいやき空\"],"
615  "[\"たいやきひいらぎ\"]"
616  "]]",
617  send_command(
618  "select Shops "
619  "--sortby '+name' "
620  "--output_columns 'name' "
621  "--limit 3 "
622  "--match_columns name "
623  "--query たいやき "
624  "--query_expansion Synonyms.words"));
625 }