]> git.notmuchmail.org Git - notmuch/blob - test/T390-python.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T390-python.sh
1 #!/usr/bin/env bash
2 test_description="python bindings"
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 test_require_external_prereq ${NOTMUCH_PYTHON}
6
7 if [ -n "${NOTMUCH_TEST_INSTALLED-}" ]; then
8     test_done
9 fi
10
11 add_email_corpus
12 add_gnupg_home
13
14 test_begin_subtest "compare thread ids"
15 test_python <<EOF
16 import notmuch
17 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
18 q_new = notmuch.Query(db, 'tag:inbox')
19 q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
20 for t in q_new.search_threads():
21     print (t.get_thread_id())
22 EOF
23 notmuch search --sort=oldest-first --output=threads tag:inbox | sed s/^thread:// > EXPECTED
24 test_expect_equal_file EXPECTED OUTPUT
25
26 test_begin_subtest "compare message ids"
27 test_python <<EOF
28 import notmuch
29 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
30 q_new = notmuch.Query(db, 'tag:inbox')
31 q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
32 for m in q_new.search_messages():
33     print (m.get_message_id())
34 EOF
35 notmuch search --sort=oldest-first --output=messages tag:inbox | sed s/^id:// > EXPECTED
36 test_expect_equal_file EXPECTED OUTPUT
37
38 test_begin_subtest "get non-existent file"
39 test_python <<EOF
40 import notmuch
41 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
42 print (db.find_message_by_filename("i-dont-exist"))
43 EOF
44 test_expect_equal "$(cat OUTPUT)" "None"
45
46 test_begin_subtest "get revision"
47 test_python ${MAIL_DIR} <<'EOF'
48 import notmuch
49 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
50 (revision, uuid) = db.get_revision()
51 print ("%s\t%lu" % (uuid, revision))
52 EOF
53 notmuch_uuid_sanitize < OUTPUT > CLEAN
54 cat <<'EOF' >EXPECTED
55 UUID    53
56 EOF
57 test_expect_equal_file EXPECTED CLEAN
58
59 grep '^[0-9a-f]' OUTPUT > INITIAL_OUTPUT
60
61 test_begin_subtest "output of count matches test code"
62 notmuch count --lastmod '*' | cut -f2-3 > OUTPUT
63 test_expect_equal_file INITIAL_OUTPUT OUTPUT
64 add_message '[content-type]="text/plain; charset=iso-8859-2"' \
65             '[content-transfer-encoding]=8bit' \
66             '[subject]="ISO-8859-2 encoded message"' \
67             "[body]=$'Czech word tu\350\362\341\350\350\355 means pinguin\'s.'" # ISO-8859-2 characters are generated by shell's escape sequences
68 test_begin_subtest "Add ISO-8859-2 encoded message, call get_message_parts"
69 test_python <<EOF
70 import notmuch
71 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
72 q_new = notmuch.Query(db, 'ISO-8859-2 encoded message')
73 for m in q_new.search_messages():
74     for mp in m.get_message_parts():
75       continue
76     print(m.get_message_id())
77 EOF
78
79 notmuch search --sort=oldest-first --output=messages "tučňáččí" | sed s/^id:// > EXPECTED
80 test_expect_equal_file EXPECTED OUTPUT
81
82 # TODO currently these tests for setting and getting config values are
83 # somewhat interdependent.  This is because the config values stored in the
84 # database are not cleaned up after each test, so they remain there for the
85 # next test.  The ./README file states that this can happen so it seems kind
86 # of ok.
87
88 test_begin_subtest "set and get config values"
89 test_python <<'EOF'
90 import notmuch
91 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
92 db.set_config('testkey1', 'testvalue1')
93 db.set_config('testkey2', 'testvalue2')
94 v1 = db.get_config('testkey1')
95 v2 = db.get_config('testkey2')
96 print('testkey1 = ' + v1)
97 print('testkey2 = ' + v2)
98 EOF
99 cat <<'EOF' >EXPECTED
100 testkey1 = testvalue1
101 testkey2 = testvalue2
102 EOF
103 test_expect_equal_file EXPECTED OUTPUT
104
105 test_begin_subtest "get_configs with no match returns empty generator"
106 test_python <<'EOF'
107 import notmuch
108 db = notmuch.Database()
109 v = db.get_configs('nonexistent')
110 print(list(v) == [])
111 EOF
112 test_expect_equal "$(cat OUTPUT)" "True"
113
114 test_begin_subtest "get_configs with no arguments returns all pairs"
115 test_python <<'EOF'
116 import notmuch
117 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
118 db.set_config("zzzafter", "afterval")
119 db.set_config("aaabefore", "beforeval")
120 v = db.get_configs()
121 for index, keyval in enumerate(v):
122     key, val = keyval
123     print('{}: {} => {}'.format(index, key, val))
124 EOF
125 cat <<'EOF' >EXPECTED
126 0: aaabefore => beforeval
127 1: testkey1 => testvalue1
128 2: testkey2 => testvalue2
129 3: zzzafter => afterval
130 EOF
131 test_expect_equal_file EXPECTED OUTPUT
132
133 test_begin_subtest "get_configs prefix is used to match keys"
134 test_python <<'EOF'
135 import notmuch
136 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
137 db.set_config('testkey1', 'testvalue1')
138 db.set_config('testkey2', 'testvalue2')
139 v = db.get_configs('testkey')
140 for index, keyval in enumerate(v):
141     key, val = keyval
142     print('{}: {} => {}'.format(index, key, val))
143 EOF
144 cat <<'EOF' >EXPECTED
145 0: testkey1 => testvalue1
146 1: testkey2 => testvalue2
147 EOF
148 test_expect_equal_file EXPECTED OUTPUT
149
150 test_begin_subtest "set_config with no value will unset config entries"
151 test_python <<'EOF'
152 import notmuch
153 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
154 db.set_config('testkey1', '')
155 db.set_config('testkey2', '')
156 db.set_config("zzzafter", '')
157 db.set_config("aaabefore", '')
158 v = db.get_configs()
159 print(list(v) == [])
160 EOF
161 test_expect_equal "$(cat OUTPUT)" "True"
162
163 mkdir -p "${MAIL_DIR}/cur"
164 fname="${MAIL_DIR}/cur/simplemsg.eml"
165 cat <<EOF > "$fname"
166 From: test_suite@notmuchmail.org
167 To: test_suite@notmuchmail.org
168 Subject: encrypted message
169 Date: Sat, 01 Jan 2000 12:00:00 +0000
170 Message-ID: <simplemsg@crypto.notmuchmail.org>
171 MIME-Version: 1.0
172 Content-Type: multipart/encrypted; boundary="=-=-=";
173         protocol="application/pgp-encrypted"
174
175 --=-=-=
176 Content-Type: application/pgp-encrypted
177
178 Version: 1
179
180 --=-=-=
181 Content-Type: application/octet-stream
182
183 $(printf 'Content-Type: text/plain\n\nThis is the sekrit message\n' | gpg --no-tty --batch --quiet --trust-model=always --encrypt --armor --recipient test_suite@notmuchmail.org)
184 --=-=-=--
185 EOF
186
187 test_begin_subtest "index message with decryption"
188 test_python <<EOF
189 import notmuch
190 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
191 (m, status) = db.index_file('$fname', decrypt_policy=notmuch.Database.DECRYPTION_POLICY.TRUE)
192 if status == notmuch.errors.STATUS.DUPLICATE_MESSAGE_ID:
193    print("got duplicate message")
194 q_new = notmuch.Query(db, 'sekrit')
195 for m in q_new.search_messages():
196     print(m.get_filename())
197 EOF
198 echo "$fname" > EXPECTED
199 test_expect_equal_file EXPECTED OUTPUT
200
201 test_done