diff --git a/tests/tests_sound_composer/test_sound_composer_source_harmonics.py b/tests/tests_sound_composer/test_sound_composer_source_harmonics.py index 98d3de12..e0dc96ea 100644 --- a/tests/tests_sound_composer/test_sound_composer_source_harmonics.py +++ b/tests/tests_sound_composer/test_sound_composer_source_harmonics.py @@ -25,6 +25,8 @@ from ansys.dpf.core import ( Field, FieldsContainer, + GenericDataContainer, + Operator, TimeFreqSupport, fields_container_factory, fields_factory, @@ -294,6 +296,77 @@ def test_source_harmonics_load_source_harmonics(): assert source_harmonics_obj.source_harmonics[0].data[3] == pytest.approx(EXP_ORDER_LEVEL03_XML) +def test_source_harmonics_set_from_generic_data_containers(): + """Test SourceHarmonics set_from_generic_data_containers method.""" + op = Operator("sound_composer_load_source_harmonics") + op.connect(0, pytest.data_path_sound_composer_harmonics_source_in_container) + op.run() + fc_data: FieldsContainer = op.get_output(0, "fields_container") + + gdc_source = GenericDataContainer() + gdc_source.set_property("sound_composer_source", fc_data) + + f_source_control = fields_factory.create_scalar_field( + num_entities=1, location=locations.time_freq + ) + f_source_control.append([1.0, 2.0, 3.0, 4.0, 5.0], 1) + gdc_source_control = GenericDataContainer() + gdc_source_control.set_property("sound_composer_source_control_one_parameter", f_source_control) + + source_harmo_obj = SourceHarmonics() + source_harmo_obj.set_from_generic_data_containers(gdc_source, gdc_source_control) + assert isinstance(source_harmo_obj.source_harmonics, FieldsContainer) + assert len(source_harmo_obj.source_harmonics) == len(fc_data) + assert isinstance(source_harmo_obj.source_control, SourceControlTime) + assert len(source_harmo_obj.source_control.control.data) == 5 + + +def test_source_harmonics_get_as_generic_data_containers(): + """Test SourceHarmonics get_as_generic_data_containers method.""" + # Source control undefined => warning. + source_harmo_obj = SourceHarmonics( + file=pytest.data_path_sound_composer_harmonics_source_in_container + ) + with pytest.warns( + PyAnsysSoundWarning, + match=( + "Cannot create source control generic data container, either because there is no " + "source control data, or because the source control data is invalid." + ), + ): + _, gdc_source_control = source_harmo_obj.get_as_generic_data_containers() + assert gdc_source_control is None + + # Source data undefined => warning. + source_harmo_obj.source_harmonics = None + f_source_control = fields_factory.create_scalar_field( + num_entities=1, location=locations.time_freq + ) + f_source_control.append([1.0, 2.0, 3.0, 4.0, 5.0], 1) + source_control_obj = SourceControlTime() + source_control_obj.control = f_source_control + source_harmo_obj.source_control = source_control_obj + with pytest.warns( + PyAnsysSoundWarning, + match="Cannot create source generic data container because there is no source data.", + ): + gdc_source, _ = source_harmo_obj.get_as_generic_data_containers() + assert gdc_source is None + + # Both source and source control are defined. + source_harmo_obj.load_source_harmonics( + pytest.data_path_sound_composer_harmonics_source_in_container, + ) + gdc_source, gdc_source_control = source_harmo_obj.get_as_generic_data_containers() + + assert isinstance(gdc_source, GenericDataContainer) + assert isinstance(gdc_source.get_property("sound_composer_source"), FieldsContainer) + assert isinstance(gdc_source_control, GenericDataContainer) + assert isinstance( + gdc_source_control.get_property("sound_composer_source_control_one_parameter"), Field + ) + + def test_source_harmonics_process(): """Test SourceHarmonics process method.""" # Create a field to use in a SourceControlTime object.