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 test_subtest_known_broken
156 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
157 EXPECT0(notmuch_message_remove_all_properties (message, NULL));
158 EXPECT0(notmuch_database_destroy(db));
159 EXPECT0(notmuch_database_open_with_config (argv[1],
160 NOTMUCH_DATABASE_MODE_READ_WRITE,
161 "", NULL, &db, &msg));
162 if (msg) fputs (msg, stderr);
163 EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
164 if (message == NULL) {
165 fprintf (stderr, "unable to find message");
168 print_properties (message, "", FALSE);
170 cat <<'EOF' >EXPECTED
174 test_expect_equal_file EXPECTED OUTPUT
176 test_begin_subtest "notmuch_message_properties: multiple values"
177 test_subtest_known_broken
178 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
179 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
180 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
181 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
182 print_properties (message, "testkey1", TRUE);
184 cat <<'EOF' >EXPECTED
188 testkey1 = testvalue2
191 test_expect_equal_file EXPECTED OUTPUT
193 test_begin_subtest "notmuch_message_properties: prefix"
194 test_subtest_known_broken
195 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
196 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
197 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
198 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
199 print_properties (message, "testkey", FALSE);
201 cat <<'EOF' >EXPECTED
205 testkey1 = testvalue2
208 testkey3 = testvalue3
211 test_expect_equal_file EXPECTED OUTPUT
213 test_begin_subtest "notmuch_message_properties: modify during iteration"
214 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
216 const char *keys[1000] = {NULL};
217 const char *vals[1000] = {NULL};
218 notmuch_message_properties_t *properties;
221 for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
222 notmuch_message_properties_valid (properties);
223 notmuch_message_properties_move_to_next (properties), i++)
225 const char *key, *value;
227 keys[i]=talloc_strdup(message,
228 notmuch_message_properties_key (properties));
229 vals[i]=talloc_strdup(message,
230 notmuch_message_properties_value (properties));
232 EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
235 print_properties (message, "", FALSE);
237 for (i = 0; keys[i] && vals[i]; i++) {
238 EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
242 cat <<'EOF' >EXPECTED
246 test_expect_equal_file EXPECTED OUTPUT
248 test_begin_subtest "dump message properties"
249 test_subtest_known_broken
250 cat <<EOF > PROPERTIES
251 #= 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 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
254 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
256 notmuch dump | grep '^#=' > OUTPUT
257 test_expect_equal_file PROPERTIES OUTPUT
259 test_begin_subtest "dump _only_ message properties"
260 test_subtest_known_broken
262 #notmuch-dump batch-tag:3 properties
263 #= 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
265 notmuch dump --include=properties > OUTPUT
266 test_expect_equal_file EXPECTED OUTPUT
269 test_begin_subtest "restore missing message property (single line)"
270 test_subtest_known_broken
271 notmuch dump | grep '^#=' > BEFORE1
272 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
273 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
275 notmuch restore < BEFORE1
276 notmuch dump | grep '^#=' > OUTPUT
277 test_expect_equal_file PROPERTIES OUTPUT
280 test_begin_subtest "restore missing message property (full dump)"
281 test_subtest_known_broken
282 notmuch dump > BEFORE2
283 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
284 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
286 notmuch restore < BEFORE2
287 notmuch dump | grep '^#=' > OUTPUT
288 test_expect_equal_file PROPERTIES OUTPUT
290 test_begin_subtest "restore clear extra message property"
291 test_subtest_known_broken
292 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
293 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
295 notmuch restore < BEFORE2
296 notmuch dump | grep '^#=' > OUTPUT
297 test_expect_equal_file PROPERTIES OUTPUT
299 test_begin_subtest "test 'property:' queries: empty"
300 notmuch search property:testkey1=charles > OUTPUT
301 test_expect_equal_file /dev/null OUTPUT
303 test_begin_subtest "test 'property:' queries: single message"
304 notmuch search --output=messages property:testkey1=alice > OUTPUT
306 id:4EFC743A.3060609@april.org
308 test_expect_equal_file EXPECTED OUTPUT
310 test_begin_subtest "msg.get_property (python)"
313 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
314 msg = db.find_message("4EFC743A.3060609@april.org")
315 print("testkey1 = {0}".format(msg.get_property("testkey1")))
316 print("testkey3 = {0}".format(msg.get_property("testkey3")))
318 cat <<'EOF' > EXPECTED
322 test_expect_equal_file EXPECTED OUTPUT
324 test_begin_subtest "msg.get_properties (python)"
325 test_subtest_known_broken
328 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
329 msg = db.find_message("4EFC743A.3060609@april.org")
330 for (key,val) in msg.get_properties("testkey1"):
331 print("{0} = {1}".format(key,val))
333 cat <<'EOF' > EXPECTED
336 testkey1 = testvalue2
338 test_expect_equal_file EXPECTED OUTPUT
340 test_begin_subtest "msg.get_properties (python, prefix)"
341 test_subtest_known_broken
344 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
345 msg = db.find_message("4EFC743A.3060609@april.org")
346 for (key,val) in msg.get_properties("testkey"):
347 print("{0} = {1}".format(key,val))
349 cat <<'EOF' > EXPECTED
352 testkey1 = testvalue2
355 testkey3 = testvalue3
357 test_expect_equal_file EXPECTED OUTPUT
359 test_begin_subtest "msg.get_properties (python, exact)"
362 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
363 msg = db.find_message("4EFC743A.3060609@april.org")
364 for (key,val) in msg.get_properties("testkey",True):
365 print("{0} = {1}".format(key,val))
367 test_expect_equal_file /dev/null OUTPUT
369 test_begin_subtest "edit property on removed message without uncaught exception"
370 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
371 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
372 stat = notmuch_message_remove_property (message, "example", "example");
373 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
374 fprintf (stderr, "unable to remove properties on message");
376 cat <<'EOF' >EXPECTED
379 unable to remove properties on message
381 test_expect_equal_file EXPECTED OUTPUT
385 test_begin_subtest "remove all properties on removed message without uncaught exception"
386 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
387 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
388 stat = notmuch_message_remove_all_properties_with_prefix (message, "");
389 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
390 fprintf (stderr, "unable to remove properties on message");
392 cat <<'EOF' >EXPECTED
395 unable to remove properties on message
397 test_expect_equal_file EXPECTED OUTPUT