コード例 #1
0
ファイル: tests.py プロジェクト: DasAllFolks/django
 def test_primary_key(self):
     """
     Tests altering of the primary key
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Tag)
     # Ensure the table is there and has the right PK
     self.assertTrue(
         self.get_indexes(Tag._meta.db_table)['id']['primary_key'],
     )
     # Alter to change the PK
     new_field = SlugField(primary_key=True)
     new_field.set_attributes_from_name("slug")
     new_field.model = Tag
     with connection.schema_editor() as editor:
         editor.remove_field(Tag, Tag._meta.get_field_by_name("id")[0])
         editor.alter_field(
             Tag,
             Tag._meta.get_field_by_name("slug")[0],
             new_field,
         )
     # Ensure the PK changed
     self.assertNotIn(
         'id',
         self.get_indexes(Tag._meta.db_table),
     )
     self.assertTrue(
         self.get_indexes(Tag._meta.db_table)['slug']['primary_key'],
     )
コード例 #2
0
ファイル: tests.py プロジェクト: trught007/django
 def test_primary_key(self):
     """
     Tests altering of the primary key
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Tag)
     # Ensure the table is there and has the right PK
     self.assertTrue(
         connection.introspection.get_indexes(
             connection.cursor(),
             Tag._meta.db_table)['id']['primary_key'], )
     # Alter to change the PK
     new_field = SlugField(primary_key=True)
     new_field.set_attributes_from_name("slug")
     new_field.model = Tag
     with connection.schema_editor() as editor:
         editor.remove_field(Tag, Tag._meta.get_field_by_name("id")[0])
         editor.alter_field(
             Tag,
             Tag._meta.get_field_by_name("slug")[0],
             new_field,
         )
     # Ensure the PK changed
     self.assertNotIn(
         'id',
         connection.introspection.get_indexes(connection.cursor(),
                                              Tag._meta.db_table),
     )
     self.assertTrue(
         connection.introspection.get_indexes(
             connection.cursor(),
             Tag._meta.db_table)['slug']['primary_key'], )
コード例 #3
0
ファイル: tests.py プロジェクト: Wilfred/django
 def test_unique_together(self):
     """
     Tests removing and adding unique_together constraints on a model.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(UniqueTest)
     # Ensure the fields are unique to begin with
     UniqueTest.objects.create(year=2012, slug="foo")
     UniqueTest.objects.create(year=2011, slug="foo")
     UniqueTest.objects.create(year=2011, slug="bar")
     self.assertRaises(IntegrityError, UniqueTest.objects.create, year=2012, slug="foo")
     UniqueTest.objects.all().delete()
     # Alter the model to it's non-unique-together companion
     with connection.schema_editor() as editor:
         editor.alter_unique_together(UniqueTest, UniqueTest._meta.unique_together, [])
     # Ensure the fields are no longer unique
     UniqueTest.objects.create(year=2012, slug="foo")
     UniqueTest.objects.create(year=2012, slug="foo")
     UniqueTest.objects.all().delete()
     # Alter it back
     new_new_field = SlugField(unique=True)
     new_new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_unique_together(UniqueTest, [], UniqueTest._meta.unique_together)
     # Ensure the fields are unique again
     UniqueTest.objects.create(year=2012, slug="foo")
     self.assertRaises(IntegrityError, UniqueTest.objects.create, year=2012, slug="foo")
     UniqueTest.objects.all().delete()
コード例 #4
0
 def test_index_together(self):
     """
     Tests removing and adding index_together constraints on a model.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Tag)
     # Ensure there's no index on the year/slug columns first
     self.assertEqual(
         False,
         any(c["index"] for c in self.get_constraints("schema_tag").values() if c["columns"] == ["slug", "title"]),
     )
     # Alter the model to add an index
     with connection.schema_editor() as editor:
         editor.alter_index_together(Tag, [], [("slug", "title")])
     # Ensure there is now an index
     self.assertEqual(
         True,
         any(c["index"] for c in self.get_constraints("schema_tag").values() if c["columns"] == ["slug", "title"]),
     )
     # Alter it back
     new_new_field = SlugField(unique=True)
     new_new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_index_together(Tag, [("slug", "title")], [])
     # Ensure there's no index
     self.assertEqual(
         False,
         any(c["index"] for c in self.get_constraints("schema_tag").values() if c["columns"] == ["slug", "title"]),
     )
コード例 #5
0
ファイル: tests.py プロジェクト: Swetachaudhary8/django
 def test_index_together(self):
     """
     Tests removing and adding index_together constraints on a model.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Tag)
     # Ensure there's no index on the year/slug columns first
     self.assertEqual(
         False,
         any(
             c["index"]
             for c in self.get_constraints("schema_tag").values()
             if c['columns'] == ["slug", "title"]
         ),
     )
     # Alter the model to add an index
     with connection.schema_editor() as editor:
         editor.alter_index_together(
             Tag,
             [],
             [("slug", "title")],
         )
     # Ensure there is now an index
     self.assertEqual(
         True,
         any(
             c["index"]
             for c in self.get_constraints("schema_tag").values()
             if c['columns'] == ["slug", "title"]
         ),
     )
     # Alter it back
     new_new_field = SlugField(unique=True)
     new_new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_index_together(
             Tag,
             [("slug", "title")],
             [],
         )
     # Ensure there's no index
     self.assertEqual(
         False,
         any(
             c["index"]
             for c in self.get_constraints("schema_tag").values()
             if c['columns'] == ["slug", "title"]
         ),
     )
コード例 #6
0
ファイル: tests.py プロジェクト: trught007/django
 def test_unique_together(self):
     """
     Tests removing and adding unique_together constraints on a model.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(UniqueTest)
     # Ensure the fields are unique to begin with
     UniqueTest.objects.create(year=2012, slug="foo")
     UniqueTest.objects.create(year=2011, slug="foo")
     UniqueTest.objects.create(year=2011, slug="bar")
     self.assertRaises(IntegrityError,
                       UniqueTest.objects.create,
                       year=2012,
                       slug="foo")
     UniqueTest.objects.all().delete()
     # Alter the model to it's non-unique-together companion
     with connection.schema_editor() as editor:
         editor.alter_unique_together(
             UniqueTest,
             UniqueTest._meta.unique_together,
             [],
         )
     # Ensure the fields are no longer unique
     UniqueTest.objects.create(year=2012, slug="foo")
     UniqueTest.objects.create(year=2012, slug="foo")
     UniqueTest.objects.all().delete()
     # Alter it back
     new_new_field = SlugField(unique=True)
     new_new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_unique_together(
             UniqueTest,
             [],
             UniqueTest._meta.unique_together,
         )
     # Ensure the fields are unique again
     UniqueTest.objects.create(year=2012, slug="foo")
     self.assertRaises(IntegrityError,
                       UniqueTest.objects.create,
                       year=2012,
                       slug="foo")
     UniqueTest.objects.all().delete()
コード例 #7
0
ファイル: tests.py プロジェクト: DasAllFolks/django
 def test_unique(self):
     """
     Tests removing and adding unique constraints to a single column.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Tag)
     # Ensure the field is unique to begin with
     Tag.objects.create(title="foo", slug="foo")
     self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
     Tag.objects.all().delete()
     # Alter the slug field to be non-unique
     new_field = SlugField(unique=False)
     new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Tag,
             Tag._meta.get_field_by_name("slug")[0],
             new_field,
             strict=True,
         )
     # Ensure the field is no longer unique
     Tag.objects.create(title="foo", slug="foo")
     Tag.objects.create(title="bar", slug="foo")
     Tag.objects.all().delete()
     # Alter the slug field to be unique
     new_new_field = SlugField(unique=True)
     new_new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Tag,
             new_field,
             new_new_field,
             strict=True,
         )
     # Ensure the field is unique again
     Tag.objects.create(title="foo", slug="foo")
     self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
     Tag.objects.all().delete()
     # Rename the field
     new_field = SlugField(unique=False)
     new_field.set_attributes_from_name("slug2")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Tag,
             Tag._meta.get_field_by_name("slug")[0],
             TagUniqueRename._meta.get_field_by_name("slug2")[0],
             strict=True,
         )
     # Ensure the field is still unique
     TagUniqueRename.objects.create(title="foo", slug2="foo")
     self.assertRaises(IntegrityError, TagUniqueRename.objects.create, title="bar", slug2="foo")
     Tag.objects.all().delete()
コード例 #8
0
ファイル: tests.py プロジェクト: Swetachaudhary8/django
 def test_unique(self):
     """
     Tests removing and adding unique constraints to a single column.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Tag)
     # Ensure the field is unique to begin with
     Tag.objects.create(title="foo", slug="foo")
     self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
     Tag.objects.all().delete()
     # Alter the slug field to be non-unique
     new_field = SlugField(unique=False)
     new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Tag,
             Tag._meta.get_field_by_name("slug")[0],
             new_field,
             strict=True,
         )
     # Ensure the field is no longer unique
     Tag.objects.create(title="foo", slug="foo")
     Tag.objects.create(title="bar", slug="foo")
     Tag.objects.all().delete()
     # Alter the slug field to be unique
     new_new_field = SlugField(unique=True)
     new_new_field.set_attributes_from_name("slug")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Tag,
             new_field,
             new_new_field,
             strict=True,
         )
     # Ensure the field is unique again
     Tag.objects.create(title="foo", slug="foo")
     self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
     Tag.objects.all().delete()
     # Rename the field
     new_field = SlugField(unique=False)
     new_field.set_attributes_from_name("slug2")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Tag,
             Tag._meta.get_field_by_name("slug")[0],
             TagUniqueRename._meta.get_field_by_name("slug2")[0],
             strict=True,
         )
     # Ensure the field is still unique
     TagUniqueRename.objects.create(title="foo", slug2="foo")
     self.assertRaises(IntegrityError, TagUniqueRename.objects.create, title="bar", slug2="foo")
     Tag.objects.all().delete()
コード例 #9
0
    def test_unique(self):
        """
        Tests removing and adding unique constraints to a single column.
        """
        print('\n')
        # Create the table
        print('1. Create the table')
        with connection.schema_editor() as editor:
            editor.create_model(Tag)

        # Ensure the field is unique to begin with
        print('2. Ensure the field is unique to begin with')
        Tag.objects.create(title="foo", slug="foo")
        self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
        Tag.objects.all().delete()

        # Alter the slug field to be non-unique
        print('3. Alter the slug field to be non-unique')
        new_field = SlugField(unique=False)
        new_field.set_attributes_from_name("slug")
        with connection.schema_editor() as editor:
            editor.alter_field(
                Tag,
                Tag._meta.get_field_by_name("slug")[0],
                new_field,
                strict=True,
            )

        # Ensure the field is no longer unique
        print('4. Ensure the field is no longer unique')
        Tag.objects.create(title="foo", slug="foo")
        Tag.objects.create(title="bar", slug="foo")
        Tag.objects.all().delete()

        # Alter the slug field to be unique
        print('5. Alter the slug field to be unique')
        new_new_field = SlugField(unique=True)
        new_new_field.set_attributes_from_name("slug")
        with connection.schema_editor() as editor:
            editor.alter_field(
                Tag,
                new_field,
                new_new_field,
                strict=True,
            )

        # Ensure the field is unique again
        print('6. Ensure the field is unique again')
        Tag.objects.create(title="foo", slug="foo")
        self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
        Tag.objects.all().delete()

        # The code below fail...
        # Firebid 2.5.x does not allow rename a field with a unique constraint defined on it.
        # Thas is to preserve data integrity.
        # Cannot update index segment used by an Integrity Constraint', -607, 335544351
        return

        # Rename the field
        print('7. Rename the field')
        new_field = SlugField(unique=False)
        new_field.set_attributes_from_name("slug2")
        with connection.schema_editor() as editor:
            editor.alter_field(
                Tag,
                Tag._meta.get_field_by_name("slug")[0],
                TagUniqueRename._meta.get_field_by_name("slug2")[0],
                strict=True,
            )

        # Ensure the field is still unique
        print('8. Ensure the field is still unique')
        TagUniqueRename.objects.create(title="foo", slug2="foo")
        self.assertRaises(IntegrityError, TagUniqueRename.objects.create, title="bar", slug2="foo")
        Tag.objects.all().delete()