def test_handle_200_two_response(self, upload_to_doc_service): code = "1234" request_id = uuid4().hex response_id = uuid4().hex tender_id, item_name, item_id = "f" * 32, "award", "a" * 32 source_date = ["2018-12-25T19:00:00+02:00"] with patch("edr_bot.tasks.requests") as requests_mock: requests_mock.get.return_value = Mock( status_code=200, json=Mock( return_value={ 'data': [{ "test": 1 }, { "test": 2 }], "meta": { "detailsSourceDate": source_date } }), headers={'X-Request-ID': response_id}) with patch("edr_bot.tasks.uuid4") as uuid4_mock: uuid4_mock.return_value = Mock(hex="b" * 32) get_edr_data(code, request_id, tender_id, item_name, item_id) self.assertEqual(upload_to_doc_service.delay.call_args_list, [ call(data={ 'meta': { 'sourceDate': source_date[0], 'id': '{}.2.1'.format("b" * 32), 'author': DOC_AUTHOR, 'sourceRequests': [request_id], 'version': VERSION }, 'data': { 'test': 1 } }, item_id=item_id, item_name=item_name, tender_id=tender_id), call(data={ 'meta': { 'sourceDate': None, 'id': '{}.2.2'.format("b" * 32), 'author': DOC_AUTHOR, 'sourceRequests': [request_id], 'version': VERSION }, 'data': { 'test': 2 } }, item_id=item_id, item_name=item_name, tender_id=tender_id) ])
def test_handle_connection_error(self): code = "1234" tender_id, item_name, item_id = "f" * 32, "award", "a" * 32 with patch("edr_bot.tasks.requests") as requests_mock: requests_mock.get.side_effect = requests.exceptions.ConnectionError() get_edr_data.retry = Mock(side_effect=Retry) with self.assertRaises(Retry): get_edr_data(code, tender_id, item_name, item_id) get_edr_data.retry.assert_called_once_with(exc=requests_mock.get.side_effect)
def test_handle_404_response(self, upload_to_doc_service): code = "1234" tender_id, item_name, item_id = "f" * 32, "award", "a" * 32 ret_aft, resp_id = 13, uuid4().hex source_date = ["2018-12-25T19:00:00+02:00"] with patch("edr_bot.tasks.requests") as requests: requests.get.return_value = Mock( status_code=404, json=Mock(return_value={ 'errors': [ { 'description': [ { "error": { "errorDetails": "Couldn't find this code in EDR.", "code": "notFound" }, "meta": {"detailsSourceDate": source_date} } ] } ] }), headers={ 'X-Request-ID': resp_id, 'content-type': 'application/json', } ) with patch("edr_bot.tasks.uuid4") as uuid4_mock: uuid4_mock.return_value = Mock(hex="b" * 32) get_edr_data(code, tender_id, item_name, item_id) upload_to_doc_service.delay.assert_called_once_with( data={ 'error': { 'errorDetails': "Couldn't find this code in EDR.", 'code': 'notFound' }, 'meta': { 'detailsSourceDate': source_date, 'id': "b" * 32, 'author': 'IdentificationBot', 'sourceRequests': [resp_id], 'version': '2.0.0' } }, tender_id=tender_id, item_name=item_name, item_id=item_id )
def test_handle_429_response(self): code = "1234" request_id = uuid4().hex tender_id, item_name, item_id = "f" * 32, "award", "a" * 32 ret_aft = 13 with patch("edr_bot.tasks.requests") as requests: requests.get.return_value = Mock(status_code=429, headers={'Retry-After': ret_aft}) get_edr_data.retry = Mock(side_effect=get_edr_data.retry) with self.assertRaises(Retry): get_edr_data(code, request_id, tender_id, item_name, item_id) get_edr_data.retry.assert_called_once_with(countdown=13)