Code :
=====
@require_http_methods(['POST', 'PUT'])
def set_session_chip(request, chip_id):
try:
chip = Chip.objects.get(id=chip_id)
set_latest_chip(request, chip_id)
chip_name = chip.name
except Chip.DoesNotExist as e:
chip_name = None
return http_response_json({'chip_id': chip_id, 'chip_name': chip_name})
UNIT Testing:
==================
class TestSendSetSessionChip(unittest.TestCase):
def setUp(self):
# Mock prepare
self.request = Mock()
self.request.method = 'POST'
@patch('model.views.http_response_json')
@patch('model.views.set_latest_chip')
@patch('model.views.Chip')
def test_set_session_chip_valid_chip_id(self, chip, set_latest_chip, http_response_json):
# Mock Prepare
chip_id = 79
chip_mock = Mock()
chip_mock.name = 'QAD682 (Horus)'
# return value
chip.objects.get.return_value = chip_mock
# Function Call
set_latest_chip(self.request, chip_id)
# Expected response
data = {'chip_id': 79, 'chip_name': 'QAD682 (Horus)'}
# function call
set_session_chip(self.request, chip_id)
# assertion
http_response_json.assert_called_with(data)
@patch('model.views.http_response_json')
def test_set_session_chip_invalid_chip_id(self, http_response_json):
# Mock Prepare
chip_id = 0
# return value
self.assertRaises(Chip.DoesNotExist, Chip.objects.get, id=0)
# Expected response
data = {'chip_id': 0, 'chip_name': None}
# function call
set_session_chip(self.request, chip_id)
# assertion
http_response_json.assert_called_with(data)
def tearDown(self):
pass
==============
No comments:
Post a Comment