def test_get_tender_contracts_resource_gone(self, mocked_contract_client, mocked_tender_client, mocked_sync_client, mocked_db, mocked_logger, mocked_gevent): error_msg = { "status": "error", "errors": [{ "location": "url", "name": "contract_id", "description": "Archived" }] } resp = MagicMock() resp.body_string.return_value = json.dumps(error_msg) resp.text = json.dumps(error_msg) resp.status_code = 410 resp.status_int = 410 cb = ContractingDataBridge({'main': {}}) tender_to_sync = { 'id': '1' * 32, 'dateModified': datetime.now().isoformat() } tender = { "data": { "id": "1" * 32, "dateModified": tender_to_sync['dateModified'], "contracts": [{ "id": "2" * 32, "status": "active" }] } } cb.tenders_queue.put(tender_to_sync) cb.cache_db = MagicMock() cb.cache_db.has.return_value = False cb.tenders_sync_client = MagicMock() cb.tenders_sync_client.get_tender.return_value = tender exception = ResourceGone(response=resp) cb.contracting_client_ro = MagicMock() cb.contracting_client_ro.get_contract.side_effect = [exception] cb._get_tender_contracts() logger_msg = 'Sync contract {} of tender {} has been archived'.format( "2" * 32, "1" * 32) extra = { 'JOURNAL_TENDER_ID': '1' * 32, 'MESSAGE_ID': 'c_bridge_contract_to_sync', 'JOURNAL_CONTRACT_ID': '2' * 32 } mocked_logger.info.assert_has_calls([call(logger_msg, extra=extra)])
def test_put_tender_in_cache_by_contract(self, mocked_contract_client, mocked_tender_client, mocked_sync_client, mocked_db): cb = ContractingDataBridge({'main': {}}) tender_id = '2001' cb.basket = {'1': 'one', '2': 'two', '42': 'why'} cb.cache_db = MagicMock() cb._put_tender_in_cache_by_contract({'id': '1984'}, tender_id) self.assertEqual(cb.basket.get('42', None), 'why') self.assertEqual(cb.cache_db.put.called, False) cb._put_tender_in_cache_by_contract({'id': '42'}, tender_id) self.assertEqual(cb.basket.get('42', None), None) cb.cache_db.put.assert_called_once_with('2001', 'why')
def test_put_contracts(self, mocked_loop, mocked_gevent, mocked_logger, mocked_contract_client, mocked_tender_client, mocked_sync_client, mocked_db): list_loop = [True, False] mocked_loop.__nonzero__.side_effect = list_loop contract = munch.munchify({'id': '42', 'tender_id': '1984'}) bridge = ContractingDataBridge({'main': {}}) bridge.contracts_put_queue = MagicMock() bridge.contracts_put_queue.get.return_value = contract bridge.contracting_client = MagicMock() bridge.contracts_retry_put_queue = MagicMock() bridge.contracting_client_init = MagicMock() bridge.cache_db = MagicMock() bridge._put_tender_in_cache_by_contract = MagicMock() bridge.put_contracts() bridge.contracts_put_queue.get.assert_called_once_with() bridge.contracting_client.create_contract.assert_called_once_with( {'data': contract.toDict()}) bridge.cache_db.put.assert_called_once_with(contract.id, True) bridge._put_tender_in_cache_by_contract.assert_called_once_with( contract.toDict(), contract.tender_id) mocked_gevent.sleep.assert_called_once_with(0) list_contracts = [] for i in range(0, 10): list_contracts.append(dict(id=i, tender_id=(i + 100))) bridge.contracts_put_queue = MagicMock() bridge.contracts_put_queue.get.side_effect = list_contracts list_loop = [True for i in range(0, 10)] list_loop.append(False) mocked_loop.__nonzero__.side_effect = list_loop bridge.put_contracts() extract_calls = [ data[0] for data, call in bridge.contracts_retry_put_queue.put.call_args_list ] for i in range(0, 10): assert extract_calls[i]['id'] == i self.assertEqual(len(extract_calls), 10) bridge.contracting_client_init.assert_called_once_with()
def test_retry_put_contracts(self, mocked_loop, mocked_gevent, mocked_logger, mocked_contract_client, mocked_tender_client, mocked_sync_client, mocked_db): true_list = [True, False] mocked_loop.__nonzero__.side_effect = true_list contract = {'id': '42', 'tender_id': '1984'} bridge = ContractingDataBridge({'main': {}}) remember_put_with_retry = bridge._put_with_retry bridge.contracts_retry_put_queue = MagicMock() bridge._put_with_retry = MagicMock() bridge.cache_db = MagicMock() bridge._put_tender_in_cache_by_contract = MagicMock() bridge.contracts_retry_put_queue.get.return_value = contract bridge.retry_put_contracts() bridge.contracts_retry_put_queue.get.assert_called_once_with() bridge._put_with_retry.assert_called_once_with(contract) bridge.cache_db.put.assert_called_once_with(contract['id'], True) bridge._put_tender_in_cache_by_contract.assert_called_once_with( contract, contract['tender_id']) mocked_gevent.sleep.assert_called_once_with(0) bridge._put_with_retry = remember_put_with_retry mocked_loop.__nonzero__.side_effect = true_list e = Exception('Boom!') bridge.contracting_client.create_contract = MagicMock( side_effect=[e, True]) contract = munch.munchify(contract) bridge.contracts_retry_put_queue.get.return_value = contract bridge.retry_put_contracts() mocked_logger.exception.assert_called_once_with(e)