def test_proxy_defaults(): proxy = SettingsProxy('test', {'hello': 'world', 'foo': None}) assert proxy.get('hello') == 'world' assert proxy.get('foo') is None assert proxy.get('foo', 'bar') == 'bar' assert not proxy.get_all(True) proxy.set('foo', 'bar') assert proxy.get_all(True) == {'foo': 'bar'} assert proxy.get_all() == {'hello': 'world', 'foo': 'bar'}
def test_proxy_preload(count_queries): defaults = {'hello': 'world', 'foo': None, 'bar': None} proxy = SettingsProxy('test', defaults) proxy.set('bar', 'test') with count_queries() as cnt: # this one preloads assert proxy.get('hello') == 'world' assert cnt() == 1 with count_queries() as cnt: # this one has no value in the db assert proxy.get('foo') is None assert proxy.get('foo', 'bar') == 'bar' assert cnt() == 0 with count_queries() as cnt: assert proxy.get('bar') is 'test' assert cnt() == 0
def test_proxy_strict(): proxy = SettingsProxy('test', {'hello': 'world'}) pytest.raises(ValueError, proxy.get, 'foo') pytest.raises(ValueError, proxy.get, 'foo', 'bar') pytest.raises(ValueError, proxy.set, 'foo', 'foobar') pytest.raises(ValueError, proxy.set_multi, { 'hello': 'world', 'foo': 'foobar' }) pytest.raises(ValueError, proxy.delete, 'hello', 'foo') assert proxy.get('hello') == 'world'
def test_proxy_strict_off(): proxy = SettingsProxy('test', {}, False) assert proxy.get('foo') is None proxy.get('foo', 'bar') == 'bar' proxy.set('foo', 'foobar') assert proxy.get('foo') == 'foobar'
def test_proxy_cache_mutable(): proxy = SettingsProxy('test', {'foo': []}) foo = proxy.get('foo') assert foo is not proxy.defaults['foo'] foo.append('test') assert not proxy.get('foo')