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