Skip to content

Commit

Permalink
Merge pull request #142 from RQC-HU/fix-test-generator
Browse files Browse the repository at this point in the history
Fixed a problem that could not create CASPT2 and IVO tests and run unit test
  • Loading branch information
kohei-noda-qcrg authored Feb 22, 2024
2 parents 29c1b7f + 594df12 commit ebe71d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions test/template/unit_test/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
43 changes: 23 additions & 20 deletions tools/dcaspt2_test_generator
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ class DIRACFiles:
output_file_path: Path
data_directory: Path

def __init__(self):
pass
def __init__(self, data_directory: Path):
self.data_directory = data_directory

def ask_dirac_files(self, default_answer_path: Path) -> Path:
# DIRAC input, mol and output files
program_name = "DIRAC"
self.output_file_path = ask_output_file_path(program_name=program_name, default_path=self.input_file_path.parent)
self.input_file_path = ask_input_file_path(program_name=program_name, default_path=default_answer_path)
self.mol_file_path = ask_mol_file_path(program_name=program_name, default_path=self.input_file_path.parent)
self.output_file_path = ask_output_file_path(program_name=program_name, default_path=self.input_file_path.parent)

def create_dirac_dir_and_copy_files(self, dest_copy_path: Path):
def create_dirac_dir_and_copy_files(self):
# mkdir the dirac_data directory
dest_copy_path.mkdir(parents=True, exist_ok=True)
self.data_directory.mkdir(parents=True, exist_ok=True)
# Copy the DIRAC input, mol and output files to the dirac_data directory
logging.info("Copying DIRAC input, mol and output files to the dirac_data directory...")
shutil.copy(self.input_file_path, self.data_directory)
Expand Down Expand Up @@ -103,7 +103,7 @@ def check_test_type() -> TestType:
# Ask for the type of the test(slow, normal, fast)
test_speed_types: dict[str, TestType] = {
"slow, 20↑ sec": TestType(marker="@pytest.mark.slowonly", dir="slow"),
"normal, 10-20 sec": TestType(marker="", dir="unmarker"),
"normal, 10-20 sec": TestType(marker="", dir="unmarked"),
"fast(dev), 1-10 sec": TestType(marker="@pytest.mark.dev", dir="dev"),
}
ref_test_speed_types = list(test_speed_types.keys())
Expand Down Expand Up @@ -199,17 +199,17 @@ def main():
input_file_path = ask_input_file_path(program_name="DIRAC-CASPT2", default_path=ref_parent_dir)

# Ask for the path of the integrals file
integral_files = IntegralFiles(default_answer_path=ref_parent_dir)
integral_files.ask_dirac_integrals_directory()
dirac_files = DIRACFiles()
integral_files = IntegralFiles()
integral_files.ask_dirac_integrals_directory(default_dir=ref_parent_dir)
dirac_files = DIRACFiles(data_directory=Path.joinpath(test_dir, "dirac_data"))
dirac_files.ask_dirac_files(default_answer_path=ref_parent_dir)

# mkdir the test directory
test_dir.mkdir(parents=True, exist_ok=True)

# Copy the DIRAC integral files and DIRAC input, mol and output files
integral_files.copy_files(dest_copy_path=test_dir)
dirac_files.create_dirac_dir_and_copy_files(dest_copy_path=Path(test_dir / "dirac_data"))
dirac_files.create_dirac_dir_and_copy_files()

# Copy the input and output file to the test directory
logging.info("Copying input, and output files to the test directory...")
Expand Down Expand Up @@ -245,17 +245,17 @@ def main():
dfpcmo_file_path = ask_general_file_path(program_name="DIRAC-IVO", file_type="DFPCMO", default_path=ref_parent_dir)
ref_dfpcmonew_file_path = ask_general_file_path(program_name="DIRAC-IVO", file_type="DFPCMONEW", default_path=ref_parent_dir)
# Ask for the path of the integrals file
integral_files = IntegralFiles(default_answer_path=ref_parent_dir)
integral_files.ask_dirac_integrals_directory()
dirac_files = DIRACFiles()
integral_files = IntegralFiles()
integral_files.ask_dirac_integrals_directory(default_dir=ref_parent_dir)
dirac_files = DIRACFiles(data_directory=Path.joinpath(test_dir, "dirac_data"))
dirac_files.ask_dirac_files(default_answer_path=ref_parent_dir)

# mkdir the test directory
test_dir.mkdir(parents=True, exist_ok=True)

# Copy the DIRAC integral files and DIRAC input, mol and output files
integral_files.copy_files(dest_copy_path=test_dir)
dirac_files.create_dirac_dir_and_copy_files(dest_copy_path=Path(test_dir / "dirac_data"))
dirac_files.create_dirac_dir_and_copy_files()

# Copy the input and output file to the test directory
logging.info("Copying input, and output files to the test directory...")
Expand Down Expand Up @@ -285,17 +285,18 @@ def main():

elif test_type == "Unit test":
# Reference output file
ref_output_path = ask_general_file_path(program_name="unit test", file_type="reference output", default_path=user_dir)
test_dir.mkdir(parents=True, exist_ok=True)
shutil.copy(ref_output_path, Path.joinpath(test_dir, "expected"))
template_expected_file_path = Path.joinpath(test_template_dir, "unit_test", "expected")
expected_file_path = Path.joinpath(test_dir, "expected")
shutil.copy(template_expected_file_path, expected_file_path)

# Python file
template_python_file_path = Path.joinpath(test_template_dir, "unit_test", "test_template_unit_test.py")
python_file_path = Path.joinpath(test_dir, f"test_{test_directory_name}.py")
replace_dict = {
"def replace_test_template": f"def test_{test_directory_name}",
"# @replace_marker": selected_test_type.marker,
"replace_test_exe": f"{test_directory_name}_test_exe",
"replace_test_exe": f"test_{test_directory_name}_exe",
}
replace_template_file(template_python_file_path, python_file_path, replace_dict)

Expand All @@ -316,12 +317,14 @@ def main():
logging.info("================== NOTE ==================")
logging.info("Then follow these steps to replace the automatically created test with the process you want to test")
logging.info(f"1. Edit {fortran_file_path} to add the process you wish to test and write the results to result.out.")
logging.info(f"2. Edit {expected_file_path} to rewrite the expected result according to the process your {fortran_file_path} file is testing.")
logging.info(
f"2. Edit {python_file_path} to rewrite the process after run_test according to the type of data you want to test.\
f"3. Edit {python_file_path} to rewrite the process after run_test according to the type of data you want to test.\
\n\tIn the automatically created file, the process takes a single line of string as a string and checks if it matches.\
\n\tIn the {test_dir}/module_testing.py file, there are functions that can be used to convert the output file to desired data type."
\n\tIn the {test_template_dir.parent}/module_testing.py file, there are functions that can be used to convert the output file to desired data type."
)
logging.info("That's it! Now you can run the test!")
logging.info(f"4. Rebuild the source code with {test_template_dir.parent.parent}/setup script.")
logging.info("That's it! Now you can run the test with pytest!!")
logging.info("==========================================")
logging.info(f"test directory: {test_dir}")
logging.info("Before you commit a new test, please run the test to make sure it passes.")
Expand Down

0 comments on commit ebe71d1

Please sign in to comment.