Files
OpenBB/tests/openbb_terminal/test_helper_funcs.py
Om Gupta b78f024a6f Be able to change user data folder and add path for exports (#2592)
* modify export function to user data function

* change to exports

* change i18n

* be able to change user data folder

* black

* add exports to paths_helper

* black

* minor fixes

* fix

* small change to exports

* fix

* exports changes

* fix export

* fix reports export

* black

* minor changes

* fix test

* fix dcf export

* type ignore

* black

* pylint

* fix tests

Co-authored-by: Chavithra <chavithra@gmail.com>
2022-09-25 12:45:22 +01:00

100 lines
2.6 KiB
Python

# IMPORTATION STANDARD
from pathlib import Path
# IMPORTATION THIRDPARTY
import pandas as pd
import pytest
# IMPORTATION INTERNAL
from openbb_terminal.helper_funcs import export_data
# pylint: disable=E1101
# pylint: disable=W0603
# pylint: disable=E1111
# pylint: disable=W0621
# pylint: disable=W0613
@pytest.fixture
def mock_compose_export_path(monkeypatch, tmp_path):
# files in tmp_dir will remain (in separate folders) for 3 sequential runs of pytest
def mock_return(func_name, *args, **kwargs):
return tmp_path / f"20220829_235959_{func_name}"
monkeypatch.setattr("openbb_terminal.helper_funcs.compose_export_path", mock_return)
@pytest.mark.parametrize(
"export_type, dir_path, func_name, df",
[
(
"csv",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"json",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"xlsx",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"png",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"jpg",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"pdf",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"svg",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
],
)
def test_export_data_filetypes(
mock_compose_export_path, export_type, dir_path, func_name, df, tmp_path
):
export_data(export_type, dir_path, func_name, df)
assert Path(tmp_path / f"20220829_235959_{func_name}.{export_type}").exists()
# TODO add assertions to check the validity of the files?
@pytest.mark.parametrize(
"export_type, dir_path, func_name, data",
[
(
# Dict instead of DataFrame
"csv",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
dict({"test": "dict"}),
),
],
)
def test_export_data_invalid_data(
mock_compose_export_path, export_type, dir_path, func_name, data
):
with pytest.raises(AttributeError):
assert export_data(export_type, dir_path, func_name, data)