示例#1
0
 def setUp(self):
     compiler = Person.objects.all().query.get_compiler(connection.alias)
     self.editor = connection.schema_editor()
     self.expressions = Expressions(
         table=Person._meta.db_table,
         expressions=ExpressionList(
             IndexExpression(F('first_name')),
             IndexExpression(F('last_name').desc()),
             IndexExpression(Upper('last_name')),
         ).resolve_expression(compiler.query),
         compiler=compiler,
         quote_value=self.editor.quote_value,
     )
示例#2
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model, alias_cols=False)
     compiler = query.get_compiler(connection=schema_editor.connection)
     expressions = self._get_expressions(schema_editor, query)
     table = model._meta.db_table
     condition = self._get_condition_sql(compiler, schema_editor, query)
     include = [
         model._meta.get_field(field_name).column for field_name in self.include
     ]
     return Statement(
         self.template,
         table=Table(table, schema_editor.quote_name),
         name=schema_editor.quote_name(self.name),
         index_type=self.index_type,
         expressions=Expressions(
             table, expressions, compiler, schema_editor.quote_value
         ),
         where=" WHERE (%s)" % condition if condition else "",
         include=schema_editor._index_include_sql(model, include),
         deferrable=schema_editor._deferrable_constraint_sql(self.deferrable),
     )
示例#3
0
 def test_rename_table_references_without_alias(self):
     compiler = Query(Person,
                      alias_cols=False).get_compiler(connection=connection)
     table = Person._meta.db_table
     expressions = Expressions(
         table=table,
         expressions=ExpressionList(
             IndexExpression(Upper('last_name')),
             IndexExpression(F('first_name')),
         ).resolve_expression(compiler.query),
         compiler=compiler,
         quote_value=self.editor.quote_value,
     )
     expressions.rename_table_references(table, 'other')
     self.assertIs(expressions.references_table(table), False)
     self.assertIs(expressions.references_table('other'), True)
     expected_str = '(UPPER(%s)), %s' % (
         self.editor.quote_name('last_name'),
         self.editor.quote_name('first_name'),
     )
     self.assertEqual(str(expressions), expected_str)
示例#4
0
class ExpressionsTests(TransactionTestCase):
    available_apps = []

    def setUp(self):
        compiler = Person.objects.all().query.get_compiler(connection.alias)
        self.editor = connection.schema_editor()
        self.expressions = Expressions(
            table=Person._meta.db_table,
            expressions=ExpressionList(
                IndexExpression(F('first_name')),
                IndexExpression(F('last_name').desc()),
                IndexExpression(Upper('last_name')),
            ).resolve_expression(compiler.query),
            compiler=compiler,
            quote_value=self.editor.quote_value,
        )

    def test_references_table(self):
        self.assertIs(self.expressions.references_table(Person._meta.db_table),
                      True)
        self.assertIs(self.expressions.references_table('other'), False)

    def test_references_column(self):
        table = Person._meta.db_table
        self.assertIs(self.expressions.references_column(table, 'first_name'),
                      True)
        self.assertIs(self.expressions.references_column(table, 'last_name'),
                      True)
        self.assertIs(self.expressions.references_column(table, 'other'),
                      False)

    def test_rename_table_references(self):
        table = Person._meta.db_table
        self.expressions.rename_table_references(table, 'other')
        self.assertIs(self.expressions.references_table(table), False)
        self.assertIs(self.expressions.references_table('other'), True)
        self.assertIn(
            '%s.%s' % (
                self.editor.quote_name('other'),
                self.editor.quote_name('first_name'),
            ),
            str(self.expressions),
        )

    def test_rename_table_references_without_alias(self):
        compiler = Query(Person,
                         alias_cols=False).get_compiler(connection=connection)
        table = Person._meta.db_table
        expressions = Expressions(
            table=table,
            expressions=ExpressionList(
                IndexExpression(Upper('last_name')),
                IndexExpression(F('first_name')),
            ).resolve_expression(compiler.query),
            compiler=compiler,
            quote_value=self.editor.quote_value,
        )
        expressions.rename_table_references(table, 'other')
        self.assertIs(expressions.references_table(table), False)
        self.assertIs(expressions.references_table('other'), True)
        expected_str = '(UPPER(%s)), %s' % (
            self.editor.quote_name('last_name'),
            self.editor.quote_name('first_name'),
        )
        self.assertEqual(str(expressions), expected_str)

    def test_rename_column_references(self):
        table = Person._meta.db_table
        self.expressions.rename_column_references(table, 'first_name', 'other')
        self.assertIs(self.expressions.references_column(table, 'other'), True)
        self.assertIs(self.expressions.references_column(table, 'first_name'),
                      False)
        self.assertIn(
            '%s.%s' %
            (self.editor.quote_name(table), self.editor.quote_name('other')),
            str(self.expressions),
        )

    def test_str(self):
        table_name = self.editor.quote_name(Person._meta.db_table)
        expected_str = '%s.%s, %s.%s DESC, (UPPER(%s.%s))' % (
            table_name,
            self.editor.quote_name('first_name'),
            table_name,
            self.editor.quote_name('last_name'),
            table_name,
            self.editor.quote_name('last_name'),
        )
        self.assertEqual(str(self.expressions), expected_str)