Skip to content

Commit e54d16b

Browse files
committed
Update InitializeServiceMixin
* Add tests for InitializeServiceMixin
1 parent 9cdb533 commit e54d16b

3 files changed

Lines changed: 72 additions & 14 deletions

File tree

openprocurement/auction/worker/mixins.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datetime import datetime, timedelta
88
from copy import deepcopy
99
from dateutil.tz import tzlocal
10-
from requests import request
10+
from requests import request, Session as RequestsSession
1111
from yaml import safe_dump as yaml_dump
1212
from couchdb.http import HTTPError, RETRYABLE_ERRORS
1313
from fractions import Fraction
@@ -687,7 +687,7 @@ def init_api(self):
687687
response = make_request(url=api_url.format(**self.worker_defaults),
688688
method="get", retry_count=5)
689689
if not response:
690-
raise Exception("Auction DS can't be reached")
690+
raise Exception("API can't be reached")
691691
else:
692692
self.tender_url = urljoin(
693693
self.worker_defaults["resource_api_server"],
@@ -703,9 +703,7 @@ def init_ds(self):
703703
Check Document service availability and set session_ds attribute
704704
"""
705705
ds_config = self.worker_defaults.get("DOCUMENT_SERVICE")
706-
resp = request("GET", ds_config.get("url"), timeout=5)
707-
if not resp or resp.status_code != 200:
708-
raise Exception("Auction DS can't be reached")
706+
request("GET", ds_config.get("url"), timeout=5)
709707
self.session_ds = RequestsSession()
710708

711709
def init_database(self):

openprocurement/auction/worker/tests/unit/conftest.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from openprocurement.auction.worker.server import (
2222
app as worker_app, BidsForm
2323
)
24-
# from openprocurement.auction.tests.functional.main import update_auctionPeriod
2524

2625

2726
def update_auctionPeriod(data):
@@ -35,8 +34,14 @@ def update_auctionPeriod(data):
3534
PWD = os.path.dirname(os.path.realpath(__file__))
3635

3736
worker_defaults_file_path = os.path.join(PWD, "../data/auction_worker_defaults.yaml")
38-
with open(worker_defaults_file_path) as stream:
39-
worker_defaults = yaml.load(stream)
37+
38+
39+
@pytest.yield_fixture(scope='function')
40+
def worker_config():
41+
update_auctionPeriod(tender_data)
42+
with open(worker_defaults_file_path) as stream:
43+
worker_defaults = yaml.load(stream)
44+
return worker_defaults
4045

4146

4247
@pytest.yield_fixture(
@@ -57,13 +62,13 @@ def universal_auction(request):
5762
lot_id=request.param['lot_id']
5863
)
5964

65+
6066
@pytest.yield_fixture(scope="function")
61-
def auction():
62-
update_auctionPeriod(tender_data)
67+
def auction(worker_config):
6368

6469
yield Auction(
6570
tender_id=tender_data['data']['auctionID'],
66-
worker_defaults=yaml.load(open(worker_defaults_file_path)),
71+
worker_defaults=worker_config,
6772
auction_data=tender_data,
6873
lot_id=False
6974
)
@@ -90,9 +95,9 @@ def features_auction():
9095

9196

9297
@pytest.fixture(scope='function')
93-
def db(request):
94-
server = couchdb.Server("http://" + worker_defaults['COUCH_DATABASE'].split('/')[2])
95-
name = worker_defaults['COUCH_DATABASE'].split('/')[3]
98+
def db(request, worker_config):
99+
server = couchdb.Server("http://" + worker_config['COUCH_DATABASE'].split('/')[2])
100+
name = worker_config['COUCH_DATABASE'].split('/')[3]
96101

97102
def delete():
98103
del server[name]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)