コード例 #1
0
 def test_filter_conditional(self):
     query = Query(Item)
     where = query.build_where(Func(output_field=BooleanField()))
     exact = where.children[0]
     self.assertIsInstance(exact, Exact)
     self.assertIsInstance(exact.lhs, Func)
     self.assertIs(exact.rhs, True)
コード例 #2
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'))
コード例 #3
0
ファイル: constraints.py プロジェクト: pope1ni/django
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return None
     query = Query(model=model, alias_cols=False)
     where = query.build_where(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     sql, params = where.as_sql(compiler, schema_editor.connection)
     return sql % tuple(schema_editor.quote_value(p) for p in params)
コード例 #4
0
ファイル: constraints.py プロジェクト: zzz-i2p/django-1
 def constraint_sql(self, model, schema_editor):
     query = Query(model)
     where = query.build_where(self.check)
     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_constraint % {'check': sql % params}
コード例 #5
0
ファイル: constraints.py プロジェクト: EmadMokhtar/django
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return None
     query = Query(model=model)
     where = query.build_where(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     sql, params = where.as_sql(compiler, schema_editor.connection)
     return sql % tuple(schema_editor.quote_value(p) for p in params)
コード例 #6
0
 def test_transform(self):
     query = Query(Author)
     with register_lookup(CharField, Lower):
         where = query.build_where(~Q(name__lower='foo'))
     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'))
コード例 #7
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'))
コード例 #8
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'))
コード例 #9
0
ファイル: test_query.py プロジェクト: ronnievdc/django
 def test_transform(self):
     query = Query(Author, alias_cols=False)
     with register_lookup(CharField, Lower):
         where = query.build_where(~Q(name__lower="foo"))
     lookup = where.children[0]
     self.assertIsInstance(lookup, Exact)
     self.assertIsInstance(lookup.lhs, Lower)
     self.assertIsInstance(lookup.lhs.lhs, Col)
     self.assertIsNone(lookup.lhs.lhs.alias)
     self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field("name"))
コード例 #10
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'))
コード例 #11
0
ファイル: test_query.py プロジェクト: ronnievdc/django
 def test_multiple_fields(self):
     query = Query(Item, alias_cols=False)
     where = query.build_where(Q(modified__gt=F("created")))
     lookup = where.children[0]
     self.assertIsInstance(lookup, GreaterThan)
     self.assertIsInstance(lookup.rhs, Col)
     self.assertIsNone(lookup.rhs.alias)
     self.assertIsInstance(lookup.lhs, Col)
     self.assertIsNone(lookup.lhs.alias)
     self.assertEqual(lookup.rhs.target, Item._meta.get_field("created"))
     self.assertEqual(lookup.lhs.target, Item._meta.get_field("modified"))
コード例 #12
0
ファイル: test_query.py プロジェクト: tbbug/django
 def test_foreign_key_exclusive(self):
     query = Query(ObjectC, alias_cols=False)
     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, Col)
     self.assertIsNone(a_isnull.lhs.alias)
     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, Col)
     self.assertIsNone(b_isnull.lhs.alias)
     self.assertEqual(b_isnull.lhs.target, ObjectC._meta.get_field("objectb"))
コード例 #13
0
ファイル: test_query.py プロジェクト: MattBlack85/django
    def test_simplecol_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2, name__isnull=False) | Q(num__lt=F('id')))

        name_isnull_lookup, num_gt_lookup = where.children[0].children
        self.assertIsInstance(num_gt_lookup, GreaterThan)
        self.assertIsInstance(num_gt_lookup.lhs, SimpleCol)
        self.assertIsInstance(name_isnull_lookup, IsNull)
        self.assertIsInstance(name_isnull_lookup.lhs, SimpleCol)

        num_lt_lookup = where.children[1]
        self.assertIsInstance(num_lt_lookup, LessThan)
        self.assertIsInstance(num_lt_lookup.rhs, SimpleCol)
        self.assertIsInstance(num_lt_lookup.lhs, SimpleCol)
コード例 #14
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'))
コード例 #15
0
ファイル: test_query.py プロジェクト: evitla/django-1
    def test_simplecol_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2, name__isnull=False) | Q(num__lt=F('id')))

        name_isnull_lookup, num_gt_lookup = where.children[0].children
        self.assertIsInstance(num_gt_lookup, GreaterThan)
        self.assertIsInstance(num_gt_lookup.lhs, SimpleCol)
        self.assertIsInstance(name_isnull_lookup, IsNull)
        self.assertIsInstance(name_isnull_lookup.lhs, SimpleCol)

        num_lt_lookup = where.children[1]
        self.assertIsInstance(num_lt_lookup, LessThan)
        self.assertIsInstance(num_lt_lookup.rhs, SimpleCol)
        self.assertIsInstance(num_lt_lookup.lhs, SimpleCol)
コード例 #16
0
ファイル: test_query.py プロジェクト: tbbug/django
    def test_non_alias_cols_query(self):
        query = Query(Author, alias_cols=False)
        where = query.build_where(Q(num__gt=2, name__isnull=False) | Q(num__lt=F("id")))

        name_isnull_lookup, num_gt_lookup = where.children[0].children
        self.assertIsInstance(num_gt_lookup, GreaterThan)
        self.assertIsInstance(num_gt_lookup.lhs, Col)
        self.assertIsNone(num_gt_lookup.lhs.alias)
        self.assertIsInstance(name_isnull_lookup, IsNull)
        self.assertIsInstance(name_isnull_lookup.lhs, Col)
        self.assertIsNone(name_isnull_lookup.lhs.alias)

        num_lt_lookup = where.children[1]
        self.assertIsInstance(num_lt_lookup, LessThan)
        self.assertIsInstance(num_lt_lookup.rhs, Col)
        self.assertIsNone(num_lt_lookup.rhs.alias)
        self.assertIsInstance(num_lt_lookup.lhs, Col)
        self.assertIsNone(num_lt_lookup.lhs.alias)
コード例 #17
0
 def test_iterable_lookup_value(self):
     query = Query(Item)
     where = query.build_where(Q(name=['a', 'b']))
     name_exact = where.children[0]
     self.assertIsInstance(name_exact, Exact)
     self.assertEqual(name_exact.rhs, "['a', 'b']")
コード例 #18
0
 def test_foreign_key_f(self):
     query = Query(Ranking)
     with self.assertRaises(FieldError):
         query.build_where(Q(rank__gt=F('author__num')))
コード例 #19
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))
コード例 #20
0
 def test_filter_conditional_join(self):
     query = Query(Item)
     filter_expr = Func('note__note', output_field=BooleanField())
     msg = 'Joined field references are not permitted in this query'
     with self.assertRaisesMessage(FieldError, msg):
         query.build_where(filter_expr)
コード例 #21
0
 def test_filter_non_conditional(self):
     query = Query(Item)
     msg = 'Cannot filter against a non-conditional expression.'
     with self.assertRaisesMessage(TypeError, msg):
         query.build_where(Func(output_field=CharField()))
コード例 #22
0
 def _get_check_sql(self, model, schema_editor):
     query = Query(model=model)
     where = query.build_where(self.check)
     compiler = query.get_compiler(connection=schema_editor.connection)
     sql, params = where.as_sql(compiler, schema_editor.connection)
     return sql % tuple(schema_editor.quote_value(p) for p in params)