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:
parent
41e3bb3aaa
commit
d24a1b5375
3 changed files with 30 additions and 10 deletions
|
@ -2,6 +2,7 @@ import pytest
|
||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
from deltachat import Account
|
from deltachat import Account
|
||||||
|
from deltachat.types import cached_property
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
|
@ -15,21 +16,23 @@ def pytest_addoption(parser):
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def acfactory(pytestconfig, tmpdir, request):
|
def acfactory(pytestconfig, tmpdir, request):
|
||||||
fn = pytestconfig.getoption("--liveconfig")
|
fn = pytestconfig.getoption("--liveconfig")
|
||||||
if not fn:
|
|
||||||
pytest.skip("specify a --liveconfig file to run tests with real accounts")
|
|
||||||
|
|
||||||
class AccountMaker:
|
class AccountMaker:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.configlist = []
|
self.live_count = 0
|
||||||
|
self.offline_count = 0
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def configlist (self):
|
||||||
|
configlist = []
|
||||||
for line in open(fn):
|
for line in open(fn):
|
||||||
if line.strip():
|
if line.strip():
|
||||||
d = {}
|
d = {}
|
||||||
for part in line.split():
|
for part in line.split():
|
||||||
name, value = part.split("=")
|
name, value = part.split("=")
|
||||||
d[name] = value
|
d[name] = value
|
||||||
self.configlist.append(d)
|
configlist.append(d)
|
||||||
self.live_count = 0
|
return configlist
|
||||||
self.offline_count = 0
|
|
||||||
|
|
||||||
def get_offline_account(self):
|
def get_offline_account(self):
|
||||||
self.offline_count += 1
|
self.offline_count += 1
|
||||||
|
@ -39,6 +42,8 @@ def acfactory(pytestconfig, tmpdir, request):
|
||||||
return ac
|
return ac
|
||||||
|
|
||||||
def get_live_account(self, started=True):
|
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
|
self.live_count += 1
|
||||||
configdict = self.configlist.pop(0)
|
configdict = self.configlist.pop(0)
|
||||||
tmpdb = tmpdir.join("livedb%d" % self.live_count)
|
tmpdb = tmpdir.join("livedb%d" % self.live_count)
|
||||||
|
|
|
@ -9,7 +9,7 @@ except ImportError:
|
||||||
|
|
||||||
import deltachat
|
import deltachat
|
||||||
from . import capi
|
from . import capi
|
||||||
from .capi import ffi
|
from .capi import ffi, lib
|
||||||
from .types import cached_property
|
from .types import cached_property
|
||||||
import attr
|
import attr
|
||||||
from attr import validators as v
|
from attr import validators as v
|
||||||
|
@ -120,12 +120,18 @@ class Chat(object):
|
||||||
capi.lib.dc_chat_unref(self.dc_chat_t)
|
capi.lib.dc_chat_unref(self.dc_chat_t)
|
||||||
|
|
||||||
def send_text_message(self, msg):
|
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)
|
msg = convert_to_bytes_utf8(msg)
|
||||||
print ("chat id", self.id)
|
print ("chat id", self.id)
|
||||||
msg_id = capi.lib.dc_send_text_msg(self.dc_context, self.id, msg)
|
msg_id = capi.lib.dc_send_text_msg(self.dc_context, self.id, msg)
|
||||||
return Message(self.dc_context, msg_id)
|
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
|
@attr.s
|
||||||
class Message(object):
|
class Message(object):
|
||||||
|
@ -272,5 +278,13 @@ def convert_to_bytes_utf8(obj):
|
||||||
return 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):
|
def ffi_unicode(obj):
|
||||||
return ffi.string(obj).decode("utf8")
|
return ffi.string(obj).decode("utf8")
|
||||||
|
|
|
@ -98,5 +98,6 @@ class TestOnlineAccount:
|
||||||
assert ev[2] == msg.id
|
assert ev[2] == msg.id
|
||||||
msg = ac2.get_message_by_id(msg.id)
|
msg = ac2.get_message_by_id(msg.id)
|
||||||
assert msg.text == "msg1"
|
assert msg.text == "msg1"
|
||||||
# note that ev[1] aka data1 contains a bogus channel id
|
messages = msg.chat.get_messages()
|
||||||
# probably should just not get passed from the core
|
assert msg in messages, (msg, messages)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue