File: test-query.cc

package info (click to toggle)
maildir-utils 1.12.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,996 kB
  • sloc: cpp: 56,429; lisp: 11,218; sh: 895; ansic: 889; makefile: 126; pascal: 12; python: 4
file content (99 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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.
**
*/
#include <config.h>

#include <vector>
#include <glib.h>

#include <iostream>
#include <sstream>
#include <unistd.h>

#include "mu-store.hh"
#include "mu-query.hh"
#include "utils/mu-result.hh"
#include "utils/mu-utils.hh"
#include "utils/mu-test-utils.hh"

using namespace Mu;

static void
test_query()
{
	allow_warnings();
	TempDir temp_dir;

	auto store = Store::make_new(temp_dir.path(), std::string{MU_TESTMAILDIR});
	assert_valid_result(store);

	auto&& idx{store->indexer()};
	g_assert_true(idx.start(Indexer::Config{}));
	while (idx.is_running()) {
		g_usleep(1000);
	}

	auto dump_matches = [](const QueryResults& res) {
		size_t n{};
		for (auto&& item : res) {
			if (g_test_verbose()) {
				std::cout << item.query_match() << '\n';
				mu_debug("{:02d} {} {}",
					 ++n,
					 item.path().value_or("<none>"),
					 item.message_id().value_or("<none>"));
			}
		}
	};

	g_assert_cmpuint(store->size(), ==, 19);

	{
		const auto res = store->run_query("", {}, QueryFlags::None);
		g_assert_true(!!res);
		g_assert_cmpuint(res->size(), ==, 19);
		dump_matches(*res);

		g_assert_cmpuint(store->count_query(""), ==, 19);

	}

	{
		const auto res = store->run_query("", Field::Id::Path, QueryFlags::None, 11);
		g_assert_true(!!res);
		g_assert_cmpuint(res->size(), ==, 11);
		dump_matches(*res);
	}
}

int
main(int argc, char* argv[]) try {

	mu_test_init(&argc, &argv);

	g_test_add_func("/query", test_query);

	return g_test_run();

} catch (const std::runtime_error& re) {
	std::cerr << re.what() << "\n";
	return 1;
} catch (...) {
	std::cerr << "caught exception\n";
	return 1;
}