Skip to content

Commit 56816c3

Browse files
author
smoghe-bw
committed
test: migrate endpoints tests to PyHamcrest and add optional param support
1 parent 8f9a3f1 commit 56816c3

16 files changed

Lines changed: 353 additions & 256 deletions

.claude/worktrees/agent-e0d029b7

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit f7060abe73e32d3515e6ea9a9808e8038bd7499c

test/unit/api/test_endpoints_api.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import unittest
1717
from datetime import datetime
1818

19+
from hamcrest import *
1920
from test.utils.env_variables import *
2021
from bandwidth import ApiClient, Configuration
2122
from bandwidth.api.endpoints_api import EndpointsApi
@@ -68,26 +69,25 @@ def test_create_endpoint(self) -> None:
6869
endpoint_body
6970
)
7071

71-
assert response.status_code == 201
72-
assert isinstance(response.data, CreateEndpointResponse)
72+
assert_that(response.status_code, equal_to(201))
73+
assert_that(response.data, instance_of(CreateEndpointResponse))
7374

74-
assert isinstance(response.data.links, list)
75-
assert len(response.data.links) > 0
76-
assert isinstance(response.data.links[0], Link)
77-
assert response.data.links[0].href.startswith('http')
78-
assert response.data.links[0].rel == 'endpoint'
75+
assert_that(response.data.links, instance_of(list))
76+
assert_that(response.data.links[0], instance_of(Link))
77+
assert_that(response.data.links[0].href, starts_with('http'))
78+
assert_that(response.data.links[0].rel, equal_to('endpoint'))
7979

80-
assert isinstance(response.data.data, CreateEndpointResponseData)
81-
assert response.data.data.endpoint_id == 'e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
82-
assert response.data.data.type == EndpointTypeEnum.WEBRTC
83-
assert response.data.data.status == EndpointStatusEnum.CONNECTED
84-
assert isinstance(response.data.data.creation_timestamp, datetime)
85-
assert isinstance(response.data.data.expiration_timestamp, datetime)
86-
assert response.data.data.token == 'xxxxx.yyyyy.zzzzz'
87-
assert response.data.data.tag == 'my-tag'
88-
assert isinstance(response.data.data.devices, list)
80+
assert_that(response.data.data, instance_of(CreateEndpointResponseData))
81+
assert_that(response.data.data.endpoint_id, equal_to('e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'))
82+
assert_that(response.data.data.type, is_in(EndpointTypeEnum))
83+
assert_that(response.data.data.status, is_in(EndpointStatusEnum))
84+
assert_that(response.data.data.creation_timestamp, instance_of(datetime))
85+
assert_that(response.data.data.expiration_timestamp, instance_of(datetime))
86+
assert_that(response.data.data.token, instance_of(str))
87+
assert_that(response.data.data.tag, instance_of(str))
88+
assert_that(response.data.data.devices, instance_of(list))
8989

90-
assert response.data.errors == []
90+
assert_that(response.data.errors, instance_of(list))
9191

9292
def test_delete_endpoint(self) -> None:
9393
"""Test case for delete_endpoint
@@ -99,7 +99,7 @@ def test_delete_endpoint(self) -> None:
9999
"ep-abc123"
100100
)
101101

102-
assert response.status_code == 204
102+
assert_that(response.status_code, equal_to(204))
103103

104104
def test_get_endpoint(self) -> None:
105105
"""Test case for get_endpoint
@@ -111,25 +111,24 @@ def test_get_endpoint(self) -> None:
111111
"ep-abc123"
112112
)
113113

114-
assert response.status_code == 200
115-
assert isinstance(response.data, EndpointResponse)
114+
assert_that(response.status_code, equal_to(200))
115+
assert_that(response.data, instance_of(EndpointResponse))
116116

117-
assert isinstance(response.data.links, list)
118-
assert len(response.data.links) > 0
119-
assert isinstance(response.data.links[0], Link)
120-
assert response.data.links[0].href.startswith('http')
121-
assert response.data.links[0].rel == 'self'
117+
assert_that(response.data.links, instance_of(list))
118+
assert_that(response.data.links[0], instance_of(Link))
119+
assert_that(response.data.links[0].href, starts_with('http'))
120+
assert_that(response.data.links[0].rel, equal_to('self'))
122121

123-
assert isinstance(response.data.data, Endpoint)
124-
assert response.data.data.endpoint_id == 'e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
125-
assert response.data.data.type == EndpointTypeEnum.WEBRTC
126-
assert response.data.data.status == EndpointStatusEnum.CONNECTED
127-
assert isinstance(response.data.data.creation_timestamp, datetime)
128-
assert isinstance(response.data.data.expiration_timestamp, datetime)
129-
assert response.data.data.tag == 'my-tag'
130-
assert response.data.data.devices == []
122+
assert_that(response.data.data, instance_of(Endpoint))
123+
assert_that(response.data.data.endpoint_id, equal_to('e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'))
124+
assert_that(response.data.data.type, is_in(EndpointTypeEnum))
125+
assert_that(response.data.data.status, is_in(EndpointStatusEnum))
126+
assert_that(response.data.data.creation_timestamp, instance_of(datetime))
127+
assert_that(response.data.data.expiration_timestamp, instance_of(datetime))
128+
assert_that(response.data.data.tag, instance_of(str))
129+
assert_that(response.data.data.devices, instance_of(list))
131130

132-
assert response.data.errors == []
131+
assert_that(response.data.errors, instance_of(list))
133132

134133
def test_list_endpoints(self) -> None:
135134
"""Test case for list_endpoints
@@ -140,33 +139,32 @@ def test_list_endpoints(self) -> None:
140139
BW_ACCOUNT_ID
141140
)
142141

143-
assert response.status_code == 200
144-
assert isinstance(response.data, ListEndpointsResponse)
145-
146-
assert isinstance(response.data.links, list)
147-
assert len(response.data.links) > 0
148-
assert isinstance(response.data.links[0], Link)
149-
assert response.data.links[0].href.startswith('http')
150-
assert response.data.links[0].rel == 'self'
151-
152-
assert response.data.page.page_size == 2
153-
assert response.data.page.total_elements == 10
154-
assert response.data.page.total_pages == 5
155-
assert response.data.page.page_number == 0
156-
157-
assert isinstance(response.data.data, list)
158-
assert len(response.data.data) == 2
159-
assert isinstance(response.data.data[0], Endpoints)
160-
assert response.data.data[0].endpoint_id == 'e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
161-
assert response.data.data[0].type == EndpointTypeEnum.WEBRTC
162-
assert response.data.data[0].status == EndpointStatusEnum.CONNECTED
163-
assert response.data.data[0].tag == 'my-tag'
164-
assert isinstance(response.data.data[0].creation_timestamp, datetime)
165-
assert isinstance(response.data.data[0].expiration_timestamp, datetime)
166-
assert response.data.data[1].endpoint_id == 'e-2cb0-4a07-b215-b22865662d85-15ac29a2-1331029c'
167-
assert response.data.data[1].tag == 'my-tag'
168-
169-
assert response.data.errors == []
142+
assert_that(response.status_code, equal_to(200))
143+
assert_that(response.data, instance_of(ListEndpointsResponse))
144+
145+
assert_that(response.data.links, instance_of(list))
146+
assert_that(response.data.links[0], instance_of(Link))
147+
assert_that(response.data.links[0].href, starts_with('http'))
148+
assert_that(response.data.links[0].rel, equal_to('self'))
149+
150+
assert_that(response.data.page, instance_of(Page))
151+
assert_that(response.data.page.page_size, equal_to(2))
152+
assert_that(response.data.page.total_elements, equal_to(10))
153+
assert_that(response.data.page.total_pages, equal_to(5))
154+
assert_that(response.data.page.page_number, equal_to(0))
155+
156+
assert_that(response.data.data, instance_of(list))
157+
assert_that(response.data.data[0], instance_of(Endpoints))
158+
assert_that(response.data.data[0].endpoint_id, equal_to('e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'))
159+
assert_that(response.data.data[0].type, is_in(EndpointTypeEnum))
160+
assert_that(response.data.data[0].status, is_in(EndpointStatusEnum))
161+
assert_that(response.data.data[0].tag, instance_of(str))
162+
assert_that(response.data.data[0].creation_timestamp, instance_of(datetime))
163+
assert_that(response.data.data[0].expiration_timestamp, instance_of(datetime))
164+
assert_that(response.data.data[1].endpoint_id, equal_to('e-2cb0-4a07-b215-b22865662d85-15ac29a2-1331029c'))
165+
assert_that(response.data.data[1].tag, instance_of(str))
166+
167+
assert_that(response.data.errors, instance_of(list))
170168

171169

172170
if __name__ == '__main__':

test/unit/models/test_create_endpoint_request_base.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,26 @@ def setUp(self):
2828
def tearDown(self):
2929
pass
3030

31-
def make_instance(self) -> CreateEndpointRequestBase:
32-
"""Test CreateEndpointRequestBase"""
33-
return CreateEndpointRequestBase(
34-
type=EndpointTypeEnum.WEBRTC,
35-
direction=EndpointDirectionEnum.BIDIRECTIONAL,
36-
event_callback_url='https://example.com/callback',
37-
event_fallback_url='https://example.com/fallback',
38-
tag='test-request'
31+
def make_instance(self, include_optional) -> CreateEndpointRequestBase:
32+
"""Test CreateEndpointRequestBase
33+
include_optional is a boolean, when False only required
34+
params are included, when True both required and
35+
optional params are included """
36+
if include_optional:
37+
return CreateEndpointRequestBase(
38+
type=EndpointTypeEnum.WEBRTC,
39+
direction=EndpointDirectionEnum.BIDIRECTIONAL,
40+
event_callback_url='https://example.com/callback',
41+
event_fallback_url='https://example.com/fallback',
42+
tag='test-request'
43+
)
44+
else:
45+
return CreateEndpointRequestBase(
3946
)
4047

4148
def testCreateEndpointRequestBase(self):
4249
"""Test CreateEndpointRequestBase"""
43-
instance = self.make_instance()
50+
instance = self.make_instance(True)
4451
assert instance is not None
4552
assert isinstance(instance, CreateEndpointRequestBase)
4653
assert instance.type == EndpointTypeEnum.WEBRTC

test/unit/models/test_create_endpoint_response.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,33 @@ def setUp(self):
3232
def tearDown(self):
3333
pass
3434

35-
def make_instance(self) -> CreateEndpointResponse:
36-
"""Test CreateEndpointResponse"""
37-
return CreateEndpointResponse(
38-
links=[
39-
Link(href='https://api.bandwidth.com/endpoint-123', rel='self')
40-
],
41-
data=CreateEndpointResponseData(
42-
endpoint_id='endpoint-123',
43-
type=EndpointTypeEnum.WEBRTC,
44-
status=EndpointStatusEnum.CONNECTED,
45-
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0),
46-
expiration_timestamp=datetime(2026, 1, 16, 10, 0, 0),
47-
token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.token'
48-
),
49-
errors=[]
35+
def make_instance(self, include_optional) -> CreateEndpointResponse:
36+
"""Test CreateEndpointResponse
37+
include_optional is a boolean, when False only required
38+
params are included, when True both required and
39+
optional params are included """
40+
if include_optional:
41+
return CreateEndpointResponse(
42+
links=[
43+
Link(href='https://api.bandwidth.com/endpoint-123', rel='self')
44+
],
45+
data=CreateEndpointResponseData(
46+
endpoint_id='endpoint-123',
47+
type=EndpointTypeEnum.WEBRTC,
48+
status=EndpointStatusEnum.CONNECTED,
49+
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0),
50+
expiration_timestamp=datetime(2026, 1, 16, 10, 0, 0),
51+
token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.token'
52+
),
53+
errors=[]
54+
)
55+
else:
56+
return CreateEndpointResponse(
5057
)
5158

5259
def testCreateEndpointResponse(self):
5360
"""Test CreateEndpointResponse"""
54-
instance = self.make_instance()
61+
instance = self.make_instance(True)
5562
assert instance is not None
5663
assert isinstance(instance, CreateEndpointResponse)
5764
assert isinstance(instance.links, list)

test/unit/models/test_create_endpoint_response_data.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,36 @@ def setUp(self):
3131
def tearDown(self):
3232
pass
3333

34-
def make_instance(self) -> CreateEndpointResponseData:
35-
"""Test CreateEndpointResponseData"""
36-
return CreateEndpointResponseData(
37-
endpoint_id='e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85',
38-
type=EndpointTypeEnum.WEBRTC,
39-
status=EndpointStatusEnum.CONNECTED,
40-
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0),
41-
expiration_timestamp=datetime(2026, 1, 16, 10, 0, 0),
42-
tag='my-tag',
43-
devices=[
44-
Device(
45-
device_id='device-456',
46-
device_name='Test Device',
47-
status=DeviceStatusEnum.CONNECTED,
48-
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0)
49-
)
50-
],
51-
token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.token'
34+
def make_instance(self, include_optional) -> CreateEndpointResponseData:
35+
"""Test CreateEndpointResponseData
36+
include_optional is a boolean, when False only required
37+
params are included, when True both required and
38+
optional params are included """
39+
if include_optional:
40+
return CreateEndpointResponseData(
41+
endpoint_id='e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85',
42+
type=EndpointTypeEnum.WEBRTC,
43+
status=EndpointStatusEnum.CONNECTED,
44+
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0),
45+
expiration_timestamp=datetime(2026, 1, 16, 10, 0, 0),
46+
tag='my-tag',
47+
devices=[
48+
Device(
49+
device_id='device-456',
50+
device_name='Test Device',
51+
status=DeviceStatusEnum.CONNECTED,
52+
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0)
53+
)
54+
],
55+
token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.token'
56+
)
57+
else:
58+
return CreateEndpointResponseData(
5259
)
5360

5461
def testCreateEndpointResponseData(self):
5562
"""Test CreateEndpointResponseData"""
56-
instance = self.make_instance()
63+
instance = self.make_instance(True)
5764
assert instance is not None
5865
assert isinstance(instance, CreateEndpointResponseData)
5966
assert instance.endpoint_id == 'e-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

test/unit/models/test_create_web_rtc_connection_request.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,30 @@ def setUp(self):
2828
def tearDown(self):
2929
pass
3030

31-
def make_instance(self) -> CreateWebRtcConnectionRequest:
32-
"""Test CreateWebRtcConnectionRequest"""
33-
return CreateWebRtcConnectionRequest(
34-
type=EndpointTypeEnum.WEBRTC,
35-
direction=EndpointDirectionEnum.BIDIRECTIONAL,
36-
event_callback_url='https://example.com/callback',
37-
event_fallback_url='https://example.com/fallback',
38-
tag='test-webrtc',
39-
connection_metadata={
40-
'key1': 'value1',
41-
'key2': 'value2'
42-
}
31+
def make_instance(self, include_optional) -> CreateWebRtcConnectionRequest:
32+
"""Test CreateWebRtcConnectionRequest
33+
include_optional is a boolean, when False only required
34+
params are included, when True both required and
35+
optional params are included """
36+
if include_optional:
37+
return CreateWebRtcConnectionRequest(
38+
type=EndpointTypeEnum.WEBRTC,
39+
direction=EndpointDirectionEnum.BIDIRECTIONAL,
40+
event_callback_url='https://example.com/callback',
41+
event_fallback_url='https://example.com/fallback',
42+
tag='test-webrtc',
43+
connection_metadata={
44+
'key1': 'value1',
45+
'key2': 'value2'
46+
}
47+
)
48+
else:
49+
return CreateWebRtcConnectionRequest(
4350
)
4451

4552
def testCreateWebRtcConnectionRequest(self):
4653
"""Test CreateWebRtcConnectionRequest"""
47-
instance = self.make_instance()
54+
instance = self.make_instance(True)
4855
assert instance is not None
4956
assert isinstance(instance, CreateWebRtcConnectionRequest)
5057
assert instance.type == EndpointTypeEnum.WEBRTC

test/unit/models/test_device.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ def setUp(self):
2828
def tearDown(self):
2929
pass
3030

31-
def make_instance(self) -> Device:
32-
"""Test Device"""
33-
return Device(
34-
device_id='device-abc-123',
35-
device_name='My Test Device',
36-
status=DeviceStatusEnum.CONNECTED,
37-
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0)
31+
def make_instance(self, include_optional) -> Device:
32+
"""Test Device
33+
include_optional is a boolean, when False only required
34+
params are included, when True both required and
35+
optional params are included """
36+
if include_optional:
37+
return Device(
38+
device_id='device-abc-123',
39+
device_name='My Test Device',
40+
status=DeviceStatusEnum.CONNECTED,
41+
creation_timestamp=datetime(2026, 1, 15, 10, 0, 0)
42+
)
43+
else:
44+
return Device(
3845
)
3946

4047
def testDevice(self):
4148
"""Test Device"""
42-
instance = self.make_instance()
49+
instance = self.make_instance(True)
4350
assert instance is not None
4451
assert isinstance(instance, Device)
4552
assert instance.device_id == 'device-abc-123'

0 commit comments

Comments
 (0)