tiramisu/tests/test_storage.py

374 lines
15 KiB
Python
Raw Normal View History

# coding: utf-8
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2017-07-21 22:34:41 +02:00
from py.test import raises
2019-12-24 15:24:20 +01:00
import pytest
2013-08-27 09:46:52 +02:00
2017-07-21 22:34:41 +02:00
from tiramisu.error import ConfigError
2019-02-23 22:52:06 +01:00
from tiramisu import Config, BoolOption, OptionDescription, Leadership, \
list_sessions, delete_session, default_storage, MetaConfig
2017-07-04 19:59:42 +02:00
from tiramisu.setting import groups, owners
2013-08-27 09:46:52 +02:00
2018-10-31 08:00:19 +01:00
def teardown_function(function):
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_non_persistent():
2013-08-27 09:46:52 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-12-24 15:24:20 +01:00
await Config(o, session_id='test_non_persistent')
2013-08-27 09:46:52 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_list():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_non_persistent')
await cfg.option('b').value.set(True)
2018-10-31 08:00:19 +01:00
assert 'test_non_persistent' in list_sessions()
2019-12-24 15:24:20 +01:00
del(cfg)
2018-10-31 08:00:19 +01:00
assert 'test_non_persistent' not in list_sessions()
2018-09-11 20:11:13 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_delete_not_persistent():
2018-09-13 22:01:27 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if not default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='not_test_persistent')
2018-10-31 08:00:19 +01:00
assert 'not_test_persistent' in list_sessions()
2019-12-24 15:24:20 +01:00
del cfg
2018-10-31 08:00:19 +01:00
assert 'not_test_persistent' not in list_sessions()
2018-09-13 22:01:27 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='not_test_persistent')
2018-09-13 22:01:27 +02:00
raises(ValueError, "delete_session('not_test_persistent')")
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
await Config(o, session_id='test_persistent', persistent=True)
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_delete_not_persistent():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if not default_storage.is_persistent():
2018-09-11 20:11:13 +02:00
raises(ValueError, "delete_session('test_persistent')")
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_list_sessions_persistent():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.option('b').value.set(True)
2018-10-31 08:00:19 +01:00
assert 'test_persistent' in list_sessions()
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_delete_session_persistent():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
await Config(o, session_id='test_persistent', persistent=True)
2018-10-31 08:00:19 +01:00
assert 'test_persistent' in list_sessions()
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
2018-10-31 08:00:19 +01:00
assert 'test_persistent' not in list_sessions()
2018-09-11 20:11:13 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_retrieve():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.option('b').value.get() is None
await cfg.option('b').value.set(True)
assert await cfg.option('b').value.get() is True
del cfg
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.option('b').value.get() is True
2018-10-31 08:00:19 +01:00
assert 'test_persistent' in list_sessions()
2019-12-24 15:24:20 +01:00
delete_session(await cfg.config.name())
del cfg
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.option('b').value.get() is None
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_two_persistent():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
cfg2 = await Config(o, session_id='test_persistent', persistent=True)
await cfg2.property.pop('cache')
assert await cfg.option('b').value.get() is None
assert await cfg2.option('b').value.get() is None
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('b').value.set(False)
assert await cfg.option('b').value.get() is False
assert await cfg2.option('b').value.get() is False
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('b').value.set(True)
assert await cfg.option('b').value.get() is True
assert await cfg2.option('b').value.get() is True
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_retrieve_owner():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.option('b').owner.isdefault()
await cfg.option('b').value.set(True)
assert await cfg.option('b').value.get()
assert await cfg.option('b').owner.get() == 'user'
2018-09-11 20:11:13 +02:00
##owners.addowner('persistentowner')
2019-12-24 15:24:20 +01:00
await cfg.option('b').owner.set('persistentowner')
assert await cfg.option('b').owner.get() == 'persistentowner'
del cfg
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.option('b').owner.set('persistentowner')
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.option('b').value.get() is None
assert await cfg.option('b').owner.isdefault()
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_retrieve_owner_leadership():
2018-09-11 20:11:13 +02:00
a = BoolOption('a', '', multi=True)
b = BoolOption('b', '', multi=True)
2019-02-23 19:06:23 +01:00
o = Leadership('a', '', [a, b])
2018-09-11 20:11:13 +02:00
o1 = OptionDescription('a', '', [o])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o1, session_id='test_persistent', persistent=True)
assert await cfg.option('a.a').owner.isdefault()
await cfg.option('a.a').value.set([True, False])
await cfg.option('a.b', 1).value.set(True)
assert await cfg.option('a.a').owner.get() == 'user'
assert await cfg.option('a.b', 0).owner.isdefault()
assert await cfg.option('a.b', 1).owner.get() == 'user'
2018-09-11 20:11:13 +02:00
#owners.addowner('persistentowner2')
2019-12-24 15:24:20 +01:00
await cfg.option('a.b', 1).owner.set('persistentowner2')
await cfg.option('a.b', 0).value.set(True)
assert await cfg.option('a.b', 0).owner.get() == 'user'
assert await cfg.option('a.b', 1).owner.get() == 'persistentowner2'
assert await cfg.option('a.a').value.get() == [True, False]
del cfg
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o1, session_id='test_persistent', persistent=True)
assert await cfg.option('a.a').value.get() == [True, False]
assert await cfg.option('a.b', 0).owner.get() == 'user'
assert await cfg.option('a.b', 1).owner.get() == 'persistentowner2'
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o1, session_id='test_persistent', persistent=True)
assert await cfg.option('a.a').value.get() == []
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_two_persistent_owner():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.property.pop('cache')
cfg2 = await Config(o, session_id='test_persistent', persistent=True)
await cfg2.property.pop('cache')
assert await cfg.option('b').owner.isdefault()
assert await cfg2.option('b').owner.isdefault()
await cfg.option('b').value.set(False)
assert await cfg.option('b').owner.get() == 'user'
assert await cfg2.option('b').owner.get() == 'user'
await cfg.option('b').owner.set('persistent')
assert await cfg.option('b').owner.get() == 'persistent'
assert await cfg2.option('b').owner.get() == 'persistent'
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_retrieve_information():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.information.set('info', 'string')
assert await cfg.information.get('info') == 'string'
del cfg
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.information.get('info') == 'string'
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.information.get('info', None) is None
delete_session(await cfg.config.name())
del cfg
2018-09-11 20:11:13 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_two_persistent_information():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.property.pop('cache')
await cfg.information.set('info', 'string')
assert await cfg.information.get('info') == 'string'
cfg2 = await Config(o, session_id='test_persistent', persistent=True)
await cfg2.property.pop('cache')
assert await cfg2.information.get('info') == 'string'
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_two_different_persistents():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.property.pop('cache')
cfg2 = await Config(o, session_id='test_persistent2', persistent=True)
await cfg2.property.pop('cache')
await cfg.option('b').property.add('test')
assert await cfg.option('b').property.get() == {'test'}
assert await cfg2.option('b').property.get() == set()
assert await cfg.option('b').value.get() is None
assert await cfg2.option('b').value.get() is None
await cfg.option('b').value.set(True)
assert await cfg.option('b').value.get() == True
assert await cfg2.option('b').value.get() is None
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
delete_session('test_persistent2')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_two_different_information():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.information.set('a', 'a')
cfg2 = await Config(o, session_id='test_persistent2', persistent=True)
await cfg2.information.set('a', 'b')
assert await cfg.information.get('a') == 'a'
assert await cfg2.information.get('a') == 'b'
2018-09-11 20:11:13 +02:00
delete_session('test_persistent')
delete_session('test_persistent2')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_exportation_importation():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-02-23 22:52:06 +01:00
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
cfg2 = await Config(o, session_id='test_persistent2', persistent=True)
cfg3 = await Config(o, session_id='test_persistent3', persistent=True)
await cfg.owner.set('export')
assert await cfg.option('b').value.get() is None
await cfg.option('b').value.set(True)
assert await cfg.option('b').value.get() is True
assert await cfg.owner.get() == 'export'
del cfg
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert await cfg.owner.get() == 'export'
assert await cfg.value.exportation() == [['b'], [None], [True], ['export']]
await cfg2.value.importation(await cfg.value.exportation())
assert await cfg.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg.owner.get() == 'export'
assert await cfg2.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg2.owner.get() == 'user'
del cfg2
#
2019-12-24 15:24:20 +01:00
cfg2 = await Config(o, session_id='test_persistent2', persistent=True)
assert await cfg2.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg2.owner.get() == 'user'
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg3.value.importation(await cfg.value.exportation(with_default_owner=True))
assert await cfg3.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg3.owner.get() == 'export'
del cfg3
2018-09-15 22:44:49 +02:00
#
2019-12-24 15:24:20 +01:00
cfg3 = await Config(o, session_id='test_persistent3', persistent=True)
assert await cfg3.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg3.owner.get() == 'export'
2018-09-15 22:44:49 +02:00
#
delete_session('test_persistent')
delete_session('test_persistent2')
2018-09-15 22:44:49 +02:00
delete_session('test_persistent3')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_context_property():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.property.add('persistent')
del cfg
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert 'persistent' in await cfg.property.get()
del cfg
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_context_property_metaconfig():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.property.add('persistent')
del cfg
#
2019-12-24 15:24:20 +01:00
meta = await MetaConfig([], optiondescription=o)
cfg = await meta.config.new(session_id='test_persistent', persistent=True)
assert 'persistent' in await cfg.property.get()
del cfg
delete_session('test_persistent')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_property():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
if default_storage.is_persistent():
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
await cfg.option('b').property.add('persistent')
del cfg
#
2019-12-24 15:24:20 +01:00
cfg = await Config(o, session_id='test_persistent', persistent=True)
assert 'persistent' in await cfg.option('b').property.get()
del cfg
delete_session('test_persistent')