Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra unit test on keyword behaviour #25

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def setup_folders(tmp_path, testcase: str = "US_ARc"):
# Create the output directory (starts empty)
output_folder.mkdir()

# Pattern to match directories starting with 'US_ARc'
# Pattern to match directories starting with the `testcase` name
pattern = os.path.join('tests/test_artifacts', f'{testcase}*')

# Use glob to find directories that match the pattern
Expand All @@ -90,7 +90,7 @@ def setup_folders(tmp_path, testcase: str = "US_ARc"):
else:
raise FileNotFoundError(f"No matching directory found for pattern: {pattern}")


data_path= os.path.join(testcase_path, '05_ustar_cp')

# Copy all files and directories from the testcase input dir to the temporary input folder
Expand Down
54 changes: 53 additions & 1 deletion tests/unit_tests/test_ustar_cp/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,59 @@ def test_launch_empty_folder(setup_test_environment, matlab_engine):

# Run the MATLAB function
exitcode = eng.launch(input_folder, output_folder)

# Check that the exitcode indicates an error
assert exitcode == 0, "Expected zero exitcode for empty input folder."

def test_missing_keywords(setup_test_environment, matlab_engine):
"""
Test the MATLAB `launch` function's behavior when a keyword is missing from the data
in the initial lines.

Args:
setup_test_environment (fixture): A fixture that sets up input and output folders
with dummy data for the test.
matlab_engine (fixture): A fixture that initializes and manages the MATLAB engine session.

Asserts:
The test asserts that the MATLAB function returns an exit code of 1 with the appropriate
error message
"""
input_folder, output_folder = setup_test_environment
eng = matlab_engine

# List of sample data fields and values
sample_data_fields = [("site","US-Arc"),
("year","2006"),
("lat","35.5465"),
("lon","-98.0401"),
("timezone","200601010030,-6"),
("htower","200601010030,4.05"),
("timeres","halfhourly"),
("sc_negl","1"),
("notes","Sample note")]

# String with 10 newlines
endbuffer = "bad,bad" * 10

# Build up successive partial sample files from the above data and
# try to launch
partial_sample_data = ""
for line in sample_data_fields:
# Write the current partial data to the file
with open(Path(input_folder) / "US-ARc_qca_ustar_2023.csv", "w") as f:
f.write(partial_sample_data + endbuffer)

# Run the MATLAB function
output = io.StringIO("")
eng.launch(input_folder, output_folder, stdout=output)

# Read standard out and get last line
output.seek(0)
output_string = output.readlines()[-1]

assert (output_string == ("processing n.01, US-ARc_qca_ustar_2023.csv..." + line[0] + " keyword not found.\n")), \
"Expected error message for missing keyword"

# Add the current line to the partial data for the next test
partial_sample_data += ",".join(line) + "\n"