def testGqlFilter(self): qry, options, bindings = query.parse_gql( "SELECT * FROM Kind WHERE prop1 = 1 AND prop2 = 'a'") self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, None) self.assertEqual(qry.filters, query.ConjunctionNode( [query.FilterNode('prop1', '=', 1), query.FilterNode('prop2', '=', 'a')])) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {})
def testAndQuery(self): class Employee(model.Model): name = model.StringProperty() age = model.IntegerProperty('Age') rank = model.IntegerProperty() q = Employee.query().filter(query.AND(Employee.age >= 42)) self.assertEqual(q.filters, query.FilterNode('Age', '>=', 42)) q = Employee.query(query.AND(Employee.age >= 42, Employee.rank <= 5)) self.assertEqual(q.filters, query.ConjunctionNode( query.FilterNode('Age', '>=', 42), query.FilterNode('rank', '<=', 5)))
def testGqlBindings(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind WHERE prop1 = :1 AND prop2 = :foo') self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, None) self.assertEqual(qry.filters, query.ConjunctionNode( [query.FilterNode('prop1', '=', query.Binding(None, 1)), query.FilterNode('prop2', '=', query.Binding(None, 'foo'))])) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {1: query.Binding(None, 1), 'foo': query.Binding(None, 'foo')})
def testModernQuerySyntax(self): class Employee(model.Model): name = model.StringProperty() age = model.IntegerProperty('Age') rank = model.IntegerProperty() @classmethod def seniors(cls, min_age, min_rank): q = cls.query().filter(cls.age >= min_age, cls.rank <= min_rank) q = q.order(cls.name, -cls.age) return q q = Employee.seniors(42, 5) self.assertEqual(q.filters, query.ConjunctionNode( [query.FilterNode('Age', '>=', 42), query.FilterNode('rank', '<=', 5)])) self.assertEqual(query.orders_to_orderings(q.orders), [('name', query.ASC), ('Age', query.DESC)])