1 """Tests for the behaviour of immutable and mutable tagsets.
3 This module tests the Pythonic behaviour of the sets.
12 from notdb import _database as database
13 from notdb 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) 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) 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) 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_iter(self, tagset):
54 expected = sorted(['unread', 'inbox'])
57 assert isinstance(tag, str)
59 assert expected == sorted(found)
61 def test_special_iter(self, tagset):
62 expected = sorted([b'unread', b'inbox'])
64 for tag in tagset.iter():
65 assert isinstance(tag, bytes)
67 assert expected == sorted(found)
69 def test_special_iter_codec(self, tagset):
70 for tag in tagset.iter(encoding='ascii', errors='surrogateescape'):
71 assert isinstance(tag, str)
73 def test_len(self, tagset):
74 assert len(tagset) == 2
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'}
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'}
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'}
94 def test_rsub(self, tagset):
95 res = {'foo', 'unread'} - tagset
96 assert isinstance(res, set)
97 assert isinstance(res, collections.abc.Set)
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'}
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'}
113 class TestMutableTagset:
116 def tagset(self, maildir, notmuch):
117 """An non-empty mutable tagset.
119 This will have the default new mail tags: inbox, unread.
121 _, pathname = maildir.deliver()
123 with database.Database(maildir.path,
124 mode=database.Mode.READ_WRITE) as db:
125 msg = db.get(pathname)
128 def test_type(self, tagset):
129 assert isinstance(tagset, collections.abc.MutableSet)
130 assert isinstance(tagset, tags.MutableTagSet)
132 def test_hash(self, tagset):
133 assert not isinstance(tagset, collections.abc.Hashable)
134 with pytest.raises(TypeError):
137 def test_add(self, tagset):
138 assert 'foo' not in tagset
140 assert 'foo' in tagset
142 def test_discard(self, tagset):
143 assert 'inbox' in tagset
144 tagset.discard('inbox')
145 assert 'inbox' not in tagset
147 def test_discard_not_present(self, tagset):
148 assert 'foo' not in tagset
149 tagset.discard('foo')
151 def test_clear(self, tagset):
152 assert len(tagset) > 0
154 assert len(tagset) == 0
156 def test_from_maildir_flags(self, maildir, notmuch):
157 _, pathname = maildir.deliver(flagged=True)
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
166 def test_to_maildir_flags(self, maildir, notmuch):
167 _, pathname = maildir.deliver(flagged=True)
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]
174 msg.tags.discard('flagged')
175 msg.tags.to_maildir_flags()
176 flags = msg.path.name.split(',')[-1]
177 assert 'F' not in flags