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_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."
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)
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)
def test_nested(unicode_val, dict_val, foo_instance, different_foo_instance, roundtrip_func): instance = Nested( unicode_=unicode_val, dict_=dict_val, foo1=foo_instance, foo2=different_foo_instance, ) check_attributes(instance, { "unicode_": unicode_val, "dict_": dict_val, }) assert_serializables_equal(instance.foo1, foo_instance) assert_serializables_equal(instance.foo2, different_foo_instance) roundtripped = roundtrip_func(instance) assert_serializables_equal(instance, roundtripped)
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}), )
def test_inheritance(roundtrip_func, foo_instance): class Parent(Serializable): a = Integer() def _a_default(self): return 3 b = Unicode() check_attributes(Parent(b="b"), {"a": 3, "b": "b"}) class Child(Parent): x = Instance(Foo) y = Dict() def _a_default(self): return 4 child = Child(b="b", x=foo_instance, y={}) check_attributes(child, {'a': 4, 'b': 'b', 'y': {}}) assert child.x is foo_instance assert_serializables_equal(roundtrip_func(child), child)
def test_dynamic_defaults(non_dynamic_val, d_val, l_val, roundtrip_func): expected = { 'non_dynamic': non_dynamic_val, 'd': d_val if d_val is not None else DynamicDefaults.DEFAULT_D, 'l': l_val if l_val is not None else DynamicDefaults.DEFAULT_L, } kwargs = {'non_dynamic': non_dynamic_val} if d_val is not None: kwargs['d'] = d_val if l_val is not None: kwargs['l'] = l_val instance = DynamicDefaults(**kwargs) check_attributes(instance, expected) check_attributes(roundtrip_func(instance), expected) # Do a check without forcing all the attributes via check_attributes. check_attributes(roundtrip_func(DynamicDefaults(**kwargs)), expected)
def test_construct_from_kwargs(foo_kwargs): instance = Foo(**foo_kwargs) check_attributes(instance, foo_kwargs)