

def test_foreign_key(self):
    from django.contrib.auth.models import Permission
    field = models.ForeignKey('auth.Permission', models.CASCADE)
    field.remote_field.model = Permission
    field.remote_field.field_name = 'id'
    (name, path, args, kwargs) = field.deconstruct()
    self.assertEqual(path, 'django.db.models.ForeignKey')
    self.assertEqual(args, [])
    self.assertEqual(kwargs, {
        'to': 'auth.Permission',
        'on_delete': models.CASCADE,
    })
    self.assertFalse(hasattr(kwargs['to'], 'setting_name'))
    field = models.ForeignKey('auth.User', models.CASCADE)
    (name, path, args, kwargs) = field.deconstruct()
    self.assertEqual(path, 'django.db.models.ForeignKey')
    self.assertEqual(args, [])
    self.assertEqual(kwargs, {
        'to': 'auth.User',
        'on_delete': models.CASCADE,
    })
    self.assertEqual(kwargs['to'].setting_name, 'AUTH_USER_MODEL')
    field = models.ForeignKey('something.Else', models.CASCADE)
    (name, path, args, kwargs) = field.deconstruct()
    self.assertEqual(path, 'django.db.models.ForeignKey')
    self.assertEqual(args, [])
    self.assertEqual(kwargs, {
        'to': 'something.Else',
        'on_delete': models.CASCADE,
    })
    field = models.ForeignKey('auth.User', models.SET_NULL)
    (name, path, args, kwargs) = field.deconstruct()
    self.assertEqual(path, 'django.db.models.ForeignKey')
    self.assertEqual(args, [])
    self.assertEqual(kwargs, {
        'to': 'auth.User',
        'on_delete': models.SET_NULL,
    })
    field = models.ForeignKey('auth.Permission', models.CASCADE, to_field='foobar')
    (name, path, args, kwargs) = field.deconstruct()
    self.assertEqual(path, 'django.db.models.ForeignKey')
    self.assertEqual(args, [])
    self.assertEqual(kwargs, {
        'to': 'auth.Permission',
        'to_field': 'foobar',
        'on_delete': models.CASCADE,
    })
    field = models.ForeignKey('auth.Permission', models.CASCADE, related_name='foobar')
    (name, path, args, kwargs) = field.deconstruct()
    self.assertEqual(path, 'django.db.models.ForeignKey')
    self.assertEqual(args, [])
    self.assertEqual(kwargs, {
        'to': 'auth.Permission',
        'related_name': 'foobar',
        'on_delete': models.CASCADE,
    })

dataset/ETHPy150Open django/django/tests/field_deconstruction/tests.py FieldDeconstructionTests.test_foreign_key/original