'Ticket #22943: Test serialization of class-based validators, including
compiled regexes.'

def test_serialize_class_based_validators(self):

    validator = RegexValidator(message='hello')
    string = MigrationWriter.serialize(validator)[0]
    self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')")
    self.serialize_round_trip(validator)
    validator = RegexValidator(regex=re.compile('^\\w+$'))
    string = MigrationWriter.serialize(validator)[0]
    self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$'))")
    self.serialize_round_trip(validator)
    validator = RegexValidator('^[0-9]+$', flags=re.S)
    string = MigrationWriter.serialize(validator)[0]
    if PY36:
        self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$',    flags=re.RegexFlag(16))")
    else:
        self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$',    flags=16)")
    self.serialize_round_trip(validator)
    validator = RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')
    string = MigrationWriter.serialize(validator)[0]
    self.assertEqual(string, "django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$',    'Invalid',    'invalid')")
    self.serialize_round_trip(validator)
    validator = EmailValidator(message='hello')
    string = MigrationWriter.serialize(validator)[0]
    self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')")
    self.serialize_round_trip(validator)
    validator = deconstructible(path='migrations.test_writer.EmailValidator')(EmailValidator)(message='hello')
    string = MigrationWriter.serialize(validator)[0]
    self.assertEqual(string, "migrations.test_writer.EmailValidator(message='hello')")
    validator = deconstructible(path='custom.EmailValidator')(EmailValidator)(message='hello')
    with self.assertRaisesMessage(ImportError, "No    module    named    'custom'"):
        MigrationWriter.serialize(validator)
    validator = deconstructible(path='django.core.validators.EmailValidator2')(EmailValidator)(message='hello')
    with self.assertRaisesMessage(ValueError, 'Could    not    find    object    EmailValidator2    in    django.core.validators.'):
        MigrationWriter.serialize(validator)
