]> git.cworth.org Git - notmuch/blob - bindings/python-cffi/tests/test_config.py
python-cffi: use config_pairs API in ConfigIterator
[notmuch] / bindings / python-cffi / tests / test_config.py
1 import collections.abc
2
3 import pytest
4
5 import notmuch2._database as dbmod
6
7 import notmuch2._config as config
8
9
10 class TestIter:
11
12     @pytest.fixture
13     def db(self, maildir):
14         with dbmod.Database.create(maildir.path) as db:
15             yield db
16
17     def test_type(self, db):
18         assert isinstance(db.config, collections.abc.MutableMapping)
19         assert isinstance(db.config, config.ConfigMapping)
20
21     def test_alive(self, db):
22         assert db.config.alive
23
24     def test_set_get(self, maildir):
25         # Ensure get-set works from different db objects
26         with dbmod.Database.create(maildir.path, config=dbmod.Database.CONFIG.EMPTY) as db0:
27             db0.config['spam'] = 'ham'
28         with dbmod.Database(maildir.path, config=dbmod.Database.CONFIG.EMPTY) as db1:
29             assert db1.config['spam'] == 'ham'
30
31     def test_get_keyerror(self, db):
32         with pytest.raises(KeyError):
33             val = db.config['not-a-key']
34             print(repr(val))
35
36     def test_iter(self, db):
37         def has_prefix(x):
38             return x.startswith('TEST.')
39
40         assert [ x for x in db.config if has_prefix(x) ] == []
41         db.config['TEST.spam'] = 'TEST.ham'
42         db.config['TEST.eggs'] = 'TEST.bacon'
43         assert { x for x in db.config if has_prefix(x) } == {'TEST.spam', 'TEST.eggs'}
44         assert { x for x in db.config.keys() if has_prefix(x) } == {'TEST.spam', 'TEST.eggs'}
45         assert { x for x in db.config.values() if has_prefix(x) } == {'TEST.ham', 'TEST.bacon'}
46         assert { (x, y) for (x,y) in db.config.items() if has_prefix(x) } == \
47             {('TEST.spam', 'TEST.ham'), ('TEST.eggs', 'TEST.bacon')}
48
49     def test_len(self, db):
50         defaults = len(db.config)
51         db.config['spam'] = 'ham'
52         assert len(db.config) == defaults + 1
53         db.config['eggs'] = 'bacon'
54         assert len(db.config) == defaults + 2
55
56     def test_del(self, db):
57         db.config['spam'] = 'ham'
58         assert db.config.get('spam') == 'ham'
59         del db.config['spam']
60         assert db.config.get('spam') is None