Example #1
0
def test_init():
    data = {'bob': 'bobby'}

    q = qube.init(data)
    assert_that(q, has_entry('sequence', 0))
    assert_that(q, has_entry('journal', []))
    assert_that(q, has_entry('data', data))
Example #2
0
def test_read_merges_siblings():
    db = StubDb()
    rollback = StubRollback()

    qa = qube.init({'test': set()})
    qb = qube.init({'test': set()})

    qube.apply_op(qa, ('add', 'test', 'abc', 'tx0'))
    qube.apply_op(qb, ('add', 'test', 'abc', 'tx0'))

    qube.apply_op(qa, ('add', 'test', 'def', 'tx123'))
    qube.apply_op(qb, ('add', 'test', 'ghi', 'tx456'))

    def doraise(a, b):
        raise riak.Conflict('the_vclock',
                            'x/zzz',
                            [StubDbResult(qube.to_json(qa)),
                             StubDbResult(qube.to_json(qb))])

    db.get.side_effect = doraise

    m = yield from FooModel.read(db, rollback, 'zzz')

    assert_that(m, instance_of(FooModel))
    assert_that(m.qube['data'], has_entry('test', {'abc', 'def', 'ghi'}))
Example #3
0
def test_read_conflict_rollback_not_reintroduced():
    db = StubDb()
    rollback = StubRollback()

    qa = qube.init({'test': set()})
    qb = qube.init({'test': set()})

    qube.apply_op(qa, ('add', 'test', 'abc', 'tx0'))
    qube.apply_op(qa, ('rem', 'test', 'abc', 'tx456'))

    qube.apply_op(qb, ('add', 'test', 'abc', 'tx0'))
    qube.apply_op(qb, ('rem', 'test', 'abc', 'tx456'))

    qube.rollback(qa, 'tx456')
    qube.apply_op(qa, ('add', 'test', 'def', 'tx789'))

    qube.apply_op(qb, ('add', 'test', 'ghi', 'tx321'))

    def doraise(a, b):
        raise riak.Conflict('the_vclock',
                            'x/zzz',
                            [StubDbResult(qube.to_json(qa)),
                             StubDbResult(qube.to_json(qb))])

    db.get.side_effect = doraise

    m = yield from FooModel.read(db, rollback, 'zzz')

    assert_that(m.qube['journal'], has_length(3))
    assert_that(m.qube['data'], has_entry('test', {'abc', 'def', 'ghi'}))

    assert_that(db.save, called_once_with('foo_bucket',
                                          'zzz',
                                          has_key('data')))
Example #4
0
def test_merge():
    qa = qube.init({'name': 'zoidberg', 'number': 5})
    qube.apply_op(qa, ('change', 'number', (5, 6), 'tx001'))

    qb = qube.from_json(qube.to_json(qa))

    qube.apply_op(qa, ('change', 'name', ('zoidberg', 'bob'), 'tx002'))
    qube.apply_op(qb, ('change', 'number', (6, 7), 'tx003'))

    error, watch = track_error()

    m = qube.merge(qa, qb, error=watch)
    assert_that(m, has_entry('sequence', 3))
    assert_that(m, has_entry('data', has_entry('name', 'bob')))
    assert_that(m, has_entry('data', has_entry('number', 7)))
    assert_that(error, has_length(0))
Example #5
0
def test_read_performs_rollback():
    db = StubDb()
    rollback = StubRollback()
    rollback.txs = ('tx123',)

    qa = qube.init({'test': set()})
    qube.apply_op(qa, ('add', 'test', 'abc', 'tx0'))
    qube.apply_op(qa, ('add', 'test', 'def', 'tx123'))
    qube.apply_op(qa, ('add', 'test', 'ghi', 'tx456'))

    db.get.return_value = StubDbResult(qube.to_json(qa), key='zzz')

    m = yield from FooModel.read(db, rollback, 'zzz')

    assert_that(m.qube['journal'], has_length(2))
    assert_that(m.qube['data'], has_entry('test', {'abc', 'ghi'}))

    assert_that(db.save, called_once_with('foo_bucket',
                                          'zzz',
                                          has_key('data')))
Example #6
0
def test_step_merge():
    qa = qube.init({'name': 'zoidberg', 'number': 5, 'value': 10})
    qube.apply_op(qa, ('change', 'number', (5, 6), 'tx001'))

    qb = qube.from_json(qube.to_json(qa))

    qube.apply_op(qa, ('change', 'name', ('zoidberg', 'bob'), 'tx002'))
    qube.apply_op(qb, ('change', 'number', (6, 7), 'tx003'))

    qc = qube.from_json(qube.to_json(qa))
    qube.apply_op(qc, ('change', 'value', (10, 9), 'tx004'))

    pprint(qa)
    pprint(qb)
    pprint(qc)

    error, watch = track_error()
    ma = qube.merge(qa, qb, error=watch)

    assert_that(error, has_length(0))
    assert_that(ma, has_entry('sequence', 3))
    assert_that(ma, has_entry('data', has_entry('name', 'bob')))
    assert_that(ma, has_entry('data', has_entry('number', 7)))
    assert_that(ma, has_entry('data', has_entry('value', 10)))

    print('---')
    pprint(ma)
    pprint(qc)

    error, watch = track_error()
    mb = qube.merge(ma, qc, error=watch)
    assert_that(error, has_length(0))
    assert_that(mb, has_entry('sequence', 4))
    assert_that(mb, has_entry('data', has_entry('name', 'bob')))
    assert_that(mb, has_entry('data', has_entry('number', 7)))
    assert_that(mb, has_entry('data', has_entry('value', 9)))