def test_SortedQuery_do_not_optimize_ancients(self, monkeypatch): from blm import testblm oid1 = ObjectId() toi1 = self.ctx.createToi(blm.testblm.Test, oid1, {'name': ['foobar']}) oid2 = ObjectId() toi2 = self.ctx.createToi(blm.testblm.Test, oid2, {'name': ['gazonk']}) oid3 = ObjectId() toi3 = self.ctx.createToi(blm.testblm.Test, oid3, {'name': ['zonka']}) self.sync() query = blm.testblm.Test._query(name=Query.Like('foo*')) link = Link.LinkSortedQuery( self.clientId, self.linkId, { 'criteria': query, 'attrList': ['name'], 'subscription': True, 'sorting': 'name' }) link.run() r = self._getResult() assert list(r['toiDiffs'].keys()) == [str(oid1)] # sanity self.ctx._query_cache.clear() # xxx we didn't need to do this in py2 self.ctx.changeToi(toi2, {'name': ['foooo']}) mongo.update_one(self.database.links, { 'client': self.clientId, 'link': self.linkId }, { '$set': { 'outdatedBy': ObjectId(), 'outdatedToids': [], 'ancient': True } }) self.sync() link = Link.LinkFactory().create(None, self.clientId, self.linkId) runQuery = self.ctx.runQuery queries = [] def _runQuery(query): queries.append(query) return runQuery(query) monkeypatch.setattr(self.ctx, 'runQuery', _runQuery) link.run() r = self._getResult() assert r['diffops'] == [[1, 1, [toi2]]] assert r['toiDiffs'][str(oid2)].diffAttrs == {'name': ['foooo']} assert queries == [query] # whitebox
def test_SortedQuery_rerun_optimization_on_id(self, monkeypatch): from blm import testblm oid1 = ObjectId() toi1 = self.ctx.createToi(blm.testblm.Test, oid1, {'name': ['foobar']}) self.sync() query = blm.testblm.Test._query(id=Query.In([str(oid1)])) link = Link.LinkSortedQuery( self.clientId, self.linkId, { 'criteria': query, 'attrList': ['name'], 'subscription': True, 'sorting': 'name' }) link.run() r = self._getResult() assert list(r['toiDiffs'].keys()) == [str(oid1)] # sanity self.ctx.changeToi(toi1, {'name': ['foooo']}) mongo.update_one(self.database.links, { 'client': self.clientId, 'link': self.linkId }, {'$set': { 'outdatedBy': ObjectId(), 'outdatedToids': [oid1] }}) self.sync() link = Link.LinkFactory().create(None, self.clientId, self.linkId) runQuery = self.ctx.runQuery queries = [] def _runQuery(query): queries.append(query) return runQuery(query) monkeypatch.setattr(self.ctx, 'runQuery', _runQuery) link.run() r = self._getResult() assert r['diffops'] == [] assert r['toiDiffs'][str(oid1)].diffAttrs == {'name': ['foooo']} assert queries == [blm.testblm.Test._query(id=[oid1])] # whitebox
def test_SortedQuery_fulltext_update(self): py.test.skip('full text index disabled for now') from blm import testblm with commit.CommitContext(self.database, user=self.user) as ctx: ctx.setMayChange(True) oid1 = ObjectId() toi1 = ctx.createToi(blm.testblm.Test, oid1, {'name': ['foo']}) ctx.runCommit([]) self.sync() query = blm.testblm.Test._query(id=Query.Fulltext('bar')) link = Link.LinkSortedQuery( self.clientId, self.linkId, { 'criteria': query, 'attrList': ['name'], 'subscription': True, 'sorting': 'name' }) link.run() self.sync() x = self._getResult() assert x['diffops'] == [] toi1, = blm.testblm.Test._query(id=oid1).run() with commit.CommitContext(self.database) as ctx: ctx.setMayChange(True) ctx.changeToi(toi1, {'name': ['foo bar']}) ctx.runCommit([]) mongo.update_one(self.database.links, { 'client': self.clientId, 'link': self.linkId }, {'$set': { 'outdatedBy': ObjectId(), 'outdatedToids': [oid1] }}) self.sync() link = Link.LinkFactory().create(None, self.clientId, self.linkId) link.run() self.sync() x = self._getResult() assert x['diffops'] == [[0, 0, [toi1]]]
def jslink(): jslink = JsLink(linkFactory=link.LinkFactory()) with commit.CommitContext(self.database): result = jslink.render(flask.request) return result
def setup_method(self, method): super(TestLinkPersistence, self).setup_method(method) self.factory = Link.LinkFactory() self.factory.Transient = LinkTransient self.factory.Persistent = LinkPersistent self.pushnewctx(commit.CommitContext, user=self.user)
def test_SortedQuery_timing_error(self, monkeypatch): # this test is nearly identical to # test_SortedQuery_rerun_optimization but sneakily modifies # the link's uptodate state while the link is running # # (it is possible that this is now a strict superset of # test_SortedQuery_rerun_optimization and if so we might want # to consider merging them) from blm import testblm oid1 = ObjectId() toi1 = self.ctx.createToi(blm.testblm.Test, oid1, {'name': ['foobar']}) oid2 = ObjectId() toi2 = self.ctx.createToi(blm.testblm.Test, oid2, {'name': ['gazonk']}) self.sync() query = blm.testblm.Test._query(name=Query.Like('foo*')) link = Link.LinkSortedQuery( self.clientId, self.linkId, { 'criteria': query, 'attrList': ['name'], 'subscription': True, 'sorting': 'name' }) link.run() r = self._getResult() assert list(r['toiDiffs'].keys()) == [str(oid1)] # sanity cid1 = ObjectId() self.ctx.changeToi(toi2, {'name': ['foooo']}) mongo.update_one(self.database.links, { 'client': self.clientId, 'link': self.linkId }, {'$set': { 'outdatedBy': cid1, 'outdatedToids': [oid2] }}) self.sync() link = Link.LinkFactory().create(None, self.clientId, self.linkId) runQuery = self.ctx.runQuery queries = [] cid2 = ObjectId() def _runQuery(query): queries.append(query) result = list(runQuery(query)) mongo.update_one(self.database.links, { 'client': self.clientId, 'link': self.linkId }, { '$set': { 'outdatedBy': cid2 }, '$addToSet': { 'outdatedToids': oid1 } }) return result monkeypatch.setattr(self.ctx, 'runQuery', _runQuery) link.run() r = self._getResult() assert r['diffops'] == [[1, 1, [toi2]]] assert r['toiDiffs'][str(oid2)].diffAttrs == {'name': ['foooo']} assert queries == [blm.testblm.Test._query(id=[oid2])] # whitebox self.sync() assert not self.uptodate() assert self._getLinkData()['outdatedBy'] == cid2 assert set(self._getLinkData()['outdatedToids']) == {oid1, oid2} monkeypatch.undo() link = Link.LinkFactory().create(None, self.clientId, self.linkId) link.run() self.sync() assert self.uptodate() assert self._getLinkData()['outdatedBy'] == None assert self._getLinkData()['outdatedToids'] == []