2010-01-31 13:14:19 +01:00
|
|
|
/*
|
2010-09-08 23:09:33 +02:00
|
|
|
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-01-31 13:14:19 +01:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-02-02 20:48:41 +01:00
|
|
|
#include "test-mu-common.h"
|
2010-08-25 20:46:16 +02:00
|
|
|
#include "src/mu-query.h"
|
2010-01-31 13:14:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
fill_database (void)
|
|
|
|
{
|
|
|
|
gchar *cmdline, *tmpdir, *xpath;
|
|
|
|
|
2010-02-02 20:48:41 +01:00
|
|
|
tmpdir = test_mu_common_get_random_tmpdir();
|
|
|
|
cmdline = g_strdup_printf ("%s index --muhome=%s --maildir=%s"
|
2010-01-31 13:14:19 +01:00
|
|
|
" --quiet",
|
2010-02-02 20:48:41 +01:00
|
|
|
MU_PROGRAM, tmpdir, MU_TESTMAILDIR);
|
2010-09-08 23:09:33 +02:00
|
|
|
|
2010-01-31 13:14:19 +01:00
|
|
|
g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL, NULL, NULL));
|
|
|
|
g_free (cmdline);
|
|
|
|
|
|
|
|
xpath= g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, "xapian");
|
|
|
|
g_free (tmpdir);
|
|
|
|
|
|
|
|
return xpath;
|
|
|
|
}
|
|
|
|
|
2010-09-05 12:01:33 +02:00
|
|
|
|
|
|
|
/* note: this also *moves the iter* */
|
2010-03-02 16:27:08 +01:00
|
|
|
static guint
|
2010-09-08 23:09:33 +02:00
|
|
|
run_and_count_matches (const char *xpath, const char *query)
|
2010-03-02 16:27:08 +01:00
|
|
|
{
|
2010-09-08 23:09:33 +02:00
|
|
|
MuQuery *mquery;
|
|
|
|
MuMsgIter *iter;
|
2010-03-02 16:27:08 +01:00
|
|
|
guint count;
|
2010-09-08 23:09:33 +02:00
|
|
|
|
|
|
|
mquery = mu_query_new (xpath);
|
|
|
|
g_assert (query);
|
2010-03-02 16:27:08 +01:00
|
|
|
|
2010-09-08 23:09:33 +02:00
|
|
|
iter = mu_query_run (mquery, query, NULL, FALSE, 1);
|
|
|
|
g_assert (iter);
|
2010-09-13 20:27:33 +02:00
|
|
|
/* { */
|
|
|
|
/* gchar *prep = mu_query_preprocess (query); */
|
|
|
|
/* g_print ("\n%s:\n(1)'%s'\n(2)'%s'\n", xpath, query, prep); */
|
|
|
|
/* g_free (prep); */
|
|
|
|
/* } */
|
2010-09-12 15:33:18 +02:00
|
|
|
|
2010-09-08 23:09:33 +02:00
|
|
|
for (count = 0; !mu_msg_iter_is_done(iter);
|
|
|
|
mu_msg_iter_next(iter), ++count);
|
|
|
|
|
|
|
|
mu_msg_iter_destroy (iter);
|
|
|
|
mu_query_destroy (mquery);
|
|
|
|
|
2010-03-02 16:27:08 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 13:14:19 +01:00
|
|
|
typedef struct {
|
|
|
|
const char *query;
|
|
|
|
size_t count; /* expected number of matches */
|
|
|
|
} QResults;
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_query_01 (void)
|
|
|
|
{
|
|
|
|
gchar *xpath;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
QResults queries[] = {
|
|
|
|
{ "basic", 3 },
|
|
|
|
{ "question", 5 },
|
|
|
|
{ "thanks", 2 },
|
|
|
|
{ "html", 4 },
|
2010-09-12 15:33:18 +02:00
|
|
|
{ "subject:elisp", 1 },
|
2010-01-31 13:14:19 +01:00
|
|
|
{ "html AND contains", 1 },
|
2010-09-12 15:33:18 +02:00
|
|
|
{ "html and contains", 1 },
|
2010-09-04 13:37:19 +02:00
|
|
|
{ "from:pepernoot", 0 },
|
2010-09-12 15:33:18 +02:00
|
|
|
{ "foo:pepernoot", 0 },
|
2010-09-26 14:23:17 +02:00
|
|
|
{ "fünkÿ", 1 },
|
|
|
|
// { "funky", 1 }
|
2010-01-31 13:14:19 +01:00
|
|
|
};
|
|
|
|
xpath = fill_database ();
|
|
|
|
g_assert (xpath != NULL);
|
|
|
|
|
2010-09-08 23:09:33 +02:00
|
|
|
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
|
|
|
g_assert_cmpuint (run_and_count_matches (xpath, queries[i].query),
|
|
|
|
==, queries[i].count);
|
2010-01-31 13:14:19 +01:00
|
|
|
g_free (xpath);
|
|
|
|
}
|
|
|
|
|
2010-02-04 21:48:13 +01:00
|
|
|
static void
|
|
|
|
test_mu_query_02 (void)
|
|
|
|
{
|
|
|
|
const char* q;
|
|
|
|
gchar *xpath;
|
|
|
|
|
|
|
|
xpath = fill_database ();
|
2010-09-08 23:09:33 +02:00
|
|
|
g_assert (xpath);
|
2010-02-04 21:48:13 +01:00
|
|
|
|
2010-02-09 21:11:15 +01:00
|
|
|
q = "i:f7ccd24b0808061357t453f5962w8b61f9a453b684d0@mail.gmail.com";
|
2010-02-04 21:48:13 +01:00
|
|
|
|
2010-09-08 23:09:33 +02:00
|
|
|
g_assert_cmpuint (run_and_count_matches(xpath, q), ==, 1);
|
2010-02-04 21:48:13 +01:00
|
|
|
g_free (xpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-02 16:27:08 +01:00
|
|
|
static void
|
|
|
|
test_mu_query_03 (void)
|
|
|
|
{
|
|
|
|
gchar *xpath;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
QResults queries[] = {
|
2010-09-12 15:33:18 +02:00
|
|
|
{ "ploughed", 1},
|
2010-11-08 23:26:24 +01:00
|
|
|
{ "i:3BE9E6535E3029448670913581E7A1A20D852173@emss35m06.us.lmco.com", 1},
|
2010-09-12 15:33:18 +02:00
|
|
|
{ "s:Re:Learning LISP; Scheme vs elisp.", 1},
|
|
|
|
{ "subject:Re Learning LISP; Scheme vs elisp.", 1},
|
|
|
|
{ "t:help-gnu-emacs@gnu.org", 4},
|
|
|
|
{ "t:help-gnu-emacs", 0},
|
2010-03-02 16:27:08 +01:00
|
|
|
};
|
2010-09-12 15:33:18 +02:00
|
|
|
|
2010-03-02 16:27:08 +01:00
|
|
|
xpath = fill_database ();
|
2010-09-12 15:33:18 +02:00
|
|
|
g_assert (xpath != NULL);
|
|
|
|
|
2010-09-08 23:09:33 +02:00
|
|
|
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
|
|
|
g_assert_cmpuint (run_and_count_matches (xpath, queries[i].query),
|
|
|
|
==, queries[i].count);
|
2010-03-02 16:27:08 +01:00
|
|
|
g_free (xpath);
|
2010-09-12 15:33:18 +02:00
|
|
|
|
2010-03-02 16:27:08 +01:00
|
|
|
}
|
|
|
|
|
2010-02-04 21:48:13 +01:00
|
|
|
|
2010-09-05 12:01:33 +02:00
|
|
|
static void
|
|
|
|
test_mu_query_04 (void)
|
2010-09-12 15:33:18 +02:00
|
|
|
{
|
|
|
|
gchar *xpath;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
QResults queries[] = {
|
|
|
|
// { "frodo@example.com", 1},
|
|
|
|
{ "f:frodo@example.com", 1},
|
|
|
|
{ "f:Frodo Baggins", 1},
|
|
|
|
// { "bilbo@anotherexample.com", 1},
|
|
|
|
{ "t:bilbo@anotherexample.com", 1},
|
|
|
|
{ "t:bilbo", 1},
|
|
|
|
{ "f:bilbo", 0},
|
|
|
|
{ "baggins", 1}
|
|
|
|
};
|
|
|
|
|
|
|
|
xpath = fill_database ();
|
|
|
|
g_assert (xpath != NULL);
|
|
|
|
|
|
|
|
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
|
|
|
g_assert_cmpuint (run_and_count_matches (xpath, queries[i].query),
|
|
|
|
==, queries[i].count);
|
|
|
|
g_free (xpath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_query_05 (void)
|
2010-09-05 12:01:33 +02:00
|
|
|
{
|
|
|
|
MuQuery *query;
|
|
|
|
MuMsgIter *iter;
|
|
|
|
MuMsg *msg;
|
|
|
|
gchar *xpath;
|
|
|
|
|
|
|
|
xpath = fill_database ();
|
|
|
|
g_assert (xpath != NULL);
|
|
|
|
|
|
|
|
query = mu_query_new (xpath);
|
|
|
|
iter = mu_query_run (query, "fünkÿ", NULL, FALSE, 1);
|
|
|
|
msg = mu_msg_iter_get_msg (iter);
|
|
|
|
|
|
|
|
g_assert_cmpstr (mu_msg_get_subject(msg),==,
|
|
|
|
"Greetings from Lothlórien");
|
|
|
|
g_assert_cmpstr (mu_msg_get_summary(msg,5),==,
|
2010-09-11 10:19:58 +02:00
|
|
|
"Let's write some fünkÿ text using umlauts. Foo.");
|
2010-09-05 12:01:33 +02:00
|
|
|
|
|
|
|
mu_msg_destroy (msg);
|
|
|
|
mu_msg_iter_destroy (iter);
|
|
|
|
mu_query_destroy (query);
|
|
|
|
g_free (xpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 13:14:19 +01:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
2010-09-12 15:33:18 +02:00
|
|
|
|
2010-03-02 16:27:08 +01:00
|
|
|
g_test_add_func ("/mu-query/test-mu-query-01", test_mu_query_01);
|
2010-09-12 15:33:18 +02:00
|
|
|
g_test_add_func ("/mu-query/test-mu-query-02", test_mu_query_02);
|
|
|
|
g_test_add_func ("/mu-query/test-mu-query-03", test_mu_query_03);
|
2010-09-05 12:01:33 +02:00
|
|
|
g_test_add_func ("/mu-query/test-mu-query-04", test_mu_query_04);
|
2010-09-12 15:33:18 +02:00
|
|
|
g_test_add_func ("/mu-query/test-mu-query-05", test_mu_query_05);
|
2010-01-31 13:14:19 +01:00
|
|
|
|
|
|
|
g_log_set_handler (NULL,
|
|
|
|
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,
|
2010-09-26 14:23:17 +02:00
|
|
|
(GLogFunc)black_hole, NULL);
|
2010-01-31 13:14:19 +01:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|
2010-09-11 10:19:58 +02:00
|
|
|
|