|
| 1 | +from copy import deepcopy |
| 2 | +from uuid import uuid4 |
| 3 | + |
| 4 | +import pytest |
| 5 | +from requests import exceptions |
| 6 | + |
| 7 | +from openprocurement.auction.worker.auction import Auction |
| 8 | +from openprocurement.auction.worker.mixins import Server |
| 9 | + |
| 10 | + |
| 11 | +def test_init_services(worker_config, logger, mocker): |
| 12 | + |
| 13 | + test_config = deepcopy(worker_config) |
| 14 | + test_config['with_document_service'] = True |
| 15 | + test_config['DOCUMENT_SERVICE']['url'] = "http://1.2.3.4:6543/" |
| 16 | + test_config['resource_api_server'] = "http://1.2.3.4:6543/" |
| 17 | + |
| 18 | + tender_id = uuid4().hex # random tender id |
| 19 | + |
| 20 | + mock_make_request = mocker.MagicMock() |
| 21 | + mock_make_request.side_effect = Exception("API can't be reached") |
| 22 | + mocker.patch('openprocurement.auction.worker.mixins.make_request', mock_make_request) |
| 23 | + |
| 24 | + with pytest.raises(Exception) as e: |
| 25 | + Auction(tender_id, test_config) |
| 26 | + log_strings = logger.log_capture_string.getvalue().split('\n') |
| 27 | + assert e.value.message == "API can't be reached" |
| 28 | + |
| 29 | + assert log_strings.count("API can't be reached") == 1 |
| 30 | + assert "ConnectTimeout" in log_strings[-2] |
| 31 | + |
| 32 | + with pytest.raises(exceptions.RequestException) as e: |
| 33 | + Auction(tender_id, test_config, {'test_auction_data': True}) |
| 34 | + log_strings = logger.log_capture_string.getvalue().split('\n') |
| 35 | + assert "ConnectTimeout" in str(e.value.message) |
| 36 | + |
| 37 | + assert log_strings.count("API can't be reached") == 1 |
| 38 | + assert "ConnectTimeout" in log_strings[-2] |
| 39 | + |
| 40 | + test_config['with_document_service'] = False |
| 41 | + auction = Auction(tender_id, test_config, {'test_auction_data': True}) |
| 42 | + |
| 43 | + assert auction.__getattribute__("tender_url") is not None |
| 44 | + assert auction.__getattribute__("db") is not None |
| 45 | + |
| 46 | + mock_server_connect = mocker.patch.object(Server, "__init__", autospec=True) |
| 47 | + mock_server_connect.side_effect = Exception("Connection refused") |
| 48 | + |
| 49 | + with pytest.raises(Exception) as e: |
| 50 | + Auction(tender_id, test_config, {'test_auction_data': True}) |
| 51 | + log_strings = logger.log_capture_string.getvalue().split('\n') |
| 52 | + assert e.value.message == "Connection refused" |
| 53 | + |
| 54 | + assert log_strings.count("API can't be reached") == 1 |
| 55 | + assert log_strings.count("Connection refused") == 1 |
0 commit comments