def test_get_updates_from_env(correct_settings: CommonSettings) -> None:
    "is the cache in the settings module updated"
    assert settings._settings is None
    env_names = config.settings_env_names()
    class_name = env_names.class_name
    value_name = env_names.value_name
    os.environ[class_name] = type(correct_settings).__name__
    os.environ[value_name] = correct_settings.json()

    returned_settings = config.get()
    assert os.getenv(class_name, None) == type(correct_settings).__name__
    assert os.getenv(value_name, None) == correct_settings.json()
    assert returned_settings == correct_settings
    assert settings._settings == correct_settings
Beispiel #2
0
def test_load_bad_config(database: Database) -> None:
    "is an error thrown if a bad config is read from the database"
    example_bad_json: str = random_json()
    # Prove the example_bad_json is not a valid configuration structure
    with pytest.raises(pydantic.ValidationError) as err:
        CommonSettings.parse_raw(example_bad_json)

    with db_session:
        database.ConfigEntity(version="current",
                              json=example_bad_json,
                              class_name=CommonSettings.__name__)
        database.commit()

    with pytest.raises(BadConfigInDBError) as err:
        get_config(db=database)
    assert "bad configuration in database" in str(err.value)
def test_set_and_get_env(
    correct_settings: CommonSettings, monkeypatch: pytest.MonkeyPatch
) -> None:
    "can settings make the round trip"
    config.set_env(correct_settings)
    assert os.getenv("URL_SHORTENER__SETTINGS", None) == correct_settings.json()
    returned_settings = config.get_env()
    assert returned_settings == correct_settings
def correct_database_settings(
        database: Database,
        correct_settings: CommonSettings) -> DatabaseSettings:
    "adds onto correct_settings to return a {DatabaseSettings.__name__}"
    with db_session:
        database_file_name = database.provider.pool.filename
    database_file = Path(database_file_name).resolve(strict=True)
    return DatabaseSettings(**correct_settings.dict(),
                            database_file=database_file)
def remove_settings_env_vars(monkeypatch: pytest.MonkeyPatch) -> None:
    """
    can't guarantee that the settings won't pull environment variables from the
    test environment, since pydantic provides a lot of ways of setting the
    names of environment variables it should in for values, but any current
    environment variables that have the same prefix as what the settings class
    will be looking for can be made unavailable to the function under test:
    """
    env_prefix: str = CommonSettings.Config().env_prefix
    for env_var_name in os.environ:
        if env_var_name.startswith(env_prefix):
            monkeypatch.delenv(env_var_name)
def test_set_env_bad_settings_class(correct_settings: CommonSettings) -> None:
    """
    does set_env raise an error when the name of the settings class matches,
    but isn't one from the settings module
    """

    class DummySettings(CommonSettings):
        pass

    dummy = DummySettings(**correct_settings.dict())
    dummy.__class__.__name__ = CommonSettings.__name__
    assert type(dummy).__name__ == CommonSettings.__name__

    with pytest.raises(TypeError) as err:
        config.set_env(env_names=env_names, new_settings=dummy)
    assert (
        f"don't know where to find '{dummy.__class__.__name__}'; not in .settings module"
        in str(err.value)
    )
def correct_settings(tmp_path: Path, database: Database) -> CommonSettings:
    "makes a complete CommonSettings object, that has realistic values"
    # NOTE:TEST::RANDOMIZATION Best to avoid hardcoded test values where possible
    env_file = (tmp_path / "test.env").resolve()
    return CommonSettings(env_file=env_file, )