def test_convert_description_to_alfacase_with_empty_dict( datadir: Path) -> None: """ Ensure that the conversion from a description into a Alfacase it's not generating empty dict. Since it's not a valid syntax for strictyaml resulting in an InconsistentIndentationDisallowed error """ simple_case = case_description.CaseDescription( name="Simple Case", pipes=[ case_description.PipeDescription( name="pipe", source="mass_source_inlet", target="pressure_outlet", segments=build_simple_segment(), profile=case_description.ProfileDescription( x_and_y=case_description.XAndYDescription( x=Array([0], "m"), y=Array([0], "m"))), ) ], ) simple_case_alfacase_content = convert_description_to_alfacase(simple_case) assert "wall_description: {}" not in simple_case_alfacase_content assert "tables: {}" not in simple_case_alfacase_content # Smoke check, ensures that the alfacase is loaded correctly without errors simple_case_alfacase_file = datadir / "simple_case.alfacase" simple_case_alfacase_file.write_text(data=simple_case_alfacase_content, encoding="UTF-8") loaded_alfacase = DescriptionDocument.from_file(simple_case_alfacase_file) assert loaded_alfacase.content["name"].data == simple_case.name
def test_get_scalar_loader(): alfacase_content = YAML( value={"foo": { "value": YAML(value=1), "unit": YAML(value="m") }}) description_document = DescriptionDocument(content=alfacase_content, file_path=Path()) # Loading Scalar passing ``category`` scalar_loader = get_scalar_loader(category="length") assert scalar_loader(key="foo", alfacase_content=description_document) == Scalar( 1.0, "m", "length") # Load Scalar passing ``from_unit`` scalar_loader = get_scalar_loader(from_unit="m") assert scalar_loader(key="foo", alfacase_content=description_document) == Scalar( 1.0, "m", "length") # Passing None expected_msg = "Either 'category' or 'from_unit' parameter must be defined" with pytest.raises(ValueError, match=expected_msg): get_scalar_loader() # Informing both parameter expected_msg = "Both parameters 'category' and 'from_unit' were provided, only one must be informed" with pytest.raises(ValueError, match=expected_msg): get_scalar_loader(category="length", from_unit="m")
def generate_description(self, alfacase_config: AlfacaseTestConfig): """ Helper method to generate a "Description" from the given alfacase_config """ alfacase_string = convert_description_to_alfacase( alfacase_config.description_expected ) alfacase_content = strictyaml.dirty_load( yaml_string=alfacase_string, schema=alfacase_config.schema, allow_flow_style=True, ) # 'LoadPvtModelsDescription' is special case and the DescriptionDocument doesn't need a FakeKey skip_dict = ( alfacase_config.load_function_name == "load_pvt_models_description" ) if alfacase_config.is_sequence: alfacase_content = [alfacase_content] elif alfacase_config.is_dict and not skip_dict: alfacase_content = YAML( CommentedMap({YAML("FakeKey"): alfacase_content}) ) description_document = DescriptionDocument( content=alfacase_content, file_path=self.tmp_path / "test_case.alfacase" ) return getattr(alfacase_to_case, alfacase_config.load_function_name)( description_document )
def convert_alfacase_to_description( file_alfacase: Path, ) -> case_description.CaseDescription: """ Return a alfasim_sdk.alfacase.case_description.Case with all information provided on file_yaml. """ from alfasim_sdk._internal.alfacase.alfacase_to_case import load_case_description from alfasim_sdk._internal.alfacase.alfacase_to_case import DescriptionDocument return load_case_description(DescriptionDocument.from_file(file_alfacase))
def test_invalid_yaml_contents_parsing(tmp_path): """ Errors while parsing YAML should be detected and raised as our custom exception. """ import re alfacase_content = "Invalid YAML contents" alfacase_file = tmp_path / "invalid-yaml.alfacase" alfacase_file.write_text(data=alfacase_content, encoding="UTF-8") expected_msg = ("when expecting a mapping\n" ' in "<unicode string>", line 1, column 1:\n' " Invalid YAML contents\n" " ^ (line: 1)\n" "found arbitrary text\n" ' in "<unicode string>", line 2, column 1:\n' " ...\n" " ^ (line: 2)") with pytest.raises(DescriptionError, match=re.escape(expected_msg)): DescriptionDocument.from_file(alfacase_file)
def test_update_multi_input_flags_behavior(): content = strictyaml.dirty_load( yaml_string=textwrap.dedent("""\ # Just constant, use "constant" flag. volumetric_flow_rates_std: gas: value: 0.0 unit: sm3/d # Constant and curve but no flag, use default flag. mass_flow_rates: gas: value: 0.0 unit: kg/s mass_flow_rates_curve: gas: image: values: [0.0, 1.0] unit: kg/s domain: values: [0, 10] unit: s # Just flag, use value from yaml; total_mass_flow_rate_input_type: curve # Just curve, use "curve" flag. water_cut_curve: image: values: [0.2, 0.3] unit: "-" domain: values: [0, 20] unit: s """), schema=mass_source_node_properties_description_schema, allow_flow_style=True, ) document = DescriptionDocument(content, Path()) mass_source_node_properties = load_mass_source_node_properties_description( document) assert (mass_source_node_properties.volumetric_flow_rates_std_input_type == MultiInputType.Constant) assert (mass_source_node_properties.mass_flow_rates_input_type == MultiInputType.Constant) assert (mass_source_node_properties.total_mass_flow_rate_input_type == MultiInputType.Curve) assert mass_source_node_properties.water_cut_input_type == MultiInputType.Curve
def test_convert_alfacase_to_description_restart_file_path(tmp_path): """ Round-trip test with a description that has a Path as type. - YAML representation should be a Str() - CaseDescription should be a pathlib.Path """ alfacase_file = tmp_path / "test_case.yaml" some_folder = tmp_path / "some_folder" some_folder.mkdir() restart_file = some_folder / "restart.state" restart_file.touch() physics_with_restart_file = attr.evolve( filled_case_descriptions.PHYSICS_DESCRIPTION, restart_filepath=restart_file ) alfacase_string = convert_description_to_alfacase(physics_with_restart_file) restart_file_relative_path = restart_file.relative_to(alfacase_file.parent) assert f"restart_filepath: {restart_file.absolute()}" in alfacase_string alfacase_string = alfacase_string.replace( f"restart_filepath: {restart_file.absolute()}", f"restart_filepath: {restart_file_relative_path}", ) alfacase_content = strictyaml.dirty_load( yaml_string=alfacase_string, schema=schema.physics_description_schema, allow_flow_style=True, ) assert isinstance(alfacase_content["restart_filepath"].value, str) assert alfacase_content["restart_filepath"].value == str( Path("some_folder/restart.state") ) description_document = DescriptionDocument( content=alfacase_content, file_path=alfacase_file ) physics_description = load_physics_description(description_document) assert physics_description.restart_filepath == restart_file
def generate_description( self, alfacase_config: AlfacaseTestConfig, remove_redundant_input_type_data: bool = False, ): """ Helper method to generate a "Description" from the given alfacase_config """ alfacase_string = convert_description_to_alfacase( alfacase_config.description_expected, remove_redundant_input_type_data= remove_redundant_input_type_data, ) alfacase_content = strictyaml.dirty_load( yaml_string=alfacase_string, schema=alfacase_config.schema, allow_flow_style=True, ) # 'LoadPvtModelsDescription' is special case and the DescriptionDocument doesn't need a FakeKey skip_dict = (alfacase_config.load_function_name == "load_pvt_models_description") if alfacase_config.is_sequence: alfacase_content = [alfacase_content] elif alfacase_config.is_dict and not skip_dict: alfacase_content = YAML( CommentedMap({YAML("FakeKey"): alfacase_content})) description_document = DescriptionDocument( content=alfacase_content, file_path=self.tmp_path / "test_case.alfacase") if hasattr(alfacase_to_case, alfacase_config.load_function_name): loader = getattr(alfacase_to_case, alfacase_config.load_function_name) else: loader = alfacase_to_case.get_instance_loader( class_=alfacase_config.description_expected.__class__) return loader(description_document)
def test_get_array_loader(): alfacase_content = YAML( value={"foo": {"values": YAML(value=[1, 2]), "unit": YAML(value="m")}} ) description_document = DescriptionDocument( content=alfacase_content, file_path=Path() ) # Loading Scalar passing ``category`` array_loader = get_array_loader(category="length") assert array_loader(key="foo", alfacase_content=description_document) == Array( "length", [1.0, 2.0], "m" ) # Load Scalar passing ``from_unit`` array_loader = get_array_loader(from_unit="m") assert array_loader(key="foo", alfacase_content=description_document) == Array( "length", [1.0, 2.0], "m" ) expected_msg = "Either 'category' or 'from_unit' parameter must be defined" with pytest.raises(ValueError, match=expected_msg): get_array_loader()
@pytest.fixture() def description_document_for_pvt_tables_test(tmp_path): case = case_description.PvtModelsDescription(tables={ "acme": "acme.tab", "acme_2": "acme.tab" }) alfacase_string = convert_description_to_alfacase(case) alfacase_file_path = tmp_path / "test_case.alfacase" shutil.copy2(get_acme_tab_file_path(), tmp_path) alfacase_content = strictyaml.dirty_load( alfacase_string, schema=schema.pvt_models_description_schema, allow_flow_style=True, ) return DescriptionDocument(content=alfacase_content, file_path=alfacase_file_path) def test_load_pvt_tables_with_relative_file( description_document_for_pvt_tables_test, tmp_path): """ PvtModelsDescription.tables should accept a path relative to a tab file """ document = description_document_for_pvt_tables_test pvt_model_description = load_pvt_models_description(document=document) assert pvt_model_description.tables == { "acme": document.file_path.parent / "acme.tab", "acme_2": document.file_path.parent / "acme.tab", }