Augier
Augier

Reputation: 311

Error Unit testing a ModelForm

I am currently writing unit tests for my Django webapp and have come across a stumbling block.

My Model:

class Employment(models.Model):
    applicant = models.ForeignKey(Applicant)
    company = models.CharField(max_length=50)
    town = models.CharField(max_length=50)
    salary = models.CharField(max_length=50)
    job_title = models.CharField(max_length=50)
    responsibilities = models.CharField(max_length=100)
    date_from = models.DateField('Date From')
    date_to = models.DateField('Date To')
    reason_for_leaving = models.CharField(max_length=50)

My Form:

class EmploymentForm(forms.ModelForm):
    error_css_class = 'error'   #set some css when an error
    company = forms.CharField(label="Company/Agency")
    town = forms.CharField(label="Town/City")
    salary = forms.CharField(label="Salary/Pay rate")
    date_from = forms.DateField(('%d/%m/%Y',), widget=forms.DateInput(format='%d/%m/%Y', attrs={'placeholder':'dd/mm/yyyy'}))
    date_to = forms.DateField(('%d/%m/%Y',), widget=forms.DateInput(format='%d/%m/%Y', attrs={'placeholder':'dd/mm/yyyy'}))
    class Meta:
            model = Employment

My Test:

"""
Complete employment form passes validation
"""
def test_employment_form_complete(self):
    applicant = Applicant(job_id=1)
    data = {
    'company': 'test company',
    'town': 'test town',
    'salary': '1000000',
    'job_title': 'test job name',
    'responsibilities': 'test responsibilities',
    'date_from': '01/01/1990',
    'date_to': '01/02/1991',
    'reason_for_leaving': 'test reason for leaving'
    }
    employment_form = EmploymentForm(instance=applicant, data=data)
    result = employment_form.is_valid()
    print "errors %s" % employment_form.errors
    self.assertEqual(result, True)

If I run the test like this I get:

<ul class="errorlist"><li>applicant<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

If I add:

'applicant': applicant

to my data object it complains it need to be an int.

If I add:

'applicant': 1

(or another integer)

it returns:

<ul class="errorlist"><li>applicant<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul>

which is understandable really.

How can I get around this? What is the best practise?

Regards,

Chris.

Upvotes: 1

Views: 82

Answers (1)

qwetty
qwetty

Reputation: 1268

I think You should call applicant.save()

You create new object, but did't save it to database. while testing django create "temporary" database.

Also add to data dictionary 'applicant': 1.

PS. Make sure applicant object will be created with foreign key=1!!

Completely working code.


def test_employment_form_complete(self):
        """
        Complete employment form passes validation
        """
        applicant = Applicant(job_id=1)
        applicant.save()
        data = {
            'applicant': 1,
        'company': 'test company',
        'town': 'test town',
        'salary': '1000000',
        'job_title': 'test job name',
        'responsibilities': 'test responsibilities',
        'date_from': '01/01/1990',
        'date_to': '01/02/1991',
        'reason_for_leaving': 'test reason for leaving'
        }
        employment_form = EmploymentForm(instance=applicant, data=data)
        result = employment_form.is_valid()
        print "errors %s" % employment_form.errors
        self.assertEqual(result, True)

Upvotes: 1

Related Questions