Skip to content

Commit 02844b4

Browse files
Merge pull request #103 from wogsland/70-get-simulations
add get simulations function
2 parents b427bf3 + fccc322 commit 02844b4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

CmixAPIClient/api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,16 @@ def create_survey(self, xml_string):
335335
response_json = response.json()
336336
self.update_project(response_json.get('projectId'), status=self.SURVEY_STATUS_DESIGN)
337337
return response_json
338+
339+
def get_survey_simulations(self, survey_id):
340+
self.check_auth_headers()
341+
simulations_url = '{}/surveys/{}/simulations'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
342+
simulations_response = requests.get(simulations_url, headers=self._authentication_headers)
343+
if simulations_response.status_code != 200:
344+
raise CmixError(
345+
'CMIX returned a non-200 response code while getting simulations: {} and error {}'.format(
346+
simulations_response.status_code,
347+
simulations_response.text
348+
)
349+
)
350+
return simulations_response.json()

tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,29 @@ def test_add_extra_url_params(self):
270270
extra_params_formatted = '&{}&{}'.format(extra_params[0], extra_params[1])
271271
expected = '{}{}'.format(url, extra_params_formatted)
272272
self.assertEqual(result, expected)
273+
274+
def test_get_survey_simulations(self):
275+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
276+
277+
# success case
278+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
279+
mock_get = mock.Mock()
280+
mock_get.status_code = 200
281+
mock_get.json.return_value = {}
282+
mock_request.get.return_value = mock_get
283+
284+
self.cmix_api.get_survey_simulations(self.survey_id)
285+
286+
base_url = CMIX_SERVICES['survey']['BASE_URL']
287+
surveys_url = '{}/surveys/{}/simulations'.format(base_url, self.survey_id)
288+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
289+
290+
# error case (survey not found)
291+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
292+
mock_get = mock.Mock()
293+
mock_get.status_code = 404
294+
mock_get.json.return_value = {}
295+
mock_request.get.return_value = mock_get
296+
297+
with self.assertRaises(CmixError):
298+
self.cmix_api.get_survey_simulations(self.survey_id)

0 commit comments

Comments
 (0)