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 "notmuch_message_get_properties: empty list"
104 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
106 notmuch_message_properties_t *list;
107 list = notmuch_message_get_properties (message, "nonexistent", TRUE);
108 printf("valid = %d\n", notmuch_message_properties_valid (list));
109 notmuch_message_properties_destroy (list);
112 cat <<'EOF' >EXPECTED
117 test_expect_equal_file EXPECTED OUTPUT
119 test_begin_subtest "notmuch_message_properties: one value"
120 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
121 print_properties (message, "testkey1", TRUE);
123 cat <<'EOF' >EXPECTED
128 test_expect_equal_file EXPECTED OUTPUT
130 test_begin_subtest "notmuch_message_properties: multiple values"
131 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
132 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
133 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
134 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
135 print_properties (message, "testkey1", TRUE);
137 cat <<'EOF' >EXPECTED
145 test_expect_equal_file EXPECTED OUTPUT
147 test_begin_subtest "notmuch_message_properties: prefix"
148 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
149 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
150 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
151 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
152 print_properties (message, "testkey", FALSE);
154 cat <<'EOF' >EXPECTED
165 test_expect_equal_file EXPECTED OUTPUT
167 test_begin_subtest "notmuch_message_properties: modify during iteration"
168 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
170 const char *keys[1000] = {NULL};
171 const char *vals[1000] = {NULL};
172 notmuch_message_properties_t *properties;
175 for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
176 notmuch_message_properties_valid (properties);
177 notmuch_message_properties_move_to_next (properties), i++)
179 const char *key, *value;
181 keys[i]=talloc_strdup(message,
182 notmuch_message_properties_key (properties));
183 vals[i]=talloc_strdup(message,
184 notmuch_message_properties_value (properties));
186 EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
189 print_properties (message, "", FALSE);
191 for (i = 0; keys[i] && vals[i]; i++) {
192 EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
196 cat <<'EOF' >EXPECTED
200 test_expect_equal_file EXPECTED OUTPUT
202 test_begin_subtest "dump message properties"
203 cat <<EOF > PROPERTIES
204 #= 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
206 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
207 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
209 notmuch dump | grep '^#=' > OUTPUT
210 test_expect_equal_file PROPERTIES OUTPUT
212 test_begin_subtest "dump _only_ message properties"
214 #notmuch-dump batch-tag:3 properties
215 #= 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
217 notmuch dump --include=properties > OUTPUT
218 test_expect_equal_file EXPECTED OUTPUT
221 test_begin_subtest "restore missing message property (single line)"
222 notmuch dump | grep '^#=' > BEFORE1
223 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
224 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
226 notmuch restore < BEFORE1
227 notmuch dump | grep '^#=' > OUTPUT
228 test_expect_equal_file PROPERTIES OUTPUT
231 test_begin_subtest "restore missing message property (full dump)"
232 notmuch dump > BEFORE2
233 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
234 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
236 notmuch restore < BEFORE2
237 notmuch dump | grep '^#=' > OUTPUT
238 test_expect_equal_file PROPERTIES OUTPUT
240 test_begin_subtest "restore clear extra message property"
241 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
242 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
244 notmuch restore < BEFORE2
245 notmuch dump | grep '^#=' > OUTPUT
246 test_expect_equal_file PROPERTIES OUTPUT
248 test_begin_subtest "test 'property:' queries: empty"
249 notmuch search property:testkey1=charles > OUTPUT
250 test_expect_equal_file /dev/null OUTPUT
252 test_begin_subtest "test 'property:' queries: single message"
253 notmuch search --output=messages property:testkey1=alice > OUTPUT
255 id:4EFC743A.3060609@april.org
257 test_expect_equal_file EXPECTED OUTPUT
259 test_begin_subtest "msg.get_property (python)"
262 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
263 msg = db.find_message("4EFC743A.3060609@april.org")
264 print("testkey1 = {0}".format(msg.get_property("testkey1")))
265 print("testkey3 = {0}".format(msg.get_property("testkey3")))
267 cat <<'EOF' > EXPECTED
271 test_expect_equal_file EXPECTED OUTPUT
273 test_begin_subtest "msg.get_properties (python)"
276 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
277 msg = db.find_message("4EFC743A.3060609@april.org")
278 for (key,val) in msg.get_properties("testkey1"):
279 print("{0} = {1}".format(key,val))
281 cat <<'EOF' > EXPECTED
284 testkey1 = testvalue1
285 testkey1 = testvalue2
287 test_expect_equal_file EXPECTED OUTPUT
289 test_begin_subtest "msg.get_properties (python, prefix)"
292 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
293 msg = db.find_message("4EFC743A.3060609@april.org")
294 for (key,val) in msg.get_properties("testkey"):
295 print("{0} = {1}".format(key,val))
297 cat <<'EOF' > EXPECTED
300 testkey1 = testvalue1
301 testkey1 = testvalue2
304 testkey3 = testvalue3
306 test_expect_equal_file EXPECTED OUTPUT
308 test_begin_subtest "msg.get_properties (python, exact)"
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("testkey",True):
314 print("{0} = {1}".format(key,val))
316 test_expect_equal_file /dev/null OUTPUT