]> git.cworth.org Git - notmuch/blob - test/T610-message-property.sh
debian: bump compat level to 11
[notmuch] / test / T610-message-property.sh
1 #!/usr/bin/env bash
2 test_description="message property API"
3
4 . $(dirname "$0")/test-lib.sh || exit 1
5
6 add_email_corpus
7
8 cat <<EOF > c_head
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <talloc.h>
13 #include <notmuch-test.h>
14
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));
20     }
21     notmuch_message_properties_destroy (list);
22 }
23
24 int main (int argc, char** argv)
25 {
26    notmuch_database_t *db;
27    notmuch_message_t *message = NULL;
28    const char *val;
29    notmuch_status_t stat;
30
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");
35         exit (1);
36    }
37 EOF
38
39 cat <<EOF > c_tail
40    EXPECT0(notmuch_database_destroy(db));
41 }
42 EOF
43
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
46 {
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));
54
55    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56    printf("testkey2 = %s\n", val);
57
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);
62
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);
67
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);
72
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);
77 }
78 EOF
79 cat <<'EOF' >EXPECTED
80 == stdout ==
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
87 testkey2 = NULL
88 == stderr ==
89 EOF
90 test_expect_equal_file EXPECTED OUTPUT
91
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);
96 EOF
97 cat <<'EOF' >EXPECTED
98 == stdout ==
99 == stderr ==
100 EOF
101 test_expect_equal_file EXPECTED OUTPUT
102
103 test_begin_subtest "notmuch_message_get_properties: empty list"
104 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
105 {
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);
110 }
111 EOF
112 cat <<'EOF' >EXPECTED
113 == stdout ==
114 valid = 0
115 == stderr ==
116 EOF
117 test_expect_equal_file EXPECTED OUTPUT
118
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);
122 EOF
123 cat <<'EOF' >EXPECTED
124 == stdout ==
125 testvalue1
126 == stderr ==
127 EOF
128 test_expect_equal_file EXPECTED OUTPUT
129
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);
136 EOF
137 cat <<'EOF' >EXPECTED
138 == stdout ==
139 alice
140 bob
141 testvalue1
142 testvalue2
143 == stderr ==
144 EOF
145 test_expect_equal_file EXPECTED OUTPUT
146
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);
153 EOF
154 cat <<'EOF' >EXPECTED
155 == stdout ==
156 alice
157 bob
158 testvalue1
159 testvalue2
160 alice3
161 bob3
162 testvalue3
163 == stderr ==
164 EOF
165 test_expect_equal_file EXPECTED OUTPUT
166
167 test_begin_subtest "notmuch_message_properties: modify during iteration"
168 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
169 {
170     const char *keys[1000] = {NULL};
171     const char *vals[1000] = {NULL};
172     notmuch_message_properties_t *properties;
173     int i;
174
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++)
178     {
179         const char *key, *value;
180
181         keys[i]=talloc_strdup(message,
182                     notmuch_message_properties_key (properties));
183         vals[i]=talloc_strdup(message,
184                     notmuch_message_properties_value (properties));
185
186         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
187     }
188
189     print_properties (message, "", FALSE);
190
191     for (i = 0; keys[i] && vals[i]; i++) {
192         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
193     }
194 }
195 EOF
196 cat <<'EOF' >EXPECTED
197 == stdout ==
198 == stderr ==
199 EOF
200 test_expect_equal_file EXPECTED OUTPUT
201
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
205 EOF
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 ="));
208 EOF
209 notmuch dump | grep '^#=' > OUTPUT
210 test_expect_equal_file PROPERTIES OUTPUT
211
212 test_begin_subtest "dump _only_ message properties"
213 cat <<EOF > EXPECTED
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
216 EOF
217 notmuch dump --include=properties > OUTPUT
218 test_expect_equal_file EXPECTED OUTPUT
219
220
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"));
225 EOF
226 notmuch restore < BEFORE1
227 notmuch dump | grep '^#=' > OUTPUT
228 test_expect_equal_file PROPERTIES OUTPUT
229
230
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"));
235 EOF
236 notmuch restore < BEFORE2
237 notmuch dump | grep '^#=' > OUTPUT
238 test_expect_equal_file PROPERTIES OUTPUT
239
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"));
243 EOF
244 notmuch restore < BEFORE2
245 notmuch dump | grep '^#=' > OUTPUT
246 test_expect_equal_file PROPERTIES OUTPUT
247
248 test_begin_subtest "test 'property:' queries: empty"
249 notmuch search property:testkey1=charles > OUTPUT
250 test_expect_equal_file /dev/null OUTPUT
251
252 test_begin_subtest "test 'property:' queries: single message"
253 notmuch search --output=messages property:testkey1=alice > OUTPUT
254 cat <<EOF >EXPECTED
255 id:4EFC743A.3060609@april.org
256 EOF
257 test_expect_equal_file EXPECTED OUTPUT
258
259 test_begin_subtest "msg.get_property (python)"
260 test_python <<'EOF'
261 import notmuch
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")))
266 EOF
267 cat <<'EOF' > EXPECTED
268 testkey1 = alice
269 testkey3 = alice3
270 EOF
271 test_expect_equal_file EXPECTED OUTPUT
272
273 test_begin_subtest "msg.get_properties (python)"
274 test_python <<'EOF'
275 import notmuch
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))
280 EOF
281 cat <<'EOF' > EXPECTED
282 testkey1 = alice
283 testkey1 = bob
284 testkey1 = testvalue1
285 testkey1 = testvalue2
286 EOF
287 test_expect_equal_file EXPECTED OUTPUT
288
289 test_begin_subtest "msg.get_properties (python, prefix)"
290 test_python <<'EOF'
291 import notmuch
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))
296 EOF
297 cat <<'EOF' > EXPECTED
298 testkey1 = alice
299 testkey1 = bob
300 testkey1 = testvalue1
301 testkey1 = testvalue2
302 testkey3 = alice3
303 testkey3 = bob3
304 testkey3 = testvalue3
305 EOF
306 test_expect_equal_file EXPECTED OUTPUT
307
308 test_begin_subtest "msg.get_properties (python, exact)"
309 test_python <<'EOF'
310 import notmuch
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))
315 EOF
316 test_expect_equal_file /dev/null OUTPUT
317
318 test_done