Skip to content

Commit

Permalink
Return response object instead of string ID (#27)
Browse files Browse the repository at this point in the history
Return response object instead of ID.
Now caller can get source error text.
  • Loading branch information
Dimedrolity authored May 4, 2023
1 parent 3edb4c7 commit 5f2bccd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
1 change: 1 addition & 0 deletions moira_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from moira_client.moira import Moira
from requests import HTTPError
9 changes: 4 additions & 5 deletions moira_client/models/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,29 @@ def _send_request(self, trigger_id=None):
raise ResponseStructureError('id not in response', res)

self._id = res['id']
return self._id
return res

def save(self):
"""
Save trigger
:return: trigger_id
:return: response object
"""
if self._id:
return self.update()
trigger = self.check_exists()

if trigger:
self._id = trigger.id
self.update()
return trigger.id
return self.update()

return self._send_request()

def update(self):
"""
Update trigger
:return: trigger id
:return: response object
"""
return self._send_request(self._id)

Expand Down
12 changes: 6 additions & 6 deletions tests/models/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ def test_save_new_trigger(self):
trigger_id = '1'

with patch.object(client, 'put', return_value={'id': trigger_id}) as put_mock:
result_id = trigger.save()
result = trigger.save()

self.assertTrue(get_mock.called)
self.assertTrue(put_mock.called)
self.assertEqual(put_mock.call_args[0][0], f'trigger?{self.QUERY_PARAM_VALIDATE_FLAG}')
self.assertEqual(result_id, trigger_id)
self.assertEqual(result['id'], trigger_id)

def test_save_existing_trigger(self):
client = Client(self.api_url)
Expand All @@ -167,12 +167,12 @@ def test_save_existing_trigger(self):
with patch.object(client, 'get', side_effect=[{'list': [trigger_from_response]}, state, trigger_from_response]) as get_mock:
trigger_dto = trigger_manager.create(**trigger)
with patch.object(client, 'put', return_value={'id': trigger_id}) as put_mock:
result_id = trigger_dto.save()
result = trigger_dto.save()

self.assertTrue(get_mock.called)
self.assertTrue(put_mock.called)
self.assertEqual(put_mock.call_args[0][0], f'trigger/{trigger_id}?{self.QUERY_PARAM_VALIDATE_FLAG}')
self.assertEqual(result_id, trigger_id)
self.assertEqual(result['id'], trigger_id)

def test_save_trigger_with_id(self):
client = Client(self.api_url)
Expand All @@ -193,9 +193,9 @@ def test_save_trigger_with_id(self):
with patch.object(client, 'get', side_effect=[state, trigger]) as get_mock:
trigger_dto = trigger_manager.create(**trigger)
with patch.object(client, 'put', return_value={'id': trigger_id}) as put_mock:
result_id = trigger_dto.save()
result = trigger_dto.save()

self.assertTrue(get_mock.called)
self.assertTrue(put_mock.called)
self.assertEqual(put_mock.call_args[0][0], f'trigger/{trigger_id}?{self.QUERY_PARAM_VALIDATE_FLAG}')
self.assertEqual(result_id, trigger_id)
self.assertEqual(result['id'], trigger_id)

0 comments on commit 5f2bccd

Please sign in to comment.