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 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");
167 print_properties (message, "", FALSE);
169 cat <<'EOF' >EXPECTED
173 test_expect_equal_file EXPECTED OUTPUT
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);
182 cat <<'EOF' >EXPECTED
186 testkey1 = testvalue2
189 test_expect_equal_file EXPECTED OUTPUT
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);
198 cat <<'EOF' >EXPECTED
202 testkey1 = testvalue2
205 testkey3 = testvalue3
208 test_expect_equal_file EXPECTED OUTPUT
210 test_begin_subtest "notmuch_message_properties: modify during iteration"
211 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
213 const char *keys[1000] = {NULL};
214 const char *vals[1000] = {NULL};
215 notmuch_message_properties_t *properties;
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++)
222 const char *key, *value;
224 keys[i]=talloc_strdup(message,
225 notmuch_message_properties_key (properties));
226 vals[i]=talloc_strdup(message,
227 notmuch_message_properties_value (properties));
229 EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
232 print_properties (message, "", FALSE);
234 for (i = 0; keys[i] && vals[i]; i++) {
235 EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
239 cat <<'EOF' >EXPECTED
243 test_expect_equal_file EXPECTED OUTPUT
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
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 ="));
252 notmuch dump | grep '^#=' > OUTPUT
253 test_expect_equal_file PROPERTIES OUTPUT
255 test_begin_subtest "dump _only_ message properties"
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
260 notmuch dump --include=properties > OUTPUT
261 test_expect_equal_file EXPECTED OUTPUT
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"));
269 notmuch restore < BEFORE1
270 notmuch dump | grep '^#=' > OUTPUT
271 test_expect_equal_file PROPERTIES OUTPUT
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"));
279 notmuch restore < BEFORE2
280 notmuch dump | grep '^#=' > OUTPUT
281 test_expect_equal_file PROPERTIES OUTPUT
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"));
287 notmuch restore < BEFORE2
288 notmuch dump | grep '^#=' > OUTPUT
289 test_expect_equal_file PROPERTIES OUTPUT
291 test_begin_subtest "test 'property:' queries: empty"
292 notmuch search property:testkey1=charles > OUTPUT
293 test_expect_equal_file /dev/null OUTPUT
295 test_begin_subtest "test 'property:' queries: single message"
296 notmuch search --output=messages property:testkey1=alice > OUTPUT
298 id:4EFC743A.3060609@april.org
300 test_expect_equal_file EXPECTED OUTPUT
302 test_begin_subtest "msg.get_property (python)"
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")))
310 cat <<'EOF' > EXPECTED
314 test_expect_equal_file EXPECTED OUTPUT
316 test_begin_subtest "msg.get_properties (python)"
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))
324 cat <<'EOF' > EXPECTED
327 testkey1 = testvalue2
329 test_expect_equal_file EXPECTED OUTPUT
331 test_begin_subtest "msg.get_properties (python, prefix)"
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))
339 cat <<'EOF' > EXPECTED
342 testkey1 = testvalue2
345 testkey3 = testvalue3
347 test_expect_equal_file EXPECTED OUTPUT
349 test_begin_subtest "msg.get_properties (python, exact)"
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))
357 test_expect_equal_file /dev/null OUTPUT
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);
364 cat <<'EOF' >EXPECTED
366 fancy key with áccènts = import value with =
369 testkey1 = testvalue2
372 test_expect_equal_file EXPECTED OUTPUT
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");
381 cat <<'EOF' >EXPECTED
384 unable to remove properties on message
386 test_expect_equal_file EXPECTED OUTPUT
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");
397 cat <<'EOF' >EXPECTED
400 unable to remove properties on message
402 test_expect_equal_file EXPECTED OUTPUT