def test_transaction_put_key_errors(kv_test_app, op, msg): cid = kv_test_app.get_containers()[0].id kv_test_app.kv.put('a', b'a', owner=cid) for succeeded in [True, False]: if succeeded: kwargs = {'conditions': [kv.exists('a')], 'on_success': [kv.put('b', b'b'), op], 'on_failure': [kv.put('c', b'c')]} else: kwargs = {'conditions': [kv.missing('a')], 'on_success': [kv.put('c', b'c')], 'on_failure': [kv.put('b', b'b'), op]} with pytest.raises(ValueError) as exc: kv_test_app.kv.transaction(**kwargs) assert str(exc.value) == msg assert not kv_test_app.kv.exists('b') assert not kv_test_app.kv.exists('c')
def test_transaction(kv_test_app): cid = kv_test_app.get_containers()[0].id kv_test_app.kv.put('a', b'a', owner=cid) kv_test_app.kv.put('aa', b'aa') kv_test_app.kv.put('aaa', b'aaa') kv_test_app.kv.put('b', b'b') true1 = kv.exists('a') true2 = kv.exists('b') false = kv.missing('a') # Test building results in success and failure for succeeded in [True, False]: if succeeded: cond, do, dont = [true1, true2], 'on_success', 'on_failure' else: cond, do, dont = [true1, false], 'on_failure', 'on_success' kwargs = { 'conditions': cond, do: [kv.get_prefix('a'), kv.get('missing')], dont: [kv.put('dont_put_me', b'bad')] } res = kv_test_app.kv.transaction(**kwargs) assert res.succeeded == succeeded assert len(res.results) == 2 assert res.results[0] == kv_test_app.kv.get_prefix('a') assert res.results[1] == kv_test_app.kv.get('missing') assert not kv_test_app.kv.exists('dont_put_me') # Test empty result branch kwargs = {'conditions': cond, dont: [kv.put('dont_put_me', b'bad')]} res = kv_test_app.kv.transaction(**kwargs) assert res == (succeeded, []) assert not kv_test_app.kv.exists('dont_put_me') # Test one operation of each type a_prefixes = kv_test_app.kv.get_prefix('a') res = kv_test_app.kv.transaction(conditions=[true1, true2], on_success=[ kv.get_prefix('a'), kv.discard_prefix('a', return_keys=True), kv.put('a', b'later') ]) assert res.succeeded assert len(res.results) == 3 assert res.results[0] == a_prefixes assert res.results[1] == list(a_prefixes) assert res.results[2] is None # Test no operations assert kv_test_app.kv.transaction() == (True, []) # invalid argument types with pytest.raises(TypeError): kv_test_app.kv.transaction(conditions=[kv.get('foo')]) with pytest.raises(TypeError): kv_test_app.kv.transaction(on_success=[kv.value('foo') == b'bar']) with pytest.raises(TypeError): kv_test_app.kv.transaction(on_failure=[kv.value('foo') == b'bar'])
# No conditions assert run() # Multiple conditions true1 = kv.exists('owner') true2 = kv.exists('no_owner') false = kv.exists('missing') assert run(true1, true2) assert not run(false, true1) assert not run(true1, false, true2) @pytest.mark.parametrize( 'op, msg', [(kv.put('missing', owner='sleeper_0'), "ignore_value=True & key isn't already set"), (kv.put('a', owner='unknown_0'), "Unknown service 'unknown'"), (kv.put('a', owner='sleeper_99'), "Service 'sleeper' has no container instance 99"), (kv.put('a', owner='sleeper_0'), "Container 'sleeper_0' has already completed")]) def test_transaction_put_key_errors(kv_test_app, op, msg): cid = kv_test_app.get_containers()[0].id kv_test_app.kv.put('a', b'a', owner=cid) for succeeded in [True, False]: if succeeded: kwargs = { 'conditions': [kv.exists('a')], 'on_success': [kv.put('b', b'b'), op], 'on_failure': [kv.put('c', b'c')]
assert not run(kv.owner('no_owner') >= cid) # No conditions assert run() # Multiple conditions true1 = kv.exists('owner') true2 = kv.exists('no_owner') false = kv.exists('missing') assert run(true1, true2) assert not run(false, true1) assert not run(true1, false, true2) @pytest.mark.parametrize('op, msg', [ (kv.put('missing', owner='sleeper_0'), "ignore_value=True & key isn't already set"), (kv.put('a', owner='unknown_0'), "Unknown service 'unknown'"), (kv.put('a', owner='sleeper_99'), "Service 'sleeper' has no container instance 99"), (kv.put('a', owner='sleeper_0'), "Container 'sleeper_0' has already completed")]) def test_transaction_put_key_errors(kv_test_app, op, msg): cid = kv_test_app.get_containers()[0].id kv_test_app.kv.put('a', b'a', owner=cid) for succeeded in [True, False]: if succeeded: kwargs = {'conditions': [kv.exists('a')], 'on_success': [kv.put('b', b'b'), op], 'on_failure': [kv.put('c', b'c')]} else: kwargs = {'conditions': [kv.missing('a')], 'on_success': [kv.put('c', b'c')],