Skip to content

Commit 445a695

Browse files
author
Sebastian Molenda
committed
Added tests and some fixes
1 parent 9e55215 commit 445a695

File tree

19 files changed

+2499
-4
lines changed

19 files changed

+2499
-4
lines changed

pubnub/pubnub_asyncio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class PubNubAsyncio(PubNubCore):
4747

4848
def __init__(self, config, custom_event_loop=None, subscription_manager=None, *, custom_request_handler=None):
4949
super(PubNubAsyncio, self).__init__(config)
50-
self.event_loop = custom_event_loop or asyncio.get_event_loop()
50+
self.event_loop = custom_event_loop or asyncio.new_event_loop()
5151

5252
self._session = None
5353

@@ -632,7 +632,7 @@ async def wait_for_presence_on(self, *channel_names):
632632
class AsyncioTelemetryManager(TelemetryManager):
633633
def __init__(self):
634634
TelemetryManager.__init__(self)
635-
self.loop = asyncio.get_event_loop()
635+
self.loop = asyncio.new_event_loop()
636636
self._schedule_next_cleanup()
637637

638638
def _schedule_next_cleanup(self):

pubnub/pubnub_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ def set_uuid_metadata(self, uuid: str = None, include_custom: bool = None, custo
288288
include_type=include_type, status=status, type=type, name=name, email=email,
289289
external_id=external_id, profile_url=profile_url)
290290

291-
def get_uuid_metadata(self, uuud: str = None, include_custom: bool = None, include_status: bool = True,
291+
def get_uuid_metadata(self, uuid: str = None, include_custom: bool = None, include_status: bool = True,
292292
include_type: bool = True) -> GetUuid:
293-
return GetUuid(self, uuid=uuud, include_custom=include_custom, include_status=include_status,
293+
return GetUuid(self, uuid=uuid, include_custom=include_custom, include_status=include_status,
294294
include_type=include_type)
295295

296296
def remove_uuid_metadata(self, uuid: str = None) -> RemoveUuid:

tests/integrational/asyncio/objects_v2/__init__.py

Whitespace-only changes.
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
import pytest
2+
from pubnub.endpoints.endpoint import Endpoint
3+
from pubnub.endpoints.objects_v2.channel.get_all_channels import GetAllChannels
4+
from pubnub.endpoints.objects_v2.channel.get_channel import GetChannel
5+
from pubnub.endpoints.objects_v2.channel.remove_channel import RemoveChannel
6+
from pubnub.endpoints.objects_v2.channel.set_channel import SetChannel
7+
from pubnub.exceptions import PubNubException
8+
from pubnub.models.consumer.common import PNStatus
9+
from pubnub.models.consumer.objects_v2.channel import PNSetChannelMetadataResult, PNGetChannelMetadataResult, \
10+
PNRemoveChannelMetadataResult, PNGetAllChannelMetadataResult
11+
from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
12+
from pubnub.pubnub_asyncio import PubNubAsyncio
13+
from pubnub.models.envelopes import AsyncioEnvelope
14+
from tests.helper import pnconf_env_copy
15+
from tests.integrational.vcr_helper import pn_vcr
16+
17+
18+
def _pubnub():
19+
config = pnconf_env_copy()
20+
return PubNubAsyncio(config)
21+
22+
23+
class TestObjectsV2Channel:
24+
_some_channel_id = "somechannelid"
25+
_some_name = "Some name"
26+
_some_description = "Some description"
27+
_some_custom = {
28+
"key1": "val1",
29+
"key2": "val2"
30+
}
31+
32+
def test_set_channel_endpoint_available(self):
33+
pn = _pubnub()
34+
set_channel = pn.set_channel_metadata()
35+
assert set_channel is not None
36+
assert isinstance(set_channel, SetChannel)
37+
assert isinstance(set_channel, Endpoint)
38+
39+
def test_set_channel_is_endpoint(self):
40+
pn = _pubnub()
41+
set_channel = pn.set_channel_metadata()
42+
assert isinstance(set_channel, SetChannel)
43+
assert isinstance(set_channel, Endpoint)
44+
45+
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/set_channel.json',
46+
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json')
47+
@pytest.mark.asyncio
48+
async def test_set_channel_happy_path(self):
49+
pn = _pubnub()
50+
51+
set_channel_result = await pn.set_channel_metadata() \
52+
.include_custom(True) \
53+
.channel(TestObjectsV2Channel._some_channel_id) \
54+
.set_name(TestObjectsV2Channel._some_name) \
55+
.description(TestObjectsV2Channel._some_description) \
56+
.custom(TestObjectsV2Channel._some_custom) \
57+
.future()
58+
59+
assert isinstance(set_channel_result, AsyncioEnvelope)
60+
assert isinstance(set_channel_result.result, PNSetChannelMetadataResult)
61+
assert isinstance(set_channel_result.status, PNStatus)
62+
assert not set_channel_result.status.is_error()
63+
data = set_channel_result.result.data
64+
assert data['id'] == TestObjectsV2Channel._some_channel_id
65+
assert data['name'] == TestObjectsV2Channel._some_name
66+
assert data['description'] == TestObjectsV2Channel._some_description
67+
assert data['custom'] == TestObjectsV2Channel._some_custom
68+
69+
def test_get_channel_endpoint_available(self):
70+
pn = _pubnub()
71+
get_channel = pn.get_channel_metadata()
72+
assert get_channel is not None
73+
assert isinstance(get_channel, GetChannel)
74+
assert isinstance(get_channel, Endpoint)
75+
76+
def test_get_channel_is_endpoint(self):
77+
pn = _pubnub()
78+
get_channel = pn.get_channel_metadata()
79+
assert isinstance(get_channel, GetChannel)
80+
assert isinstance(get_channel, Endpoint)
81+
82+
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/get_channel.json',
83+
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json')
84+
@pytest.mark.asyncio
85+
async def test_get_channel_happy_path(self):
86+
pn = _pubnub()
87+
88+
get_channel_result = await pn.get_channel_metadata() \
89+
.include_custom(True) \
90+
.channel(TestObjectsV2Channel._some_channel_id) \
91+
.future()
92+
93+
assert isinstance(get_channel_result, AsyncioEnvelope)
94+
assert isinstance(get_channel_result.result, PNGetChannelMetadataResult)
95+
assert isinstance(get_channel_result.status, PNStatus)
96+
assert not get_channel_result.status.is_error()
97+
data = get_channel_result.result.data
98+
assert data['id'] == TestObjectsV2Channel._some_channel_id
99+
assert data['name'] == TestObjectsV2Channel._some_name
100+
assert data['description'] == TestObjectsV2Channel._some_description
101+
assert data['custom'] == TestObjectsV2Channel._some_custom
102+
103+
def test_remove_channel_endpoint_available(self):
104+
pn = _pubnub()
105+
remove_channel = pn.remove_channel_metadata()
106+
assert remove_channel is not None
107+
assert isinstance(remove_channel, RemoveChannel)
108+
assert isinstance(remove_channel, Endpoint)
109+
110+
def test_remove_channel_is_endpoint(self):
111+
pn = _pubnub()
112+
remove_channel = pn.remove_channel_metadata()
113+
assert isinstance(remove_channel, RemoveChannel)
114+
assert isinstance(remove_channel, Endpoint)
115+
116+
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/remove_channel.json',
117+
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json')
118+
@pytest.mark.asyncio
119+
async def test_remove_channel_happy_path(self):
120+
pn = _pubnub()
121+
122+
remove_uid_result = await pn.remove_channel_metadata() \
123+
.channel(TestObjectsV2Channel._some_channel_id) \
124+
.future()
125+
126+
assert isinstance(remove_uid_result, AsyncioEnvelope)
127+
assert isinstance(remove_uid_result.result, PNRemoveChannelMetadataResult)
128+
assert isinstance(remove_uid_result.status, PNStatus)
129+
assert not remove_uid_result.status.is_error()
130+
131+
def test_get_all_channel_endpoint_available(self):
132+
pn = _pubnub()
133+
get_all_channel = pn.get_all_channel_metadata()
134+
assert get_all_channel is not None
135+
assert isinstance(get_all_channel, GetAllChannels)
136+
assert isinstance(get_all_channel, Endpoint)
137+
138+
def test_get_all_channel_is_endpoint(self):
139+
pn = _pubnub()
140+
get_all_channel = pn.get_all_channel_metadata()
141+
assert isinstance(get_all_channel, GetAllChannels)
142+
assert isinstance(get_all_channel, Endpoint)
143+
144+
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/get_all_channel.json',
145+
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json')
146+
@pytest.mark.asyncio
147+
async def test_get_all_channel_happy_path(self):
148+
pn = _pubnub()
149+
150+
await pn.set_channel_metadata() \
151+
.include_custom(True) \
152+
.channel(TestObjectsV2Channel._some_channel_id) \
153+
.set_name(TestObjectsV2Channel._some_name) \
154+
.description(TestObjectsV2Channel._some_description) \
155+
.custom(TestObjectsV2Channel._some_custom) \
156+
.future()
157+
158+
get_all_channel_result = await pn.get_all_channel_metadata() \
159+
.include_custom(True) \
160+
.limit(10) \
161+
.include_total_count(True) \
162+
.sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
163+
.page(None) \
164+
.future()
165+
166+
assert isinstance(get_all_channel_result, AsyncioEnvelope)
167+
assert isinstance(get_all_channel_result.result, PNGetAllChannelMetadataResult)
168+
assert isinstance(get_all_channel_result.status, PNStatus)
169+
assert not get_all_channel_result.status.is_error()
170+
data = get_all_channel_result.result.data
171+
assert isinstance(data, list)
172+
assert get_all_channel_result.result.total_count != 0
173+
assert get_all_channel_result.result.next is not None
174+
assert get_all_channel_result.result.prev is None
175+
176+
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/if_matches_etag.json',
177+
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json')
178+
@pytest.mark.asyncio
179+
async def test_if_matches_etag(self):
180+
pubnub = _pubnub()
181+
182+
set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=self._some_name).future()
183+
original_etag = set_channel.result.data.get('eTag')
184+
get_channel = await pubnub.get_channel_metadata(channel=self._some_channel_id).future()
185+
assert original_etag == get_channel.result.data.get('eTag')
186+
187+
# Update without eTag should be possible
188+
set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=f"{self._some_name}-2") \
189+
.future()
190+
191+
# Response should contain new eTag
192+
new_etag = set_channel.result.data.get('eTag')
193+
assert original_etag != new_etag
194+
assert set_channel.result.data.get('name') == f"{self._some_name}-2"
195+
196+
get_channel = await pubnub.get_channel_metadata(channel=self._some_channel_id).future()
197+
assert original_etag != get_channel.result.data.get('eTag')
198+
assert get_channel.result.data.get('name') == f"{self._some_name}-2"
199+
200+
# Update with correct eTag should be possible
201+
set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=f"{self._some_name}-3") \
202+
.if_matches_etag(new_etag) \
203+
.future()
204+
assert set_channel.result.data.get('name') == f"{self._some_name}-3"
205+
206+
try:
207+
# Update with original - now outdated - eTag should fail
208+
set_channel = await pubnub.set_channel_metadata(
209+
channel=self._some_channel_id,
210+
name=f"{self._some_name}-3"
211+
).if_matches_etag(original_etag).future()
212+
213+
except PubNubException as e:
214+
assert e.get_status_code() == 412
215+
assert e.get_error_message().get('message') == 'Channel to update has been modified after it was read.'

0 commit comments

Comments
 (0)