'Tests the AlterOrderWithRespectTo operation.'

def test_alter_order_with_respect_to(self):

    project_state = self.set_up_test_model('test_alorwrtto', related_model=True)
    operation = migrations.AlterOrderWithRespectTo('Rider', 'pony')
    self.assertEqual(operation.describe(), 'Set    order_with_respect_to    on    Rider    to    pony')
    new_state = project_state.clone()
    operation.state_forwards('test_alorwrtto', new_state)
    self.assertIsNone(project_state.models[('test_alorwrtto', 'rider')].options.get('order_with_respect_to', None))
    self.assertEqual(new_state.models[('test_alorwrtto', 'rider')].options.get('order_with_respect_to', None), 'pony')
    self.assertColumnNotExists('test_alorwrtto_rider', '_order')
    rendered_state = project_state.apps
    pony = rendered_state.get_model('test_alorwrtto', 'Pony').objects.create(weight=50)
    rendered_state.get_model('test_alorwrtto', 'Rider').objects.create(pony=pony, friend_id=1)
    rendered_state.get_model('test_alorwrtto', 'Rider').objects.create(pony=pony, friend_id=2)
    with connection.schema_editor() as editor:
        operation.database_forwards('test_alorwrtto', editor, project_state, new_state)
    self.assertColumnExists('test_alorwrtto_rider', '_order')
    updated_riders = new_state.apps.get_model('test_alorwrtto', 'Rider').objects.all()
    self.assertEqual(updated_riders[0]._order, 0)
    self.assertEqual(updated_riders[1]._order, 0)
    with connection.schema_editor() as editor:
        operation.database_backwards('test_alorwrtto', editor, new_state, project_state)
    self.assertColumnNotExists('test_alorwrtto_rider', '_order')
    definition = operation.deconstruct()
    self.assertEqual(definition[0], 'AlterOrderWithRespectTo')
    self.assertEqual(definition[1], [])
    self.assertEqual(definition[2], {'name': 'Rider', 'order_with_respect_to': 'pony'})
