1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-05 02:29:28 +02:00

some initial test code, also fixing callback machinery to actually call the callback (thanks flub for the hint), also test file rename

This commit is contained in:
holger krekel 2018-09-04 15:38:18 +02:00
parent 9967b2127a
commit eec2261cca
4 changed files with 100 additions and 18 deletions

View file

@ -11,4 +11,8 @@ def py_dc_callback(ctx, evt, data1, data2):
CFFI only allows us to set one global event handler, so this one CFFI only allows us to set one global event handler, so this one
looks up the correct event handler for the given context. looks up the correct event handler for the given context.
""" """
return _DC_CALLBACK_MAP.get(ctx, lambda *a: 0) callback = _DC_CALLBACK_MAP.get(ctx, lambda *a: 0)
ret = callback(ctx, evt, data1, data2)
if ret is None:
return 0
return ret

View file

@ -1,6 +1,8 @@
import pytest import pytest
import deltachat import deltachat
from deltachat import capi
import threading
@pytest.fixture @pytest.fixture
@ -13,3 +15,56 @@ def register_dc_callback(monkeypatch):
def register_dc_callback(ctx, func): def register_dc_callback(ctx, func):
monkeypatch.setitem(deltachat._DC_CALLBACK_MAP, ctx, func) monkeypatch.setitem(deltachat._DC_CALLBACK_MAP, ctx, func)
return register_dc_callback return register_dc_callback
def pytest_addoption(parser):
parser.addoption("--user", action="store", default=None,
help="user and domain of test account: example user@example.org")
parser.addoption("--password", action="store", default=None)
@pytest.fixture
def userpassword(pytestconfig):
user = pytestconfig.getoption("--user")
passwd = pytestconfig.getoption("--password")
if user and passwd:
return user, passwd
pytest.skip("specify a test account with --user and --password options")
def imap_thread(context, quitflag):
print ("starting imap thread")
while not quitflag.is_set():
capi.lib.dc_perform_imap_jobs(context)
capi.lib.dc_perform_imap_fetch(context)
capi.lib.dc_perform_imap_idle(context)
def smtp_thread(context, quitflag):
print ("starting smtp thread")
while not quitflag.is_set():
capi.lib.dc_perform_smtp_jobs(context)
capi.lib.dc_perform_smtp_idle(context)
@pytest.fixture
def dc_context():
ctx = capi.lib.dc_context_new(capi.lib.py_dc_callback,
capi.ffi.NULL, capi.ffi.NULL)
yield ctx
capi.lib.dc_close(ctx)
@pytest.fixture
def dc_threads(dc_context):
quitflag = threading.Event()
t1 = threading.Thread(target=imap_thread, name="imap", args=[dc_context, quitflag])
t1.setDaemon(1)
t1.start()
t2 = threading.Thread(target=smtp_thread, name="smtp", args=[dc_context, quitflag])
t2.setDaemon(1)
t2.start()
yield
quitflag.set()

View file

@ -0,0 +1,40 @@
from __future__ import print_function
import deltachat
from deltachat import capi
import queue
def test_empty_context():
ctx = capi.lib.dc_context_new(capi.ffi.NULL, capi.ffi.NULL, capi.ffi.NULL)
capi.lib.dc_close(ctx)
def test_cb(register_dc_callback):
def cb(ctx, evt, data1, data2):
return 0
ctx = capi.lib.dc_context_new(capi.lib.py_dc_callback,
capi.ffi.NULL, capi.ffi.NULL)
register_dc_callback(ctx, cb)
capi.lib.dc_close(ctx)
assert deltachat._DC_CALLBACK_MAP[ctx] is cb
def test_basic_events(dc_context, dc_threads, register_dc_callback, tmpdir, userpassword):
q = queue.Queue()
def cb(dc_context, evt, data1, data2):
q.put((evt, data1, data2))
return 0
register_dc_callback(dc_context, cb)
dbfile = tmpdir.join("test.db")
capi.lib.dc_open(dc_context, dbfile.strpath, capi.ffi.NULL)
capi.lib.dc_set_config(dc_context, "addr", userpassword[0])
capi.lib.dc_set_config(dc_context, "mail_pw", userpassword[1])
capi.lib.dc_configure(dc_context)
while 1:
evt1, data1, data2 = q.get(timeout=1.0)
if evt1 == 100:
print ("info event", data2)
elif evt1:
print ("other event", evt1, data1, data2)

View file

@ -1,17 +0,0 @@
import deltachat
from deltachat import capi
def test_empty_context():
ctx = capi.lib.dc_context_new(capi.ffi.NULL, capi.ffi.NULL, capi.ffi.NULL)
capi.lib.dc_close(ctx)
def test_cb(register_dc_callback):
def cb(ctx, evt, data1, data2):
return 0
ctx = capi.lib.dc_context_new(capi.lib.py_dc_callback,
capi.ffi.NULL, capi.ffi.NULL)
register_dc_callback(ctx, cb)
capi.lib.dc_close(ctx)
assert deltachat._DC_CALLBACK_MAP[ctx] is cb