From d24a1b5375136e9597ab6e7e30df615cde241a1e Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 13 Sep 2018 22:56:02 +0200 Subject: [PATCH] add some basic array handling and a get_chat_msgs() --- python/conftest.py | 17 +++++++++++------ python/src/deltachat/account.py | 18 ++++++++++++++++-- python/tests/test_account.py | 5 +++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/python/conftest.py b/python/conftest.py index 5455a57b..9d7d67a3 100644 --- a/python/conftest.py +++ b/python/conftest.py @@ -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) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index b37947bc..2cb502fa 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -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") diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 953f1c91..1cb5d059 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -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) +