'Tests the RenameField operation.'

def test_rename_field(self):

    project_state = self.set_up_test_model('test_rnfl', unique_together=True, index_together=True)
    operation = migrations.RenameField('Pony', 'pink', 'blue')
    self.assertEqual(operation.describe(), 'Rename    field    pink    on    Pony    to    blue')
    new_state = project_state.clone()
    operation.state_forwards('test_rnfl', new_state)
    self.assertIn('blue', [n for (n, f) in new_state.models[('test_rnfl', 'pony')].fields])
    self.assertNotIn('pink', [n for (n, f) in new_state.models[('test_rnfl', 'pony')].fields])
    self.assertIn('blue', new_state.models[('test_rnfl', 'pony')].options['unique_together'][0])
    self.assertNotIn('pink', new_state.models[('test_rnfl', 'pony')].options['unique_together'][0])
    self.assertIn('blue', new_state.models[('test_rnfl', 'pony')].options['index_together'][0])
    self.assertNotIn('pink', new_state.models[('test_rnfl', 'pony')].options['index_together'][0])
    self.assertColumnExists('test_rnfl_pony', 'pink')
    self.assertColumnNotExists('test_rnfl_pony', 'blue')
    with connection.schema_editor() as editor:
        operation.database_forwards('test_rnfl', editor, project_state, new_state)
    self.assertColumnExists('test_rnfl_pony', 'blue')
    self.assertColumnNotExists('test_rnfl_pony', 'pink')
    with connection.cursor() as cursor:
        cursor.execute('INSERT    INTO    test_rnfl_pony    (blue,    weight)    VALUES    (1,    1)')
        with self.assertRaises(IntegrityError):
            with atomic():
                cursor.execute('INSERT    INTO    test_rnfl_pony    (blue,    weight)    VALUES    (1,    1)')
        cursor.execute('DELETE    FROM    test_rnfl_pony')
    self.assertIndexExists('test_rnfl_pony', ['weight', 'blue'])
    with connection.schema_editor() as editor:
        operation.database_backwards('test_rnfl', editor, new_state, project_state)
    self.assertColumnExists('test_rnfl_pony', 'pink')
    self.assertColumnNotExists('test_rnfl_pony', 'blue')
    self.assertIndexExists('test_rnfl_pony', ['weight', 'pink'])
    definition = operation.deconstruct()
    self.assertEqual(definition[0], 'RenameField')
    self.assertEqual(definition[1], [])
    self.assertEqual(definition[2], {'model_name': 'Pony', 'old_name': 'pink', 'new_name': 'blue'})
