from typing import Any, Dict

import numpy as np
import pandas as pd

import core.artificial_signal_generators as sig_gen
import core.statistics as stats
import core.timeseries_study as tss
import helpers.unit_test as hut


class TestTimeSeriesDailyStudy(hut.TestCase):
    def test_usual_case(self) -> None:
        idx = pd.date_range("2018-12-31", "2019-01-31")
        vals = np.random.randn(len(idx))
        ts = pd.Series(vals, index=idx)
        tsds = tss.TimeSeriesDailyStudy(ts)
        tsds.execute()


class TestTimeSeriesMinutelyStudy(hut.TestCase):
    def test_usual_case(self) -> None:
        idx = pd.date_range("2018-12-31", "2019-01-31", freq="5T")
        vals = np.random.randn(len(idx))
        ts = pd.Series(vals, index=idx)
        tsms = tss.TimeSeriesMinutelyStudy(ts, freq_name="5 minutes")
        tsms.execute()


class TestMapDictToDataframeTest1(hut.TestCase):
    def test1(self) -> None:
        stat_funcs = {
            "norm_": stats.apply_normality_test,
            "adf_": stats.apply_adf_test,
            "kpss_": stats.apply_kpss_test,
        }
        result_dict = self._get_dict_of_series(1)
        actual = tss.map_dict_to_dataframe(
            dict_=result_dict, functions=stat_funcs
        )
        actual_string = hut.convert_df_to_string(actual, index=True)
        self.check_string(actual_string)

    def test2(self) -> None:
        stat_funcs = {
            "norm_": stats.apply_normality_test,
            "adf_": stats.apply_adf_test,
            "kpss_": stats.apply_kpss_test,
        }
        result_dict = self._get_dict_of_series(1)
        actual = tss.map_dict_to_dataframe(
            dict_=result_dict,
            functions=stat_funcs,
            add_prefix=False,
        )
        actual_string = hut.convert_df_to_string(actual, index=True)
        self.check_string(actual_string)

    def test3(self) -> None:
        stat_funcs = {
            "norm_": stats.apply_normality_test,
            "adf_": stats.apply_adf_test,
            "kpss_": stats.apply_kpss_test,
        }
        result_dict = self._get_dict_of_series(1)
        actual = tss.map_dict_to_dataframe(
            dict_=result_dict,
            functions=stat_funcs,
            progress_bar=False,
        )
        actual_string = hut.convert_df_to_string(actual, index=True)
        self.check_string(actual_string)

    @staticmethod
    def _get_series(seed: int) -> pd.Series:
        arparams = np.array([0.75, -0.25])
        maparams = np.array([0.65, 0.35])
        arma_process = sig_gen.ArmaProcess(arparams, maparams)
        date_range = {"start": "1/1/2010", "periods": 40, "freq": "M"}
        series = arma_process.generate_sample(
            date_range_kwargs=date_range, seed=seed
        )
        return series

    def _get_dict_of_series(self, seed: int) -> Dict[Any, pd.Series]:
        n_items = 15
        test_keys = ["test_key_" + str(x) for x in range(n_items)]
        result_dict = {key: self._get_series(seed) for key in test_keys}
        return result_dict

[((323, 364), 'pandas.date_range', 'pd.date_range', (['"""2018-12-31"""', '"""2019-01-31"""'], {}), "('2018-12-31', '2019-01-31')\n", (336, 364), True, 'import pandas as pd\n'), ((419, 445), 'pandas.Series', 'pd.Series', (['vals'], {'index': 'idx'}), '(vals, index=idx)\n', (428, 445), True, 'import pandas as pd\n'), ((461, 489), 'core.timeseries_study.TimeSeriesDailyStudy', 'tss.TimeSeriesDailyStudy', (['ts'], {}), '(ts)\n', (485, 489), True, 'import core.timeseries_study as tss\n'), ((617, 669), 'pandas.date_range', 'pd.date_range', (['"""2018-12-31"""', '"""2019-01-31"""'], {'freq': '"""5T"""'}), "('2018-12-31', '2019-01-31', freq='5T')\n", (630, 669), True, 'import pandas as pd\n'), ((724, 750), 'pandas.Series', 'pd.Series', (['vals'], {'index': 'idx'}), '(vals, index=idx)\n', (733, 750), True, 'import pandas as pd\n'), ((766, 820), 'core.timeseries_study.TimeSeriesMinutelyStudy', 'tss.TimeSeriesMinutelyStudy', (['ts'], {'freq_name': '"""5 minutes"""'}), "(ts, freq_name='5 minutes')\n", (793, 820), True, 'import core.timeseries_study as tss\n'), ((1159, 1225), 'core.timeseries_study.map_dict_to_dataframe', 'tss.map_dict_to_dataframe', ([], {'dict_': 'result_dict', 'functions': 'stat_funcs'}), '(dict_=result_dict, functions=stat_funcs)\n', (1184, 1225), True, 'import core.timeseries_study as tss\n'), ((1272, 1316), 'helpers.unit_test.convert_df_to_string', 'hut.convert_df_to_string', (['actual'], {'index': '(True)'}), '(actual, index=True)\n', (1296, 1316), True, 'import helpers.unit_test as hut\n'), ((1623, 1711), 'core.timeseries_study.map_dict_to_dataframe', 'tss.map_dict_to_dataframe', ([], {'dict_': 'result_dict', 'functions': 'stat_funcs', 'add_prefix': '(False)'}), '(dict_=result_dict, functions=stat_funcs,\n    add_prefix=False)\n', (1648, 1711), True, 'import core.timeseries_study as tss\n'), ((1779, 1823), 'helpers.unit_test.convert_df_to_string', 'hut.convert_df_to_string', (['actual'], {'index': '(True)'}), '(actual, index=True)\n', (1803, 1823), True, 'import helpers.unit_test as hut\n'), ((2130, 2220), 'core.timeseries_study.map_dict_to_dataframe', 'tss.map_dict_to_dataframe', ([], {'dict_': 'result_dict', 'functions': 'stat_funcs', 'progress_bar': '(False)'}), '(dict_=result_dict, functions=stat_funcs,\n    progress_bar=False)\n', (2155, 2220), True, 'import core.timeseries_study as tss\n'), ((2288, 2332), 'helpers.unit_test.convert_df_to_string', 'hut.convert_df_to_string', (['actual'], {'index': '(True)'}), '(actual, index=True)\n', (2312, 2332), True, 'import helpers.unit_test as hut\n'), ((2457, 2480), 'numpy.array', 'np.array', (['[0.75, -0.25]'], {}), '([0.75, -0.25])\n', (2465, 2480), True, 'import numpy as np\n'), ((2500, 2522), 'numpy.array', 'np.array', (['[0.65, 0.35]'], {}), '([0.65, 0.35])\n', (2508, 2522), True, 'import numpy as np\n'), ((2546, 2585), 'core.artificial_signal_generators.ArmaProcess', 'sig_gen.ArmaProcess', (['arparams', 'maparams'], {}), '(arparams, maparams)\n', (2565, 2585), True, 'import core.artificial_signal_generators as sig_gen\n')]