def test_sync_stub_patcher(monkeypatch): a = Setting('a', int, ['user', 'theme'], default_value=0) with SyncStubHeksherClient() as client: b = Setting('b', Mapping[str, int], ['user', 'theme'], default_value=0) monkeypatch.setattr(client[a], 'rules', 10) monkeypatch.setattr(client[b], 'rules', { 't': 1, 'z': 2 }) c = Setting('c', int, ['user', 'theme'], default_value=0) monkeypatch.setattr(client[c], 'rules', [ Rule({}, 0), Rule({'theme': 'dark'}, 1), Rule({'user': '******'}, 2) ]) assert a.get(user='', theme='') == 10 assert b.get(user='', theme='') == { 't': 1, 'z': 2 } assert c.get(user='', theme='') == 0 assert c.get(user='', theme='dark') == 1 assert c.get(user='******', theme='') == 2 assert c.get(user='******', theme='dark') == 1
async def test_async_stub(): a = Setting('a', int, ['user', 'theme'], default_value=0) async with AsyncStubHeksherClient() as client: b = Setting('b', Mapping[str, int], ['user', 'theme'], default_value=0) with client.patch(a, 10), client.patch(b, { 't': 1, 'z': 2 }): c = Setting('c', int, ['user', 'theme'], default_value=0) client.patch(c, [ Rule({}, 0), Rule({'theme': 'dark'}, 1), Rule({'user': '******', 'theme': None}, 2) ]) assert a.get(user='', theme='') == 10 assert b.get(user='', theme='') == { 't': 1, 'z': 2 } assert c.get(user='', theme='') == 0 assert c.get(user='', theme='dark') == 1 assert c.get(user='******', theme='') == 2 assert c.get(user='******', theme='dark') == 1
def test_multi_update(): a = Setting('a', int, 'abcx', default_value=-1) with SyncStubHeksherClient() as client: client.set_defaults(a='', b='', c='') client.patch(a, 10) assert a.get(x='') == 10 with client.patch(a, [ Rule({'x': '0'}, 0), Rule({'x': '1'}, 1) ]): assert a.get(x='0') == 0 assert a.get(x='1') == 1 assert a.get(x='15') == -1 assert a.get(x='0') == 10
def test_resolve(): a = Setting('a', int, 'abcx', default_value=-1) with SyncStubHeksherClient() as client, \ client.patch(a, [ Rule({'x': '0'}, 0), Rule({'x': '1'}, 1) ]): client.set_defaults(a='', b='', c='') assert a.get(x='0') == 0 assert a.get(x='1') == 1 assert a.get(x='15') == -1 with raises(RuntimeError): a.get() client.set_defaults(x='0') assert a.get(x='1') == 1
def test_multi_switch(caplog): a = Setting('a', int, 'abcx', default_value=-1) c1 = SyncStubHeksherClient() c1.set_defaults(a='', b='', c='') c1.set_as_main() c1.patch(a, 10) assert a.get(x='') == 10 heksher.main_client.Main = TemporaryClient() c2 = SyncStubHeksherClient() c2.set_defaults(a='', b='', c='') c2.set_as_main() with assert_logs(caplog, WARNING): c2.patch(a, [ Rule({'x': '0'}, 0), Rule({'x': '1'}, 1) ]) assert a.get(x='0') == 0 assert a.get(x='1') == 1 assert a.get(x='15') == -1
def test_setting_rules_collection_callback(): a = Setting('a', int, 'abcx', default_value=-1) def setting_callback_1(value: int, rule, setting: Setting) -> int: if setting.name == 'a' and value == 0: return 3 return value def setting_callback_2(value: int, rule, setting: Setting) -> int: if setting.name == 'a' and value == 3: return 7 return value a.add_validator(setting_callback_1) a.add_validator(setting_callback_2) c1 = SyncStubHeksherClient() c1.set_as_main() c1.set_defaults(a='', b='', c='', x='') c1.patch(a, [ Rule({'x': '0'}, 0) ]) assert a.get(x='0') == 0 assert a.get(x='1') == -1