def test_add_column_with_index(self): """ Test adding a column with an index to a MigrationManager. """ manager = MigrationManager() manager.add_column( table_class_name="Manager", tablename="manager", column_name="email", column_class=Varchar, column_class_name="Varchar", params={ "length": 100, "default": "", "null": True, "primary": False, "key": False, "unique": True, "index": True, }, ) index_name = Manager._get_index_name(["email"]) asyncio.run(manager.run()) self.assertTrue(index_name in Manager.indexes().run_sync()) # Reverse asyncio.run(manager.run_backwards()) self.assertTrue(index_name not in Manager.indexes().run_sync())
def test_alter_column_add_index(self): """ Test altering a column to add an index with MigrationManager. """ manager = MigrationManager() manager.alter_column( table_class_name="Manager", tablename="manager", column_name="name", params={"index": True}, old_params={"index": False}, ) asyncio.run(manager.run()) self.assertTrue( Manager._get_index_name(["name"]) in Manager.indexes().run_sync() ) asyncio.run(manager.run_backwards()) self.assertTrue( Manager._get_index_name(["name"]) not in Manager.indexes().run_sync() )
def test_create_index(self): """ Test single column and multi column indexes. """ for columns in [ [Manager.name], [Manager._meta.primary_key, Manager.name], ]: Manager.create_index(columns).run_sync() index_name = Manager._get_index_name( [i._meta.name for i in columns]) index_names = Manager.indexes().run_sync() self.assertIn(index_name, index_names) Manager.drop_index(columns).run_sync() index_names = Manager.indexes().run_sync() self.assertNotIn(index_name, index_names)
def test_index_name(self): self.assertEqual(Manager._get_index_name(["name", "id"]), "manager_name_id")