diff --git a/tests/example_structure/configurations/configuration_no_transform.yml b/tests/example_structure/configurations/configuration_no_transform.yml new file mode 100644 index 0000000..bcd60b3 --- /dev/null +++ b/tests/example_structure/configurations/configuration_no_transform.yml @@ -0,0 +1,9 @@ +overrides: +envelope_structure: Envelope structure +envelopes: + My envelope name 1: { filler: Model 1 } + Envelope_2: { filler: Model 2 } +source: Main source +tallies: [fine_mesh] +materials: [Standard materials] +transforms: [My transform] \ No newline at end of file diff --git a/tests/example_structure/project_summary.csv b/tests/example_structure/project_summary.csv index 246b8b8..4f8c680 100644 --- a/tests/example_structure/project_summary.csv +++ b/tests/example_structure/project_summary.csv @@ -1,16 +1,16 @@ Name,Relative path,Description -fine_mesh,data_cards\fine_mesh.tally, -My transform,data_cards\my_transform.transform, -Ring source,data_cards\ring_source.source, -Main source,data_cards\volumetric_source.source, -Wrong data card,data_cards\wrong_data_card.mat, -Materials BAD,data_cards\materials_bad_library.mat, -Standard materials,data_cards\materials.mat, -Wrong suffix,data_cards\wrong_suffix.wrong, -Model 1,models\filler_model_1.mcnp, -Model 2,models\filler_model_2.mcnp, -Envelope structure,models\main_input.mcnp, -Model 3,models\path_to_be_excluded\filler_model_3.mcnp, -Bad model 1,models\path_to_be_excluded\only_cells_block.mcnp, -Bad model 2,models\path_to_be_excluded\wrong_surfaces_block.mcnp, -Standard configuration,configurations\configuration_1.yml, +fine_mesh,data_cards/fine_mesh.tally, +My transform,data_cards/my_transform.transform, +Ring source,data_cards/ring_source.source, +Main source,data_cards/volumetric_source.source, +Wrong data card,data_cards/wrong_data_card.mat, +Materials BAD,data_cards/materials_bad_library.mat, +Standard materials,data_cards/materials.mat, +Wrong suffix,data_cards/wrong_suffix.wrong, +Model 1,models/filler_model_1.mcnp, +Model 2,models/filler_model_2.mcnp, +Envelope structure,models/main_input.mcnp, +Model 3,models/path_to_be_excluded/filler_model_3.mcnp, +Bad model 1,models/path_to_be_excluded/only_cells_block.mcnp, +Bad model 2,models/path_to_be_excluded/wrong_surfaces_block.mcnp, +Standard configuration,configurations/configuration_1.yml, diff --git a/tests/test_generate_model.py b/tests/test_generate_model.py index 8b39134..0e70218 100644 --- a/tests/test_generate_model.py +++ b/tests/test_generate_model.py @@ -1,8 +1,9 @@ from pathlib import Path +from unittest.mock import patch, mock_open import pytest -from gitronics.generate_model import generate_model +from gitronics.generate_model import generate_model, _get_envelope_structure_first_cell_id ROOT_FOLDER_PATH = Path(__file__).resolve().parents[1] / "tests" / "example_structure" PROJECT_SUMMARY_PATH = ( @@ -79,3 +80,83 @@ def test_envelope_name_not_found_in_envelope_structure(tmpdir): project_summary_path=PROJECT_SUMMARY_PATH, write_path=Path(tmpdir), ) + + +def test_envelope_with_transform_branch(tmpdir): + """Test case to ensure the transform branch is properly covered. + This tests the if envelope_data.transform branch on line 84-85.""" + configuration_path = ( + Path(__file__).resolve().parents[1] + / "tests" + / "example_structure" + / "configurations/configuration_1.yml" + ) + generate_model( + root_folder_path=ROOT_FOLDER_PATH, + configuration_file_path=configuration_path, + project_summary_path=PROJECT_SUMMARY_PATH, + write_path=Path(tmpdir), + ) + + # Verify the transform was included in the output + with open(tmpdir / "assembled.mcnp") as infile: + result_text = infile.read() + + # Check that the transform "(1)" was included in the FILL card for Envelope_2 + # From the configuration: Envelope_2: { filler: Model 2, transform: (1) } + # Model 2 has universe ID u=125 + assert " FILL = 125 (1) " in result_text + assert "$ Envelope_2" in result_text + + +def test_get_envelope_structure_first_cell_id_no_match(): + """Test the exception case in _get_envelope_structure_first_cell_id + when no line starts with a digit.""" + from gitronics.model_manager import ModelManager + + # Create a minimal configuration and project that will work + configuration_path = ( + Path(__file__).resolve().parents[1] + / "tests" + / "example_structure" + / "configurations/configuration_1.yml" + ) + + model_manager = ModelManager( + root_folder_path=ROOT_FOLDER_PATH, + configuration_file_path=configuration_path, + project_summary_path=PROJECT_SUMMARY_PATH, + ) + + # Mock the file content to have no lines starting with digits + mock_file_content = "Title\nC Comment line\nC Another comment\n Some indented text\n" + + with patch("builtins.open", mock_open(read_data=mock_file_content)): + with pytest.raises(ValueError, match="Could not find the first cell ID"): + _get_envelope_structure_first_cell_id(model_manager) + + +def test_envelope_without_transform_branch(tmpdir): + """Test case to ensure the no-transform branch is properly covered. + This tests the else branch when envelope_data.transform is None/empty.""" + configuration_path = ( + Path(__file__).resolve().parents[1] + / "tests" + / "example_structure" + / "configurations/configuration_no_transform.yml" + ) + generate_model( + root_folder_path=ROOT_FOLDER_PATH, + configuration_file_path=configuration_path, + project_summary_path=PROJECT_SUMMARY_PATH, + write_path=Path(tmpdir), + ) + + # Verify the transform was NOT included in the output for Envelope_2 + with open(tmpdir / "assembled.mcnp") as infile: + result_text = infile.read() + + # Check that there's no transform in the FILL card for Envelope_2 + # Should be "FILL = 125 " without any transform + assert " FILL = 125 \n $ Envelope_2" in result_text + assert "$ Envelope_2" in result_text