def test_retry_upload_to_tender(self, mrequest, gevent_sleep): gevent_sleep.side_effect = custom_sleep mrequest.post(self.url, json=self.stat_200(), status_code=200) self.client._create_tender_resource_item.side_effect = [ Unauthorized(http_code=401), Unauthorized(http_code=403), Unauthorized(http_code=429), self.get_tender() ] self.upload_to_doc_service_queue.put(self.data) self.assertItemsEqual(self.process_tracker.processing_items.keys(), [item_key(self.tender_id, self.award_id)]) self.assertEqual(self.upload_to_doc_service_queue.qsize(), 1) self.shutdown_when_done(self.worker) self.assertEqual(self.upload_to_doc_service_queue.qsize(), 0, 'Queue should be empty') self.assertEqual(self.upload_to_tender_queue.qsize(), 0, 'Queue should be empty') self.assertEqual(mrequest.call_count, 1) self.assertEqual(mrequest.request_history[0].url, u'127.0.0.1:80/upload') self.assertIsNotNone( mrequest.request_history[0].headers['X-Client-Request-ID']) self.assertEqual(self.process_tracker.processing_items, {}) # test that item removed from processing_items self.assertEqual(self.client._create_tender_resource_item.call_count, 4) # check upload to tender
def request(self, method, path=None, payload=None, headers=None, params_dict=None, **params): """ HTTP request This method may be the only one you want to override when subclassing `restkit.rest.Resource`. - payload: string or File object passed to the body of the request - path: string additionnal path to the uri - headers: dict, optionnal headers that will be added to HTTP request. :params_dict: Options parameters added to the request as a dict - params: Optionnal parameterss added to the request """ params = params or {} params.update(params_dict or {}) while True: uri = util.make_uri(self.uri, path, charset=self.charset, safe=self.safe, encode_keys=self.encode_keys, **self.make_params(params)) # make request resp = self.client.request(uri, method=method, body=payload, headers=self.make_headers(headers)) if resp is None: # race condition raise ValueError("Unkown error: response object is None") if resp.status_int >= 400: if resp.status_int == 404: raise ResourceNotFound(resp.body_string(), response=resp) elif resp.status_int in (401, 403): if self.unauthorized(resp): raise Unauthorized(resp.body_string(), http_code=resp.status_int, response=resp) elif resp.status_int == 410: raise ResourceGone(resp.body_string(), response=resp) else: raise RequestFailed(resp.body_string(), http_code=resp.status_int, response=resp) else: break return resp
def test_retry_upload_to_tender(self, gevent_sleep): gevent_sleep.side_effect = custom_sleep self.client._create_tender_resource_item.side_effect = [ Unauthorized(http_code=401), Unauthorized(http_code=403), Unauthorized(http_code=429), self.get_tender() ] self.upload_to_tender_queue.put(self.data) self.assertItemsEqual(self.process_tracker.processing_items.keys(), [item_key(self.tender_id, self.award_id)]) self.assertEqual(self.upload_to_tender_queue.qsize(), 1) self.shutdown_when_done(self.worker) self.assertEqual(self.upload_to_tender_queue.qsize(), 0, 'Queue should be empty') self.assertEqual(self.process_tracker.processing_items, {}) # test that item removed from processing_items self.assertEqual(self.client._create_tender_resource_item.call_count, 4) # check upload to tender
def test_worker(self, gevent_sleep): """ Returns tenders, check queue elements after filtering """ gevent_sleep.side_effect = custom_sleep self.client.sync_tenders.side_effect = [RequestFailed(), # worker must restart self.mock_tenders("active.qualification", self.tenders_id[0], 'UA'), Unauthorized(), self.mock_tenders("active.tendering", uuid.uuid4().hex, 'UA'), self.mock_tenders("active.pre-qualification", self.tenders_id[1], 'EU')] for tender_id in self.tenders_id[0:2]: self.assertEqual(self.tender_queue.get(), tender_id)
def test_upload_to_tender_exception_status_int_none(self, gevent_sleep): gevent_sleep.side_effect = custom_sleep self.upload_to_tender_queue.put(self.data) client = MagicMock() client._create_tender_resource_item = MagicMock( side_effect=[Unauthorized()]) self.shutdown_when_done(self.worker) self.assertEqual(self.upload_to_tender_queue.qsize(), 0, 'Queue should be empty') self.assertEqual(self.worker.sleep_change_value.time_between_requests, 0)
def test_worker_restart(self, gevent_sleep): """ Process tender after catch Unauthorized exception """ gevent_sleep.side_effect = custom_sleep self.client.request.side_effect = [ Unauthorized(http_code=403), Unauthorized(http_code=403), Unauthorized(http_code=403), ResponseMock({'X-Request-ID': self.request_ids[0]}, munchify({'prev_page': {'offset': '123'}, 'next_page': {'offset': '1234'}, 'data': {'status': tender_status, 'id': self.tender_id, 'procurementMethodType': 'aboveThresholdEU', 'awards': [self.awards(0, 0, AWARD_STATUS, CODES[0]), self.awards(1, 1, 'unsuccessful', CODES[2])]}}))] data = Data(self.tender_id, self.award_ids[0], CODES[0], "company_name", {"meta": {"sourceRequests": [self.request_ids[0]]}}) self.assertEqual(self.edrpou_codes_queue.get(), data) self.assertItemsEqual(self.process_tracker.processing_items.keys(), [item_key(self.tender_id, self.award_ids[0])])