tiramisu/tests/test_storage.py

334 lines
12 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
2020-01-22 20:46:18 +01:00
from .config import event_loop
2018-10-31 08:00:19 +01:00
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])
2020-01-22 20:46:18 +01:00
async with await Config(o, session_id='test_non_persistent', delete_old_session=True) as cfg:
pass
assert not await list_sessions()
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])
2020-01-22 20:46:18 +01:00
async with await Config(o, session_id='test_non_persistent') as cfg:
await cfg.option('b').value.set(True)
assert 'test_non_persistent' in await list_sessions()
assert 'test_non_persistent' not in await list_sessions()
assert not await list_sessions()
2018-09-13 22:01:27 +02:00
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])
2020-01-22 20:46:18 +01:00
await Config(o, session_id='test_persistent')
await delete_session('test_persistent')
assert not await list_sessions()
2018-09-11 20:11:13 +02:00
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.option('b').value.set(True)
assert 'test_persistent' in await list_sessions()
await delete_session('test_persistent')
assert not await 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_session_persistent():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
await Config(o, session_id='test_persistent')
assert 'test_persistent' in await list_sessions()
await delete_session('test_persistent')
assert 'test_persistent' not in await list_sessions()
assert not await 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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
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')
assert await cfg.option('b').value.get() is True
assert 'test_persistent' in await list_sessions()
await delete_session(await cfg.session.id())
del cfg
cfg = await Config(o, session_id='test_persistent')
assert await cfg.option('b').value.get() is None
await delete_session(await cfg.session.id())
del cfg
assert not await list_sessions()
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
cfg2 = await Config(o, session_id='test_persistent')
await cfg2.property.pop('cache')
assert await cfg.option('b').value.get() is None
assert await cfg2.option('b').value.get() is None
#
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
#
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
await delete_session('test_persistent')
assert not await 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_owner():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
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'
##owners.addowner('persistentowner')
await cfg.option('b').owner.set('persistentowner')
assert await cfg.option('b').owner.get() == 'persistentowner'
del cfg
#
cfg = await Config(o, session_id='test_persistent')
await cfg.option('b').owner.set('persistentowner')
await delete_session(await cfg.session.id())
del cfg
#
cfg = await Config(o, session_id='test_persistent')
assert await cfg.option('b').value.get() is None
assert await cfg.option('b').owner.isdefault()
await delete_session(await cfg.session.id())
del cfg
assert not await 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_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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o1, session_id='test_persistent')
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'
#owners.addowner('persistentowner2')
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
#
cfg = await Config(o1, session_id='test_persistent')
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'
await delete_session(await cfg.session.id())
del cfg
#
cfg = await Config(o1, session_id='test_persistent')
assert await cfg.option('a.a').value.get() == []
await delete_session(await cfg.session.id())
del cfg
assert not await list_sessions()
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.property.pop('cache')
cfg2 = await Config(o, session_id='test_persistent')
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'
await delete_session('test_persistent')
assert not await 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_information():
2018-09-11 20:11:13 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.information.set('info', 'string')
assert await cfg.information.get('info') == 'string'
del cfg
#
cfg = await Config(o, session_id='test_persistent')
assert await cfg.information.get('info') == 'string'
await delete_session(await cfg.session.id())
del cfg
#
cfg = await Config(o, session_id='test_persistent')
assert await cfg.information.get('info', None) is None
await delete_session(await cfg.session.id())
del cfg
assert not await list_sessions()
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
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')
await cfg2.property.pop('cache')
assert await cfg2.information.get('info') == 'string'
await delete_session('test_persistent')
assert not await list_sessions()
2018-09-11 20:11:13 +02:00
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.property.pop('cache')
cfg2 = await Config(o, session_id='test_persistent2')
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
2020-01-22 20:46:18 +01:00
await delete_session('test_persistent')
await delete_session('test_persistent2')
assert not await list_sessions()
2018-09-11 20:11:13 +02:00
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.information.set('a', 'a')
cfg2 = await Config(o, session_id='test_persistent2')
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
2020-01-22 20:46:18 +01:00
await delete_session('test_persistent')
await delete_session('test_persistent2')
assert not await list_sessions()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_exportation_importation():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
cfg2 = await Config(o, session_id='test_persistent2')
cfg3 = await Config(o, session_id='test_persistent3')
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
#
cfg = await Config(o, session_id='test_persistent')
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
#
cfg2 = await Config(o, session_id='test_persistent2')
assert await cfg2.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg2.owner.get() == 'user'
#
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
#
cfg3 = await Config(o, session_id='test_persistent3')
assert await cfg3.value.exportation() == [['b'], [None], [True], ['export']]
assert await cfg3.owner.get() == 'export'
#
await delete_session('test_persistent')
await delete_session('test_persistent2')
await delete_session('test_persistent3')
assert not await list_sessions()
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])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.property.add('persistent')
del cfg
#
cfg = await Config(o, session_id='test_persistent')
assert 'persistent' in await cfg.property.get()
del cfg
await delete_session('test_persistent')
assert not await list_sessions()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_create_persistent_property():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
cfg = await Config(o, session_id='test_persistent')
await cfg.option('b').property.add('persistent')
del cfg
#
cfg = await Config(o, session_id='test_persistent')
assert 'persistent' in await cfg.option('b').property.get()
del cfg
await delete_session('test_persistent')