Skip to content

Commit

Permalink
refactor(tests): update test files
Browse files Browse the repository at this point in the history
  • Loading branch information
entelecheia committed Jul 2, 2023
1 parent fede1f4 commit 1b62cc7
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 244 deletions.
3 changes: 2 additions & 1 deletion tests/hyfi/dotenv/test_dotenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def test_dotenv_config():
os.environ["HYFI_PROJECT_NAME"] = "hyfi"
os.environ["HYFI_GLOBAL_ROOT"] = ENVs.expand_posix_vars("$WORKSPACE_ROOT")
config = DotEnvConfig()
pprint(config.dict())
print(type(config))
pprint(config.model_dump())
# Test that the default values are set correctly
assert config._config_name_ == "__init__"
assert config.HYFI_PROJECT_NAME == "hyfi"
Expand Down
8 changes: 4 additions & 4 deletions tests/hyfi/main/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_about():
print(HyFI.about())
assert HyFI.config.__version__ == __version__
assert HyFI.config._version_ == __version__


def test_version():
Expand All @@ -16,7 +16,7 @@ def test_version():
def test_envs():
os.environ["HYFI_PROJECT_NAME"] = "hyfi"
envs = HyFI.dotenv()
pprint(envs.dict())
pprint(envs.model_dump())
assert envs.HYFI_PROJECT_NAME == "hyfi"


Expand All @@ -29,15 +29,15 @@ def test_init_project():
log_level="WARNING",
verbose=True,
)
pprint(prj.dict())
pprint(prj.model_dump())
assert prj.path.global_workspace_name == "testspace"


def test_compose():
os.environ["HYFI_PROJECT_ROOT"] = "."
cfg = HyFI.compose("path=__task__")
print(cfg["task_root"])
assert cfg["task_root"] == "workspace/tasks/task1"
assert cfg["task_root"] == "workspace/tasks"


if __name__ == "__main__":
Expand Down
11 changes: 8 additions & 3 deletions tests/hyfi/path/test_batch_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@


def test_path_config():
config = BatchPathConfig(task_root="workspace/test_task")
pprint(config.dict())
config = BatchPathConfig(
task_root="workspace/tasks",
task_name="test-task",
)
pprint(config.model_dump())
# Test that the default values are set correctly
assert config._config_name_ == "__batch__"
config.batch_name = "test-batch"
print(config.batch_dir)
assert (
config.batch_dir == Path("workspace/test_task/outputs/batch-outputs").absolute()
config.batch_dir
== Path("workspace/tasks/test-task/outputs/test-batch")
)

# Test that the log_dir is created
Expand Down
9 changes: 5 additions & 4 deletions tests/hyfi/path/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@

def test_path_config():
config = PathConfig(
_config_name_="__init__",
project_root="workspace/tmp",
global_hyfi_root=ENVs.expand_posix_vars("$HOME/.hyfi"),
project_workspace_name="testspace",
)
pprint(config.dict())
pprint(config.model_dump())
# Test that the default values are set correctly
assert config._config_name_ == "__init__"
assert config.home == ENVs.expand_posix_vars("$HOME")
assert Path(config.project_root).absolute() == (Path.cwd() / "workspace/tmp").absolute()
assert (
Path(config.project_workspace_root).absolute()
== Path.cwd().absolute() / "workspace/tmp/testspace"
Path(config.project_root).absolute()
== (Path.cwd() / "workspace/tmp").absolute()
)
assert config.workspace_dir == Path("workspace/tmp/testspace")

# Test that the log_dir and cache_dir properties return the correct values
assert Path(config.log_dir).is_dir()
Expand Down
6 changes: 3 additions & 3 deletions tests/hyfi/pipeline/test_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def test_pipe():
print(df3)
config = HyFI.compose("pipe=__dataframe_instance_methods__")
pipe_config = DataframePipeConfig(**config)
pipe_config._run_ = "filter"
pipe_config._with_ = {"items": ["id"]}
pipe_config.run = "filter"
pipe_config.run_with = {"items": ["id"]}
print(pipe_config)
df4 = HyFI.run_pipe(df, pipe_config)
print(df4)
config = HyFI.compose("pipe=__dataframe_external_funcs__")
pipe_config = DataframePipeConfig(**config)
pipe_config._run_ = "lambda x: x.replace('Economic', 'ECON')"
pipe_config.run = "lambda x: x.replace('Economic', 'ECON')"
pipe_config.columns_to_apply = "text"
pipe_config.num_workers = 5
pipe_config.verbose = True
Expand Down
20 changes: 14 additions & 6 deletions tests/hyfi/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
import pandas as pd

from hyfi.composer import Composer
from hyfi.main import HyFI
from hyfi.pipeline import PipelineConfig
from hyfi.pipeline.configs import PipeConfig, RunningConfig


def test_running_config():
assert Composer.generate_alias_for_special_keys("_run_") == "run"
assert Composer.generate_alias_for_special_keys("_pipe_") == "pipe_target"
assert Composer.generate_alias_for_special_keys("_with_") == "run_with"
assert Composer.generate_alias_for_special_keys("with") == "run_with"

config = RunningConfig(**{"with": {"a": 1, "b": 2}})
print(config.dict())
print(config.model_dump())
print(config.kwargs)
assert config.kwargs == {"a": 1, "b": 2}


def test_pipe():
data_path = "https://assets.entelecheia.ai/datasets/bok_minutes/meta-bok_minutes-train.parquet"
config = HyFI.compose("pipe=load_dataframes")
config._with_ = {"data_files": data_path}
config.run_with = {"data_files": data_path}
config.verbose = True
HyFI.print(config)
pipe = PipeConfig(**config)
HyFI.print(pipe.dict())
HyFI.print(pipe.model_dump())
print(pipe.kwargs)
assert pipe._run_ == "hyfi.main.HyFI.load_dataframes"
assert pipe.run == "hyfi.main.HyFI.load_dataframes"


def test_pipeline():
data_path = (
"https://assets.entelecheia.ai/datasets/esg_coverage/ESG_ratings_raw.csv"
)
config = HyFI.compose("pipeline=__test_dataframe__")
config.pipe1._with_ = {"data_files": data_path}
config.pipe1.run_with = {"data_files": data_path}
# HyFI.print(config)
config = PipelineConfig(**config)
HyFI.print(config.dict(exclude_none=True))
HyFI.print(config.model_dump(exclude_none=True))
data = HyFI.run_pipeline(config)
assert type(data) == pd.DataFrame
print(data[data.code == "A005930"])
Expand Down
4 changes: 2 additions & 2 deletions tests/hyfi/pipeline/test_task_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def test_task_config():
)

task = TaskConfig(_config_name_="__test__")
HyFI.print(task.dict())
HyFI.print(task.model_dump())
HyFI.run_task(task, project=project)
HyFI.print(task.dict())
HyFI.print(task.model_dump())


if __name__ == "__main__":
Expand Down
15 changes: 9 additions & 6 deletions tests/hyfi/project/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@


def test_project_config():
os.environ["HYFI_PROJECT_NAME"] = "hyfi"
config = ProjectConfig(project_name="test", project_root="./workspace/tmp", num_workers=2)
os.environ["HYFI_PROJECT_NAME"] = "hyfi-test"
config = ProjectConfig(
project_root="./workspace/tmp", num_workers=2
)
# config.init_project()
pprint(config.dict())
assert config.project_name == "test"
assert Path(config.project_root).absolute() == (Path.cwd() / "workspace/tmp").absolute()
pprint(config.model_dump())
assert config.project_name == "hyfi-test"
print(config.workspace_dir)
assert config.workspace_dir == Path("workspace/tmp/workspace")
assert config.num_workers == 2
# pprint(config.path.dict())
# pprint(config.path.model_dump())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/hyfi/task/test_batch_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_batch_task_config():
task_name="demo2",
batch_name="batch11",
)
pprint(config.batch.dict())
pprint(config.batch.model_dump())
# Test that the default values are set correctly

assert config.batch.batch_name == "batch11"
Expand Down
12 changes: 11 additions & 1 deletion tests/hyfi/task/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ def test_task_config():
config = TaskConfig(
task_name="demo2",
)
pprint(config.dict())
pprint(config.model_dump())
# Test that the default values are set correctly

assert config.task_name == "demo2"
# Test that the log_dir and cache_dir properties return the correct values
assert Path(config.log_dir).is_dir()
assert Path(config.cache_dir).is_dir()

config.task_name = "demo3"
pprint(config.model_dump())
# Test that the default values are set correctly
print(config.task_dir)
assert config.task_dir == Path("workspace/tasks/demo3")
config.task_root = "workspace/tmp"
pprint(config.model_dump())
print(config.root_dir)
assert config.task_dir == Path("workspace/tmp/demo3")


if __name__ == "__main__":
test_task_config()
Loading

0 comments on commit 1b62cc7

Please sign in to comment.