Пример #1
0
def test_mongo_config(mongo_required_kwargs,
                      mongo_optional_kwargs,
                      roundtrip_func):

    with pytest.raises(TraitError):
        MongoConfig(**mongo_optional_kwargs)

    optional_kwarg_defaults = {
        'replicaset': None,
        'slave_ok': True,
        'prefer_secondary': True,
        'ssl': False,
    }

    without_optionals = MongoConfig(**mongo_required_kwargs)
    check_attributes(
        without_optionals,
        merge(mongo_required_kwargs, optional_kwarg_defaults),
    )
    assert_serializables_equal(
        without_optionals,
        roundtrip_func(without_optionals)
    )

    full_kwargs = merge(mongo_required_kwargs, mongo_optional_kwargs)
    with_optionals = MongoConfig(**full_kwargs)
    check_attributes(with_optionals, full_kwargs)
    assert_serializables_equal(with_optionals, roundtrip_func(with_optionals))
def test_merge():
    with pytest.raises(ValueError):
        merge()

    d1 = {'a': 'b'}
    assert merge(d1) == d1

    d2 = {'c': 'd'}
    assert merge(d1, d2) == {'a': 'b', 'c': 'd'}

    d3 = {'a': 'z', 'e': 'f'}
    assert merge(d1, d2, d3) == {'a': 'z', 'c': 'd', 'e': 'f'}
Пример #3
0
def test_pg_port_requires_hostname(pg_required_kwargs):

    # Hostname without port is ok.
    cfg = PostgresConfig(hostname='localhost', **pg_required_kwargs)
    check_attributes(cfg, merge(pg_required_kwargs, {'hostname': 'localhost'}))
    assert cfg.url == "postgresql://user@localhost/db"

    # Port without hostname is an error.
    with pytest.raises(TraitError) as e:
        PostgresConfig(port=5432, **pg_required_kwargs)
    assert str(e.value) == "Received port 5432 but no hostname."
Пример #4
0
def test_postgres_config_required(pg_required_kwargs, roundtrip_func):
    cfg = PostgresConfig(**pg_required_kwargs)
    check_attributes(
        cfg,
        merge(pg_required_kwargs, {'port': None, 'password': None}),
    )
    assert_urls_equal(cfg.url, "postgresql://user@/db")
    rounded = roundtrip_func(cfg)
    assert_serializables_equal(cfg, rounded, skip=['url'])
    assert_urls_equal(rounded.url, cfg.url)

    from_url = PostgresConfig.from_url(cfg.url)
    assert_serializables_equal(cfg, from_url, skip=['url'])
    assert_urls_equal(from_url.url, cfg.url)
Пример #5
0
def test_postgres_config_optional(pg_required_kwargs, pg_optional_kwargs,
                                  roundtrip_func):
    kwargs = merge(pg_required_kwargs, pg_optional_kwargs)
    cfg = PostgresConfig(**kwargs)
    check_attributes(cfg, kwargs)

    assert_urls_equal(
        cfg.url, "postgresql://*****:*****@localhost:5432/db?"
        "connect_timeout=10&sslmode=require")

    rounded = roundtrip_func(cfg)
    assert_serializables_equal(cfg, rounded)
    assert_urls_equal(rounded.url, cfg.url)

    from_url = PostgresConfig.from_url(cfg.url)
    assert_serializables_equal(cfg, from_url, skip=['url'])
    assert_urls_equal(from_url.url, cfg.url)
Пример #6
0
def test_mongo_config_username_password_both_or_neither(mongo_required_kwargs):

    kwargs = mongo_required_kwargs.copy()

    with removed_keys(kwargs, ['username']), pytest.raises(TraitError) as e:
        MongoConfig(**kwargs)
    assert str(e.value) == "Password supplied without username."

    with removed_keys(kwargs, ['password']), pytest.raises(TraitError) as e:
        MongoConfig(**kwargs)
    assert str(e.value) == "Username 'user' supplied without password."

    with removed_keys(kwargs, ['username', 'password']):
        cfg = MongoConfig(**kwargs)

        check_attributes(
            cfg,
            merge(kwargs, {'username': None, 'password': None}),
        )