2 test_description="message property API"
4 . $(dirname "$0")/test-lib.sh || exit 1
9 #include <notmuch-test.h>
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));
17 notmuch_message_properties_destroy (list);
20 int main (int argc, char** argv)
22 notmuch_database_t *db;
23 notmuch_message_t *message = NULL;
25 notmuch_status_t stat;
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");
40 EXPECT0(notmuch_database_destroy(db));
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
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));
55 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56 printf("testkey2 = %s\n", val);
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);
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);
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);
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);
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
90 test_expect_equal_file EXPECTED OUTPUT
92 test_begin_subtest "testing string map binary search (via message properties)"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
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]));
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);
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);
125 test_expect_equal_file EXPECTED OUTPUT
127 test_begin_subtest "notmuch_message_get_properties: empty list"
128 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
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);
136 cat <<'EOF' >EXPECTED
141 test_expect_equal_file EXPECTED OUTPUT
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);
147 cat <<'EOF' >EXPECTED
149 testkey1 = testvalue1
152 test_expect_equal_file EXPECTED OUTPUT
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);
159 cat <<'EOF' >EXPECTED
163 test_expect_equal_file EXPECTED OUTPUT
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);
173 cat <<'EOF' >EXPECTED
177 testkey1 = testvalue2
180 test_expect_equal_file EXPECTED OUTPUT
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);
190 cat <<'EOF' >EXPECTED
194 testkey1 = testvalue2
197 testkey3 = testvalue3
200 test_expect_equal_file EXPECTED OUTPUT
202 test_begin_subtest "notmuch_message_properties: modify during iteration"
203 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
205 const char *keys[1000] = {NULL};
206 const char *vals[1000] = {NULL};
207 notmuch_message_properties_t *properties;
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++)
214 const char *key, *value;
216 keys[i]=talloc_strdup(message,
217 notmuch_message_properties_key (properties));
218 vals[i]=talloc_strdup(message,
219 notmuch_message_properties_value (properties));
221 EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
224 print_properties (message, "", FALSE);
226 for (i = 0; keys[i] && vals[i]; i++) {
227 EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
231 cat <<'EOF' >EXPECTED
235 test_expect_equal_file EXPECTED OUTPUT
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
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 ="));
245 notmuch dump | grep '^#=' > OUTPUT
246 test_expect_equal_file PROPERTIES OUTPUT
248 test_begin_subtest "dump _only_ message properties"
249 test_subtest_known_broken
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
254 notmuch dump --include=properties > OUTPUT
255 test_expect_equal_file EXPECTED OUTPUT
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"));
264 notmuch restore < BEFORE1
265 notmuch dump | grep '^#=' > OUTPUT
266 test_expect_equal_file PROPERTIES OUTPUT
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"));
275 notmuch restore < BEFORE2
276 notmuch dump | grep '^#=' > OUTPUT
277 test_expect_equal_file PROPERTIES OUTPUT
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"));
284 notmuch restore < BEFORE2
285 notmuch dump | grep '^#=' > OUTPUT
286 test_expect_equal_file PROPERTIES OUTPUT
288 test_begin_subtest "test 'property:' queries: empty"
289 notmuch search property:testkey1=charles > OUTPUT
290 test_expect_equal_file /dev/null OUTPUT
292 test_begin_subtest "test 'property:' queries: single message"
293 notmuch search --output=messages property:testkey1=alice > OUTPUT
295 id:4EFC743A.3060609@april.org
297 test_expect_equal_file EXPECTED OUTPUT
299 test_begin_subtest "msg.get_property (python)"
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")))
307 cat <<'EOF' > EXPECTED
311 test_expect_equal_file EXPECTED OUTPUT
313 test_begin_subtest "msg.get_properties (python)"
314 test_subtest_known_broken
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))
322 cat <<'EOF' > EXPECTED
325 testkey1 = testvalue2
327 test_expect_equal_file EXPECTED OUTPUT
329 test_begin_subtest "msg.get_properties (python, prefix)"
330 test_subtest_known_broken
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))
338 cat <<'EOF' > EXPECTED
341 testkey1 = testvalue2
344 testkey3 = testvalue3
346 test_expect_equal_file EXPECTED OUTPUT
348 test_begin_subtest "msg.get_properties (python, exact)"
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))
356 test_expect_equal_file /dev/null OUTPUT
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");
365 cat <<'EOF' >EXPECTED
368 unable to remove properties on message
370 test_expect_equal_file EXPECTED OUTPUT
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");
381 cat <<'EOF' >EXPECTED
384 unable to remove properties on message
386 test_expect_equal_file EXPECTED OUTPUT