2 test_description="message property API"
4 . $(dirname "$0")/test-lib.sh || exit 1
13 #include <notmuch-test.h>
15 void print_properties (notmuch_message_t *message, const char *prefix, notmuch_bool_t exact) {
16 notmuch_message_properties_t *list;
17 for (list = notmuch_message_get_properties (message, prefix, exact);
18 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
19 printf("%s\n", notmuch_message_properties_value(list));
21 notmuch_message_properties_destroy (list);
24 int main (int argc, char** argv)
26 notmuch_database_t *db;
27 notmuch_message_t *message = NULL;
29 notmuch_status_t stat;
31 EXPECT0(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));
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-existant 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 "notmuch_message_remove_all_properties"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94 EXPECT0(notmuch_message_remove_all_properties (message, NULL));
95 print_properties (message, "", FALSE);
101 test_expect_equal_file EXPECTED OUTPUT
103 test_begin_subtest "testing string map binary search (via message properties)"
104 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
106 char *keys[] = {"a", "b", "c", "d", "e", NULL};
107 for (int i=0; keys[i]; i++)
108 EXPECT0(notmuch_message_add_property (message, keys[i], keys[i]));
110 for (int i=0; keys[i]; i++) {
111 EXPECT0(notmuch_message_get_property (message, keys[i], &val));
112 printf("%s = %s\n", keys[i], val);
115 for (int i=0; keys[i]; i++) {
116 EXPECT0(notmuch_message_remove_property (message, keys[i], keys[i]));
117 EXPECT0(notmuch_message_get_property (message, keys[i], &val));
118 printf("%s = %s\n", keys[i], val == NULL ? "NULL" : val);
136 test_expect_equal_file EXPECTED OUTPUT
138 test_begin_subtest "notmuch_message_get_properties: empty list"
139 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
141 notmuch_message_properties_t *list;
142 list = notmuch_message_get_properties (message, "nonexistent", TRUE);
143 printf("valid = %d\n", notmuch_message_properties_valid (list));
144 notmuch_message_properties_destroy (list);
147 cat <<'EOF' >EXPECTED
152 test_expect_equal_file EXPECTED OUTPUT
154 test_begin_subtest "notmuch_message_properties: one value"
155 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
156 print_properties (message, "testkey1", TRUE);
158 cat <<'EOF' >EXPECTED
163 test_expect_equal_file EXPECTED OUTPUT
165 test_begin_subtest "notmuch_message_properties: multiple values"
166 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
167 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
168 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
169 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
170 print_properties (message, "testkey1", TRUE);
172 cat <<'EOF' >EXPECTED
180 test_expect_equal_file EXPECTED OUTPUT
182 test_begin_subtest "notmuch_message_properties: prefix"
183 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
184 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
185 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
186 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
187 print_properties (message, "testkey", FALSE);
189 cat <<'EOF' >EXPECTED
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 cat <<EOF > PROPERTIES
239 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue1 testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
241 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
242 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
244 notmuch dump | grep '^#=' > OUTPUT
245 test_expect_equal_file PROPERTIES OUTPUT
247 test_begin_subtest "dump _only_ message properties"
249 #notmuch-dump batch-tag:3 properties
250 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue1 testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
252 notmuch dump --include=properties > OUTPUT
253 test_expect_equal_file EXPECTED OUTPUT
256 test_begin_subtest "restore missing message property (single line)"
257 notmuch dump | grep '^#=' > BEFORE1
258 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
259 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
261 notmuch restore < BEFORE1
262 notmuch dump | grep '^#=' > OUTPUT
263 test_expect_equal_file PROPERTIES OUTPUT
266 test_begin_subtest "restore missing message property (full dump)"
267 notmuch dump > BEFORE2
268 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
269 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
271 notmuch restore < BEFORE2
272 notmuch dump | grep '^#=' > OUTPUT
273 test_expect_equal_file PROPERTIES OUTPUT
275 test_begin_subtest "restore clear extra message property"
276 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
277 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
279 notmuch restore < BEFORE2
280 notmuch dump | grep '^#=' > OUTPUT
281 test_expect_equal_file PROPERTIES OUTPUT
283 test_begin_subtest "test 'property:' queries: empty"
284 notmuch search property:testkey1=charles > OUTPUT
285 test_expect_equal_file /dev/null OUTPUT
287 test_begin_subtest "test 'property:' queries: single message"
288 notmuch search --output=messages property:testkey1=alice > OUTPUT
290 id:4EFC743A.3060609@april.org
292 test_expect_equal_file EXPECTED OUTPUT
294 test_begin_subtest "msg.get_property (python)"
297 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
298 msg = db.find_message("4EFC743A.3060609@april.org")
299 print("testkey1 = {0}".format(msg.get_property("testkey1")))
300 print("testkey3 = {0}".format(msg.get_property("testkey3")))
302 cat <<'EOF' > EXPECTED
306 test_expect_equal_file EXPECTED OUTPUT
308 test_begin_subtest "msg.get_properties (python)"
311 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
312 msg = db.find_message("4EFC743A.3060609@april.org")
313 for (key,val) in msg.get_properties("testkey1"):
314 print("{0} = {1}".format(key,val))
316 cat <<'EOF' > EXPECTED
319 testkey1 = testvalue1
320 testkey1 = testvalue2
322 test_expect_equal_file EXPECTED OUTPUT
324 test_begin_subtest "msg.get_properties (python, prefix)"
327 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
328 msg = db.find_message("4EFC743A.3060609@april.org")
329 for (key,val) in msg.get_properties("testkey"):
330 print("{0} = {1}".format(key,val))
332 cat <<'EOF' > EXPECTED
335 testkey1 = testvalue1
336 testkey1 = testvalue2
339 testkey3 = testvalue3
341 test_expect_equal_file EXPECTED OUTPUT
343 test_begin_subtest "msg.get_properties (python, exact)"
346 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
347 msg = db.find_message("4EFC743A.3060609@april.org")
348 for (key,val) in msg.get_properties("testkey",True):
349 print("{0} = {1}".format(key,val))
351 test_expect_equal_file /dev/null OUTPUT