<commit_before>import couchdb
import os
import pytest
import yaml
from openprocurement.auction.chronograph import AuctionsChronograph
from openprocurement.auction.tests.utils import update_auctionPeriod, \
    AUCTION_DATA, PWD
from openprocurement.auction.tests.unit.utils import TestClient
from openprocurement.auction.tests.unit.utils import kill_child_processes
from openprocurement.auction.worker.auction import Auction
from gevent import spawn, killall, GreenletExit
import json
import logging
from openprocurement.auction.helpers.chronograph import \
    MAX_AUCTION_START_TIME_RESERV
import datetime
import gc
from greenlet import greenlet
from openprocurement.auction.databridge import AuctionsDataBridge
from copy import deepcopy
from openprocurement.auction import core as core_module
import openprocurement.auction.databridge as databridge_module
from openprocurement.auction.tests.unit.utils import get_tenders_dummy


LOGGER = logging.getLogger('Log For Tests')
CONF_FILES_FOLDER = os.path.join(PWD, "unit", "data")

test_log_config = {
     'version': 1,
     'disable_existing_loggers': False,
     'formatters': {'simpleFormatter': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}},
     'handlers': {'journal': {'class': 'ExtendedJournalHandler.ExtendedJournalHandler', 'formatter': 'simpleFormatter', 'SYSLOG_IDENTIFIER': 'AUCTIONS_LOG_FOR_TESTS', 'level': 'DEBUG'}},
     'loggers': {'Log For Tests': {'handlers': ['journal'], 'propagate': False, 'level': 'DEBUG'},
                 '': {'handlers': ['journal'], 'propagate': False, 'level': 'DEBUG'}}
     }

logging.config.dictConfig(test_log_config)

# def pytest_generate_tests(metafunc):
#     for funcargs in getattr(metafunc.function, 'funcarglist', ()):
#         metafunc.addcall(funcargs=funcargs)


auction_data_simple = AUCTION_DATA['simple']
auction_data_multilot = AUCTION_DATA['multilot']

worker_defaults_file_path = \
    os.path.join(CONF_FILES_FOLDER, "auction_worker_defaults.yaml")
with open(worker_defaults_file_path) as stream:
    worker_defaults = yaml.load(stream)

chronograph_conf_file_path = \
    os.path.join(CONF_FILES_FOLDER, 'auctions_chronograph.yaml')
with open(chronograph_conf_file_path) as stream:
    test_chronograph_config = yaml.load(stream)
    test_chronograph_config['disable_existing_loggers'] = False
    test_chronograph_config['handlers']['journal']['formatter'] = 'simple'
    test_chronograph_config['main']['auction_worker'] = \
        os.path.join(PWD, (".." + os.path.sep)*5, "bin", "auction_worker")
    test_chronograph_config['main']['auction_worker_config'] = \
        os.path.join(PWD, 'unit', 'data', 'auction_worker_defaults.yaml')
    test_chronograph_config['main'] \
    ['auction_worker_config_for_api_version_dev'] = \
        os.path.join(PWD, 'unit', 'data', 'auction_worker_defaults.yaml')

databridge_conf_file_path = \
    os.path.join(CONF_FILES_FOLDER, 'auctions_data_bridge.yaml')
with open(databridge_conf_file_path) as stream:
    test_bridge_config = yaml.load(stream)
    test_bridge_config['disable_existing_loggers'] = False
    test_bridge_config['handlers']['journal']['formatter'] = 'simple'

test_bridge_config_error_port = deepcopy(test_bridge_config)
test_bridge_config_error_port['main']['couch_url'] = 'http://admin:zaq1xsw2@0.0.0.0:9001/'


# todo: pass URL server
@pytest.fixture(scope='function')
def db(request):
    server = couchdb.Server("http://" + worker_defaults['COUCH_DATABASE'].split('/')[2])
    name = worker_defaults['COUCH_DATABASE'].split('/')[3]

    document = getattr(request, 'param', None)

    def delete():
        del server[name]

    if name in server:
        delete()

    data_base = server.create(name)

    if document:<commit_msg>Improve bridge configuration with error port<commit_after>import couchdb
import os
import pytest
import yaml
from openprocurement.auction.chronograph import AuctionsChronograph
from openprocurement.auction.tests.utils import update_auctionPeriod, \
    AUCTION_DATA, PWD
from openprocurement.auction.tests.unit.utils import TestClient
from openprocurement.auction.tests.unit.utils import kill_child_processes
from openprocurement.auction.worker.auction import Auction
from gevent import spawn, killall, GreenletExit
import json
import logging
from openprocurement.auction.helpers.chronograph import \
    MAX_AUCTION_START_TIME_RESERV
import datetime
import gc
from greenlet import greenlet
from openprocurement.auction.databridge import AuctionsDataBridge
from copy import deepcopy
from openprocurement.auction import core as core_module
import openprocurement.auction.databridge as databridge_module
from openprocurement.auction.tests.unit.utils import get_tenders_dummy


LOGGER = logging.getLogger('Log For Tests')
CONF_FILES_FOLDER = os.path.join(PWD, "unit", "data")

test_log_config = {
     'version': 1,
     'disable_existing_loggers': False,
     'formatters': {'simpleFormatter': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}},
     'handlers': {'journal': {'class': 'ExtendedJournalHandler.ExtendedJournalHandler', 'formatter': 'simpleFormatter', 'SYSLOG_IDENTIFIER': 'AUCTIONS_LOG_FOR_TESTS', 'level': 'DEBUG'}},
     'loggers': {'Log For Tests': {'handlers': ['journal'], 'propagate': False, 'level': 'DEBUG'},
                 '': {'handlers': ['journal'], 'propagate': False, 'level': 'DEBUG'}}
     }

logging.config.dictConfig(test_log_config)

# def pytest_generate_tests(metafunc):
#     for funcargs in getattr(metafunc.function, 'funcarglist', ()):
#         metafunc.addcall(funcargs=funcargs)


auction_data_simple = AUCTION_DATA['simple']
auction_data_multilot = AUCTION_DATA['multilot']

worker_defaults_file_path = \
    os.path.join(CONF_FILES_FOLDER, "auction_worker_defaults.yaml")
with open(worker_defaults_file_path) as stream:
    worker_defaults = yaml.load(stream)

chronograph_conf_file_path = \
    os.path.join(CONF_FILES_FOLDER, 'auctions_chronograph.yaml')
with open(chronograph_conf_file_path) as stream:
    test_chronograph_config = yaml.load(stream)
    test_chronograph_config['disable_existing_loggers'] = False
    test_chronograph_config['handlers']['journal']['formatter'] = 'simple'
    test_chronograph_config['main']['auction_worker'] = \
        os.path.join(PWD, (".." + os.path.sep)*5, "bin", "auction_worker")
    test_chronograph_config['main']['auction_worker_config'] = \
        os.path.join(PWD, 'unit', 'data', 'auction_worker_defaults.yaml')
    test_chronograph_config['main'] \
    ['auction_worker_config_for_api_version_dev'] = \
        os.path.join(PWD, 'unit', 'data', 'auction_worker_defaults.yaml')

databridge_conf_file_path = \
    os.path.join(CONF_FILES_FOLDER, 'auctions_data_bridge.yaml')
with open(databridge_conf_file_path) as stream:
    test_bridge_config = yaml.load(stream)
    test_bridge_config['disable_existing_loggers'] = False
    test_bridge_config['handlers']['journal']['formatter'] = 'simple'

test_bridge_config_error_port = deepcopy(test_bridge_config)
couch_url = test_bridge_config_error_port['main']['couch_url']
error_port = str(int(couch_url.split(':')[-1][:-1]) + 1)
couch_url_parts = couch_url.split(':')[0:-1]
couch_url_parts.append(error_port)
test_bridge_config_error_port['main']['couch_url'] = ':'.join(couch_url_parts)


@pytest.fixture(scope='function')
def db(request):
    server = couchdb.Server("http://" + worker_defaults['COUCH_DATABASE'].split('/')[2])
    name = worker_defaults['COUCH_DATABASE'].split('/')[3]

    document = getattr(request, 'param', None)

    def delete():
        del server[name]

    if name in server:
        delete()

    data_base = server.create(name)

    if document:
{'fraction_non_alphanumeric': 0.12862908475458926, 'fraction_numerical': 0.0029944017706027863, 'mean_word_length': 5.786219081272085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2132153', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2504, 'n_words': 442}