def test_load_xrf_standard_yaml_file2(tmp_path): r"""Test for reporting schema violation""" yaml_path = ["yaml", "param", "file"] file_name = "standard.yaml" yaml_path = os.path.join(tmp_path, *yaml_path, file_name) standard_data = copy.deepcopy(_standard_data_sample) # Change name from string to number (this will not satisfy the built-in schema) for v in standard_data: v["name"] = 50.36 # Save the changed dataset save_xrf_standard_yaml_file(yaml_path, standard_data) with pytest.raises(jsonschema.ValidationError): load_xrf_standard_yaml_file(yaml_path) # Disable schema validation by setting 'schema=None' load_xrf_standard_yaml_file(yaml_path, schema=None) # Change the schema to match data and validate with changed schema schema = copy.deepcopy(_xrf_standard_schema) schema["properties"]["name"] = {"type": ["string", "number"]} standard_data_out = load_xrf_standard_yaml_file(yaml_path, schema=schema) assert standard_data_out == standard_data, "Loaded data is different from the original"
def test_save_xrf_standard_yaml_file3(tmp_path): r"""Test the case of existing file""" yaml_path = ["yaml", "param", "file"] file_name = "standard.yaml" yaml_path = os.path.join(tmp_path, *yaml_path, file_name) standard_data = copy.deepcopy(_standard_data_sample) standard_data[0]['density'] = 50.0 # Wrong value of total density # Save incorrect data save_xrf_standard_yaml_file(yaml_path, standard_data) # and now try to read it with pytest.raises(RuntimeError, match="Sum of areal densities does not match total density"): load_xrf_standard_yaml_file(yaml_path)
def test_load_xrf_standard_yaml_file1(tmp_path): r"""Try loading non-existent YAML file""" file_name = "standard.yaml" # Try loading from the existing directory yaml_path = os.path.join(tmp_path, file_name) with pytest.raises(IOError, match=f"File '{yaml_path}' does not exist"): load_xrf_standard_yaml_file(yaml_path) # Try loading from the non-existing directory yaml_path = ["yaml", "param", "file"] yaml_path = os.path.join(tmp_path, *yaml_path, file_name) with pytest.raises(IOError, match=f"File '{yaml_path}' does not exist"): load_xrf_standard_yaml_file(yaml_path)
def test_save_xrf_standard_yaml_file1(tmp_path, standard_data): r"""Basic test of function 'save_xrf_standard_yaml_file' and 'load_xrf_standard_yaml_file'""" yaml_path = ["yaml", "param", "file"] file_name = "standard.yaml" yaml_path = os.path.join(tmp_path, *yaml_path, file_name) # Sample data save_xrf_standard_yaml_file(yaml_path, standard_data) data_loaded = load_xrf_standard_yaml_file(yaml_path) assert data_loaded == standard_data, "Loaded data is not equal to the original data"