'Tests the RunPython operation correctly handles the "atomic" keyword'

def test_run_python_atomic(self):

    project_state = self.set_up_test_model('test_runpythonatomic', mti_model=True)
    def inner_method(models, schema_editor):
        Pony = models.get_model('test_runpythonatomic', 'Pony')
        Pony.objects.create(pink=1, weight=3.55)
        raise ValueError('Adrian    hates    ponies.')
    atomic_migration = Migration('test', 'test_runpythonatomic')
    atomic_migration.operations = [migrations.RunPython(inner_method)]
    non_atomic_migration = Migration('test', 'test_runpythonatomic')
    non_atomic_migration.operations = [migrations.RunPython(inner_method, atomic=False)]
    if connection.features.can_rollback_ddl:
        self.assertEqual(project_state.apps.get_model('test_runpythonatomic', 'Pony').objects.count(), 0)
        with self.assertRaises(ValueError):
            with connection.schema_editor() as editor:
                atomic_migration.apply(project_state, editor)
        self.assertEqual(project_state.apps.get_model('test_runpythonatomic', 'Pony').objects.count(), 0)
        with self.assertRaises(ValueError):
            with connection.schema_editor() as editor:
                non_atomic_migration.apply(project_state, editor)
        self.assertEqual(project_state.apps.get_model('test_runpythonatomic', 'Pony').objects.count(), 0)
    else:
        self.assertEqual(project_state.apps.get_model('test_runpythonatomic', 'Pony').objects.count(), 0)
        with self.assertRaises(ValueError):
            with connection.schema_editor() as editor:
                atomic_migration.apply(project_state, editor)
        self.assertEqual(project_state.apps.get_model('test_runpythonatomic', 'Pony').objects.count(), 0)
        with self.assertRaises(ValueError):
            with connection.schema_editor() as editor:
                non_atomic_migration.apply(project_state, editor)
        self.assertEqual(project_state.apps.get_model('test_runpythonatomic', 'Pony').objects.count(), 1)
    definition = non_atomic_migration.operations[0].deconstruct()
    self.assertEqual(definition[0], 'RunPython')
    self.assertEqual(definition[1], [])
    self.assertEqual(sorted(definition[2]), ['atomic', 'code'])
