Skip to content

Commit c91dd69

Browse files
Improve tests
1 parent eaea689 commit c91dd69

File tree

9 files changed

+217
-7
lines changed

9 files changed

+217
-7
lines changed

connect/resources/fulfillment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from connect.resources.asset_request import AssetRequestResource
55

66

7-
class Fulfillment(object):
7+
class FulfillmentResource(object):
88
""" Allows listing and obtaining several types of objects.
99
:param Config config: Config object or ``None`` to use environment config (default).
1010
"""
@@ -36,4 +36,4 @@ def update_param_asset_request(self, request_id, data, note=None):
3636
return self._asset_requests.update_param_asset_request(request_id, data, note)
3737

3838
def create_purchase_request(self, obj):
39-
pass
39+
return self._asset_requests.create(obj)

connect/resources/tier_account_request_automation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77

88
from connect.logger import function_log
9-
from connect.resources.fulfillment import Fulfillment
9+
from connect.resources.fulfillment import FulfillmentResource
1010

1111

1212
class TierAccountRequestAction:
@@ -44,7 +44,7 @@ class TierAccountRequestAutomation:
4444

4545
def __init__(self, config):
4646
self.config = config
47-
self.fulfillment = Fulfillment(config=self.config)
47+
self.fulfillment = FulfillmentResource(config=self.config)
4848

4949
def process(self, filters=None):
5050
for request in self.fulfillment.search_tier_account_requests(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"apiEndpoint": "https://api.connect.cloudblue.com/public/v1",
3-
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000000000",
3+
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000",
44
"products": "PRD-000-000-000"
55
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"type": "purchase",
3+
"asset": {
4+
"connection": {
5+
"id": "CT-0000-0000-0000"
6+
},
7+
"product": {
8+
"id": "PRD-845-746-468-0002"
9+
},
10+
"external_id": "1212132321",
11+
"external_uid": "123456789",
12+
"items": [
13+
{
14+
"global_id": "PRD-845-746-468-0002",
15+
"quantity": "1"
16+
}
17+
],
18+
"params": [
19+
{
20+
"id": "param_a",
21+
"value": "pepe"
22+
}
23+
],
24+
"external_attributes": [
25+
{
26+
"id":"<Parameter ID Or key>",
27+
"value": "<Parameter value>"
28+
}
29+
],
30+
"tiers": {
31+
"customer": {
32+
"id": "TA-4230-9406-8994",
33+
"external_id": "66550",
34+
"external_uid": "6b4bea23-de09-4d28-8246-b67d53a11170",
35+
"name": "Hansen, Boehm and Reichert",
36+
"contact_info": {
37+
"address_line1": "Queen Fall",
38+
"address_line2": "Sheldon River",
39+
"city": "Baldwin",
40+
"state": "Alabama",
41+
"postal_code": "36507",
42+
"country": "US",
43+
"contact": {
44+
"first_name": "Meagan",
45+
"last_name": "Larson",
46+
"email": "[email protected]",
47+
"phone_number": {
48+
"country_code": "+1",
49+
"area_code": "555",
50+
"phone_number": "8677089",
51+
"extension": ""
52+
}
53+
}
54+
}
55+
},
56+
"tier1": {
57+
"id": "TA-4403-4060-9893",
58+
"external_id": "43167",
59+
"external_uid": "52837073-a7ba-4181-9f6a-0018b1dea545",
60+
"name": "McLaughlin - Koepp",
61+
"contact_info": {
62+
"address_line1": "Sanford Summit",
63+
"address_line2": "Fabiola Knoll",
64+
"city": "Baldwin",
65+
"state": "Alabama",
66+
"postal_code": "36507",
67+
"country": "US",
68+
"contact": {
69+
"first_name": "Enrique",
70+
"last_name": "Barrows",
71+
"email": "[email protected]",
72+
"phone_number": {
73+
"country_code": "+1",
74+
"area_code": "555",
75+
"phone_number": "8677089",
76+
"extension": ""
77+
}
78+
}
79+
}
80+
},
81+
"tier2": {}
82+
}
83+
},
84+
"marketplace": {
85+
"id": "MP-34186"
86+
}
87+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": "PR-9301-9893-8624-001",
3+
"type": "purchase",
4+
"status": "pending",
5+
"asset": {
6+
"id": "AS-9301-9893-8624",
7+
"external_uid": "fa86ca75-4738-4fc1-9883-e79236d941f6"
8+
}
9+
}

tests/data/response_usage_file.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
"acceptance_note": "All usage data is correct",
3535
"rejection_note": "Rejected due to wrong usage for item 56",
3636
"error_details": "Error details in case of usage file is marked as invalid",
37+
"stats": {
38+
"uploaded": 100,
39+
"validated": 100,
40+
"pending": 0,
41+
"accepted": 0,
42+
"closed": 0
43+
},
44+
3745
"records": {
3846
"valid": 56,
3947
"invalid": 0,

tests/test_directory.py

Lines changed: 5 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, ProductConfiguration
1515
from connect.resources import Directory
1616
from .common import Response, load_str
1717

@@ -105,7 +105,10 @@ def test_list_products(get_mock):
105105
def test_get_product(get_mock):
106106
get_mock.return_value = _get_product_response()
107107
product = Directory().get_product('CN-783-317-575')
108+
product_configuration = product.configurations
108109
assert isinstance(product, Product)
110+
assert isinstance(product_configuration, ProductConfiguration)
111+
109112
assert product.id == 'CN-783-317-575'
110113

111114
get_mock.assert_called_with(
@@ -164,3 +167,4 @@ def test_search_tier_account_bad():
164167
def test_get_tier_account_bad():
165168
with pytest.raises(ServerError):
166169
Directory().get_tier_account('TAR-0000-0000-0000-000-000')
170+

tests/test_fulfillment.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import os
2+
import unittest
3+
import pytest
4+
from mock import patch, MagicMock
5+
from connect.exceptions import ServerError
6+
from .common import Response, load_str
7+
8+
from connect.config import Config
9+
from connect.resources.fulfillment import FulfillmentResource
10+
11+
create_tier_account_request_body = load_str(
12+
os.path.join(os.path.dirname(__file__), 'data', 'create_tier_account_request_body.json'))
13+
create_tier_account_request_response = load_str(
14+
os.path.join(os.path.dirname(__file__), 'data', 'create_tier_account_request_response.json'))
15+
create_purchase_request_body = load_str(
16+
os.path.join(os.path.dirname(__file__), 'data', 'create_purchase_request_body.json'))
17+
create_purchase_request_response = load_str(
18+
os.path.join(os.path.dirname(__file__), 'data', 'create_purchase_request_response.json'))
19+
20+
21+
class FulfillmentRequest(unittest.TestCase):
22+
def setUp(self):
23+
self.config = Config(file='tests/config.json')
24+
25+
def _get_bad_response():
26+
return Response(
27+
ok=False,
28+
text='{}',
29+
status_code=404
30+
)
31+
32+
@patch('requests.put')
33+
def test_update_param_asset_request(self, put_mock):
34+
request = FulfillmentResource(config=self.config)
35+
pr_id = 'PR-4405-9454-9305-001'
36+
test_data = {
37+
"params": [{
38+
"id": "PM-9861-7949-8492-0001",
39+
"value": "32323323"
40+
}]
41+
}
42+
request.update_param_asset_request(pr_id, test_data, "i'm a note!")
43+
assert put_mock.call_count == 1
44+
put_mock.assert_called_with(
45+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
46+
timeout=300,
47+
url=('http://localhost:8080/api/public/v1/'
48+
'requests/PR-4405-9454-9305-001/'),
49+
json={
50+
'asset': test_data,
51+
'note': "i'm a note!",
52+
})
53+
54+
@patch('requests.post')
55+
def test_create_tier_account_request(self, post_mock):
56+
post_mock.return_value = Response(True, create_tier_account_request_response, 200)
57+
body = create_tier_account_request_body
58+
request = FulfillmentResource(config=self.config)
59+
tier_account_request = request.create_tier_account_request(body)
60+
post_mock.assert_called_with(
61+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
62+
json=body,
63+
timeout=300,
64+
url='http://localhost:8080/api/public/v1/tier/account-requests')
65+
assert tier_account_request[0].id == 'TAR-6458-9737-0065-004-001'
66+
67+
@patch('requests.get')
68+
def test_get_pending_tier_account(self, get_mock):
69+
get_mock.return_value = Response(True, create_tier_account_request_response, 200)
70+
request = FulfillmentResource(config=self.config)
71+
request.get_pending_tier_account_requests()
72+
get_mock.assert_called_with(
73+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
74+
params={'status': 'pending'},
75+
timeout=300,
76+
url='http://localhost:8080/api/public/v1/tier/account-requests')
77+
78+
@patch('requests.get', MagicMock(return_value=_get_bad_response()))
79+
def test_search_pending_tier_account_bad(self):
80+
with pytest.raises(ServerError):
81+
request = FulfillmentResource(config=self.config)
82+
request.search_tier_account_requests(dict(status='pending'))
83+
84+
@patch('requests.post')
85+
def test_create_purchase_request(self, post_mock):
86+
post_mock.return_value = Response(True, create_purchase_request_response, 200)
87+
body = create_purchase_request_body
88+
request = FulfillmentResource(config=self.config)
89+
purchase_request = request.create_purchase_request(body)
90+
post_mock.assert_called_with(
91+
headers={'Content-Type': 'application/json', 'Authorization': 'ApiKey XXXX:YYYYY'},
92+
json=body,
93+
timeout=300,
94+
url='http://localhost:8080/api/public/v1/requests')
95+
assert purchase_request.id == 'PR-9301-9893-8624-001'
96+
97+
98+
if __name__ == "__main__":
99+
unittest.main()

tests/test_usage_file.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
from connect.exceptions import AcceptUsageFile, CloseUsageFile, DeleteUsageFile, RejectUsageFile, \
1313
SkipRequest, SubmitUsageFile
14-
from connect.models import Company, Contract, Marketplace, Product, UsageRecords, UsageFile
14+
from connect.models import Company, Contract, Marketplace, Product, UsageRecords, UsageFile, \
15+
UsageStats, UsageRecord
1516
from connect.resources import UsageFileAutomation
1617
from .common import Response, load_str
1718

@@ -78,6 +79,8 @@ def test_create_resource():
7879

7980
records = request.records
8081
assert isinstance(records, UsageRecords)
82+
stats = request.stats
83+
assert isinstance(stats, UsageStats)
8184
assert records.valid == 56
8285
assert records.invalid == 0
8386

0 commit comments

Comments
 (0)