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'])
def test_transaction_conditions(kv_test_app): cid = kv_test_app.get_containers()[0].id service, instance = cid.split('_') instance = int(instance) cid_larger = '%s_%d' % (service, instance + 1) cid_smaller = '%s_%d' % (service, instance - 1) val = b'bcd' larger = b'bce' smaller = b'bcc' kv_test_app.kv.put('owner', val, owner=cid) kv_test_app.kv.put('no_owner', val) def run(*conds): return kv_test_app.kv.transaction(conds).succeeded # -- value -- # exists assert run(kv.exists('owner')) assert not run(kv.exists('missing')) assert run(kv.missing('missing')) assert not run(kv.missing('owner')) # == assert run(kv.value('owner') == val) assert not run(kv.value('owner') == larger) assert not run(kv.value('missing') == val) # != assert run(kv.value('owner') != larger) assert not run(kv.value('owner') != val) assert run(kv.value('missing') != val) # < assert run(kv.value('owner') < larger) assert not run(kv.value('owner') < val) assert not run(kv.value('owner') < smaller) assert not run(kv.value('missing') < val) # <= assert run(kv.value('owner') <= larger) assert run(kv.value('owner') <= val) assert not run(kv.value('owner') <= smaller) assert not run(kv.value('missing') <= val) # > assert not run(kv.value('owner') > larger) assert not run(kv.value('owner') > val) assert run(kv.value('owner') > smaller) assert not run(kv.value('missing') > val) # >= assert not run(kv.value('owner') >= larger) assert run(kv.value('owner') >= val) assert run(kv.value('owner') >= smaller) assert not run(kv.value('missing') >= val) # -- owner -- # Owner presence/absence assert run(kv.owner('owner') != None) # noqa assert not run(kv.owner('owner') == None) # noqa assert run(kv.owner('no_owner') == None) # noqa assert not run(kv.owner('no_owner') != None) # noqa # == assert run(kv.owner('owner') == cid) assert not run(kv.owner('owner') == cid_larger) assert not run(kv.owner('no_owner') == cid) # != assert run(kv.owner('owner') != cid_larger) assert not run(kv.owner('owner') != cid) assert run(kv.owner('no_owner') != cid) # < assert run(kv.owner('owner') < cid_larger) assert not run(kv.owner('owner') < cid) assert not run(kv.owner('owner') < cid_smaller) assert not run(kv.owner('no_owner') < cid) # <= assert run(kv.owner('owner') <= cid_larger) assert run(kv.owner('owner') <= cid) assert not run(kv.owner('owner') <= cid_smaller) assert not run(kv.owner('no_owner') <= cid) # > assert not run(kv.owner('owner') > cid_larger) assert not run(kv.owner('owner') > cid) assert run(kv.owner('owner') > cid_smaller) assert not run(kv.owner('no_owner') > cid) # >= assert not run(kv.owner('owner') >= cid_larger) assert run(kv.owner('owner') >= cid) assert run(kv.owner('owner') >= cid_smaller) 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)