Exemplo n.º 1
0
def test_comparison_type():
    # test using `comparison` directly
    v = kv.comparison('foo', 'value', '==', b'bar')
    assert v == (kv.value('foo') == b'bar')

    o = kv.comparison('foo', 'owner', '==', 'bar_1')
    assert o == (kv.owner('foo') == 'bar_1')

    with pytest.raises(TypeError):
        kv.comparison(1, 'value', '==', b'bar')

    with pytest.raises(ValueError):
        kv.comparison('foo', 'unknown', '==', b'bar')

    with pytest.raises(ValueError):
        kv.comparison('foo', 'value', 'unknown', b'bar')

    with pytest.raises(TypeError):
        kv.comparison('foo', 'owner', '<=', None)
Exemplo n.º 2
0
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'])
Exemplo n.º 3
0
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)