unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob feb20a0e3f0b9a0056a1c2294dc425d812708b00 7980 bytes (raw)
name: bindings/python-cffi/tests/test_tags.py 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
 
"""Tests for the behaviour of immutable and mutable tagsets.

This module tests the Pythonic behaviour of the sets.
"""

import collections
import subprocess
import textwrap

import pytest

from notmuch2 import _database as database
from notmuch2 import _tags as tags


class TestImmutable:

    @pytest.fixture
    def tagset(self, maildir, notmuch):
        """An non-empty immutable tagset.

        This will have the default new mail tags: inbox, unread.
        """
        maildir.deliver()
        notmuch('new')
        with database.Database(maildir.path, cfg_path="") as db:
            yield db.tags

    def test_type(self, tagset):
        assert isinstance(tagset, tags.ImmutableTagSet)
        assert isinstance(tagset, collections.abc.Set)

    def test_hash(self, tagset, maildir, notmuch):
        h0 = hash(tagset)
        notmuch('tag', '+foo', '*')
        with database.Database(maildir.path) as db:
            h1 = hash(db.tags)
        assert h0 != h1

    def test_eq(self, tagset):
        assert tagset == tagset

    def test_neq(self, tagset, maildir, notmuch):
        notmuch('tag', '+foo', '*')
        with database.Database(maildir.path) as db:
            assert tagset != db.tags

    def test_contains(self, tagset):
        print(tuple(tagset))
        assert 'unread' in tagset
        assert 'foo' not in tagset

    def test_isdisjoint(self, tagset):
        assert tagset.isdisjoint(set(['spam', 'ham']))
        assert not tagset.isdisjoint(set(['inbox']))

    def test_issubset(self, tagset):
        assert {'inbox'} <= tagset
        assert {'inbox'}.issubset(tagset)
        assert tagset <= {'inbox', 'unread', 'spam'}
        assert tagset.issubset({'inbox', 'unread', 'spam'})

    def test_issuperset(self, tagset):
        assert {'inbox', 'unread', 'spam'} >= tagset
        assert {'inbox', 'unread', 'spam'}.issuperset(tagset)
        assert tagset >= {'inbox'}
        assert tagset.issuperset({'inbox'})

    def test_iter(self, tagset):
        expected = sorted(['unread', 'inbox'])
        found = []
        for tag in tagset:
            assert isinstance(tag, str)
            found.append(tag)
        assert expected == sorted(found)

    def test_special_iter(self, tagset):
        expected = sorted([b'unread', b'inbox'])
        found = []
        for tag in tagset.iter():
            assert isinstance(tag, bytes)
            found.append(tag)
        assert expected == sorted(found)

    def test_special_iter_codec(self, tagset):
        for tag in tagset.iter(encoding='ascii', errors='surrogateescape'):
            assert isinstance(tag, str)

    def test_len(self, tagset):
        assert len(tagset) == 2

    def test_and(self, tagset):
        common = tagset & {'unread'}
        assert isinstance(common, set)
        assert isinstance(common, collections.abc.Set)
        assert common == {'unread'}
        common = tagset.intersection({'unread'})
        assert isinstance(common, set)
        assert isinstance(common, collections.abc.Set)
        assert common == {'unread'}

    def test_or(self, tagset):
        res = tagset | {'foo'}
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'unread', 'inbox', 'foo'}
        res = tagset.union({'foo'})
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'unread', 'inbox', 'foo'}

    def test_sub(self, tagset):
        res = tagset - {'unread'}
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'inbox'}
        res = tagset.difference({'unread'})
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'inbox'}

    def test_rsub(self, tagset):
        res = {'foo', 'unread'} - tagset
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'foo'}

    def test_xor(self, tagset):
        res = tagset ^ {'unread', 'foo'}
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'inbox', 'foo'}
        res = tagset.symmetric_difference({'unread', 'foo'})
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'inbox', 'foo'}

    def test_rxor(self, tagset):
        res = {'unread', 'foo'} ^ tagset
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'inbox', 'foo'}

    def test_copy(self, tagset):
        res = tagset.copy()
        assert isinstance(res, set)
        assert isinstance(res, collections.abc.Set)
        assert res == {'inbox', 'unread'}


class TestMutableTagset:

    @pytest.fixture
    def tagset(self, maildir, notmuch):
        """An non-empty mutable tagset.

        This will have the default new mail tags: inbox, unread.
        """
        _, pathname = maildir.deliver()
        notmuch('new')
        with database.Database(maildir.path,
                               mode=database.Mode.READ_WRITE, cfg_path="") as db:
            msg = db.get(pathname)
            yield msg.tags

    def test_type(self, tagset):
        assert isinstance(tagset, collections.abc.MutableSet)
        assert isinstance(tagset, tags.MutableTagSet)

    def test_hash(self, tagset):
        assert not isinstance(tagset, collections.abc.Hashable)
        with pytest.raises(TypeError):
            hash(tagset)

    def test_add(self, tagset):
        assert 'foo' not in tagset
        tagset.add('foo')
        assert 'foo' in tagset

    def test_discard(self, tagset):
        assert 'inbox' in tagset
        tagset.discard('inbox')
        assert 'inbox' not in tagset

    def test_discard_not_present(self, tagset):
        assert 'foo' not in tagset
        tagset.discard('foo')

    def test_clear(self, tagset):
        assert len(tagset) > 0
        tagset.clear()
        assert len(tagset) == 0

    def test_from_maildir_flags(self, maildir, notmuch):
        _, pathname = maildir.deliver(flagged=True)
        notmuch('new')
        with database.Database(maildir.path,
                               mode=database.Mode.READ_WRITE, cfg_path="") as db:
            msg = db.get(pathname)
            msg.tags.discard('flagged')
            msg.tags.from_maildir_flags()
            assert 'flagged' in msg.tags

    def test_to_maildir_flags(self, maildir, notmuch):
        _, pathname = maildir.deliver(flagged=True)
        notmuch('new')
        with database.Database(maildir.path,
                               mode=database.Mode.READ_WRITE, cfg_path="") as db:
            msg = db.get(pathname)
            flags = msg.path.name.split(',')[-1]
            assert 'F' in flags
            msg.tags.discard('flagged')
            msg.tags.to_maildir_flags()
            flags = msg.path.name.split(',')[-1]
            assert 'F' not in flags

    def test_isdisjoint(self, tagset):
        assert tagset.isdisjoint(set(['spam', 'ham']))
        assert not tagset.isdisjoint(set(['inbox']))

    def test_issubset(self, tagset):
        assert {'inbox'} <= tagset
        assert {'inbox'}.issubset(tagset)
        assert not {'spam'} <= tagset
        assert not {'spam'}.issubset(tagset)
        assert tagset <= {'inbox', 'unread', 'spam'}
        assert tagset.issubset({'inbox', 'unread', 'spam'})
        assert not {'inbox', 'unread', 'spam'} <= tagset
        assert not {'inbox', 'unread', 'spam'}.issubset(tagset)

    def test_issuperset(self, tagset):
        assert {'inbox', 'unread', 'spam'} >= tagset
        assert {'inbox', 'unread', 'spam'}.issuperset(tagset)
        assert tagset >= {'inbox'}
        assert tagset.issuperset({'inbox'})

    def test_union(self, tagset):
        assert {'spam'}.union(tagset) == {'inbox', 'unread', 'spam'}
        assert tagset.union({'spam'}) == {'inbox', 'unread', 'spam'}

debug log:

solving feb20a0e ...
found feb20a0e in https://yhetil.org/notmuch/20211027023653.3535874-3-david@tethera.net/
found faf3947b in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 faf3947b6090f901a63f5828c962fc64e395591f	bindings/python-cffi/tests/test_tags.py

applying [1/1] https://yhetil.org/notmuch/20211027023653.3535874-3-david@tethera.net/
diff --git a/bindings/python-cffi/tests/test_tags.py b/bindings/python-cffi/tests/test_tags.py
index faf3947b..feb20a0e 100644

Checking patch bindings/python-cffi/tests/test_tags.py...
Applied patch bindings/python-cffi/tests/test_tags.py cleanly.

index at:
100644 feb20a0e3f0b9a0056a1c2294dc425d812708b00	bindings/python-cffi/tests/test_tags.py

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).