]> git.cworth.org Git - notmuch/blob - test/T610-message-property.sh
test: reorganize tests and mark a few of them as broken
[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 print_properties (message, "", FALSE);
158 EOF
159 cat <<'EOF' >EXPECTED
160 == stdout ==
161 == stderr ==
162 EOF
163 test_expect_equal_file EXPECTED OUTPUT
164
165 test_begin_subtest "notmuch_message_properties: multiple values"
166 test_subtest_known_broken
167 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
168 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
169 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
170 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
171 print_properties (message, "testkey1", TRUE);
172 EOF
173 cat <<'EOF' >EXPECTED
174 == stdout ==
175 testkey1 = alice
176 testkey1 = bob
177 testkey1 = testvalue2
178 == stderr ==
179 EOF
180 test_expect_equal_file EXPECTED OUTPUT
181
182 test_begin_subtest "notmuch_message_properties: prefix"
183 test_subtest_known_broken
184 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
185 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
186 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
187 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
188 print_properties (message, "testkey", FALSE);
189 EOF
190 cat <<'EOF' >EXPECTED
191 == stdout ==
192 testkey1 = alice
193 testkey1 = bob
194 testkey1 = testvalue2
195 testkey3 = alice3
196 testkey3 = bob3
197 testkey3 = testvalue3
198 == stderr ==
199 EOF
200 test_expect_equal_file EXPECTED OUTPUT
201
202 test_begin_subtest "notmuch_message_properties: modify during iteration"
203 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
204 {
205     const char *keys[1000] = {NULL};
206     const char *vals[1000] = {NULL};
207     notmuch_message_properties_t *properties;
208     int i;
209
210     for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
211          notmuch_message_properties_valid (properties);
212          notmuch_message_properties_move_to_next (properties), i++)
213     {
214         const char *key, *value;
215
216         keys[i]=talloc_strdup(message,
217                     notmuch_message_properties_key (properties));
218         vals[i]=talloc_strdup(message,
219                     notmuch_message_properties_value (properties));
220
221         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
222     }
223
224     print_properties (message, "", FALSE);
225
226     for (i = 0; keys[i] && vals[i]; i++) {
227         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
228     }
229 }
230 EOF
231 cat <<'EOF' >EXPECTED
232 == stdout ==
233 == stderr ==
234 EOF
235 test_expect_equal_file EXPECTED OUTPUT
236
237 test_begin_subtest "dump message properties"
238 test_subtest_known_broken
239 cat <<EOF > PROPERTIES
240 #= 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
241 EOF
242 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
243 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
244 EOF
245 notmuch dump | grep '^#=' > OUTPUT
246 test_expect_equal_file PROPERTIES OUTPUT
247
248 test_begin_subtest "dump _only_ message properties"
249 test_subtest_known_broken
250 cat <<EOF > EXPECTED
251 #notmuch-dump batch-tag:3 properties
252 #= 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
253 EOF
254 notmuch dump --include=properties > OUTPUT
255 test_expect_equal_file EXPECTED OUTPUT
256
257
258 test_begin_subtest "restore missing message property (single line)"
259 test_subtest_known_broken
260 notmuch dump | grep '^#=' > BEFORE1
261 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
262 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
263 EOF
264 notmuch restore < BEFORE1
265 notmuch dump | grep '^#=' > OUTPUT
266 test_expect_equal_file PROPERTIES OUTPUT
267
268
269 test_begin_subtest "restore missing message property (full dump)"
270 test_subtest_known_broken
271 notmuch dump > BEFORE2
272 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
273 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
274 EOF
275 notmuch restore < BEFORE2
276 notmuch dump | grep '^#=' > OUTPUT
277 test_expect_equal_file PROPERTIES OUTPUT
278
279 test_begin_subtest "restore clear extra message property"
280 test_subtest_known_broken
281 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
282 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
283 EOF
284 notmuch restore < BEFORE2
285 notmuch dump | grep '^#=' > OUTPUT
286 test_expect_equal_file PROPERTIES OUTPUT
287
288 test_begin_subtest "test 'property:' queries: empty"
289 notmuch search property:testkey1=charles > OUTPUT
290 test_expect_equal_file /dev/null OUTPUT
291
292 test_begin_subtest "test 'property:' queries: single message"
293 notmuch search --output=messages property:testkey1=alice > OUTPUT
294 cat <<EOF >EXPECTED
295 id:4EFC743A.3060609@april.org
296 EOF
297 test_expect_equal_file EXPECTED OUTPUT
298
299 test_begin_subtest "msg.get_property (python)"
300 test_python <<'EOF'
301 import notmuch
302 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
303 msg = db.find_message("4EFC743A.3060609@april.org")
304 print("testkey1 = {0}".format(msg.get_property("testkey1")))
305 print("testkey3 = {0}".format(msg.get_property("testkey3")))
306 EOF
307 cat <<'EOF' > EXPECTED
308 testkey1 = alice
309 testkey3 = alice3
310 EOF
311 test_expect_equal_file EXPECTED OUTPUT
312
313 test_begin_subtest "msg.get_properties (python)"
314 test_subtest_known_broken
315 test_python <<'EOF'
316 import notmuch
317 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
318 msg = db.find_message("4EFC743A.3060609@april.org")
319 for (key,val) in msg.get_properties("testkey1"):
320         print("{0} = {1}".format(key,val))
321 EOF
322 cat <<'EOF' > EXPECTED
323 testkey1 = alice
324 testkey1 = bob
325 testkey1 = testvalue2
326 EOF
327 test_expect_equal_file EXPECTED OUTPUT
328
329 test_begin_subtest "msg.get_properties (python, prefix)"
330 test_subtest_known_broken
331 test_python <<'EOF'
332 import notmuch
333 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
334 msg = db.find_message("4EFC743A.3060609@april.org")
335 for (key,val) in msg.get_properties("testkey"):
336         print("{0} = {1}".format(key,val))
337 EOF
338 cat <<'EOF' > EXPECTED
339 testkey1 = alice
340 testkey1 = bob
341 testkey1 = testvalue2
342 testkey3 = alice3
343 testkey3 = bob3
344 testkey3 = testvalue3
345 EOF
346 test_expect_equal_file EXPECTED OUTPUT
347
348 test_begin_subtest "msg.get_properties (python, exact)"
349 test_python <<'EOF'
350 import notmuch
351 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
352 msg = db.find_message("4EFC743A.3060609@april.org")
353 for (key,val) in msg.get_properties("testkey",True):
354         print("{0} = {1}".format(key,val))
355 EOF
356 test_expect_equal_file /dev/null OUTPUT
357
358 test_begin_subtest "edit property on removed message without uncaught exception"
359 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
360 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
361 stat = notmuch_message_remove_property (message, "example", "example");
362 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
363     fprintf (stderr, "unable to remove properties on message");
364 EOF
365 cat <<'EOF' >EXPECTED
366 == stdout ==
367 == stderr ==
368 unable to remove properties on message
369 EOF
370 test_expect_equal_file EXPECTED OUTPUT
371
372 add_email_corpus
373
374 test_begin_subtest "remove all properties 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_all_properties_with_prefix (message, "");
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 test_done