def filter(self, *arguments, **kwargs): """ Filters a queryset in order to produce a different set of document from subsequent queries. Usage:: await User.objects.filter(first_name="Bernardo").filter(last_name="Bernardo").find_all() # or await User.objects.filter(first_name="Bernardo", starting_year__gt=2010).find_all() The available filter options are the same as used in MongoEngine. """ from jetengine.query_builder.node import Q, QCombination, QNot from jetengine.query_builder.transform import validate_fields if arguments and len(arguments) == 1 and isinstance( arguments[0], (Q, QNot, QCombination)): if self._filters: self._filters = self._filters & arguments[0] else: self._filters = arguments[0] else: validate_fields(self.__klass__, kwargs) if self._filters: self._filters = self._filters & Q(**kwargs) else: if arguments and len(arguments) == 1 and isinstance( arguments[0], dict): self._filters = Q(arguments[0]) else: self._filters = Q(**kwargs) return self
def test_gets_proper_query_when_many_operators_with_and_first(self): query = Q(last_name="Whatever") & ( (Q(embedded__test__lte="Test") & Q(first_name="Someone")) | Q(first_name="Else")) query_result = query.to_query(User) expect(query_result).to_be_like({ "$and": [ { "last_name": "Whatever" }, { "$or": [ { "embedded_document.other": { "$lte": "Test" }, "whatever": "Someone" }, { "whatever": "Else" }, ] }, ] })
def test_gets_proper_query_when_and(self): query = Q(embedded__test__lte="Test") & Q(first_name="Someone") query_result = query.to_query(User) expect(query_result).to_be_like({ "embedded_document.other": { "$lte": "Test" }, "whatever": "Someone" })
def test_gets_proper_query_when_or(self): query = Q(first_name="Someone") | Q(first_name="Else") query_result = query.to_query(User) expect(query_result).to_be_like( {"$or": [{ "whatever": "Someone" }, { "whatever": "Else" }]})
def test_can_query_using_not_in_with_real_users(self): yield from self.create_test_users() names = ["Someone", "John"] query = ~Q(first_name__in=names) & Q(last_name="Heynemann") users = yield from User.objects.filter(query).find_all() expect(users).to_length(1) expect(users[0].first_name).to_equal("Bernardo")
def test_can_query_using_q_with_is_null(self): yield from self.create_test_users() users = yield from User.objects.filter( Q(nullable__is_null=True)).find_all() expect(users).to_length(1) expect(users[0]._id).to_equal(self.user._id) users = yield from User.objects.filter( Q(nullable__is_null=False)).find_all() expect(users).to_length(2) expect(users[0]._id).to_equal(self.user2._id) expect(users[1]._id).to_equal(self.user3._id)
def test_can_query_using_q(self): yield from self.create_test_users() users = yield from User.objects.find_all() expect(users).to_length(3) users = yield from User.objects.filter( Q(first_name="Bernardo")).find_all() expect(users).to_length(1) users = yield from User.objects.filter( Q(first_name="Bernardo") | Q(first_name="Someone")).find_all() expect(users).to_length(2)
def test_gets_proper_query_when_embedded_document(self): query = Q(embedded__test__lte="Test") query_result = query.to_query(User) expect(query_result).to_be_like( {"embedded_document.other": { "$lte": "Test" }})
def test_gets_proper_query_when_embedded_document_in_many_levels(self): query = Q(embedded__embedded2__test__lte="Test") query_result = query.to_query(User) expect(query_result).to_be_like( {"embedded_document.embedded2.else": { "$lte": "Test" }})
def test_can_find_with_multiple_filters(self): yield from User.objects.create(email="*****@*****.**", first_name="Bernardo", last_name="Heynemann") yield from User.objects.create(email="*****@*****.**", first_name="Someone", last_name="Else") user = yield from User.objects.create(email="*****@*****.**", first_name="Bernardo", last_name="Heynemann") last_user = yield from User.objects.create(email="*****@*****.**", first_name="Bernardo", last_name="Silva") # filter and filter not users = yield from User.objects.filter( email="*****@*****.**").filter_not( first_name="Someone").find_all() expect(users).to_be_instance_of(list) expect(users).to_length(1) first_user = users[0] expect(first_user._id).to_equal(user._id) # filter and filter not for Q from jetengine import Q users = yield from User.objects.filter( email="*****@*****.**").filter_not( Q(first_name="Someone")).find_all() expect(users).to_be_instance_of(list) expect(users).to_length(1) first_user = users[0] expect(first_user._id).to_equal(user._id) # filter not and filter not users = yield from User.objects.filter_not( last_name="Heynemann").filter_not(first_name="Someone").find_all() expect(users).to_be_instance_of(list) expect(users).to_length(1) first_user = users[0] expect(first_user._id).to_equal(last_user._id) # filter and filter users = yield from User.objects.filter(last_name="Silva").filter( first_name="Bernardo").find_all() expect(users).to_be_instance_of(list) expect(users).to_length(1) expect(users[0]._id).to_equal(last_user._id)
def test_can_query_using_q_for_list(self): yield from User.objects.create( email="*****@*****.**", first_name="Bernardo", last_name="Heynemann", embedded=EmbeddedDocument(test="test"), numbers=[1, 2, 3], ) user2 = yield from User.objects.create( email="*****@*****.**", first_name="Someone", last_name="Else", embedded=EmbeddedDocument(test="test2"), numbers=[4, 5, 6], ) yield from User.objects.create( email="*****@*****.**", first_name="John", last_name="Doe", embedded=EmbeddedDocument(test="test3"), numbers=[7, 8, 9], ) users = yield from User.objects.filter(Q(numbers=[4])).find_all() expect(users).to_length(1) expect(users[0]._id).to_equal(user2._id) users = yield from User.objects.filter(Q(numbers=[5])).find_all() expect(users).to_length(1) expect(users[0]._id).to_equal(user2._id) users = yield from User.objects.filter(Q(numbers=[6])).find_all() expect(users).to_length(1) expect(users[0]._id).to_equal(user2._id) users = yield from User.objects.filter(Q(numbers=[4, 5, 6])).find_all() expect(users).to_length(1) expect(users[0]._id).to_equal(user2._id) users = yield from User.objects.filter(Q(numbers=[20])).find_all() expect(users).to_length(0)
def to_query(self): from jetengine import Q match_obj = {"$match": {}} query = self.aggregation.queryset.get_query_from_filters( Q(**self.filters)) update(match_obj["$match"], query) return match_obj
def test_can_query_using_not_in(self): names = ["Someone", "John"] query = ~Q(first_name__in=names) query_result = query.to_query(User) expect(query_result).to_be_like( {"whatever": { "$not": { "$in": ["Someone", "John"] } }})
def filter_not(self, *arguments, **kwargs): """ Filters a queryset to negate all the filters passed in subsequent queries. Usage:: await User.objects.filter_not(first_name="Bernardo").filter_not(last_name="Bernardo").find_all() # or await User.objects.filter_not(first_name="Bernardo", starting_year__gt=2010).find_all() The available filter options are the same as used in MongoEngine. """ from jetengine.query_builder.node import Q, QCombination, QNot if arguments and len(arguments) == 1 and isinstance( arguments[0], (Q, QCombination)): self.filter(QNot(arguments[0])) else: self.filter(QNot(Q(**kwargs))) return self
async def get(self, id=None, alias=None, **kwargs): """ Gets a single item of the current queryset collection using it's id. In order to query a different database, please specify the `alias` of the database to query. """ from jetengine import Q if id is None and not kwargs: raise RuntimeError( "Either an id or a filter must be provided to get") if id is not None: if not isinstance(id, ObjectId): id = ObjectId(id) filters = {"_id": id} else: filters = Q(**kwargs) filters = self.get_query_from_filters(filters) instance = await self.coll(alias).find_one( filters, projection=self._loaded_fields.to_query(self.__klass__)) if instance is None: return None else: doc = self.__klass__.from_son( instance, # if _loaded_fields is not empty then # document is partly loaded _is_partly_loaded=bool(self._loaded_fields), # set projections for references (if any) _reference_loaded_fields=self._reference_loaded_fields, ) if self.is_lazy: return doc else: await doc.load_references() return doc
def test_gets_proper_query_when_query_operator_used(self): query = Q(first_name__lte="Test") query_result = query.to_query(User) expect(query_result).to_be_like({"whatever": {"$lte": "Test"}})
def test_gets_proper_query_when_list_field(self): query = Q(numbers=[10]) query_result = query.to_query(User) expect(query_result).to_be_like({"numbers": {"$all": [10]}})
def test_gets_proper_type(self): query = Q(first_name="Test") expect(query).to_be_instance_of(Q) query = Q(first_name="Test") | Q(first_name="Else") expect(query).to_be_instance_of(QCombination)
def test_gets_proper_query(self): query = Q(first_name="Test") query_result = query.to_query(User) expect(query_result).to_be_like({"whatever": "Test"})