mercredi 4 mai 2016

django unit test raise AssertionError for "blank=False" field

I have a model field namely descrition and the constraints for the field is

description = models.TextField(
    verbose_name=_('Description'),
    help_text=_('Description of the requirement'),
    blank=True, default=''
)

so it can not be blank from the form level.But i want to write a unit test for this and i have write one and its raising the error AssertionError: ValidationError not raised

My test code(please avoid the other fields and methods,i didn't mention those fields and methods in detail because they are not relevant at this moment)

class RequirementHeaderTestCase(TestCase):

    def setUp(self):
       self.user = Customer.objects.create_user(email="user@user.com")
       self.user_id = self.user.id
       self.anonymous_user_email_id = None
       self.session = None
       self.description = 'Test Description'
       self.requirement_status = 'Complete'
       self.service = RequirementService()

    def test_requirement_header_without_description(self):
        with self.assertRaises(ValidationError) as ex:
            self.service.create_requirement_header(
                user_id=self.user_id,
                anonymous_user_email_id=self.anonymous_user_email_id,
                session=self.session,
                requirement_status=self.requirement_status,
        )
        self.assertEqual(ex.exception.__class__, ValidationError)

after run this test, its showing the followin error,

FAIL: test_requirement_header_without_description     (requirements.tests.RequirementHeaderTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "/home/equirements/tests.py", line 25, in test_requirement_header_without_description
    requirement_status=self.requirement_status,
    AssertionError: ValidationError not raised

my question ,how can i write a unit test for a field which is blank=False from the form level.

UPDATE

Here is the relevant method

def create_requirement_header(self,  user_id=None,anonymous_user_email_id=None,session=None,description='',requirement_status=None):
    # getting Customer object from id
    user = None
    if user_id is not None:
        try:
            user = Customer.objects.get(pk=user_id)
        except Customer.DoesNotExist:
            raise ObjectDoesNotExist('User was not found')

    # getting AnonymousUserEmail object from id
    anonymous_user_email = None
    if anonymous_user_email_id is not None:
        try:
            anonymous_user_email = AnonymousUserEmail.objects.get(pk=anonymous_user_email_id)
        except AnonymousUserEmail.DoesNotExist:
            raise ObjectDoesNotExist('AnonymousUserEmail object was not found')  # Need to re-think about message

    requirement_header = RequirementHeader.objects.create_requirement_header(user=user,
                                                                             anonymous_email=anonymous_user_email,
                                                                             session=session,
                                                                             description=description,
                                                                             requirement_status=requirement_status)
    return {
        "id": requirement_header.id,
        "description": requirement_header.description,
        "status": requirement_header.requirement_status
    }

Aucun commentaire:

Enregistrer un commentaire