aboutsummaryrefslogtreecommitdiff
path: root/bindings/python-cffi/tests/test_thread.py
diff options
context:
space:
mode:
authorFloris Bruynooghe <flub@google.com>2019-10-08 23:03:12 +0200
committerDavid Bremner <david@tethera.net>2019-12-03 08:12:30 -0400
commit83c2d158983875bf77a9b7662894df585b61741c (patch)
tree8443e3ab530a9cbf00b17c395f03e19138d3bae0 /bindings/python-cffi/tests/test_thread.py
parent5f9ea4d2908a597acaf0b809b6f27fa74b70520b (diff)
Introduce CFFI-based python bindings
This introduces CFFI-based Python3-only bindings. The bindings aim at: - Better performance on pypy - Easier to use Python-C interface - More "pythonic" - The API should not allow invalid operations - Use native object protocol where possible - Memory safety; whatever you do from python, it should not coredump.
Diffstat (limited to 'bindings/python-cffi/tests/test_thread.py')
-rw-r--r--bindings/python-cffi/tests/test_thread.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/bindings/python-cffi/tests/test_thread.py b/bindings/python-cffi/tests/test_thread.py
new file mode 100644
index 00000000..366bd8a5
--- /dev/null
+++ b/bindings/python-cffi/tests/test_thread.py
@@ -0,0 +1,102 @@
+import collections.abc
+import time
+
+import pytest
+
+import notdb
+
+
+@pytest.fixture
+def thread(maildir, notmuch):
+ """Return a single thread with one matched message."""
+ msgid, _ = maildir.deliver(body='foo')
+ maildir.deliver(body='bar',
+ headers=[('In-Reply-To', '<{}>'.format(msgid))])
+ notmuch('new')
+ with notdb.Database(maildir.path) as db:
+ yield next(db.threads('foo'))
+
+
+def test_type(thread):
+ assert isinstance(thread, notdb.Thread)
+ assert isinstance(thread, collections.abc.Iterable)
+
+
+def test_threadid(thread):
+ assert isinstance(thread.threadid, notdb.BinString)
+ assert thread.threadid
+
+
+def test_len(thread):
+ assert len(thread) == 2
+
+
+def test_toplevel_type(thread):
+ assert isinstance(thread.toplevel(), collections.abc.Iterator)
+
+
+def test_toplevel(thread):
+ msgs = thread.toplevel()
+ assert isinstance(next(msgs), notdb.Message)
+ with pytest.raises(StopIteration):
+ next(msgs)
+
+
+def test_toplevel_reply(thread):
+ msg = next(thread.toplevel())
+ assert isinstance(next(msg.replies()), notdb.Message)
+
+
+def test_iter(thread):
+ msgs = list(iter(thread))
+ assert len(msgs) == len(thread)
+ for msg in msgs:
+ assert isinstance(msg, notdb.Message)
+
+
+def test_matched(thread):
+ assert thread.matched == 1
+
+
+def test_authors_type(thread):
+ assert isinstance(thread.authors, notdb.BinString)
+
+
+def test_authors(thread):
+ assert thread.authors == 'src@example.com'
+
+
+def test_subject(thread):
+ assert thread.subject == 'Test mail'
+
+
+def test_first(thread):
+ # XXX Someone seems to treat things as local time instead of
+ # UTC or the other way around.
+ now = int(time.time())
+ assert abs(now - thread.first) < 3600*24
+
+
+def test_last(thread):
+ # XXX Someone seems to treat things as local time instead of
+ # UTC or the other way around.
+ now = int(time.time())
+ assert abs(now - thread.last) < 3600*24
+
+
+def test_first_last(thread):
+ # Sadly we only have second resolution so these will always be the
+ # same time in our tests.
+ assert thread.first <= thread.last
+
+
+def test_tags_type(thread):
+ assert isinstance(thread.tags, notdb.ImmutableTagSet)
+
+
+def test_tags_cache(thread):
+ assert thread.tags is thread.tags
+
+
+def test_tags(thread):
+ assert 'inbox' in thread.tags