Skip to content

Commit 8c09824

Browse files
Merge pull request #11 from dynata/POP-2289
[POP-2289] Added buy project function and tests.
2 parents 49d0948 + 1bbf04e commit 8c09824

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

dynatademand/api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ def create_project(self, project_data):
187187
)
188188
return response_data
189189

190+
def buy_project(self, project_id, buy_data):
191+
'''
192+
Buy the line items for a project, agreeing to the price. A Line Item
193+
can only be bought if the feasibility for the line item is in status=READY
194+
and totalCount > 0.
195+
'''
196+
self.validator.validate_request(
197+
'buy_project',
198+
path_data={'extProjectId': '{}'.format(project_id)},
199+
request_body=buy_data,
200+
)
201+
response_data = self._api_post('/projects/{}/buy'.format(project_id), buy_data)
202+
if response_data.get('status').get('message') != 'success':
203+
raise DemandAPIError(
204+
"Could not buy project. Demand API responded with: {}".format(
205+
response_data
206+
)
207+
)
208+
return response_data
209+
190210
def close_project(self, project_id):
191211
# Closes the requested project. Once a project is closed, all traffic
192212
# is stopped, and the project is automatically sent for invoicing.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"type": "array",
3+
"items": {
4+
"type": "object",
5+
"required": [
6+
"extLineItemId",
7+
"surveyURL",
8+
"surveyTestURL"
9+
],
10+
"properties": {
11+
"extLineItemId": {
12+
"type": "string"
13+
},
14+
"surveyURL": {
15+
"type": "string"
16+
},
17+
"surveyTestURL": {
18+
"type": "string"
19+
}
20+
}
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"extProjectId": {
5+
"type": "string",
6+
"required": true
7+
}
8+
},
9+
"required": [
10+
"extProjectId"
11+
]
12+
}

dynatademand/validator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'get_project': ['path', ],
1414
'close_project': ['path', ],
1515
'update_project': ['path', 'body', ],
16+
'buy_project': ['path', 'body', ],
1617
'get_project_detailed_report': ['path', ],
1718

1819
# Line items

tests/test_files/buy_project.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"extLineItemId": "lineItem001",
4+
"surveyURL": "www.mysurvey.com/live/survey?pid=<#DubKnowledge[1500/Entity id]>&k2=<#Project[Secure Key 2]>&psid=<#IdParameter[Value]>",
5+
"surveyTestURL": "www.mysurvey.com/test/survey?pid=<#DubKnowledge[1500/Entity id]>&k2=<#Project[Secure Key 2]>&psid=<#IdParameter[Value]>"
6+
},
7+
{
8+
"extLineItemId": "lineItem002",
9+
"surveyURL": "www.mysurvey.com/live/survey?pid=<#DubKnowledge[1500/Entity id]>&k2=<#Project[Secure Key 2]>&psid=<#IdParameter[Value]>",
10+
"surveyTestURL": "www.mysurvey.com/test/survey?pid=<#DubKnowledge[1500/Entity id]>&k2=<#Project[Secure Key 2]>&psid=<#IdParameter[Value]>"
11+
}
12+
]

tests/test_projects.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ def test_create_project(self):
6060
self.api.create_project(new_project_data)
6161
self.assertEqual(len(responses.calls), 1)
6262

63+
@responses.activate
64+
def test_buy_project(self):
65+
# Tests buying a project.
66+
with open('./tests/test_files/buy_project.json', 'r') as buy_project_file:
67+
buy_project_data = json.load(buy_project_file)
68+
# Success response
69+
responses.add(
70+
responses.POST,
71+
'{}/sample/v1/projects/24/buy'.format(BASE_HOST),
72+
json={'status': {'message': 'success'}},
73+
status=200)
74+
# Response with error status
75+
responses.add(
76+
responses.POST,
77+
'{}/sample/v1/projects/24/buy'.format(BASE_HOST),
78+
json={'status': {'message': 'error'}},
79+
status=200)
80+
# Test success response
81+
self.api.buy_project(24, buy_project_data)
82+
self.assertEqual(len(responses.calls), 1)
83+
# Test error response
84+
with self.assertRaises(DemandAPIError):
85+
self.api.buy_project(24, buy_project_data)
86+
self.assertEqual(len(responses.calls), 2)
87+
6388
@responses.activate
6489
def test_close_project(self):
6590
# Tests closing a project.

0 commit comments

Comments
 (0)