Пример #1
0
def test_insert_t(session):
    keyid = key('test_list_insert_t')
    list_ = session.set(keyid, 'abcdefg', List)
    with Transaction(session, [keyid]):
        first = list_[0]
        assert first == 'a'
        list_.insert(0, 'Z')
    assert list_[:] == list('Zabcdefg')
    with Transaction(session, [keyid]):
        first = list_[0]
        assert first == 'Z'
        list_.insert(-1, 'G')
    assert list_[:] == list('ZabcdefGg')
    with Transaction(session, [keyid]):
        first = list_[0]
        assert first == 'Z'
        list_.insert(2, 'A')
    assert list_[:] == list('ZaAbcdefGg')
    with Transaction(session, [keyid]):
        first = list_[0]
        assert first == 'Z'
        list_.insert(-3, 'F')
    assert list_[:] == list('ZaAbcdeFfGg')
Пример #2
0
def test_delitem_t(session):
    session2 = get_session()
    keyid = key('test_hash_delitem_t')
    hash_ = session.set(keyid, fixture_a, Hash)
    hashx = session2.get(keyid, Hash)
    with Transaction(session, [keyid]):
        len(hash_)
        del hash_['a']
        assert 'a' in hashx
        with raises(CommitError):
            len(hash_)
    assert len(hash_) == 1
    assert 'a' not in hash_
    assert 'a' not in hashx
Пример #3
0
def test_raw_transaction():
    session1 = get_session()
    session2 = get_session()
    keyid = key('test_transaction_raw')
    list1 = session1.set(keyid, 'abc', List)
    list2 = session2.get(keyid, List)
    with Transaction(session1, [keyid]) as t:
        assert isinstance(t, Transaction)
        assert list1[:] == ['a', 'b', 'c']
        assert list2[:] == ['a', 'b', 'c']
        t.begin_commit()
        list1.append('d')
        assert list2[:] == ['a', 'b', 'c']
    assert list1[:] == list2[:] == ['a', 'b', 'c', 'd']
Пример #4
0
def test_pop_t(session):
    keyid = key('test_list_pop_t')
    list_ = session.set(keyid, 'abcdefg', List)
    with Transaction(session, [keyid]):
        first = list_[0]
        assert first == 'a'
        popped = list_.pop()
        assert popped == 'g'
        list_.append('h')
    assert list_[:] == list('abcdefh')
    with Transaction(session, [keyid]):
        last = list_[-1]
        assert last == 'h'
        popped = list_.pop(0)
        assert popped == 'a'
        list_.append('i')
    assert list_[:] == list('bcdefhi')
    with Transaction(session, [keyid]):
        last = list_[-1]
        assert last == 'i'
        popped = list_.pop(2)
        assert popped == 'd'
        list_.append('j')
    assert list_[:] == list('bcefhij')
    with Transaction(session, [keyid]):
        last = list_[-1]
        assert last == 'j'
        popped = list_.pop(-3)
        assert popped == 'h'
        list_.append('k')
    assert list_[:] == list('bcefijk')
    with Transaction(session, [keyid]):
        list_.pop()
        with raises(CommitError):
            len(list_)
    with Transaction(session, [keyid]):
        list_.pop(-1)
        with raises(CommitError):
            len(list_)
    with Transaction(session, [keyid]):
        list_.pop(2)
        with raises(CommitError):
            len(list_)
    with Transaction(session, [keyid]):
        list_.pop(-2)
        with raises(CommitError):
            len(list_)
Пример #5
0
def test_automatic_watch():
    session = get_session()
    session2 = get_session()
    keyid = key('test_transaction_automatic_watch')
    list_ = session.set(keyid, 'abc', List)
    list2 = session2.get(keyid, List)
    with raises(ConflictError):
        with Transaction(session):
            list_.append('d')
            list2.append('e')
    # Transation.keys have to be initialized for each context:
    t = Transaction(session)
    with t:
        list_.append('d')
    listx = session.set(key('test_transaction_automatic_watch2'), 'abc', List)
    with t:
        listx.append('a')
        list2.append('b')
    with t:
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            t.watch('asdf')
            assert len(w) == 1
            assert issubclass(w[0].category, SiderWarning)
Пример #6
0
def test_commit_error():
    session = get_session()
    keyid = key('test_transaction_commit_error')
    list_ = session.set(keyid, 'abc', List)
    try:
        with Transaction(session, [keyid]):
            list_.append('d')
            try:
                list(list_)
            except CommitError:
                raise
            else:
                assert False, 'expected CommitError'
    except CommitError:
        pass
    assert list_[:] == list('abc'), 'transaction must be rolled back'
Пример #7
0
def test_transaction_call():
    session = get_session()
    session2 = get_session()
    keyid = key('test_transaction_call')
    list1 = session.set(keyid, 'abc', List)
    list2 = session2.get(keyid, List)
    t = Transaction(session, [keyid])
    total_trial = [0]

    def block(trial, transaction):
        total_trial[0] = trial
        up = list1[0].upper()
        if trial < 3:
            list2.append('x')
        list1[0] = up

    t(block)
    assert list1[:] == list2[:] == list('Abcxxx')
    assert total_trial[0] == 3
Пример #8
0
def test_pop_dict_t(session):
    session2 = get_session()
    keyid = key('test_sortedset_dict_set_t')
    set_ = session.set(keyid, {'h': 1, 'o': 1, 'n': 3, 'g': 4}, SortedSet)
    set2 = session2.get(keyid, SortedSet)
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.pop('o')
        assert popped == 1
        assert dict(set2) == {'h': 1, 'o': 1, 'n': 3, 'g': 4}
        with raises(CommitError):
            len(set_)
    assert dict(set_) == dict(set2) == {'h': 1, 'n': 3, 'g': 4}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.pop('n', score=0.5)
        assert popped == 3
        assert dict(set2) == {'h': 1, 'n': 3, 'g': 4}
        with raises(CommitError):
            len(set_)
    assert dict(set_) == dict(set2) == {'h': 1, 'n': 2.5, 'g': 4}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.pop('n', remove=1.5)
        assert popped == 2.5
        assert dict(set2) == {'h': 1, 'n': 2.5, 'g': 4}
        with raises(CommitError):
            len(set_)
    assert dict(set_) == dict(set2) == {'h': 1, 'g': 4}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.pop('g', remove=None)
        assert popped == 4
        assert dict(set2) == {'h': 1, 'g': 4}
        with raises(CommitError):
            len(set_)
    assert dict(set_) == dict(set2) == {'h': 1}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.pop('n')
        assert popped is None
        assert dict(set2) == {'h': 1}
    assert dict(set_) == dict(set2) == {'h': 1}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.pop('n', default='default value')
        assert popped == 'default value'
        assert dict(set2) == {'h': 1}
    assert dict(set_) == dict(set2) == {'h': 1}
Пример #9
0
def test_popitem_t(session):
    session2 = get_session()
    keyid = key('test_sortedset_popitem_t')
    set_ = session.set(keyid, {'h': 1, 'o': 2, 'n': 3, 'g': 4.5}, SortedSet)
    set2 = session2.get(keyid, SortedSet)
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.popitem()
        assert popped == ('h', 1)
        assert dict(set2) == {'h': 1, 'o': 2, 'n': 3, 'g': 4.5}
        with raises(CommitError):
            len(set_)
    assert dict(set_) == dict(set2) == {'o': 2, 'n': 3, 'g': 4.5}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.popitem(desc=True)
        assert popped == ('g', 4.5)
        assert dict(set2) == {'o': 2, 'n': 3, 'g': 4.5}
        with raises(CommitError):
            len(set_)
    assert dict(set_) == dict(set2) == {'o': 2, 'n': 3, 'g': 3.5}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.popitem(score=0.5)
        assert popped == ('o', 2)
        assert dict(set2) == {'o': 2, 'n': 3, 'g': 3.5}
    assert dict(set_) == dict(set2) == {'o': 1.5, 'n': 3, 'g': 3.5}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.popitem(remove=0.5)
        assert popped == ('o', 1.5)
        assert dict(set2) == {'o': 1.5, 'n': 3, 'g': 3.5}
    assert dict(set_) == dict(set2) == {'n': 3, 'g': 3.5}
    with Transaction(session, [keyid]):
        len(set_)
        popped = set_.popitem(remove=None)
        assert popped == ('n', 3)
        assert dict(set2) == {'n': 3, 'g': 3.5}
    assert dict(set_) == dict(set2) == {'g': 3.5}
    set_.clear()
    with Transaction(session, [keyid]):
        with raises(KeyError):
            set_.popitem()
Пример #10
0
def test_delete_t(session):
    session2 = get_session()
    keyid = key('test_list_delete_t')
    list_ = session.set(keyid, 'abcdefg', List)
    list2 = session2.get(keyid, List)
    with Transaction(session, [keyid]):
        size = len(list_)
        assert size == 7
        del list_[0]
        assert list2[:] == list('abcdefg')
    assert list_[:] == list2[:] == list('bcdefg')
    with Transaction(session, [keyid]):
        size = len(list_)
        assert size == 6
        del list_[-1]
        assert list2[:] == list('bcdefg')
    assert list_[:] == list2[:] == list('bcdef')
    with Transaction(session, [keyid]):
        size = len(list_)
        assert size == 5
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            del list_[2]
            assert len(w) == 1
            assert issubclass(w[0].category, PerformanceWarning)
        assert list2[:] == list('bcdef')
    assert list_[:] == list2[:] == list('bcef')
    with Transaction(session, [keyid]):
        size = len(list_)
        assert size == 4
        del list_[:]
        assert list2[:] == list('bcef')
    assert len(list_[:]) == len(list2[:]) == 0
    with Transaction(session, [keyid]):
        with raises(IndexError):
            del list_[-1]
    with Transaction(session, [keyid]):
        with raises(IndexError):
            del list_[0]
Пример #11
0
def test_delitem_t(session):
    session2 = get_session()
    keyid = key('test_sortedset_delitem_t')
    set_ = session.set(keyid, S('abc'), SortedSet)
    set2 = session2.get(keyid, SortedSet)
    with Transaction(session, [keyid]):
        card = len(set_)
        assert card == 3
        del set_['b']
        assert S(set2) == S('abc')
        with raises(CommitError):
            len(set_)
    assert S(set_) == S(set2) == S('ac')
    with Transaction(session, [keyid]):
        with raises(KeyError):
            del set_['d']
    with Transaction(session, [keyid]):
        with raises(TypeError):
            del set_[123]
    keyid2 = key('test_sortedsetx_delitem_t')
    setx = session.set(keyid2, S([1, 2, 3]), IntSet)
    sety = session2.get(keyid2, IntSet)
    with Transaction(session, [keyid]):
        card = len(setx)
        assert card == 3
        del setx[2]
        assert S(sety) == S([1, 2, 3])
        with raises(CommitError):
            len(setx)
    assert S(setx) == S(sety) == S([1, 3])
    with Transaction(session, [keyid]):
        with raises(KeyError):
            del setx[4]
    with Transaction(session, [keyid]):
        with raises(TypeError):
            del setx['a']