# Path: datarobot_batch_scoring/utils.py
# def get_config_file():
#     """
#     Lookup for config file at user home directory or working directory.
#     Returns
#     -------
#     str or None
#         Path to config file or None if it not exists.
#     """
#     home_path = path_join(expanduser('~'), CONFIG_FILENAME)
#     cwd_path = path_join(getcwd(), CONFIG_FILENAME)
#     if isfile(cwd_path):
#         return cwd_path
#     elif isfile(home_path):
#         return home_path
#     return None
# 
# CONFIG_FILENAME = 'batch_scoring.ini'
# 
# def parse_config_file(file_path):
#     config = ConfigParser()
#     config.read(file_path)
#     if 'batch_scoring' not in config.sections():
#         #  We are return empty dict, because there is nothing in this file
#         #  that related to arguments to batch scoring.
#         return {}
#     parsed_dict = dict(config.items('batch_scoring'))
#     return config_validator(parsed_dict)
# 
# Path: datarobot_batch_scoring/main.py
# def main(argv=sys.argv[1:]):
#     return _main(argv, deployment_aware=False)
# 
# def main_deployment_aware(argv=sys.argv[1:]):
#     return _main(argv, deployment_aware=True)


import os
import textwrap
import mock
import pytest
from tempfile import NamedTemporaryFile
from datarobot_batch_scoring.utils import (get_config_file,
                                           CONFIG_FILENAME,
                                           parse_config_file)
from datarobot_batch_scoring.main import (
    main as batch_scoring_main, main_deployment_aware)

def test_run_main_with_conf_file(monkeypatch):
    main_args = ['--host',
                 'http://localhost:53646/api',
                 '56dd9570018e213242dfa93c',
                 '56dd9570018e213242dfa93d',
                 'tests/fixtures/temperatura_predict.csv',
                 '--n_samples',
                 '10',
                 '--n_concurrent', '1', '--no', '--fast']
    raw_data = textwrap.dedent("""\
        [batch_scoring]
        host=file_host
        project_id=56dd9570018e213242dfa93c
        model_id=56dd9570018e213242dfa93d
        user=file_username
        password=file_password
        max_prediction_explanations=3
        pred_threshold=threshold
        pred_decision=label""")
    with NamedTemporaryFile(suffix='.ini', delete=False) as test_file:
        test_file.write(str(raw_data).encode('utf-8'))

    try:
        monkeypatch.setattr(
            'datarobot_batch_scoring.main.get_config_file',
            lambda: test_file.name)
        with mock.patch(
                'datarobot_batch_scoring.main'
                '.run_batch_predictions') as mock_method:

# Path: datarobot_batch_scoring/utils.py
# def get_config_file():
#     """
#     Lookup for config file at user home directory or working directory.
#     Returns
#     -------
#     str or None
#         Path to config file or None if it not exists.
#     """
#     home_path = path_join(expanduser('~'), CONFIG_FILENAME)
#     cwd_path = path_join(getcwd(), CONFIG_FILENAME)
#     if isfile(cwd_path):
#         return cwd_path
#     elif isfile(home_path):
#         return home_path
#     return None
# 
# CONFIG_FILENAME = 'batch_scoring.ini'
# 
# def parse_config_file(file_path):
#     config = ConfigParser()
#     config.read(file_path)
#     if 'batch_scoring' not in config.sections():
#         #  We are return empty dict, because there is nothing in this file
#         #  that related to arguments to batch scoring.
#         return {}
#     parsed_dict = dict(config.items('batch_scoring'))
#     return config_validator(parsed_dict)
# 
# Path: datarobot_batch_scoring/main.py
# def main(argv=sys.argv[1:]):
#     return _main(argv, deployment_aware=False)
# 
# def main_deployment_aware(argv=sys.argv[1:]):
#     return _main(argv, deployment_aware=True)


# Path: tests/test_conf_file.py
import os
import textwrap
import mock
import pytest
from tempfile import NamedTemporaryFile
from datarobot_batch_scoring.utils import (get_config_file,
                                           CONFIG_FILENAME,
                                           parse_config_file)
from datarobot_batch_scoring.main import (
    main as batch_scoring_main, main_deployment_aware)


def test_run_main_with_conf_file(monkeypatch):
    main_args = ['--host',
                 'http://localhost:53646/api',
                 '56dd9570018e213242dfa93c',
                 '56dd9570018e213242dfa93d',
                 'tests/fixtures/temperatura_predict.csv',
                 '--n_samples',
                 '10',
                 '--n_concurrent', '1', '--no', '--fast']
    raw_data = textwrap.dedent("""\
        [batch_scoring]
        host=file_host
        project_id=56dd9570018e213242dfa93c
        model_id=56dd9570018e213242dfa93d
        user=file_username
        password=file_password
        max_prediction_explanations=3
        pred_threshold=threshold
        pred_decision=label""")
    with NamedTemporaryFile(suffix='.ini', delete=False) as test_file:
        test_file.write(str(raw_data).encode('utf-8'))

    try:
        monkeypatch.setattr(
            'datarobot_batch_scoring.main.get_config_file',
            lambda: test_file.name)
        with mock.patch(
                'datarobot_batch_scoring.main'
                '.run_batch_predictions') as mock_method:

            batch_scoring_main(argv=main_args)