1 """Tests for the behaviour of immutable and mutable tagsets.
3 This module tests the Pythonic behaviour of the sets.
12 from notmuch2 import _database as database
13 from notmuch2 import _tags as tags
19 def tagset(self, maildir, notmuch):
20 """An non-empty immutable tagset.
22 This will have the default new mail tags: inbox, unread.
26 with database.Database(maildir.path, config=database.Database.CONFIG.EMPTY) as db:
29 def test_type(self, tagset):
30 assert isinstance(tagset, tags.ImmutableTagSet)
31 assert isinstance(tagset, collections.abc.Set)
33 def test_hash(self, tagset, maildir, notmuch):
35 notmuch('tag', '+foo', '*')
36 with database.Database(maildir.path, config=database.Database.CONFIG.EMPTY) as db:
40 def test_eq(self, tagset):
41 assert tagset == tagset
43 def test_neq(self, tagset, maildir, notmuch):
44 notmuch('tag', '+foo', '*')
45 with database.Database(maildir.path, config=database.Database.CONFIG.EMPTY) as db:
46 assert tagset != db.tags
48 def test_contains(self, tagset):
50 assert 'unread' in tagset
51 assert 'foo' not in tagset
53 def test_isdisjoint(self, tagset):
54 assert tagset.isdisjoint(set(['spam', 'ham']))
55 assert not tagset.isdisjoint(set(['inbox']))
57 def test_issubset(self, tagset):
58 assert {'inbox'} <= tagset
59 assert {'inbox'}.issubset(tagset)
60 assert tagset <= {'inbox', 'unread', 'spam'}
61 assert tagset.issubset({'inbox', 'unread', 'spam'})
63 def test_issuperset(self, tagset):
64 assert {'inbox', 'unread', 'spam'} >= tagset
65 assert {'inbox', 'unread', 'spam'}.issuperset(tagset)
66 assert tagset >= {'inbox'}
67 assert tagset.issuperset({'inbox'})
69 def test_iter(self, tagset):
70 expected = sorted(['unread', 'inbox'])
73 assert isinstance(tag, str)
75 assert expected == sorted(found)
77 def test_special_iter(self, tagset):
78 expected = sorted([b'unread', b'inbox'])
80 for tag in tagset.iter():
81 assert isinstance(tag, bytes)
83 assert expected == sorted(found)
85 def test_special_iter_codec(self, tagset):
86 for tag in tagset.iter(encoding='ascii', errors='surrogateescape'):
87 assert isinstance(tag, str)
89 def test_len(self, tagset):
90 assert len(tagset) == 2
92 def test_and(self, tagset):
93 common = tagset & {'unread'}
94 assert isinstance(common, set)
95 assert isinstance(common, collections.abc.Set)
96 assert common == {'unread'}
97 common = tagset.intersection({'unread'})
98 assert isinstance(common, set)
99 assert isinstance(common, collections.abc.Set)
100 assert common == {'unread'}
102 def test_or(self, tagset):
103 res = tagset | {'foo'}
104 assert isinstance(res, set)
105 assert isinstance(res, collections.abc.Set)
106 assert res == {'unread', 'inbox', 'foo'}
107 res = tagset.union({'foo'})
108 assert isinstance(res, set)
109 assert isinstance(res, collections.abc.Set)
110 assert res == {'unread', 'inbox', 'foo'}
112 def test_sub(self, tagset):
113 res = tagset - {'unread'}
114 assert isinstance(res, set)
115 assert isinstance(res, collections.abc.Set)
116 assert res == {'inbox'}
117 res = tagset.difference({'unread'})
118 assert isinstance(res, set)
119 assert isinstance(res, collections.abc.Set)
120 assert res == {'inbox'}
122 def test_rsub(self, tagset):
123 res = {'foo', 'unread'} - tagset
124 assert isinstance(res, set)
125 assert isinstance(res, collections.abc.Set)
126 assert res == {'foo'}
128 def test_xor(self, tagset):
129 res = tagset ^ {'unread', 'foo'}
130 assert isinstance(res, set)
131 assert isinstance(res, collections.abc.Set)
132 assert res == {'inbox', 'foo'}
133 res = tagset.symmetric_difference({'unread', 'foo'})
134 assert isinstance(res, set)
135 assert isinstance(res, collections.abc.Set)
136 assert res == {'inbox', 'foo'}
138 def test_rxor(self, tagset):
139 res = {'unread', 'foo'} ^ tagset
140 assert isinstance(res, set)
141 assert isinstance(res, collections.abc.Set)
142 assert res == {'inbox', 'foo'}
144 def test_copy(self, tagset):
146 assert isinstance(res, set)
147 assert isinstance(res, collections.abc.Set)
148 assert res == {'inbox', 'unread'}
151 class TestMutableTagset:
154 def tagset(self, maildir, notmuch):
155 """An non-empty mutable tagset.
157 This will have the default new mail tags: inbox, unread.
159 _, pathname = maildir.deliver()
161 with database.Database(maildir.path,
162 mode=database.Mode.READ_WRITE,
163 config=database.Database.CONFIG.EMPTY) as db:
164 msg = db.get(pathname)
167 def test_type(self, tagset):
168 assert isinstance(tagset, collections.abc.MutableSet)
169 assert isinstance(tagset, tags.MutableTagSet)
171 def test_hash(self, tagset):
172 assert not isinstance(tagset, collections.abc.Hashable)
173 with pytest.raises(TypeError):
176 def test_add(self, tagset):
177 assert 'foo' not in tagset
179 assert 'foo' in tagset
181 def test_discard(self, tagset):
182 assert 'inbox' in tagset
183 tagset.discard('inbox')
184 assert 'inbox' not in tagset
186 def test_discard_not_present(self, tagset):
187 assert 'foo' not in tagset
188 tagset.discard('foo')
190 def test_clear(self, tagset):
191 assert len(tagset) > 0
193 assert len(tagset) == 0
195 def test_from_maildir_flags(self, maildir, notmuch):
196 _, pathname = maildir.deliver(flagged=True)
198 with database.Database(maildir.path,
199 mode=database.Mode.READ_WRITE,
200 config=database.Database.CONFIG.EMPTY) as db:
201 msg = db.get(pathname)
202 msg.tags.discard('flagged')
203 msg.tags.from_maildir_flags()
204 assert 'flagged' in msg.tags
206 def test_to_maildir_flags(self, maildir, notmuch):
207 _, pathname = maildir.deliver(flagged=True)
209 with database.Database(maildir.path,
210 mode=database.Mode.READ_WRITE,
211 config=database.Database.CONFIG.EMPTY) as db:
212 msg = db.get(pathname)
213 flags = msg.path.name.split(',')[-1]
215 msg.tags.discard('flagged')
216 msg.tags.to_maildir_flags()
217 flags = msg.path.name.split(',')[-1]
218 assert 'F' not in flags
220 def test_isdisjoint(self, tagset):
221 assert tagset.isdisjoint(set(['spam', 'ham']))
222 assert not tagset.isdisjoint(set(['inbox']))
224 def test_issubset(self, tagset):
225 assert {'inbox'} <= tagset
226 assert {'inbox'}.issubset(tagset)
227 assert not {'spam'} <= tagset
228 assert not {'spam'}.issubset(tagset)
229 assert tagset <= {'inbox', 'unread', 'spam'}
230 assert tagset.issubset({'inbox', 'unread', 'spam'})
231 assert not {'inbox', 'unread', 'spam'} <= tagset
232 assert not {'inbox', 'unread', 'spam'}.issubset(tagset)
234 def test_issuperset(self, tagset):
235 assert {'inbox', 'unread', 'spam'} >= tagset
236 assert {'inbox', 'unread', 'spam'}.issuperset(tagset)
237 assert tagset >= {'inbox'}
238 assert tagset.issuperset({'inbox'})
240 def test_union(self, tagset):
241 assert {'spam'}.union(tagset) == {'inbox', 'unread', 'spam'}
242 assert tagset.union({'spam'}) == {'inbox', 'unread', 'spam'}