]> git.notmuchmail.org Git - notmuch/blob - bindings/python-cffi/tests/test_tags.py
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / bindings / python-cffi / tests / test_tags.py
1 """Tests for the behaviour of immutable and mutable tagsets.
2
3 This module tests the Pythonic behaviour of the sets.
4 """
5
6 import collections
7 import subprocess
8 import textwrap
9
10 import pytest
11
12 from notmuch2 import _database as database
13 from notmuch2 import _tags as tags
14
15
16 class TestImmutable:
17
18     @pytest.fixture
19     def tagset(self, maildir, notmuch):
20         """An non-empty immutable tagset.
21
22         This will have the default new mail tags: inbox, unread.
23         """
24         maildir.deliver()
25         notmuch('new')
26         with database.Database(maildir.path) as db:
27             yield db.tags
28
29     def test_type(self, tagset):
30         assert isinstance(tagset, tags.ImmutableTagSet)
31         assert isinstance(tagset, collections.abc.Set)
32
33     def test_hash(self, tagset, maildir, notmuch):
34         h0 = hash(tagset)
35         notmuch('tag', '+foo', '*')
36         with database.Database(maildir.path) as db:
37             h1 = hash(db.tags)
38         assert h0 != h1
39
40     def test_eq(self, tagset):
41         assert tagset == tagset
42
43     def test_neq(self, tagset, maildir, notmuch):
44         notmuch('tag', '+foo', '*')
45         with database.Database(maildir.path) as db:
46             assert tagset != db.tags
47
48     def test_contains(self, tagset):
49         print(tuple(tagset))
50         assert 'unread' in tagset
51         assert 'foo' not in tagset
52
53     def test_iter(self, tagset):
54         expected = sorted(['unread', 'inbox'])
55         found = []
56         for tag in tagset:
57             assert isinstance(tag, str)
58             found.append(tag)
59         assert expected == sorted(found)
60
61     def test_special_iter(self, tagset):
62         expected = sorted([b'unread', b'inbox'])
63         found = []
64         for tag in tagset.iter():
65             assert isinstance(tag, bytes)
66             found.append(tag)
67         assert expected == sorted(found)
68
69     def test_special_iter_codec(self, tagset):
70         for tag in tagset.iter(encoding='ascii', errors='surrogateescape'):
71             assert isinstance(tag, str)
72
73     def test_len(self, tagset):
74         assert len(tagset) == 2
75
76     def test_and(self, tagset):
77         common = tagset & {'unread'}
78         assert isinstance(common, set)
79         assert isinstance(common, collections.abc.Set)
80         assert common == {'unread'}
81
82     def test_or(self, tagset):
83         res = tagset | {'foo'}
84         assert isinstance(res, set)
85         assert isinstance(res, collections.abc.Set)
86         assert res == {'unread', 'inbox', 'foo'}
87
88     def test_sub(self, tagset):
89         res = tagset - {'unread'}
90         assert isinstance(res, set)
91         assert isinstance(res, collections.abc.Set)
92         assert res == {'inbox'}
93
94     def test_rsub(self, tagset):
95         res = {'foo', 'unread'} - tagset
96         assert isinstance(res, set)
97         assert isinstance(res, collections.abc.Set)
98         assert res == {'foo'}
99
100     def test_xor(self, tagset):
101         res = tagset ^ {'unread', 'foo'}
102         assert isinstance(res, set)
103         assert isinstance(res, collections.abc.Set)
104         assert res == {'inbox', 'foo'}
105
106     def test_rxor(self, tagset):
107         res = {'unread', 'foo'} ^ tagset
108         assert isinstance(res, set)
109         assert isinstance(res, collections.abc.Set)
110         assert res == {'inbox', 'foo'}
111
112
113 class TestMutableTagset:
114
115     @pytest.fixture
116     def tagset(self, maildir, notmuch):
117         """An non-empty mutable tagset.
118
119         This will have the default new mail tags: inbox, unread.
120         """
121         _, pathname = maildir.deliver()
122         notmuch('new')
123         with database.Database(maildir.path,
124                                mode=database.Mode.READ_WRITE) as db:
125             msg = db.get(pathname)
126             yield msg.tags
127
128     def test_type(self, tagset):
129         assert isinstance(tagset, collections.abc.MutableSet)
130         assert isinstance(tagset, tags.MutableTagSet)
131
132     def test_hash(self, tagset):
133         assert not isinstance(tagset, collections.abc.Hashable)
134         with pytest.raises(TypeError):
135             hash(tagset)
136
137     def test_add(self, tagset):
138         assert 'foo' not in tagset
139         tagset.add('foo')
140         assert 'foo' in tagset
141
142     def test_discard(self, tagset):
143         assert 'inbox' in tagset
144         tagset.discard('inbox')
145         assert 'inbox' not in tagset
146
147     def test_discard_not_present(self, tagset):
148         assert 'foo' not in tagset
149         tagset.discard('foo')
150
151     def test_clear(self, tagset):
152         assert len(tagset) > 0
153         tagset.clear()
154         assert len(tagset) == 0
155
156     def test_from_maildir_flags(self, maildir, notmuch):
157         _, pathname = maildir.deliver(flagged=True)
158         notmuch('new')
159         with database.Database(maildir.path,
160                                mode=database.Mode.READ_WRITE) as db:
161             msg = db.get(pathname)
162             msg.tags.discard('flagged')
163             msg.tags.from_maildir_flags()
164             assert 'flagged' in msg.tags
165
166     def test_to_maildir_flags(self, maildir, notmuch):
167         _, pathname = maildir.deliver(flagged=True)
168         notmuch('new')
169         with database.Database(maildir.path,
170                                mode=database.Mode.READ_WRITE) as db:
171             msg = db.get(pathname)
172             flags = msg.path.name.split(',')[-1]
173             assert 'F' in flags
174             msg.tags.discard('flagged')
175             msg.tags.to_maildir_flags()
176             flags = msg.path.name.split(',')[-1]
177             assert 'F' not in flags