lib: add support for named queries
[notmuch] / test / T600-named-queries.sh
1 #!/usr/bin/env bash
2 test_description='named queries'
3 . ./test-lib.sh || exit 1
4
5 QUERYSTR="date:2009-11-18..2009-11-18 and tag:unread"
6
7 test_expect_code 1 "error adding named query before initializing DB" \
8                  "notmuch config set query.test \"$QUERYSTR\""
9
10 add_email_corpus
11
12 test_expect_success "adding named query" \
13                     "notmuch config set query.test \"$QUERYSTR\""
14
15 QUERYSTR2="query:test and subject:Maildir"
16 test_expect_success "adding nested named query" \
17                     "notmuch config set query.test2 \"$QUERYSTR2\""
18
19 test_begin_subtest "retrieve named query"
20 output=$(notmuch config get query.test)
21 test_expect_equal "$QUERYSTR" "$output"
22
23 test_begin_subtest "List all queries"
24 notmuch config list | grep ^query | notmuch_config_sanitize > OUTPUT
25 cat <<EOF > EXPECTED
26 query.test=date:2009-11-18..2009-11-18 and tag:unread
27 query.test2=query:test and subject:Maildir
28 EOF
29 test_expect_equal_file EXPECTED OUTPUT
30
31 test_begin_subtest "dump named queries"
32 notmuch dump | grep '^#@' > OUTPUT
33 cat<<EOF > QUERIES.BEFORE
34 #@ query.test date%3a2009-11-18..2009-11-18%20and%20tag%3aunread
35 #@ query.test2 query%3atest%20and%20subject%3aMaildir
36 EOF
37 test_expect_equal_file QUERIES.BEFORE OUTPUT
38
39 test_begin_subtest "delete named queries"
40 notmuch dump > BEFORE
41 notmuch config set query.test
42 notmuch dump | grep '^#@' > OUTPUT
43 cat<<EOF > EXPECTED
44 #@ query.test2 query%3atest%20and%20subject%3aMaildir
45 EOF
46 test_expect_equal_file EXPECTED OUTPUT
47
48 test_begin_subtest "restore named queries"
49 notmuch restore < BEFORE
50 notmuch dump | grep '^#@' > OUTPUT
51 test_expect_equal_file QUERIES.BEFORE OUTPUT
52
53 if [ $NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR -eq 1 ]; then
54     test_begin_subtest "search named query"
55     notmuch search query:test > OUTPUT
56     notmuch search $QUERYSTR > EXPECTED
57     test_expect_equal_file EXPECTED OUTPUT
58
59     test_begin_subtest "search named query with other terms"
60     notmuch search query:test and subject:Maildir > OUTPUT
61     notmuch search $QUERYSTR and subject:Maildir > EXPECTED
62     test_expect_equal_file EXPECTED OUTPUT
63
64     test_begin_subtest "search nested named query"
65     notmuch search query:test2 > OUTPUT
66     notmuch search $QUERYSTR2 > EXPECTED
67     test_expect_equal_file EXPECTED OUTPUT
68 fi
69
70 test_done