def test_unsafe(): dummy = Dummy() with pytest.raises(yaml.representer.RepresenterError): yaml.dump_all([dummy]) with pytest.raises(yaml.representer.RepresenterError): yaml.dump(dummy, Dumper=yDumper) # reverse monkey patch and try again monkey_patch_pyyaml_reverse() with tempfile.TemporaryFile(suffix='.yaml', mode='w+t') as f: yaml.dump_all([dummy], stream=f) f.seek(0) # rewind doc_unsafe = yaml.load(f) assert type(doc_unsafe) is Dummy monkey_patch_pyyaml() with pytest.raises(yaml.constructor.ConstructorError): f.seek(0) # rewind safe_yaml_load(f) with pytest.raises(yaml.constructor.ConstructorError): f.seek(0) # rewind yaml.load(f)
def test_load(): conf = os.path.join(FIXTURE_PATH, "valid_conf.yaml") with open(conf) as f: stream = f.read() yaml_config_safe = safe_yaml_load(stream) yaml_config_native = yaml.load(stream) assert yaml_config_safe is not None assert yaml_config_native is not None assert yaml_config_native == yaml_config_safe yaml_config_safe = [entry for entry in safe_yaml_load_all(stream)] yaml_config_native = [entry for entry in yaml.load_all(stream)] assert yaml_config_safe is not [] assert yaml_config_native is not [] assert len(yaml_config_safe) == len(yaml_config_native) for safe, native in zip(yaml_config_safe, yaml_config_native): assert safe == native