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_decode_simple_query(self): data = json.dumps({ '_cls_': 'Query', 'toc': 'Foo', 'cgs': [{ '_cls_': 'ConditionGroup', 'a': [{ '_cls_': 'Like', 'value': 'abc*' }], 'b': [{ '_cls_': 'Between', 'value': [1, 2] }] }] }) decoded = self.decoder.decode(data) query = q.Query('Foo', a=q.Like('abc*'), b=q.Between(1, 2)) assert decoded == query
def test_Like(self): op = q.Like('foo*bar') mongo = op.mongo() assert mongo == {'$regex': comp_re('^foo.*bar$')}
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'] == []
def test_Like(): "Test Like operator" assert q.Like('foo*').matches(['foobar', 'xyzzy']) assert not q.Like('foo*').matches(['barfoo', 'xyzzy'])