예제 #1
0
class IntegerUserPKFieldTestCase(TestCase):
    """
    Test that a user model with a custom integer based primary key works.
    """

    class IntegerUserModel(AbstractBaseUser):
        id = models.IntegerField(primary_key=True, unique=True, validators=[integer_validator])
        username = models.CharField(max_length=255)
        USERNAME_FIELD = 'username'

        class Meta:
            app_label = 'timberjack'

    def setUp(self):
        self.field = UserPKField()

    def test_validate_integer(self):
        self.assertIsNone(self.field.validate(1))

    def test_validate_string_fails(self):
        try:
            self.field.validate('this is not an integer!')
            self.fail('Did not fail when trying to validate a string.')
        except ValidationError as e:
            self.assertEqual(str(e), "'this is not an integer!' value must be an integer.")

    def test_value_conversion(self):
        value = 1
        mongo_val = self.field.to_mongo(value)
        python_val = self.field.to_python(mongo_val)
        self.assertEqual(value, python_val)
예제 #2
0
class UUIDUserPKFieldTestCase(TestCase):
    """
    Test that a user model with a custom UUID based primary key works.
    """

    class UUIDUserModel(AbstractBaseUser):
        id = models.UUIDField(primary_key=True, unique=True, validators=[uuid_validator])
        username = models.CharField(max_length=255)
        USERNAME_FIELD = 'username'

        class Meta:
            app_label = 'timberjack'

    def setUp(self):
        self.field = UserPKField()

    def test_validate_uuid_string(self):
        self.assertIsNone(self.field.validate('6a3de76f-ebd6-448d-a70f-62e0866d37f4'))

    def test_validate_uuid_hex_string(self):
        self.assertIsNone(self.field.validate('74a40ec6a74d44199d8457861bb51484'))

    def test_validate_non_valid_uuid_fails(self):
        try:
            self.field.validate('this-is-not-an-uuid')
            self.fail('Did not fail when trying to validate a non-valid uuid string.')
        except ValidationError as e:
            self.assertEqual(str(e), "'this-is-not-an-uuid' is not a valid UUID.")

    def test_value_conversion(self):
        value = uuid.uuid4()
        mongo_val = self.field.to_mongo(value)
        python_val = self.field.to_python(mongo_val)
        self.assertEqual(value, python_val)
예제 #3
0
class NoPKFieldUserTestCase(TestCase):
    """
    Test that a user model with no defined primary key works.
    """

    class NoPKFieldUserModel(AbstractBaseUser):
        username = models.CharField(max_length=255)
        USERNAME_FIELD = 'username'

        class Meta:
            app_label = 'timberjack'

    def setUp(self):
        self.field = UserPKField()

    def test_pk_field_is_autofield(self):
        self.assertIsInstance(self.field.pk_field, models.AutoField)

    def test_validate_integer(self):
        self.assertIsNone(self.field.validate(1))
예제 #4
0
class StringUserPKFieldTestCase(TestCase):
    """
    Test that a user model with a custom string based primary key works.
    """

    class StringUserModel(AbstractBaseUser):
        id = models.CharField(primary_key=True, unique=True, max_length=6)
        username = models.CharField(max_length=255)
        USERNAME_FIELD = 'username'

        class Meta:
            app_label = 'timberjack'

    def setUp(self):
        self.field = UserPKField()

    def test_validate_string(self):
        self.assertIsNone(self.field.validate('unique'))

    def test_validate_too_long_string_fails(self):
        try:
            self.field.validate('this string is more than 6 characters long!')
            self.fail('Did not fail when trying to validate a too long string.')
        except ValidationError as e:
            self.assertEqual(str(e), 'Ensure this value has at most 6 characters (it has 43).')

    def test_validate_int_fails(self):
        try:
            self.field.validate(1)
            self.fail('Did not fail when trying to validate a non string type.')
        except ValidationError as e:
            self.assertEqual(str(e), 'Value %(value)r is not a valid primary key value for '
                                     '<django.db.models.fields.CharField: id>.' % {'value': 1})

    def test_value_conversion(self):
        value = 'value'
        mongo_val = self.field.to_mongo(value)
        python_val = self.field.to_python(mongo_val)
        self.assertEqual(value, python_val)
예제 #5
0
 def setUp(self):
     self.field = UserPKField()