Skip to content

Commit edbb0f8

Browse files
author
Rafael Marinho
committed
tests(cha-648) fix unit tests
1 parent 1a34fc8 commit edbb0f8

File tree

5 files changed

+61
-41
lines changed

5 files changed

+61
-41
lines changed

stream_chat/async_chat/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,13 +959,15 @@ async def get_user_locations(self, user_id: str, **options: Any) -> StreamRespon
959959

960960
async def update_user_location(
961961
self,
962+
user_id: str,
962963
message_id: str,
963964
options: Optional[SharedLocationsOptions] = None,
964965
) -> StreamResponse:
965966
data = {"message_id": message_id}
966967
if options is not None:
967968
data.update(cast(dict, options))
968-
return await self.put("users/live_locations", data=data)
969+
params = {"user_id": user_id, **options}
970+
return await self.put("users/live_locations", data=data, params=params)
969971

970972
async def close(self) -> None:
971973
await self.session.close()

stream_chat/base/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ def get_user_locations(
15171517

15181518
@abc.abstractmethod
15191519
def update_user_location(
1520-
self, message_id: str, options: Optional[SharedLocationsOptions] = None
1520+
self, user_id: str, message_id: str, options: Optional[SharedLocationsOptions] = None
15211521
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
15221522
"""
15231523
Update the location of a user.

stream_chat/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,12 @@ def get_user_locations(self, user_id: str, **options: Any) -> StreamResponse:
906906

907907
def update_user_location(
908908
self,
909+
user_id: str,
909910
message_id: str,
910911
options: Optional[SharedLocationsOptions] = None,
911912
) -> StreamResponse:
912913
data = {"message_id": message_id}
913914
if options is not None:
914915
data.update(cast(dict, options))
915-
return self.put("users/live_locations", data=data)
916+
params = {"user_id": user_id, **options}
917+
return self.put("users/live_locations", data=data, params=params)

stream_chat/tests/async_chat/test_live_locations.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from typing import Dict
23

34
import pytest
@@ -7,15 +8,28 @@
78

89
@pytest.mark.incremental
910
class TestLiveLocations:
10-
async def test_get_user_locations(self, client: StreamChatAsync, random_user: Dict):
11-
# First create a message to attach location to
12-
channel = client.channel("messaging", str(random_user["id"]))
13-
channel.create(random_user["id"])
11+
@pytest.fixture(autouse=True)
12+
@pytest.mark.asyncio
13+
async def setup_channel_for_shared_locations(self, channel):
14+
await channel.update_partial(
15+
{"config_overrides": {"shared_locations": True}},
16+
)
17+
yield
18+
await channel.update_partial(
19+
{"config_overrides": {"shared_locations": False}},
20+
)
1421

22+
async def test_get_user_locations(
23+
self, client: StreamChatAsync, channel, random_user: Dict
24+
):
1525
# Create a message to attach location to
26+
now = datetime.datetime.now(datetime.timezone.utc)
27+
one_hour_later = now + datetime.timedelta(hours=1)
1628
shared_location = {
29+
"created_by_device_id": "test_device_id",
1730
"latitude": 37.7749,
1831
"longitude": -122.4194,
32+
"end_at": one_hour_later.isoformat(),
1933
}
2034

2135
channel.send_message(
@@ -30,26 +44,36 @@ async def test_get_user_locations(self, client: StreamChatAsync, random_user: Di
3044
assert isinstance(response["active_live_locations"], list)
3145

3246
async def test_update_user_location(
33-
self, client: StreamChatAsync, random_user: Dict
47+
self, client: StreamChatAsync, channel, random_user: Dict
3448
):
35-
# First create a message to attach location to
36-
channel = client.channel("messaging", str(random_user["id"]))
37-
await channel.create(random_user["id"])
49+
# Create a message to attach location to
50+
now = datetime.datetime.now(datetime.timezone.utc)
51+
one_hour_later = now + datetime.timedelta(hours=1)
52+
shared_location = {
53+
"created_by_device_id": "test_device_id",
54+
"latitude": 37.7749,
55+
"longitude": -122.4194,
56+
"end_at": one_hour_later.isoformat(),
57+
}
58+
3859
msg = await channel.send_message(
39-
{"text": "Message with location"}, random_user["id"]
60+
{"text": "Message with location", "shared_location": shared_location},
61+
random_user["id"],
4062
)
4163
message_id = msg["message"]["id"]
4264

4365
# Update user location
4466
location_data = {
67+
"created_by_device_id": "test_device_id",
4568
"latitude": 37.7749,
4669
"longitude": -122.4194,
4770
}
48-
response = await client.update_user_location(message_id, location_data)
71+
response = await client.update_user_location(
72+
random_user["id"], message_id, location_data
73+
)
4974

50-
assert "shared_location" in response
51-
assert response["shared_location"]["latitude"] == location_data["latitude"]
52-
assert response["shared_location"]["longitude"] == location_data["longitude"]
75+
assert response["latitude"] == location_data["latitude"]
76+
assert response["longitude"] == location_data["longitude"]
5377

5478
# Get user locations to verify
5579
locations_response = await client.get_user_locations(random_user["id"])
@@ -58,9 +82,3 @@ async def test_update_user_location(
5882
location = locations_response["active_live_locations"][0]
5983
assert location["latitude"] == location_data["latitude"]
6084
assert location["longitude"] == location_data["longitude"]
61-
62-
# Cleanup
63-
try:
64-
await channel.delete()
65-
except Exception:
66-
pass

stream_chat/tests/test_live_locations.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88

99
@pytest.mark.incremental
1010
class TestLiveLocations:
11-
def test_get_user_locations(self, client: StreamChat, random_user: Dict):
12-
# First create a message to attach location to
13-
channel = client.channel("messaging", str(random_user["id"]))
14-
channel.create(random_user["id"])
11+
@pytest.fixture(autouse=True)
12+
def setup_channel_for_shared_locations(self, channel):
13+
channel.update_partial(
14+
{"config_overrides": {"shared_locations": True}},
15+
)
16+
yield
17+
channel.update_partial(
18+
{"config_overrides": {"shared_locations": False}},
19+
)
1520

21+
def test_get_user_locations(self, client: StreamChat, channel, random_user: Dict):
1622
# Create a message to attach location to
1723
now = datetime.datetime.now(datetime.timezone.utc)
1824
one_hour_later = now + datetime.timedelta(hours=1)
@@ -34,11 +40,7 @@ def test_get_user_locations(self, client: StreamChat, random_user: Dict):
3440
assert "active_live_locations" in response
3541
assert isinstance(response["active_live_locations"], list)
3642

37-
def test_update_user_location(self, client: StreamChat, random_user: Dict):
38-
# First create a message to attach location to
39-
channel = client.channel("messaging", str(random_user["id"]))
40-
channel.create(random_user["id"])
41-
43+
def test_update_user_location(self, client: StreamChat, channel, random_user: Dict):
4244
# Create a message to attach location to
4345
now = datetime.datetime.now(datetime.timezone.utc)
4446
one_hour_later = now + datetime.timedelta(hours=1)
@@ -57,14 +59,16 @@ def test_update_user_location(self, client: StreamChat, random_user: Dict):
5759

5860
# Update user location
5961
location_data = {
62+
"created_by_device_id": "test_device_id",
6063
"latitude": 37.7749,
6164
"longitude": -122.4194,
6265
}
63-
response = client.update_user_location(message_id, location_data)
66+
response = client.update_user_location(
67+
random_user["id"], message_id, location_data
68+
)
6469

65-
assert "shared_location" in response
66-
assert response["shared_location"]["latitude"] == location_data["latitude"]
67-
assert response["shared_location"]["longitude"] == location_data["longitude"]
70+
assert response["latitude"] == location_data["latitude"]
71+
assert response["longitude"] == location_data["longitude"]
6872

6973
# Get user locations to verify
7074
locations_response = client.get_user_locations(random_user["id"])
@@ -73,9 +77,3 @@ def test_update_user_location(self, client: StreamChat, random_user: Dict):
7377
location = locations_response["active_live_locations"][0]
7478
assert location["latitude"] == location_data["latitude"]
7579
assert location["longitude"] == location_data["longitude"]
76-
77-
# Cleanup
78-
try:
79-
channel.delete()
80-
except Exception:
81-
pass

0 commit comments

Comments
 (0)