1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 10:19:16 +02:00

add some basic array handling and a get_chat_msgs()

This commit is contained in:
holger krekel 2018-09-13 22:56:02 +02:00
parent 41e3bb3aaa
commit d24a1b5375
3 changed files with 30 additions and 10 deletions

View file

@ -2,6 +2,7 @@ import pytest
import re
import threading
from deltachat import Account
from deltachat.types import cached_property
def pytest_addoption(parser):
@ -15,21 +16,23 @@ def pytest_addoption(parser):
@pytest.fixture
def acfactory(pytestconfig, tmpdir, request):
fn = pytestconfig.getoption("--liveconfig")
if not fn:
pytest.skip("specify a --liveconfig file to run tests with real accounts")
class AccountMaker:
def __init__(self):
self.configlist = []
self.live_count = 0
self.offline_count = 0
@cached_property
def configlist (self):
configlist = []
for line in open(fn):
if line.strip():
d = {}
for part in line.split():
name, value = part.split("=")
d[name] = value
self.configlist.append(d)
self.live_count = 0
self.offline_count = 0
configlist.append(d)
return configlist
def get_offline_account(self):
self.offline_count += 1
@ -39,6 +42,8 @@ def acfactory(pytestconfig, tmpdir, request):
return ac
def get_live_account(self, started=True):
if not fn:
pytest.skip("specify a --liveconfig file to run tests with real accounts")
self.live_count += 1
configdict = self.configlist.pop(0)
tmpdb = tmpdir.join("livedb%d" % self.live_count)

View file

@ -9,7 +9,7 @@ except ImportError:
import deltachat
from . import capi
from .capi import ffi
from .capi import ffi, lib
from .types import cached_property
import attr
from attr import validators as v
@ -120,12 +120,18 @@ class Chat(object):
capi.lib.dc_chat_unref(self.dc_chat_t)
def send_text_message(self, msg):
""" send a text message and return Message instance. """
""" send a text message and return the resulting Message instance. """
msg = convert_to_bytes_utf8(msg)
print ("chat id", self.id)
msg_id = capi.lib.dc_send_text_msg(self.dc_context, self.id, msg)
return Message(self.dc_context, msg_id)
def get_messages(self):
""" return list of messages in this chat. """
dc_array_t = lib.dc_get_chat_msgs(self.dc_context, self.id, 0, 0)
return map(lambda x: Message(self.dc_context, x),
iter_array_and_unref(dc_array_t))
@attr.s
class Message(object):
@ -272,5 +278,13 @@ def convert_to_bytes_utf8(obj):
return obj
def iter_array_and_unref(dc_array_t):
try:
for i in range(0, lib.dc_array_get_cnt(dc_array_t)):
yield lib.dc_array_get_id(dc_array_t, i)
finally:
lib.dc_array_unref(dc_array_t)
def ffi_unicode(obj):
return ffi.string(obj).decode("utf8")

View file

@ -98,5 +98,6 @@ class TestOnlineAccount:
assert ev[2] == msg.id
msg = ac2.get_message_by_id(msg.id)
assert msg.text == "msg1"
# note that ev[1] aka data1 contains a bogus channel id
# probably should just not get passed from the core
messages = msg.chat.get_messages()
assert msg in messages, (msg, messages)