Exemple #1
0
 def test_simple_query(self):
     query = Query(Author)
     where = query.build_where(Q(num__gt=2))
     lookup = where.children[0]
     self.assertIsInstance(lookup, GreaterThan)
     self.assertEqual(lookup.rhs, 2)
     self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))
Exemple #2
0
 def test_multiple_fields(self):
     query = Query(Item)
     where = query.build_where(Q(modified__gt=F('created')))
     lookup = where.children[0]
     self.assertIsInstance(lookup, GreaterThan)
     self.assertIsInstance(lookup.rhs, SimpleCol)
     self.assertIsInstance(lookup.lhs, SimpleCol)
     self.assertEqual(lookup.rhs.target, Item._meta.get_field('created'))
     self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
Exemple #3
0
 def test_negated_nullable(self):
     query = Query(Item)
     where = query.build_where(~Q(modified__lt=datetime(2017, 1, 1)))
     self.assertTrue(where.negated)
     lookup = where.children[0]
     self.assertIsInstance(lookup, LessThan)
     self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
     lookup = where.children[1]
     self.assertIsInstance(lookup, IsNull)
     self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
Exemple #4
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model)
     where = query.build_where(self.constraint)
     connection = schema_editor.connection
     compiler = connection.ops.compiler('SQLCompiler')(query, connection,
                                                       'default')
     sql, params = where.as_sql(compiler, connection)
     params = tuple(schema_editor.quote_value(p) for p in params)
     return schema_editor.sql_check % {
         'name': schema_editor.quote_name(self.name),
         'check': sql % params,
     }
Exemple #5
0
 def test_transform(self):
     query = Query(Author)
     CharField.register_lookup(Lower, 'lower')
     try:
         where = query.build_where(~Q(name__lower='foo'))
     finally:
         CharField._unregister_lookup(Lower, 'lower')
     lookup = where.children[0]
     self.assertIsInstance(lookup, Exact)
     self.assertIsInstance(lookup.lhs, Lower)
     self.assertIsInstance(lookup.lhs.lhs, SimpleCol)
     self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field('name'))
Exemple #6
0
 def test_foreign_key_exclusive(self):
     query = Query(ObjectC)
     where = query.build_where(Q(objecta=None) | Q(objectb=None))
     a_isnull = where.children[0]
     self.assertIsInstance(a_isnull, RelatedIsNull)
     self.assertIsInstance(a_isnull.lhs, SimpleCol)
     self.assertEqual(a_isnull.lhs.target,
                      ObjectC._meta.get_field('objecta'))
     b_isnull = where.children[1]
     self.assertIsInstance(b_isnull, RelatedIsNull)
     self.assertIsInstance(b_isnull.lhs, SimpleCol)
     self.assertEqual(b_isnull.lhs.target,
                      ObjectC._meta.get_field('objectb'))
Exemple #7
0
    def test_complex_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2) | Q(num__lt=0))
        self.assertEqual(where.connector, OR)

        lookup = where.children[0]
        self.assertIsInstance(lookup, GreaterThan)
        self.assertEqual(lookup.rhs, 2)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))

        lookup = where.children[1]
        self.assertIsInstance(lookup, LessThan)
        self.assertEqual(lookup.rhs, 0)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))
Exemple #8
0
 def test_foreign_key_f(self):
     query = Query(Ranking)
     with self.assertRaises(FieldError):
         query.build_where(Q(rank__gt=F('author__num')))
Exemple #9
0
 def test_foreign_key(self):
     query = Query(Item)
     msg = 'Joined field references are not permitted in this query'
     with self.assertRaisesMessage(FieldError, msg):
         query.build_where(Q(creator__num__gt=2))