from cbutil.util import read_file_line_wise, get_git_infos, time_conversion from contextlib import contextmanager from pathlib import Path import os import git import pytest @contextmanager def tmp_file(path: Path): """Contextmanager that creates a temporary file and removes it afterwards.""" try: with open(path, "w") as tmp_test_file: yield tmp_test_file finally: os.remove(path) def test_read_file(): test_lines = ["test1", "test2"] test_path = Path("/tmp/tmp_test_file") with tmp_file(test_path) as tmp_test_file: for line in test_lines: tmp_test_file.write(f"{line}\n") for expected, actual in zip(test_lines, read_file_line_wise(test_path)): assert expected == actual def test_git_infos(): infos = get_git_infos(".", commit_key="commit", commit_msg_key="commit_message") assert infos["commit"] == git.Repo(".").head.commit.hexsha def test_time_conv(): time_str = "2022-08-03 15:34:46" assert time_conversion(time_str) == int(1659533686.0) assert time_conversion(12341234) == 12341234 with pytest.raises(ValueError): time_conversion("ajsdkfljaskldj") with pytest.raises(ValueError): time_conversion("2022-08-0315:34:46")