def test_serialize_settings(self): self.assertSerializedEqual( SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL")) self.assertSerializedResultEqual( SettingsReference("someapp.model", "AUTH_USER_MODEL"), ("settings.AUTH_USER_MODEL", {"from django.conf import settings"})) self.assertSerializedResultEqual(((x, x * x) for x in range(3)), ("((0, 0), (1, 1), (2, 4))", set()))
def test_serialize(self): """ Tests various different forms of the serializer. This does not care about formatting, just that the parsed result is correct, so we always exec() the result and check that. """ # Basic values self.assertSerializedEqual(1) self.assertSerializedEqual(None) self.assertSerializedEqual(b"foobar") self.assertSerializedEqual("föobár") self.assertSerializedEqual({1: 2}) self.assertSerializedEqual(["a", 2, True, None]) self.assertSerializedEqual(set([2, 3, "eighty"])) self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]}) self.assertSerializedEqual(_('Hello')) # Functions with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'): self.assertSerializedEqual(lambda x: 42) self.assertSerializedEqual(models.SET_NULL) string, imports = MigrationWriter.serialize(models.SET(42)) self.assertEqual(string, 'models.SET(42)') self.serialize_round_trip(models.SET(42)) # Datetime stuff self.assertSerializedEqual(datetime.datetime.utcnow()) self.assertSerializedEqual(datetime.datetime.utcnow) self.assertSerializedEqual(datetime.datetime.today()) self.assertSerializedEqual(datetime.datetime.today) self.assertSerializedEqual(datetime.date.today()) self.assertSerializedEqual(datetime.date.today) # Classes validator = RegexValidator(message="hello") string, imports = MigrationWriter.serialize(validator) self.assertEqual(string, "django.core.validators.RegexValidator(message=%s)" % repr("hello")) self.serialize_round_trip(validator) validator = EmailValidator(message="hello") # Test with a subclass. string, imports = MigrationWriter.serialize(validator) self.assertEqual(string, "django.core.validators.EmailValidator(message=%s)" % repr("hello")) self.serialize_round_trip(validator) validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello") string, imports = MigrationWriter.serialize(validator) self.assertEqual(string, "custom.EmailValidator(message=%s)" % repr("hello")) # Django fields self.assertSerializedFieldEqual(models.CharField(max_length=255)) self.assertSerializedFieldEqual(models.TextField(null=True, blank=True)) # Setting references self.assertSerializedEqual(SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL")) self.assertSerializedResultEqual( SettingsReference("someapp.model", "AUTH_USER_MODEL"), ( "settings.AUTH_USER_MODEL", set(["from django.conf import settings"]), ) )
def test_serialize_settings(self): self.assertSerializedEqual( SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL")) self.assertSerializedResultEqual( SettingsReference("someapp.model", "AUTH_USER_MODEL"), ("settings.AUTH_USER_MODEL", {"from django.conf import settings"}))
def deconstruct(self): name, path, args, kwargs = super(AESField, self).deconstruct() kwargs['aes_method'] = SettingsReference(self.aes_method, 'AES_METHOD') kwargs['aes_prefix'] = self.aes_prefix kwargs['aes_key'] = self.aes_key return name, path, args, kwargs
def deconstruct(self): name, path, args, kwargs = super(GM2MField, self).deconstruct() kwargs.pop('null', None) # generate related models list (cannot get it from rel, as it can # be changed by add_relation) for rel in self.remote_field.rels: if getattr(rel, '_added', False): continue if isinstance(rel.model, six.string_types): args.append(rel.model) else: # see if the related model is a swappable model swappable_setting = rel.swappable_setting if swappable_setting is not None: setting_name = getattr(rel.model, 'setting_name', None) if setting_name != swappable_setting: raise ValueError( 'Cannot deconstruct a GM2MField pointing to a ' 'model that is swapped in place of more than one ' 'model (%s and %s)' % (setting_name, swappable_setting)) from django.db.migrations.writer import SettingsReference model = SettingsReference(rel.model, swappable_setting) else: model = '%s.%s' % (rel.model._meta.app_label, rel.model._meta.object_name) args.append(model) # handle parameters if self.db_table: kwargs['db_table'] = self.db_table if self.pk_maxlength is not False: kwargs['pk_maxlength'] = self.pk_maxlength through = self.remote_field.through if through: if isinstance(through, six.string_types): kwargs['through'] = through elif not through._meta.auto_created: kwargs['through'] = '%s.%s' % (through._meta.app_label, through._meta.object_name) # rel options for k in REL_ATTRS_NAMES: if k == 'through': # through has been dealt with just above continue # retrieve default value try: default = REL_ATTRS[k] except KeyError: if k.startswith('on_delete_'): default = kwargs.get('on_delete', REL_ATTRS['on_delete']) else: # this is a fixed attribute, we don't need to care about it continue # retrieve actual initial value, possibly from _init_attr dict try: value = self.remote_field._init_attrs[k] except KeyError: value = getattr(self.remote_field, k) if value != default: if k == 'related_name': value = force_text(value) kwargs[k] = value return name, path, args, kwargs
def test_serialize(self): """ Tests various different forms of the serializer. This does not care about formatting, just that the parsed result is correct, so we always exec() the result and check that. """ # Basic values self.assertSerializedEqual(1) self.assertSerializedEqual(None) self.assertSerializedEqual(b"foobar") string, imports = MigrationWriter.serialize(b"foobar") self.assertEqual(string, "b'foobar'") self.assertSerializedEqual("föobár") string, imports = MigrationWriter.serialize("foobar") self.assertEqual(string, "'foobar'") self.assertSerializedEqual({1: 2}) self.assertSerializedEqual(["a", 2, True, None]) self.assertSerializedEqual(set([2, 3, "eighty"])) self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]}) self.assertSerializedEqual(_('Hello')) # Functions with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'): self.assertSerializedEqual(lambda x: 42) self.assertSerializedEqual(models.SET_NULL) string, imports = MigrationWriter.serialize(models.SET(42)) self.assertEqual(string, 'models.SET(42)') self.serialize_round_trip(models.SET(42)) # Datetime stuff self.assertSerializedEqual(datetime.datetime.utcnow()) self.assertSerializedEqual(datetime.datetime.utcnow) self.assertSerializedEqual(datetime.datetime.today()) self.assertSerializedEqual(datetime.datetime.today) self.assertSerializedEqual(datetime.date.today()) self.assertSerializedEqual(datetime.date.today) with self.assertRaises(ValueError): self.assertSerializedEqual(datetime.datetime(2012, 1, 1, 1, 1, tzinfo=get_default_timezone())) safe_date = datetime_safe.date(2014, 3, 31) string, imports = MigrationWriter.serialize(safe_date) self.assertEqual(string, repr(datetime.date(2014, 3, 31))) self.assertEqual(imports, {'import datetime'}) safe_datetime = datetime_safe.datetime(2014, 3, 31, 16, 4, 31) string, imports = MigrationWriter.serialize(safe_datetime) self.assertEqual(string, repr(datetime.datetime(2014, 3, 31, 16, 4, 31))) self.assertEqual(imports, {'import datetime'}) # Django fields self.assertSerializedFieldEqual(models.CharField(max_length=255)) self.assertSerializedFieldEqual(models.TextField(null=True, blank=True)) # Setting references self.assertSerializedEqual(SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL")) self.assertSerializedResultEqual( SettingsReference("someapp.model", "AUTH_USER_MODEL"), ( "settings.AUTH_USER_MODEL", set(["from django.conf import settings"]), ) ) self.assertSerializedResultEqual( ((x, x * x) for x in range(3)), ( "((0, 0), (1, 1), (2, 4))", set(), ) )