from unittest import TestCase

from .schema import DatabaseSchemaEditor


class SqlUniqueDeferrableForSortOrderTestCase(TestCase):

    @staticmethod
    def quote_name(column):
        """Simulate the ``quote_name`` from the default schema editor."""
        return '"%s"' % column

    def test_default_interpolation_for_create_unique_with_one_column(self):
        """Test that the it returns the correct result with one column ("sort_order")."""

        sql = DatabaseSchemaEditor.sql_create_unique % {
            'table': self.quote_name('my_table'),
            'name': self.quote_name('my_index'),
            'columns': ", ".join(self.quote_name(column) for column in ['sort_order']),
        }
        self.assertEqual(sql, 'ALTER TABLE "my_table" ADD CONSTRAINT "my_index" '
                              'UNIQUE ("sort_order") DEFERRABLE INITIALLY DEFERRED')

    def test_default_interpolation_for_create_unique_with_many_columns(self):
        """Test that the it returns the correct result with many columns (one is "sort_order")."""

        sql = DatabaseSchemaEditor.sql_create_unique % {
            'table': self.quote_name('my_table'),
            'name': self.quote_name('my_index'),
            'columns': ", ".join(self.quote_name(column) for column in ['foo', 'sort_order']),
        }
        self.assertEqual(sql, 'ALTER TABLE "my_table" ADD CONSTRAINT "my_index" '
                              'UNIQUE ("foo", "sort_order") DEFERRABLE INITIALLY DEFERRED')
