def test_matcher_with_defaults_values_undefined(): config = YamlLoader( path=config_directory.joinpath("defaults.yaml"), matcher=MatcherWithDefaults ).load() assert config["db"]["password"] is None assert config["mail"]["password"] == "My123~!@#$%^&*())_+Password!" assert config["logging"]["password"] == ":-test:-" assert config["sentry"]["password"] == ""
def test_pydantic_validator_fail(name): with pytest.raises(ValidationError, match=r"db -> password"): YamlLoader( path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher, validator=PydanticValidator, schema=PydanticConfig, ).load()
def test_strict_matcher_values_set(name): with mock.patch("piny.matchers.StrictMatcher.constructor") as expand_mock: expand_mock.return_value = config_map[name] config = YamlLoader( path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher, ).load() assert config[name]["password"] == config_map[name]
def test_matcher_with_defaults_values_set(): with mock.patch("piny.matchers.os.environ.get") as expand_mock: expand_mock.side_effect = lambda v, _: config_map[v.split("_")[0].lower()] config = YamlLoader( path=config_directory.joinpath("defaults.yaml"), matcher=MatcherWithDefaults ).load() assert config["db"]["password"] == config_map["db"] assert config["mail"]["password"] == config_map["mail"] assert config["sentry"]["password"] == config_map["sentry"] assert config["logging"]["password"] == config_map["logging"]
def test_strict_matcher_default_do_not_matched(): config = YamlLoader( path=config_directory.joinpath("defaults.yaml"), matcher=StrictMatcher ).load() assert config["db"]["password"] is None assert ( config["mail"]["password"] == "${MAIL_PASSWORD:-My123~!@#$%^&*())_+Password!}" ) assert config["logging"]["password"] == "${LOGGING_PASSWORD:-:-test:-}" assert config["sentry"]["password"] == "${SENTRY_PASSWORD:-}"
def test_marshmallow_validator_fail(name): with pytest.raises( ValidationError, match=r"\{'db': \{'password': \['Field may not be null.'\]\}\}"): YamlLoader( path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher, validator=MarshmallowValidator, schema=MarshmallowConfig, strict=True, ).load(many=False)
def test_trafaret_validator_fail(name): with pytest.raises( ValidationError, match= r"\{'db': DataError\(\{'password': DataError\(value is not a string\)\}\)\}", ): YamlLoader( path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher, validator=TrafaretValidator, schema=TrafaretConfig, ).load()
def test_marshmallow_validator_success(name): with mock.patch("piny.matchers.StrictMatcher.constructor") as expand_mock: expand_mock.return_value = config_map[name] config = YamlLoader( path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher, validator=MarshmallowValidator, schema=MarshmallowConfig, ).load() assert config[name]["host"] == "db.example.com" assert config[name]["login"] == "user" assert config[name]["password"] == config_map[name]
def test_base_matcher(): """ WATCH OUT! Black magic of Pytest in action! When placed at the beginning of test module this test make all other tests fail. Make sure this test is at the bottom! """ with pytest.raises(NotImplementedError): with mock.patch( "piny.matchers.Matcher.matcher", new_callable=mock.PropertyMock ) as matcher_mock: matcher_mock.return_value = re.compile("") YamlLoader( path=config_directory.joinpath("defaults.yaml"), matcher=Matcher ).load()
def create_app(path: str) -> Flask: """ Application factory """ # Get and validate config config = YamlLoader( path=path, matcher=StrictMatcher, validator=PydanticValidator, schema=Configuration, ).load() # Initialize app app = Flask(__name__) # Configure app configure_app(app, config) configure_logging(app) return app
from pydantic import BaseModel from piny import PydanticValidator, StrictMatcher, YamlLoader class DBModel(BaseModel): login: str password: str class ConfigModel(BaseModel): db: DBModel config = YamlLoader( path="database.yaml", matcher=StrictMatcher, validator=PydanticValidator, schema=ConfigModel, ).load()
from piny import YamlLoader config = YamlLoader(path="config.yaml").load() print(config) # {'db': {'login': '******', 'password': '******'}, # 'mail': {'login': '******', 'password': '******'}, # 'sentry': {'dsn': None}}
from piny import YamlLoader, StrictMatcher config = YamlLoader(path="config.yaml", matcher=StrictMatcher).load()
import marshmallow as ma from piny import MarshmallowValidator, StrictMatcher, YamlLoader class DBSchema(ma.Schema): login = ma.fields.String(required=True) password = ma.fields.String() class ConfigSchema(ma.Schema): db = ma.fields.Nested(DBSchema) config = YamlLoader( path="database.yaml", matcher=StrictMatcher, validator=MarshmallowValidator, schema=ConfigSchema, strict=True ).load(many=False)
import trafaret from piny import TrafaretValidator, StrictMatcher, YamlLoader DBSchema = trafaret.Dict(login=trafaret.String, password=trafaret.String) ConfigSchema = trafaret.Dict(db=DBSchema) config = YamlLoader( path="database.yaml", matcher=StrictMatcher, validator=TrafaretValidator, schema=ConfigSchema, ).load()
class GerritConfig(BaseModel): url: HttpUrl user: str password: str class JiraConfig(BaseModel): url: HttpUrl user: str password: str class SentryConfig(BaseModel): dsn: Optional[str] = None class Settings(BaseModel): gerrit: GerritConfig jira: JiraConfig sentry: SentryConfig ca_certs: str cfg = YamlLoader( os.environ.get("SKYLLA_CONFIG", "skylla.conf"), matcher=StrictMatcher, validator=PydanticValidator, schema=Settings, strict=True, ).load()
def test_strict_matcher_values_undefined(name): config = YamlLoader( path=config_directory.joinpath("{}.yaml".format(name)), matcher=StrictMatcher ).load() assert config[name]["password"] is None