Skip to content

Commit 6e46cc9

Browse files
Added unit tests for Asset and Fulfillment. Field 'external_id' changed to int on all models.
1 parent 75c054b commit 6e46cc9

File tree

5 files changed

+229
-21
lines changed

5 files changed

+229
-21
lines changed

connect/models/asset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class Asset(BaseModel):
18-
def get_parameter_by_id(self, identity):
18+
def get_param_by_id(self, identity):
1919
return self._get_by_id(self.params, identity)
2020

2121
def get_item_by_id(self, identity):
@@ -24,7 +24,7 @@ def get_item_by_id(self, identity):
2424

2525
class AssetSchema(BaseSchema):
2626
status = fields.Str()
27-
external_id = fields.Str()
27+
external_id = fields.Int()
2828
external_uid = fields.UUID()
2929
product = fields.Nested(ProductSchema, only=('id', 'name'))
3030
connection = fields.Nested(

connect/models/hub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def make_object(self, data):
2424

2525
class HubsSchemaMixin(Schema):
2626
hub = fields.Nested(HubSchema, only=('id', 'name'))
27-
external_id = fields.Str()
27+
external_id = fields.Int()

connect/models/tiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Tier(BaseModel):
1818
class TierSchema(BaseSchema):
1919
name = fields.Str()
2020
contact_info = fields.Nested(ContactInfoSchema)
21-
external_id = fields.Str()
21+
external_id = fields.Int()
2222
external_uid = fields.UUID()
2323

2424
@post_load

tests/response2.json

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
[
2+
{
3+
"asset": {
4+
"connection": {
5+
"id": "CT-0000-0000-0000",
6+
"provider": {
7+
"id": "PA-473-705",
8+
"name": "ACME Provider"
9+
},
10+
"type": "preview",
11+
"vendor": {
12+
"id": "VA-691-879",
13+
"name": "Marc FSG"
14+
}
15+
},
16+
"external_id": 899,
17+
"id": "AS-893-495-635-9",
18+
"items": [
19+
{
20+
"id": "TEAM_ST3L2T1Y",
21+
"mpn": "TEAM-ST3L2T1Y",
22+
"old_quantity": "0",
23+
"quantity": "100"
24+
},
25+
{
26+
"id": "TEAM_ST3L2TAC1M",
27+
"mpn": "TEAM-ST3L2TAC1M",
28+
"old_quantity": "0",
29+
"quantity": "200"
30+
},
31+
{
32+
"id": "UPSIZE_TEST",
33+
"mpn": "UPSIZE_TEST",
34+
"old_quantity": "200",
35+
"quantity": "201"
36+
},
37+
{
38+
"id": "DOWNSIZE_TEST",
39+
"mpn": "DOWNSIZE_TEST",
40+
"old_quantity": "200",
41+
"quantity": "199"
42+
},
43+
{
44+
"id": "DELETE_TEST",
45+
"mpn": "DELETE_TEST",
46+
"old_quantity": "200",
47+
"quantity": "0"
48+
}
49+
],
50+
"params": [
51+
{
52+
"description": "How customer Feels",
53+
"id": "howyoufeel",
54+
"name": "How You feel today?",
55+
"type": "dropdown",
56+
"value": "Good",
57+
"value_choices": [
58+
{
59+
"label": "Really Good",
60+
"value": "reallygood"
61+
},
62+
{
63+
"label": "Good",
64+
"value": "Good"
65+
},
66+
{
67+
"label": "Don't ask",
68+
"value": "bad"
69+
}
70+
],
71+
"value_error": ""
72+
},
73+
{
74+
"description": "Team Identifier on Dropbox system",
75+
"id": "teamID",
76+
"name": "Team ID",
77+
"type": "text",
78+
"value": "1",
79+
"value_choices": [],
80+
"value_error": ""
81+
}
82+
],
83+
"product": {
84+
"id": "CN-573-708-587",
85+
"name": "Dropbox Test"
86+
},
87+
"tiers": {
88+
"customer": {
89+
"contact_info": {
90+
"address_line1": "Darren Passage",
91+
"address_line2": "",
92+
"city": "West Douglas",
93+
"contact": {
94+
"email": "[email protected]",
95+
"first_name": "Mark",
96+
"last_name": "Brooks",
97+
"phone_number": {
98+
"area_code": "1",
99+
"country_code": "+252",
100+
"extension": "",
101+
"phone_number": "474842"
102+
}
103+
},
104+
"country": "us",
105+
"postal_code": "30690",
106+
"state": "Nevada"
107+
},
108+
"external_id": 889,
109+
"id": "TA-0-5870-9239-0029",
110+
"name": "Kaiser LLC"
111+
},
112+
"tier1": {
113+
"contact_info": {
114+
"address_line1": "noname",
115+
"address_line2": "",
116+
"city": "noname",
117+
"contact": {
118+
"email": "[email protected]",
119+
"first_name": "ACME",
120+
"last_name": "Reseller",
121+
"phone_number": {
122+
"area_code": "234",
123+
"country_code": "+1",
124+
"extension": "",
125+
"phone_number": "567890"
126+
}
127+
},
128+
"country": "us",
129+
"postal_code": "12111",
130+
"state": "Alaska"
131+
},
132+
"external_id": 1,
133+
"id": "TA-0-7251-3930-7482",
134+
"name": "ACME Reseller"
135+
},
136+
"tier2": {}
137+
}
138+
},
139+
"created": "2018-09-03T10:28:18.472670+00:00",
140+
"id": "PR-5620-6510-8214",
141+
"status": "approved",
142+
"type": "purchase",
143+
"updated": "2018-09-04T10:38:58.045330+00:00"
144+
}
145+
]

tests/test_models.py

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,39 @@
1111

1212
from mock import MagicMock, patch
1313

14-
from connect.config import Config
14+
from connect.models import Param
15+
from connect.models.asset import Asset
16+
from connect.models.fulfillment import Fulfillment
17+
from connect.models.product import Item
1518
from connect.resource import FulfillmentResource
1619

17-
response = namedtuple('Response', ('ok', 'content'))
20+
Response = namedtuple('Response', ('ok', 'content'))
1821

1922

2023
def _get_response_ok():
21-
response.ok = True
22-
with open(os.path.join(os.path.dirname(__file__), 'response.json')) as file:
23-
response.content = file.read()
24+
with open(os.path.join(os.path.dirname(__file__), 'response.json')) as file_handle:
25+
content = file_handle.read()
26+
return Response(ok=True, content=content)
2427

25-
return response
28+
29+
def _get_response2_ok():
30+
with open(os.path.join(os.path.dirname(__file__), 'response2.json')) as file_handle:
31+
content = file_handle.read()
32+
return Response(ok=True, content=content)
2633

2734

2835
@patch('requests.get', MagicMock(return_value=_get_response_ok()))
2936
def test_create_model_from_response():
30-
config = Config(api_key='ApiKey XXXX:YYYYY', api_url='http://localhost:8080/api/public/v1/')
31-
32-
requests = FulfillmentResource(config).list()
33-
request_obj = FulfillmentResource(config).get(pk='PR-000-000-000')
37+
# Parse JSON data from response file
38+
with open(os.path.join(os.path.dirname(__file__), 'response.json')) as file_handle:
39+
content = json.loads(file_handle.read())[0]
3440

35-
assert requests[0].id == request_obj.id
36-
content = json.loads(response.content)[0]
37-
assert request_obj.id == content['id']
38-
assert request_obj.contract.id == content['contract']['id']
39-
assert request_obj.marketplace.id == content['marketplace']['id']
40-
assert request_obj.asset.id == content['asset']['id']
41-
assert request_obj.asset.product.id == content['asset']['product']['id']
41+
# Get requests from response
42+
resource = FulfillmentResource()
43+
requests = resource.list()
44+
request_obj = resource.get(pk='PR-000-000-000')
4245

46+
# Assert that all properties exist
4347
try:
4448
(
4549
request_obj.contract,
@@ -63,3 +67,62 @@ def test_create_model_from_response():
6367
)
6468
except AttributeError:
6569
assert False, 'Incorrectly initialized model '
70+
71+
# Assert that returned data matches the one in the file
72+
assert requests[0].id == request_obj.id
73+
assert request_obj.id == content['id']
74+
assert request_obj.contract.id == content['contract']['id']
75+
assert request_obj.marketplace.id == content['marketplace']['id']
76+
assert request_obj.asset.id == content['asset']['id']
77+
assert request_obj.asset.product.id == content['asset']['product']['id']
78+
assert isinstance(request_obj.asset.external_id, int)
79+
80+
81+
@patch('requests.get', MagicMock(return_value=_get_response2_ok()))
82+
def test_fulfillment_items():
83+
# Get request
84+
requests = FulfillmentResource().list()
85+
assert len(requests) == 1
86+
request = requests[0]
87+
assert isinstance(request, Fulfillment)
88+
89+
# Test new items
90+
new_items = request.new_items
91+
assert isinstance(new_items, list)
92+
assert len(new_items) == 2
93+
for item in new_items:
94+
assert isinstance(item, Item)
95+
96+
# Test changed items
97+
changed_items = request.changed_items
98+
assert isinstance(new_items, list)
99+
assert len(changed_items) == 2
100+
for item in changed_items:
101+
assert isinstance(item, Item)
102+
103+
# Test removed items
104+
removed_items = request.removed_items
105+
assert isinstance(removed_items, list)
106+
assert len(removed_items) == 1
107+
for item in removed_items:
108+
assert isinstance(item, Item)
109+
110+
111+
@patch('requests.get', MagicMock(return_value=_get_response2_ok()))
112+
def test_asset_methods():
113+
# Get asset
114+
requests = FulfillmentResource().list()
115+
assert len(requests) == 1
116+
assert isinstance(requests[0], Fulfillment)
117+
asset = requests[0].asset
118+
assert isinstance(asset, Asset)
119+
120+
# Get param by id
121+
assert isinstance(asset.get_param_by_id('howyoufeel'), Param)
122+
assert asset.get_param_by_id('howyoufeel').id == 'howyoufeel'
123+
assert not asset.get_param_by_id('invalid-id')
124+
125+
# Get item by id
126+
assert isinstance(asset.get_item_by_id('TEAM_ST3L2T1Y'), Item)
127+
assert asset.get_item_by_id('TEAM_ST3L2T1Y').id == 'TEAM_ST3L2T1Y'
128+
assert not asset.get_item_by_id('invalid-id')

0 commit comments

Comments
 (0)