'Tests the RunSQL operation.'

@unittest.skipIf(((sqlparse is None) and connection.features.requires_sqlparse_for_splitting), 'Missing    sqlparse')
def test_run_sql(self):

    project_state = self.set_up_test_model('test_runsql')
    operation = migrations.RunSQL("CREATE    TABLE    i_love_ponies    (id    int,    special_thing    varchar(15));\nINSERT    INTO    i_love_ponies    (id,    special_thing)    VALUES    (1,    'i    love    ponies');    --    this    is    magic!\nINSERT    INTO    i_love_ponies    (id,    special_thing)    VALUES    (2,    'i    love    django');\nUPDATE    i_love_ponies    SET    special_thing    =    'Ponies'    WHERE    special_thing    LIKE    '%%ponies';UPDATE    i_love_ponies    SET    special_thing    =    'Django'    WHERE    special_thing    LIKE    '%django';", "DELETE    FROM    i_love_ponies    WHERE    special_thing    LIKE    '%Django%';DELETE    FROM    i_love_ponies    WHERE    special_thing    LIKE    '%%Ponies%%';DROP    TABLE    i_love_ponies", state_operations=[migrations.CreateModel('SomethingElse', [('id', models.AutoField(primary_key=True))])])
    self.assertEqual(operation.describe(), 'Raw    SQL    operation')
    new_state = project_state.clone()
    operation.state_forwards('test_runsql', new_state)
    self.assertEqual(len(new_state.models[('test_runsql', 'somethingelse')].fields), 1)
    self.assertTableNotExists('i_love_ponies')
    with connection.schema_editor(collect_sql=True) as editor:
        operation.database_forwards('test_runsql', editor, project_state, new_state)
        self.assertIn("LIKE    '%%ponies';", '\n'.join(editor.collected_sql))
        operation.database_backwards('test_runsql', editor, project_state, new_state)
        self.assertIn("LIKE    '%%Ponies%%';", '\n'.join(editor.collected_sql))
    with connection.schema_editor() as editor:
        operation.database_forwards('test_runsql', editor, project_state, new_state)
    self.assertTableExists('i_love_ponies')
    with connection.cursor() as cursor:
        cursor.execute('SELECT    COUNT(*)    FROM    i_love_ponies')
        self.assertEqual(cursor.fetchall()[0][0], 2)
        cursor.execute("SELECT    COUNT(*)    FROM    i_love_ponies    WHERE    special_thing    =    'Django'")
        self.assertEqual(cursor.fetchall()[0][0], 1)
        cursor.execute("SELECT    COUNT(*)    FROM    i_love_ponies    WHERE    special_thing    =    'Ponies'")
        self.assertEqual(cursor.fetchall()[0][0], 1)
    self.assertTrue(operation.reversible)
    with connection.schema_editor() as editor:
        operation.database_backwards('test_runsql', editor, new_state, project_state)
    self.assertTableNotExists('i_love_ponies')
    definition = operation.deconstruct()
    self.assertEqual(definition[0], 'RunSQL')
    self.assertEqual(definition[1], [])
    self.assertEqual(sorted(definition[2]), ['reverse_sql', 'sql', 'state_operations'])
    self.assertIs(False, operation.reduce(operation, []))
    elidable_operation = migrations.RunSQL('SELECT    1    FROM    void;', elidable=True)
    self.assertEqual(elidable_operation.reduce(operation, []), [operation])
