]> git.notmuchmail.org Git - notmuch/blob - bindings/python-cffi/tests/test_base.py
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / bindings / python-cffi / tests / test_base.py
1 import pytest
2
3 from notmuch2 import _base as base
4 from notmuch2 import _errors as errors
5
6
7 class TestNotmuchObject:
8
9     def test_no_impl_methods(self):
10         class Object(base.NotmuchObject):
11             pass
12         with pytest.raises(TypeError):
13             Object()
14
15     def test_impl_methods(self):
16
17         class Object(base.NotmuchObject):
18
19             def __init__(self):
20                 pass
21
22             @property
23             def alive(self):
24                 pass
25
26             def _destroy(self, parent=False):
27                 pass
28
29         Object()
30
31     def test_del(self):
32         destroyed = False
33
34         class Object(base.NotmuchObject):
35
36             def __init__(self):
37                 pass
38
39             @property
40             def alive(self):
41                 pass
42
43             def _destroy(self, parent=False):
44                 nonlocal destroyed
45                 destroyed = True
46
47         o = Object()
48         o.__del__()
49         assert destroyed
50
51
52 class TestMemoryPointer:
53
54     @pytest.fixture
55     def obj(self):
56         class Cls:
57             ptr = base.MemoryPointer()
58         return Cls()
59
60     def test_unset(self, obj):
61         with pytest.raises(errors.ObjectDestroyedError):
62             obj.ptr
63
64     def test_set(self, obj):
65         obj.ptr = 'some'
66         assert obj.ptr == 'some'
67
68     def test_cleared(self, obj):
69         obj.ptr = 'some'
70         obj.ptr
71         obj.ptr = None
72         with pytest.raises(errors.ObjectDestroyedError):
73             obj.ptr
74
75     def test_two_instances(self, obj):
76         obj2 = obj.__class__()
77         obj.ptr = 'foo'
78         obj2.ptr = 'bar'
79         assert obj.ptr != obj2.ptr
80
81
82 class TestBinString:
83
84     def test_type(self):
85         s = base.BinString(b'foo')
86         assert isinstance(s, str)
87
88     def test_init_bytes(self):
89         s = base.BinString(b'foo')
90         assert s == 'foo'
91
92     def test_init_str(self):
93         s = base.BinString('foo')
94         assert s == 'foo'
95
96     def test_bytes(self):
97         s = base.BinString(b'foo')
98         assert bytes(s) == b'foo'
99
100     def test_invalid_utf8(self):
101         s = base.BinString(b'\x80foo')
102         assert s == 'foo'
103         assert bytes(s) == b'\x80foo'
104
105     def test_errors(self):
106         s = base.BinString(b'\x80foo', errors='replace')
107         assert s == '�foo'
108         assert bytes(s) == b'\x80foo'
109
110     def test_encoding(self):
111         # pound sign: '£' == '\u00a3' latin-1: b'\xa3', utf-8: b'\xc2\xa3'
112         with pytest.raises(UnicodeDecodeError):
113             base.BinString(b'\xa3', errors='strict')
114         s = base.BinString(b'\xa3', encoding='latin-1', errors='strict')
115         assert s == '£'
116         assert bytes(s) == b'\xa3'