def test_special_case_op(self): query = q.Query(self.toc) query.clear() query.pushDict({'foo': [q.In([27, 42]), q.Empty()]}) mongo = query.mongo() # xxx Empty overrides In assert mongo == {'_bases': {'$in': [self.toc._fullname]}, 'foo': {'$in': [None, [], {}]}}
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 runQuery(self, query): if not isSuperUser(self.user): query = query.copy() privileges = self.user._privileges.value op = Query.In(privileges) if len(query): for cg in query: cg[query.toc.allowRead] = [op] else: query.pushDict({query.toc.allowRead: [op]}) mongo_query = query.mongo() key = Query.freeze(mongo_query) try: attrList, result = self._query_cache[key] except KeyError: attrList = set() else: if attrList >= set(query.attrList): return result results = [] fields = set(query.attrList) | attrList for doc in mongo.run_query(self.database, mongo_query, projection=fields): toc = getTocByFullname(doc.pop('_toc')) id = doc.pop('_id') for attrName in fields: if attrName not in doc and attrName in toc._attributes: doc[attrName] = copy.copy( toc._attributes[attrName].default) toi = toc._create(id, kw=doc) toi._phantom = False results.append(toi) self._query_cache[key] = fields, results return results
def test_InSingleValue(): "Test In operator for single values" assert q.In(42).matches([42]) assert not q.In(42).matches([41])
def test_In(self): op = q.In([27, 42]) mongo = op.mongo() assert mongo == {'$in': uolist([27, 42])}
def test_In(): "Test In operator" assert q.In([41, 42]).matches([1, 2, 41]) assert q.In([41, 42]).matches([1, 2, 42]) assert not q.In([41, 42]).matches([1, 2, 43])