Skip to content

Commit 8e3fbc0

Browse files
Improving tests
1 parent ea0a8d8 commit 8e3fbc0

File tree

6 files changed

+156
-14
lines changed

6 files changed

+156
-14
lines changed

tests/test_asset_request.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
44

55
import os
6+
import pytest
67
import unittest
78
from mock import patch
89
from connect.resources.asset_request import AssetRequestResource
910
from connect.config import Config
10-
from .common import Response, load_str
11+
from .common import load_str
1112

1213
update_param_asset_request_response = load_str(
1314
os.path.join(os.path.dirname(__file__), 'data', 'update_param_asset_request_response.json'))
@@ -17,19 +18,33 @@ class TestAssetRequest(unittest.TestCase):
1718
def setUp(self):
1819
self.config = Config(file='tests/config.json')
1920

20-
@patch('requests.post')
21-
def update_param_asset_request(self, post_mock):
22-
post_mock.return_value = Response(True, update_param_asset_request_response, 200)
21+
@patch('requests.put')
22+
def test_update_param_asset_request(self, put_mock):
2323
request = AssetRequestResource(config=self.config)
2424
pr_id = 'PR-4405-9454-9305-001'
25-
asset_request = request(pr_id)
26-
assert post_mock.call_count == 1
27-
post_mock.assert_called_with(
25+
test_data = {
26+
"params": [{
27+
"id": "PM-9861-7949-8492-0001",
28+
"value": "32323323"
29+
}]
30+
}
31+
request.update_param_asset_request(pr_id, test_data, "i'm a note!")
32+
assert put_mock.call_count == 1
33+
put_mock.assert_called_with(
2834
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
2935
timeout=300,
3036
url=('http://localhost:8080/api/public/v1/'
31-
'tier/requests/PR-4405-9454-9305-001'))
32-
assert asset_request.id == 'PR-4405-9454-9305-001'
37+
'requests/PR-4405-9454-9305-001/'),
38+
json={
39+
'asset': test_data,
40+
'note': "i'm a note!",
41+
})
42+
43+
def test_invalid_request_id(self):
44+
asset_request = AssetRequestResource(config=self.config)
45+
with pytest.raises(ValueError) as e:
46+
asset_request.update_param_asset_request(None, None, None)
47+
assert str(e.value) == 'Invalid ID'
3348

3449

3550
if __name__ == "__main__":

tests/test_billing_request.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
import os
77
import unittest
8+
import pytest
89

910
from mock import Mock, call, patch
1011

1112
from connect.config import Config
1213
from connect.models import BillingRequest
13-
from connect.resources.subscription import BillingRequestResource
14+
from connect.resources.billing_request import BillingRequestResource
1415

1516
from .common import Response, load_json, load_str
1617

@@ -103,7 +104,6 @@ def test_add_billing_request(self, post_mock):
103104
json=body,
104105
timeout=300,
105106
url='http://localhost:8080/api/public/v1/subscriptions/requests')
106-
107107
assert isinstance(billing_request, BillingRequest)
108108
assert billing_request.id == 'BRP-6750-9514-7931-0001'
109109

@@ -115,17 +115,21 @@ def test_modify_billing_request(self, put_mock):
115115
pk = 'BRP-6750-9514-7931-0001'
116116
request = BillingRequestResource(config=self.config)
117117
billing_request = request.update_billing_request(pk, body)
118-
print(billing_request)
119118
url = ('http://localhost:8080/api/public/v1'
120119
'/subscriptions/requests/BRP-6750-9514-7931-0001/attributes')
121120
put_mock.assert_called_with(
122121
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
123122
json=body,
124123
timeout=300,
125124
url=url)
126-
127125
assert billing_request == ({'provider': {'external_id': '321-123'}}, 200)
128126

127+
def test_update_billing_request_bad(self):
128+
request = BillingRequestResource(config=self.config)
129+
with pytest.raises(ValueError) as e:
130+
request.update_billing_request(None, None)
131+
assert str(e.value) == 'Invalid ID'
132+
129133

130134
if __name__ == "__main__":
131135
unittest.main()

tests/test_directory.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212

1313
from connect.exceptions import ServerError
14-
from connect.models import Asset, Product, TierConfig
14+
from connect.models import Asset, Product, TierConfig, TierAccount
1515
from connect.resources import Directory
1616
from .common import Response, load_str
1717

@@ -152,3 +152,15 @@ def test_get_tier_config(get_mock):
152152
def test_get_tier_config_bad():
153153
with pytest.raises(ServerError):
154154
Directory().get_tier_config('TC-000-000-000')
155+
156+
157+
@patch('requests.get', MagicMock(return_value=_get_bad_response()))
158+
def test_search_tier_account_bad():
159+
with pytest.raises(ServerError):
160+
Directory().search_tier_accounts(None)
161+
162+
163+
@patch('requests.get', MagicMock(return_value=_get_bad_response()))
164+
def test_get_tier_account_bad():
165+
with pytest.raises(ServerError):
166+
Directory().get_tier_account('TAR-0000-0000-0000-000-000')

tests/test_subscriptions.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
6+
import os
7+
import unittest
8+
9+
from mock import Mock, patch
10+
11+
from connect.config import Config
12+
from connect.resources.subscription import Subscription
13+
from connect.models import BillingRequest
14+
from .common import Response, load_json, load_str
15+
16+
add_billing_request_body = load_json(
17+
os.path.join(os.path.dirname(__file__), 'data', 'add_billing_request.json'))
18+
add_billing_request_response = load_str(
19+
os.path.join(os.path.dirname(__file__), 'data', 'response_add_billing_request.json'))
20+
21+
22+
class testSubscriptions(unittest.TestCase):
23+
24+
def setUp(self):
25+
self.config = Config(file='tests/config.json')
26+
27+
@patch('requests.post')
28+
def test_create_billing_request(self, post_mock):
29+
# type: (Mock) -> None
30+
post_mock.return_value = Response(True, add_billing_request_response, 200)
31+
body = add_billing_request_body
32+
request = Subscription(config=self.config)
33+
billing_request = request.create_billing_request(body)
34+
assert isinstance(billing_request, BillingRequest)
35+
assert billing_request.id == 'BRP-6750-9514-7931-0001'
36+
37+
@patch('requests.put')
38+
def test_update_billing_request(self, put_mock):
39+
update_billing_request_response = {'provider': {'external_id': '321-123'}}
40+
put_mock.return_value = Response(True, update_billing_request_response, 200)
41+
body = update_billing_request_response
42+
pk = 'BRP-6750-9514-7931-0001'
43+
request = Subscription(config=self.config)
44+
billing_request = request.update_billing_request(pk, body)
45+
assert billing_request == ({'provider': {'external_id': '321-123'}}, 200)
46+
47+
48+
if __name__ == "__main__":
49+
unittest.main()

tests/test_template.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from connect.exceptions import ServerError
11+
from connect.models.activation_tile_response import ActivationTileResponse
1112
from connect.resources import TemplateResource
1213
from .common import Response, load_str
1314

@@ -70,3 +71,28 @@ def test_get_template(get_mock):
7071
template = TemplateResource().get('TL-313-655-502')
7172
assert template.template_id == 'TL-313-655-502'
7273

74+
75+
def test_render_bad():
76+
template_resource = TemplateResource()
77+
with pytest.raises(ValueError) as e:
78+
template_resource.render(None, None)
79+
assert str(e.value) == 'Invalid ids for render template'
80+
81+
82+
@patch('requests.get')
83+
def test_renders(get_mock):
84+
get_mock.return_value = Response(
85+
ok=True,
86+
text='# Sample Activation Template',
87+
status_code=200,
88+
)
89+
template_resource = TemplateResource()
90+
response = template_resource.render('TL-313-655-502', 'PR-845-746-468')
91+
assert isinstance(response, ActivationTileResponse)
92+
assert response.tile == '# Sample Activation Template'
93+
94+
get_mock.assert_called_with(
95+
url='http://localhost:8080/api/public/v1/templates/TL-313-655-502/render',
96+
params={'request_id': 'PR-845-746-468'},
97+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
98+
timeout=300)

tests/test_tier_config_request.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
import unittest
8+
import pytest
89

910
from mock import call, patch
1011

@@ -167,6 +168,41 @@ def test_fail_tier_account_request(self, post_mock):
167168
'tier/config-requests/TAR-6458-9737-0065-004-001/fail'))
168169
assert tier_config_request == ('', 204)
169170

171+
def test_fail_tar_pend(self):
172+
request = TierConfigRequestResource()
173+
with pytest.raises(ValueError) as e:
174+
request.pend(None)
175+
assert str(e.value) == 'Invalid ID'
176+
177+
def test_fail_tar_inquire(self):
178+
request = TierConfigRequestResource()
179+
with pytest.raises(ValueError) as e:
180+
request.inquire(None)
181+
assert str(e.value) == 'Invalid ID'
182+
183+
def test_fail_tar_approve(self):
184+
request = TierConfigRequestResource()
185+
with pytest.raises(ValueError) as e:
186+
request.approve(None, None)
187+
assert str(e.value) == 'Invalid ID'
188+
189+
def test_fail_tar_fail(self):
190+
request = TierConfigRequestResource()
191+
with pytest.raises(ValueError) as e:
192+
request.fail(None, None)
193+
assert str(e.value) == 'Invalid ID'
194+
195+
def test_fail_tar_assign(self):
196+
request = TierConfigRequestResource()
197+
with pytest.raises(ValueError) as e:
198+
request.assign(None)
199+
assert str(e.value) == 'Invalid ID'
200+
201+
def test_fail_tar_unassign(self):
202+
request = TierConfigRequestResource()
203+
with pytest.raises(ValueError) as e:
204+
request.unassign(None)
205+
assert str(e.value) == 'Invalid ID'
170206

171207
if __name__ == "__main__":
172208
unittest.main()

0 commit comments

Comments
 (0)