def test_rerun_conflicting_commit(self): toi = blm.testcommit.Test(name=['foo']) self.sync() assert self.find_one({'_id': toi.id[0]}) cctx = self.newcontext() op = commit.CallToi(toi.id[0], 'add', [['bar']]) commitId = cctx.runCommit([op], processCommits=False) ContextBroker().popContext() toi.extra = ['conflict'] self.sync() _rerunCommit = cctx.rerunCommit def rerunCommit(*args, **kw): db_toi_data = self.find_one({'_id': toi.id[0]}) assert db_toi_data.get('_terms', []) == [] return _rerunCommit(*args, **kw) cctx.rerunCommit = rerunCommit cctx.processCommits(commitId) self.sync() cctx = self.newcontext() toi, = blm.testcommit.Test._query().run() assert toi.extra == ['conflict', 'bar'] db_toi_data = self.find_one({'_id': toi.id[0]}) py.test.skip('full text index disabled for now') assert db_toi_data['_terms'] == [{ 'toid': toi.id[0], 'data': ['bar', 'conflict'] }]
def test_BlobVals(self): val = BlobVal('foo') op = commit.CallToi(ObjectId(), 'foo', [[val], ['apa']]) assert set(op.blobVals()) == {val} op = commit.CallBlm('theblm', 'foo', [[val], ['apa']]) assert set(op.blobVals()) == {val}
def test_CallToi_simple(self): toi = blm.testcommit.Test(name=['test']) self.sync() cctx = self.newcontext() op = commit.CallToi(toi.id[0], 'simple', [['bar']]) commitDoc = cctx.runCommit([op]) result = commitDoc.results assert result == [['test', 'bar']]
def invoke_toi(request, database, user, toid, methodName): with context.ReadonlyContext(database, user): try: toi, = blm.TO._query(id=toid).run() method = getattr(toi, methodName) except (ValueError, AttributeError): log.debug('No such toi method: %s.%s', toid, methodName) flask.abort(httplib.NOT_FOUND) params = _get_params(request, method) op = commit.CallToi(toid, methodName, params) return _invoke(request, database, user, op)
def main(pipe): database = accounting.db.connect() with context.ReadonlyContext(database) as ctx: print 'Fetching accounts and balances.' accounts = [ account for account in blm.accounting.Account._query( _attrList=['balance', 'balance_quantity']).run() ] balances = dict((toi.id[0], (toi.balance[0], toi.balance_quantity[0])) for toi in accounts) accounts = [account.id[0] for account in accounts] for n, toids in enumerate(iterate.chunks(accounts, 100)): interested = 'update-%s' % toids[0] print 'Creating commit %d for %s' % (n, interested) with commit.CommitContext(database) as ctx: blm.accounting.Account._query(id=toids, _attrList=[ 'opening_balance', 'opening_quantity', 'balance', 'quantity' ]).run() ops = [] for toid in toids: op = commit.CallToi(toid, 'updateBalance', []) ops.append(op) ctx.runCommit(ops, interested=interested) pipe.send(interested) pipe.send(None) pipe.close() with context.ReadonlyContext(database) as ctx: print 'Fetching accounts and balances for verification.' accounts = [ account for account in blm.accounting.Account._query( _attrList=['balance', 'balance_quantity']).run() ] new_balances = dict( (toi.id[0], (toi.balance[0], toi.balance_quantity[0])) for toi in accounts) for account, old in balances.iteritems(): new = new_balances.get(account) if new != old: print 'DIFF: %s %r -> %r' % (account, old, new)
def test_rerun_locked_tois_commit_self(self): toi = blm.testcommit.Test(name=['foo']) cctx = self.newcontext() op = commit.CallToi(toi.id[0], 'add', [['bar']]) commitId = cctx.runCommit([op], processCommits=False) # toi is locked by a commit already, abuse the fact that # locked toi check does not care about who has locked it, so # using commit context's own ID which will be removed by # unlocking mongo.find_and_modify(self.database.tois, {'_id': toi.id[0]}, {'$set': { '_handled_by': cctx.id }}) cctx.processCommits(commitId) self.sync() cctx = self.newcontext() toi, = blm.testcommit.Test._query().run() assert toi.extra == ['bar']
def test_commit_blobval_reference_handling(self): commitId = ObjectId() bv1 = BlobVal('foo') bv2 = BlobVal('bar') bv3 = BlobVal('baz') bv1.large_blob = bv2.large_blob = bv3.large_blob = 2 bv1.addref(commitId) bv3.addref(commitId) op = commit.CallToi(ObjectId(), 'foo', [[bv1, bv3]]) result = [[bv2, bv3, 'baz']] commitId = self._commit(interested=[1, 2], _id=commitId, operations=[op], result=result) cmt = mongo.find_one(self.database.commits, {'_id': commitId}) py.test.raises(gridfs.NoFile, bv1.gridfs(self.database).get, bv1.value._id) assert cmt['results'][0][0].references == {commitId} assert cmt['results'][0][1].references == {commitId}
def _run(self, params, state): me = self.clientId, self.linkId if state is None: toid = params.get('toid') blmName = params.get('blmName') if not (bool(toid) ^ bool(blmName)): # XOR raise RuntimeError("Only one of toid or blmName " "may be supplied.") if toid is not None and not isinstance(toid, blm.TO): toid = ObjectId(toid) methodName = params['methodName'] args = params['args'] self.save(None, 'processing') # arg only used in tests if toid: op = commit.CallToi(toid, methodName, args) else: op = commit.CallBlm(blmName, methodName, args) self.context.runCommit([op], interested=me) done = commit.Commit.fromquery(self.database, {'handled_by': me, 'state': 'done'}) if done: error = done.error if error is None: result = done.results[0] else: result = [] for obj in iterate.walk(result): if isinstance(obj, Attribute.BlobVal): obj.addref(self.clientId) self.update({'result': result, 'error': error}) done.delete(self.database) self.remove()