Skip to content

Commit 1dd2361

Browse files
authored
Merge pull request #19 from JaviCerveraIngram/model_methods
Model methods
2 parents 03cd3e4 + 4eebbc1 commit 1dd2361

File tree

5 files changed

+259
-20
lines changed

5 files changed

+259
-20
lines changed

connect/models/asset.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@
1515

1616

1717
class Asset(BaseModel):
18-
pass
18+
def get_param_by_id(self, id_):
19+
try:
20+
return list(filter(lambda param: param.id == id_, self.params))[0]
21+
except IndexError:
22+
return None
23+
24+
def get_item_by_mpn(self, mpn):
25+
try:
26+
return list(filter(lambda item: item.mpn == mpn, self.items))[0]
27+
except IndexError:
28+
return None
1929

2030

2131
class AssetSchema(BaseSchema):

connect/models/fulfillment.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@
1313

1414

1515
class Fulfillment(BaseModel):
16-
pass
16+
@property
17+
def new_items(self):
18+
return list(filter(
19+
lambda item: item.quantity > 0 and item.old_quantity == 0,
20+
self.asset.items))
21+
22+
@property
23+
def changed_items(self):
24+
return list(filter(
25+
lambda item: item.quantity > 0 and item.old_quantity > 0,
26+
self.asset.items))
27+
28+
@property
29+
def removed_items(self):
30+
return list(filter(
31+
lambda item: item.quantity == 0 and item.old_quantity > 0,
32+
self.asset.items))
1733

1834

1935
class FulfillmentSchema(BaseSchema):

connect/models/product.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Item(BaseModel):
2929
class ItemSchema(BaseSchema):
3030
global_id = fields.Str()
3131
mpn = fields.Str()
32-
old_quantity = fields.Str()
32+
old_quantity = fields.Integer()
3333
quantity = fields.Integer()
3434

3535
@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: 85 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/')
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]
3140

32-
requests = FulfillmentResource(config).list()
33-
request_obj = FulfillmentResource(config).get(pk='PR-000-000-000')
34-
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,67 @@ 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+
try:
79+
# Python 2
80+
assert isinstance(request_obj.asset.external_id, basestring)
81+
except NameError:
82+
# Python 3
83+
assert isinstance(request_obj.asset.external_id, str)
84+
85+
86+
@patch('requests.get', MagicMock(return_value=_get_response2_ok()))
87+
def test_fulfillment_items():
88+
# Get request
89+
requests = FulfillmentResource().list()
90+
assert len(requests) == 1
91+
request = requests[0]
92+
assert isinstance(request, Fulfillment)
93+
94+
# Test new items
95+
new_items = request.new_items
96+
assert isinstance(new_items, list)
97+
assert len(new_items) == 2
98+
for item in new_items:
99+
assert isinstance(item, Item)
100+
101+
# Test changed items
102+
changed_items = request.changed_items
103+
assert isinstance(new_items, list)
104+
assert len(changed_items) == 2
105+
for item in changed_items:
106+
assert isinstance(item, Item)
107+
108+
# Test removed items
109+
removed_items = request.removed_items
110+
assert isinstance(removed_items, list)
111+
assert len(removed_items) == 1
112+
for item in removed_items:
113+
assert isinstance(item, Item)
114+
115+
116+
@patch('requests.get', MagicMock(return_value=_get_response2_ok()))
117+
def test_asset_methods():
118+
# Get asset
119+
requests = FulfillmentResource().list()
120+
assert len(requests) == 1
121+
assert isinstance(requests[0], Fulfillment)
122+
asset = requests[0].asset
123+
assert isinstance(asset, Asset)
124+
125+
# Get param by id
126+
assert isinstance(asset.get_param_by_id('howyoufeel'), Param)
127+
assert asset.get_param_by_id('howyoufeel').id == 'howyoufeel'
128+
assert not asset.get_param_by_id('invalid-id')
129+
130+
# Get item by id
131+
assert isinstance(asset.get_item_by_mpn('TEAM-ST3L2T1Y'), Item)
132+
assert asset.get_item_by_mpn('TEAM-ST3L2T1Y').mpn == 'TEAM-ST3L2T1Y'
133+
assert not asset.get_item_by_mpn('invalid-mpn')

0 commit comments

Comments
 (0)