import json
import re
import sys
from unittest import mock

import pandas as pd
import pytest

from cohortreport import __main__


class TestParseArgs:
    def test_with_config(self):
        config = {"output_path": "output", "variable_types": {}}
        input_files = ["output/input.csv"]
        test_args = ["--config", json.dumps(config)] + input_files
        args = __main__.parse_args(test_args)
        assert args.config == config
        assert args.input_files == input_files

    def test_without_config(self):
        input_files = ["output/input.csv"]
        args = __main__.parse_args(input_files)
        assert args.config is None
        assert args.input_files == input_files


@pytest.fixture
def path_to_input_csv(tmp_path):
    """Gets a `pathlib.Path` to an input CSV.

    The input CSV is similar to that generated by cohort-extractor from a study
    definition. It exists within a subdirectory of the default base temporary directory,
    which you can override by passing `--basetemp=my_basetemp` to `pytest`.
    """
    patient_records = pd.DataFrame(
        {
            "sex": ["M", "F"] * 50,
            "bmi": range(100),
            "has_copd": [0, 1] * 50,
        }
    )
    patient_records["patient_id"] = range(len(patient_records))

    path_to_input_csv = tmp_path / "input.csv"
    patient_records.to_csv(path_to_input_csv, index=False)

    return path_to_input_csv


def test_main(path_to_input_csv):
    path_to_output_dir = path_to_input_csv.parent
    config = {
        "output_path": str(path_to_output_dir),
        "variable_types": {
            "sex": "categorical",
            "bmi": "float",
            "has_copd": "binary",
        },
    }
    test_argv = ["", "--config", json.dumps(config), str(path_to_input_csv)]

    with mock.patch.object(sys, "argv", test_argv):
        __main__.main()

    path_to_output_html = (
        path_to_output_dir / f"descriptives_{path_to_input_csv.stem}.html"
    )
    assert path_to_output_html.exists()
    output_html = path_to_output_html.read_text()
    src_attrs = re.findall(r'src="([\w\.]+)"', output_html)
    assert src_attrs == ["sex.png", "bmi.png", "has_copd.png"]

import json
import re
import sys
from unittest import mock

import pandas as pd
import pytest

from cohortreport import __main__


class TestParseArgs:
    def test_with_config(self):
        config = {"output_path": "output", "variable_types": {}}
        input_files = ["output/input.csv"]
        test_args = ["--config", json.dumps(config)] + input_files
        args = __main__.parse_args(test_args)
        assert args.config == config
        assert args.input_files == input_files

    def test_without_config(self):
        input_files = ["output/input.csv"]
        args = __main__.parse_args(input_files)
        assert args.config is None
        assert args.input_files == input_files


@pytest.fixture
def path_to_input_csv(tmp_path):
    """Gets a `pathlib.Path` to an input CSV.

    The input CSV is similar to that generated by cohort-extractor from a study
    definition. It exists within a subdirectory of the default base temporary directory,
    which you can override by passing `--basetemp=my_basetemp` to `pytest`.
    """
    patient_records = pd.DataFrame(
        {
            "sex": ["M", "F"] * 50,
            "bmi": range(100),
            "has_copd": [0, 1] * 50,
        }
    )
    patient_records["patient_id"] = range(len(patient_records))

    path_to_input_csv = tmp_path / "input.csv"
    patient_records.to_csv(path_to_input_csv, index=False)

    return path_to_input_csv


def test_main(path_to_input_csv):
    path_to_output_dir = path_to_input_csv.parent
    config = {
        "output_path": str(path_to_output_dir),
        "variable_types": {
            "sex": "categorical",
            "bmi": "float",
            "has_copd": "binary",
        },
    }
    test_argv = ["", "--config", json.dumps(config), str(path_to_input_csv)]

    with mock.patch.object(sys, "argv", test_argv):
        __main__.main()

    path_to_output_html = (
        path_to_output_dir / f"descriptives_{path_to_input_csv.stem}.html"
    )
    assert path_to_output_html.exists()
    output_html = path_to_output_html.read_text()
    src_attrs = re.findall(r'src="([\w\.]+)"', output_html)
    assert src_attrs == ["sex.png", "bmi.png", "has_copd.png"]

Gets a `pathlib.Path` to an input CSV.      The input CSV is similar to that generated by cohort-extractor from a study     definition. It exists within a subdirectory of the default base temporary directory,     which you can override by passing `--basetemp=my_basetemp` to `pytest`.