]> git.notmuchmail.org Git - notmuch/blob - test/T610-message-property.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T610-message-property.sh
1 #!/usr/bin/env bash
2 test_description="message property API"
3
4 . $(dirname "$0")/test-lib.sh || exit 1
5
6 add_email_corpus
7
8 cat <<EOF > c_head
9 #include <notmuch-test.h>
10
11 void print_properties (notmuch_message_t *message, const char *prefix, notmuch_bool_t exact) {
12     notmuch_message_properties_t *list;
13     for (list = notmuch_message_get_properties (message, prefix, exact);
14          notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
15        printf("%s = %s\n", notmuch_message_properties_key(list), notmuch_message_properties_value(list));
16     }
17     notmuch_message_properties_destroy (list);
18 }
19
20 int main (int argc, char** argv)
21 {
22    notmuch_database_t *db;
23    notmuch_message_t *message = NULL;
24    const char *val;
25    notmuch_status_t stat;
26    char* msg = NULL;
27
28    EXPECT0(notmuch_database_open_with_config (argv[1],
29                                               NOTMUCH_DATABASE_MODE_READ_WRITE,
30                                               "", NULL, &db, &msg));
31    if (msg) fputs (msg, stderr);
32    EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
33    if (message == NULL) {
34         fprintf (stderr, "unable to find message");
35         exit (1);
36    }
37 EOF
38
39 cat <<EOF > c_tail
40    EXPECT0(notmuch_database_destroy(db));
41 }
42 EOF
43
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
46 {
47    EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue1"));
48    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
49    printf("testkey1[1] = %s\n", val);
50    EXPECT0(notmuch_message_add_property (message, "testkey2", "this value has spaces and = sign"));
51    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
52    printf("testkey1[2] = %s\n", val);
53    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
54
55    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56    printf("testkey2 = %s\n", val);
57
58    /* Add second value for key */
59    EXPECT0(notmuch_message_add_property (message, "testkey2", "zztestvalue3"));
60    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
61    printf("testkey2 = %s\n", val);
62
63    /* remove first value for key */
64    EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
65    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
66    printf("testkey2 = %s\n", val);
67
68    /* remove non-existent value for key */
69    EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
70    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
71    printf("testkey2 = %s\n", val);
72
73    /* remove only value for key */
74    EXPECT0(notmuch_message_remove_property (message, "testkey2", "zztestvalue3"));
75    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
76    printf("testkey2 = %s\n", val == NULL ? "NULL" : val);
77 }
78 EOF
79 cat <<'EOF' >EXPECTED
80 == stdout ==
81 testkey1[1] = testvalue1
82 testkey1[2] = testvalue1
83 testkey2 = this value has spaces and = sign
84 testkey2 = this value has spaces and = sign
85 testkey2 = zztestvalue3
86 testkey2 = zztestvalue3
87 testkey2 = NULL
88 == stderr ==
89 EOF
90 test_expect_equal_file EXPECTED OUTPUT
91
92 test_begin_subtest "testing string map binary search (via message properties)"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94 {
95    char *keys[] = {"a", "b", "c", "d", "e", NULL};
96    for (int i=0; keys[i]; i++)
97        EXPECT0(notmuch_message_add_property (message, keys[i], keys[i]));
98
99    for (int i=0; keys[i]; i++) {
100       EXPECT0(notmuch_message_get_property (message, keys[i], &val));
101       printf("%s = %s\n", keys[i], val);
102    }
103
104    for (int i=0; keys[i]; i++) {
105       EXPECT0(notmuch_message_remove_property (message, keys[i], keys[i]));
106       EXPECT0(notmuch_message_get_property (message, keys[i], &val));
107       printf("%s = %s\n", keys[i], val == NULL ? "NULL" : val);
108    }
109 }
110 EOF
111 cat <<EOF > EXPECTED
112 == stdout ==
113 a = a
114 b = b
115 c = c
116 d = d
117 e = e
118 a = NULL
119 b = NULL
120 c = NULL
121 d = NULL
122 e = NULL
123 == stderr ==
124 EOF
125 test_expect_equal_file EXPECTED OUTPUT
126
127 test_begin_subtest "notmuch_message_get_properties: empty list"
128 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
129 {
130    notmuch_message_properties_t *list;
131    list = notmuch_message_get_properties (message, "nonexistent", TRUE);
132    printf("valid = %d\n", notmuch_message_properties_valid (list));
133    notmuch_message_properties_destroy (list);
134 }
135 EOF
136 cat <<'EOF' >EXPECTED
137 == stdout ==
138 valid = 0
139 == stderr ==
140 EOF
141 test_expect_equal_file EXPECTED OUTPUT
142
143 test_begin_subtest "notmuch_message_properties: one value"
144 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
145 print_properties (message, "testkey1", TRUE);
146 EOF
147 cat <<'EOF' >EXPECTED
148 == stdout ==
149 testkey1 = testvalue1
150 == stderr ==
151 EOF
152 test_expect_equal_file EXPECTED OUTPUT
153
154 test_begin_subtest "notmuch_message_remove_all_properties"
155 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
156 EXPECT0(notmuch_message_remove_all_properties (message, NULL));
157 EXPECT0(notmuch_database_destroy(db));
158 EXPECT0(notmuch_database_open_with_config (argv[1],
159                                            NOTMUCH_DATABASE_MODE_READ_WRITE,
160                                            "", NULL, &db, &msg));
161 if (msg) fputs (msg, stderr);
162 EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
163 if (message == NULL) {
164   fprintf (stderr, "unable to find message");
165   exit (1);
166 }
167 print_properties (message, "", FALSE);
168 EOF
169 cat <<'EOF' >EXPECTED
170 == stdout ==
171 == stderr ==
172 EOF
173 test_expect_equal_file EXPECTED OUTPUT
174
175 test_begin_subtest "notmuch_message_properties: multiple values"
176 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
177 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
178 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
179 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
180 print_properties (message, "testkey1", TRUE);
181 EOF
182 cat <<'EOF' >EXPECTED
183 == stdout ==
184 testkey1 = alice
185 testkey1 = bob
186 testkey1 = testvalue2
187 == stderr ==
188 EOF
189 test_expect_equal_file EXPECTED OUTPUT
190
191 test_begin_subtest "notmuch_message_properties: prefix"
192 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
193 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
194 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
195 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
196 print_properties (message, "testkey", FALSE);
197 EOF
198 cat <<'EOF' >EXPECTED
199 == stdout ==
200 testkey1 = alice
201 testkey1 = bob
202 testkey1 = testvalue2
203 testkey3 = alice3
204 testkey3 = bob3
205 testkey3 = testvalue3
206 == stderr ==
207 EOF
208 test_expect_equal_file EXPECTED OUTPUT
209
210 test_begin_subtest "notmuch_message_properties: modify during iteration"
211 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
212 {
213     const char *keys[1000] = {NULL};
214     const char *vals[1000] = {NULL};
215     notmuch_message_properties_t *properties;
216     int i;
217
218     for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
219          notmuch_message_properties_valid (properties);
220          notmuch_message_properties_move_to_next (properties), i++)
221     {
222         const char *key, *value;
223
224         keys[i]=talloc_strdup(message,
225                     notmuch_message_properties_key (properties));
226         vals[i]=talloc_strdup(message,
227                     notmuch_message_properties_value (properties));
228
229         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
230     }
231
232     print_properties (message, "", FALSE);
233
234     for (i = 0; keys[i] && vals[i]; i++) {
235         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
236     }
237 }
238 EOF
239 cat <<'EOF' >EXPECTED
240 == stdout ==
241 == stderr ==
242 EOF
243 test_expect_equal_file EXPECTED OUTPUT
244
245 test_begin_subtest "dump message properties"
246 cat <<EOF > PROPERTIES
247 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
248 EOF
249 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
250 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
251 EOF
252 notmuch dump | grep '^#=' > OUTPUT
253 test_expect_equal_file PROPERTIES OUTPUT
254
255 test_begin_subtest "dump _only_ message properties"
256 cat <<EOF > EXPECTED
257 #notmuch-dump batch-tag:3 properties
258 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
259 EOF
260 notmuch dump --include=properties > OUTPUT
261 test_expect_equal_file EXPECTED OUTPUT
262
263
264 test_begin_subtest "restore missing message property (single line)"
265 notmuch dump | grep '^#=' > BEFORE1
266 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
267 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
268 EOF
269 notmuch restore < BEFORE1
270 notmuch dump | grep '^#=' > OUTPUT
271 test_expect_equal_file PROPERTIES OUTPUT
272
273
274 test_begin_subtest "restore missing message property (full dump)"
275 notmuch dump > BEFORE2
276 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
277 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
278 EOF
279 notmuch restore < BEFORE2
280 notmuch dump | grep '^#=' > OUTPUT
281 test_expect_equal_file PROPERTIES OUTPUT
282
283 test_begin_subtest "restore clear extra message property"
284 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
285 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
286 EOF
287 notmuch restore < BEFORE2
288 notmuch dump | grep '^#=' > OUTPUT
289 test_expect_equal_file PROPERTIES OUTPUT
290
291 test_begin_subtest "test 'property:' queries: empty"
292 notmuch search property:testkey1=charles > OUTPUT
293 test_expect_equal_file /dev/null OUTPUT
294
295 test_begin_subtest "test 'property:' queries: single message"
296 notmuch search --output=messages property:testkey1=alice > OUTPUT
297 cat <<EOF >EXPECTED
298 id:4EFC743A.3060609@april.org
299 EOF
300 test_expect_equal_file EXPECTED OUTPUT
301
302 test_begin_subtest "msg.get_property (python)"
303 test_python <<'EOF'
304 import notmuch
305 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
306 msg = db.find_message("4EFC743A.3060609@april.org")
307 print("testkey1 = {0}".format(msg.get_property("testkey1")))
308 print("testkey3 = {0}".format(msg.get_property("testkey3")))
309 EOF
310 cat <<'EOF' > EXPECTED
311 testkey1 = alice
312 testkey3 = alice3
313 EOF
314 test_expect_equal_file EXPECTED OUTPUT
315
316 test_begin_subtest "msg.get_properties (python)"
317 test_python <<'EOF'
318 import notmuch
319 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
320 msg = db.find_message("4EFC743A.3060609@april.org")
321 for (key,val) in msg.get_properties("testkey1"):
322         print("{0} = {1}".format(key,val))
323 EOF
324 cat <<'EOF' > EXPECTED
325 testkey1 = alice
326 testkey1 = bob
327 testkey1 = testvalue2
328 EOF
329 test_expect_equal_file EXPECTED OUTPUT
330
331 test_begin_subtest "msg.get_properties (python, prefix)"
332 test_python <<'EOF'
333 import notmuch
334 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
335 msg = db.find_message("4EFC743A.3060609@april.org")
336 for (key,val) in msg.get_properties("testkey"):
337         print("{0} = {1}".format(key,val))
338 EOF
339 cat <<'EOF' > EXPECTED
340 testkey1 = alice
341 testkey1 = bob
342 testkey1 = testvalue2
343 testkey3 = alice3
344 testkey3 = bob3
345 testkey3 = testvalue3
346 EOF
347 test_expect_equal_file EXPECTED OUTPUT
348
349 test_begin_subtest "msg.get_properties (python, exact)"
350 test_python <<'EOF'
351 import notmuch
352 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
353 msg = db.find_message("4EFC743A.3060609@april.org")
354 for (key,val) in msg.get_properties("testkey",True):
355         print("{0} = {1}".format(key,val))
356 EOF
357 test_expect_equal_file /dev/null OUTPUT
358
359 test_begin_subtest "notmuch_message_remove_all_properties_with_prefix"
360 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
361 EXPECT0(notmuch_message_remove_all_properties_with_prefix (message, "testkey3"));
362 print_properties (message, "", FALSE);
363 EOF
364 cat <<'EOF' >EXPECTED
365 == stdout ==
366 fancy key with áccènts = import value with =
367 testkey1 = alice
368 testkey1 = bob
369 testkey1 = testvalue2
370 == stderr ==
371 EOF
372 test_expect_equal_file EXPECTED OUTPUT
373
374 test_begin_subtest "edit property on removed message without uncaught exception"
375 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
376 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
377 stat = notmuch_message_remove_property (message, "example", "example");
378 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
379     fprintf (stderr, "unable to remove properties on message");
380 EOF
381 cat <<'EOF' >EXPECTED
382 == stdout ==
383 == stderr ==
384 unable to remove properties on message
385 EOF
386 test_expect_equal_file EXPECTED OUTPUT
387
388 add_email_corpus
389
390 test_begin_subtest "remove all properties on removed message without uncaught exception"
391 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
392 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
393 stat = notmuch_message_remove_all_properties_with_prefix (message, "");
394 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
395     fprintf (stderr, "unable to remove properties on message");
396 EOF
397 cat <<'EOF' >EXPECTED
398 == stdout ==
399 == stderr ==
400 unable to remove properties on message
401 EOF
402 test_expect_equal_file EXPECTED OUTPUT
403
404 test_done