]> git.notmuchmail.org Git - notmuch/blob - test/insert
test: add tests for insert
[notmuch] / test / insert
1 #!/usr/bin/env bash
2 test_description='"notmuch insert"'
3 . ./test-lib.sh
4
5 # Create directories and database before inserting.
6 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
7 mkdir -p "$MAIL_DIR"/Drafts/{cur,new,tmp}
8 notmuch new > /dev/null
9
10 # We use generate_message to create the temporary message files.
11 # They happen to be in the mail directory already but that is okay
12 # since we do not call notmuch new hereafter.
13
14 gen_insert_msg() {
15     generate_message \
16         "[subject]=\"insert-subject\"" \
17         "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" \
18         "[body]=\"insert-message\""
19 }
20
21 test_expect_code 1 "Insert zero-length file" \
22     "notmuch insert < /dev/null"
23
24 # This test is a proxy for other errors that may occur while trying to
25 # add a message to the notmuch database, e.g. database locked.
26 test_expect_code 0 "Insert non-message" \
27     "echo bad_message | notmuch insert"
28
29 test_begin_subtest "Database empty so far"
30 test_expect_equal "0" "`notmuch count --output=messages '*'`"
31
32 test_begin_subtest "Insert message"
33 gen_insert_msg
34 notmuch insert < "$gen_msg_filename"
35 cur_msg_filename=$(notmuch search --output=files "subject:insert-subject")
36 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
37
38 test_begin_subtest "Insert message adds default tags"
39 output=$(notmuch show --format=json "subject:insert-subject")
40 expected='[[[{
41  "id": "'"${gen_msg_id}"'",
42  "match": true,
43  "excluded": false,
44  "filename": "'"${cur_msg_filename}"'",
45  "timestamp": 946728000,
46  "date_relative": "2000-01-01",
47  "tags": ["inbox","unread"],
48  "headers": {
49   "Subject": "insert-subject",
50   "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
51   "To": "Notmuch Test Suite <test_suite@notmuchmail.org>",
52   "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
53  "body": [{"id": 1,
54   "content-type": "text/plain",
55   "content": "insert-message\n"}]},
56  []]]]'
57 test_expect_equal_json "$output" "$expected"
58
59 test_begin_subtest "Insert duplicate message"
60 notmuch insert +duptag -unread < "$gen_msg_filename"
61 output=$(notmuch search --output=files "subject:insert-subject" | wc -l)
62 test_expect_equal "$output" 2
63
64 test_begin_subtest "Duplicate message does not change tags"
65 output=$(notmuch search --format=json --output=tags "subject:insert-subject")
66 test_expect_equal_json "$output" '["inbox", "unread"]'
67
68 test_begin_subtest "Insert message, add tag"
69 gen_insert_msg
70 notmuch insert +custom < "$gen_msg_filename"
71 output=$(notmuch count tag:custom)
72 test_expect_equal "$output" "1"
73
74 test_begin_subtest "Insert message, add/remove tags"
75 gen_insert_msg
76 notmuch insert +custom -unread < "$gen_msg_filename"
77 output=$(notmuch count tag:custom NOT tag:unread)
78 test_expect_equal "$output" "1"
79
80 test_done