Skip to content

Commit d06952f

Browse files
committed
Added buy project function and tests.
Additionally, fixed issue with schema validator not having keyword-args in the validate call. This caused an issue with the new project schema, which this commit fixes.
1 parent 87ea217 commit d06952f

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

dynatademand/api.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
SCHEMAS = [
99
"project_new",
10+
"project_buy",
1011
]
1112

1213

@@ -50,7 +51,7 @@ def _load_schemas(self):
5051
def _validate_object(self, schema_type, data):
5152
# jsonschema.validate will return none if there is no error,
5253
# otherwise it will raise its' own error with details on the failure.
53-
jsonschema.validate(self._schemas[schema_type], data)
54+
jsonschema.validate(schema=self._schemas[schema_type], instance=data)
5455

5556
def _check_authentication(self):
5657
# This doesn't check if the access token is valid, just that it exists.
@@ -158,6 +159,22 @@ def create_project(self, project_data):
158159
)
159160
return response_data
160161

162+
def buy_project(self, project_id, buy_data):
163+
'''
164+
Buy the line items for a project, agreeing to the price. A Line Item
165+
can only be bought if the feasibility for the line item is in status=READY
166+
and totalCount > 0.
167+
'''
168+
self._validate_object("project_buy", buy_data)
169+
response_data = self._api_post('/projects/{}/buy'.format(project_id), buy_data)
170+
if response_data.get('status').get('message') != 'success':
171+
raise DemandAPIError(
172+
"Could not buy project. Demand API responded with: {}".format(
173+
response_data
174+
)
175+
)
176+
return response_data
177+
161178
def get_project(self, project_id):
162179
return self._api_get('/projects/{}'.format(project_id))
163180

dynatademand/schemas/project_buy.json

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+
}

dynatademand/schemas/project_new.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
"devices": {
2929
"type": "array",
3030
"description": "Device targeting for the project",
31-
"enum": [
32-
"mobile",
33-
"desktop",
34-
"tablet"
35-
],
3631
"default": [
3732
"mobile",
3833
"desktop",
3934
"tablet"
4035
],
4136
"items": {
42-
"type": "string"
37+
"type": "string",
38+
"enum": [
39+
"mobile",
40+
"desktop",
41+
"tablet"
42+
]
4343
}
4444
},
4545
"category": {
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mock import patch
1212

1313
from dynatademand.api import DemandAPIClient
14+
from dynatademand.errors import DemandAPIError
1415

1516
BASE_HOST = "http://test-url.example"
1617

@@ -55,3 +56,20 @@ def test_create_project(self):
5556
responses.add(responses.POST, '{}/sample/v1/projects'.format(BASE_HOST), json={'status': {'message': 'success'}}, status=200)
5657
self.api.create_project(new_project_data)
5758
self.assertEqual(len(responses.calls), 1)
59+
60+
@responses.activate
61+
def test_buy_project(self):
62+
# Tests buying a project.
63+
with open('./tests/test_files/examples/project_buy.json', 'r') as buy_project_file:
64+
buy_project_data = json.load(buy_project_file)
65+
# Success response
66+
responses.add(responses.POST, '{}/sample/v1/projects/24/buy'.format(BASE_HOST), json={'status': {'message': 'success'}}, status=200)
67+
# Response with error status
68+
responses.add(responses.POST, '{}/sample/v1/projects/24/buy'.format(BASE_HOST), json={'status': {'message': 'error'}}, status=200)
69+
# Test success response
70+
self.api.buy_project(24, buy_project_data)
71+
self.assertEqual(len(responses.calls), 1)
72+
# Test error response
73+
with self.assertRaises(DemandAPIError):
74+
self.api.buy_project(24, buy_project_data)
75+
self.assertEqual(len(responses.calls), 2)

0 commit comments

Comments
 (0)