Skip to content

Commit 4caf540

Browse files
authored
Add tests for exports (#72)
1 parent 6784c5f commit 4caf540

File tree

5 files changed

+89
-23
lines changed

5 files changed

+89
-23
lines changed

stream_chat/async_chat/client.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,17 @@ async def revoke_users_token(self, user_ids, before):
581581
)
582582
await self.update_users_partial(updates)
583583

584-
async def export_channel(self, channel_id: str, channel_type: str, messages_since: str = None,
585-
messages_until=None):
584+
async def export_channel(
585+
self,
586+
channel_type,
587+
channel_id,
588+
messages_since=None,
589+
messages_until=None,
590+
):
586591
"""
587592
Requests a channel export
588-
:param channel_id: channel_id of channel which needs to be exported
589593
:param channel_type: channel_type of channel which needs to be exported
594+
:param channel_id: channel_id of channel which needs to be exported
590595
:param messages_since: RFC-3339 string or datetime to filter messages since that time, optional
591596
:param messages_until: RFC-3339 string or datetime to filter messages until that time, optional
592597
:type channel_id: str
@@ -605,22 +610,22 @@ async def export_channel(self, channel_id: str, channel_type: str, messages_sinc
605610
"id": channel_id,
606611
"type": channel_type,
607612
"messages_since": messages_since,
608-
"messages_until": messages_until
613+
"messages_until": messages_until,
609614
}
610615
]
611616
)
612617

613-
async def export_channels(self, channels_data: list):
618+
async def export_channels(self, channels):
614619
"""
615620
Requests a channels export
616621
:param channels_data: list of channel's data which need to be exported with keys:
617-
- `channel_id`: str
618622
- `channel_type`: str
623+
- `channel_id`: str
619624
- `messages_since` (optional, nullable): str
620625
- `messages_until` (optional, nullable): str
621626
:type channels_data: List[Dict[str, str]]
622627
"""
623-
return await self.post("export_channels", data={"channels": channels_data})
628+
return await self.post("export_channels", data={"channels": channels})
624629

625630
async def get_export_channel_status(self, task_id: str):
626631
"""

stream_chat/base/client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,22 +549,23 @@ def revoke_users_token(self, user_ids, since):
549549
pass
550550

551551
@abc.abstractmethod
552-
def export_channel(self, channel_id: str, channel_type: str, messages_since: str = None,
553-
messages_until=None):
552+
def export_channel(
553+
self, channel_type, channel_id, messages_since=None, messages_until=None
554+
):
554555
"""
555556
Requests a channel export
556557
"""
557558
pass
558559

559560
@abc.abstractmethod
560-
def export_channels(self, channels_data: list):
561+
def export_channels(self, channels):
561562
"""
562563
Requests a channels export
563564
"""
564565
pass
565566

566567
@abc.abstractmethod
567-
def get_export_channel_status(self, task_id: str):
568+
def get_export_channel_status(self, task_id):
568569
"""
569570
Retrieves status of export
570571
"""

stream_chat/client.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -573,16 +573,21 @@ def revoke_users_token(self, user_ids, before):
573573
)
574574
self.update_users_partial(updates)
575575

576-
def export_channel(self, channel_id: str, channel_type: str, messages_since: str = None,
577-
messages_until=None):
576+
def export_channel(
577+
self,
578+
channel_type,
579+
channel_id,
580+
messages_since=None,
581+
messages_until=None,
582+
):
578583
"""
579584
Requests a channel export
580-
:param channel_id: channel_id of channel which needs to be exported
581585
:param channel_type: channel_type of channel which needs to be exported
586+
:param channel_id: channel_id of channel which needs to be exported
582587
:param messages_since: RFC-3339 string or datetime to filter messages since that time, optional
583588
:param messages_until: RFC-3339 string or datetime to filter messages until that time, optional
584-
:type channel_id: str
585589
:type channel_type: str
590+
:type channel_id: str
586591
:type messages_since: Union[str, datetime.datetime]
587592
:type messages_until: Union[str, datetime.datetime]
588593
"""
@@ -597,24 +602,24 @@ def export_channel(self, channel_id: str, channel_type: str, messages_since: str
597602
"id": channel_id,
598603
"type": channel_type,
599604
"messages_since": messages_since,
600-
"messages_until": messages_until
605+
"messages_until": messages_until,
601606
}
602607
]
603608
)
604609

605-
def export_channels(self, channels_data: list):
610+
def export_channels(self, channels):
606611
"""
607612
Requests a channels export
608-
:param channels_data: list of channel's data which need to be exported with keys:
609-
- `channel_id`: str
613+
:param channels: list of channel's data which need to be exported with keys:
610614
- `channel_type`: str
615+
- `channel_id`: str
611616
- `messages_since` (optional, nullable): str
612617
- `messages_until` (optional, nullable): str
613-
:type channels_data: List[Dict[str, str]]
618+
:type channels: List[Dict[str, str]]
614619
"""
615-
return self.post("export_channels", data={"channels": channels_data})
620+
return self.post("export_channels", data={"channels": channels})
616621

617-
def get_export_channel_status(self, task_id: str):
622+
def get_export_channel_status(self, task_id):
618623
"""
619624
Retrieves status of export
620625
:param task_id: task_id of task which status needs to be retrieved

stream_chat/tests/async_chat/test_channel.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid
2+
import time
23
import pytest
34

45
from stream_chat.base.exceptions import StreamAPIException
@@ -298,3 +299,31 @@ async def test_mute_umute(self, event_loop, client, channel, random_users):
298299
user_id=user_id,
299300
)
300301
assert len(response["channels"]) == 0
302+
303+
@pytest.mark.asyncio
304+
async def test_export_channel_status(self, client, channel):
305+
with pytest.raises(StreamAPIException, match=r".*Can't find task.*"):
306+
await client.get_export_channel_status(str(uuid.uuid4()))
307+
308+
with pytest.raises(StreamAPIException, match=r".*Can't find channel.*"):
309+
await client.export_channel("messaging", str(uuid.uuid4()))
310+
311+
@pytest.mark.asyncio
312+
async def test_export_channel(self, client, channel, random_users):
313+
await channel.send_message({"text": "Hey Joni"}, random_users[0]["id"])
314+
315+
resp = await client.export_channel(channel.channel_type, channel.id)
316+
task_id = resp["task_id"]
317+
assert task_id != ""
318+
319+
while True:
320+
resp = await client.get_export_channel_status(task_id)
321+
assert resp["status"] != ""
322+
assert resp["created_at"] != ""
323+
assert resp["updated_at"] != ""
324+
if resp["status"] == "completed":
325+
assert len(resp["result"]) != 0
326+
assert resp["result"]["url"] != ""
327+
assert len(resp["error"]) != 0
328+
break
329+
time.sleep(0.5)

stream_chat/tests/test_channel.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid
2+
import time
23

34
import pytest
45

@@ -263,7 +264,6 @@ def test_mute_unmute(self, client, channel, random_users):
263264
response = client.query_channels(
264265
{"muted": True, "cid": channel.cid}, user_id=user_id
265266
)
266-
print(response, channel.cid)
267267
assert len(response["channels"]) == 1
268268

269269
channel.unmute(user_id)
@@ -272,3 +272,29 @@ def test_mute_unmute(self, client, channel, random_users):
272272
user_id=user_id,
273273
)
274274
assert len(response["channels"]) == 0
275+
276+
def test_export_channel_status(self, client, channel):
277+
with pytest.raises(StreamAPIException, match=r".*Can't find task.*"):
278+
client.get_export_channel_status(str(uuid.uuid4()))
279+
280+
with pytest.raises(StreamAPIException, match=r".*Can't find channel.*"):
281+
client.export_channel("messaging", str(uuid.uuid4()))
282+
283+
def test_export_channel(self, client, channel, random_users):
284+
channel.send_message({"text": "Hey Joni"}, random_users[0]["id"])
285+
286+
resp = client.export_channel(channel.channel_type, channel.id)
287+
task_id = resp["task_id"]
288+
assert task_id != ""
289+
290+
while True:
291+
resp = client.get_export_channel_status(task_id)
292+
assert resp["status"] != ""
293+
assert resp["created_at"] != ""
294+
assert resp["updated_at"] != ""
295+
if resp["status"] == "completed":
296+
assert len(resp["result"]) != 0
297+
assert resp["result"]["url"] != ""
298+
assert len(resp["error"]) != 0
299+
break
300+
time.sleep(0.5)

0 commit comments

Comments
 (0)