Reputation: 149
I have few models in my models.py
. Two of them are as follows:
class Event():
eventName = models.CharField( unique = True )
class Job():
event = models.ForeignKey(Event)
jobName = models.CharField
class Meta:
unique_together(('event','jobName'))
I was testing a modelform of this class, using Client module
from django.test
. The test where I am failing is while testing for unique_together
property
Following is my test.py
class TestJobView(TestCase):
def test_duplicate_job_in_same_event(self):
Event.objects.create(eventName='test_event', noOfVolunteersRequired=10,
startDate='2014-05-05 05:05:05', endDate='2014-05-05 05:05:05')
Job.objects.create(event=Event.objects.get(eventName='test_event'),
jobName='test_jobName',jobDescription='test_jobDescription_1',
noOfVolunteersRequired=10, startDate='2014-05-05 05:05:05',
endDate='2014-05-05 05:05:05')
self.assertEqual(1, Job.objects.filter(event__eventName='test_event',
jobName='test_jobName').count())
c = Client()
response = c.post('/AdminUnit/job/',{'event' : 'test_event',
'jobName' : 'test_jobName','jobDescription' : 'test_jobDescription_2',
'startDate' : '2014-05-05 05:05:05', 'endDate' : '2014-05-05 05:05:05',
'noOfVolunteersRequired' : 5})
self.assertEqual(200, response.status_code)
print response.context['jobsForm']['event'].errors
The result of the above test prints Select a valid choice. That choice is not one of the available choices.
Now, I have two questions. Firstly, this exception should not have been raised as an event is already created and is also asserted True
in the above method. Secondly, If I try the same thing from my view, It raises a non_field_error
and says Job with this Event and JobName already exists.
. How do I capture this in my tests.py using response ?
Upvotes: 1
Views: 190
Reputation: 599946
You don't show your view or form, but presumably you are just using the default ModelForm which represents a ForeignKey as a ModelChoiceField. So, the value to post for event
is not the name, but the ID, which you should capture when you create the Event at the start of the test.
I should add, however, that I don't think you should be testing this at all. Unit tests are for your code, not Django's. unique_together
is a part of Django itself and as such is very well-covered by the Django's own unit tests. There is no need for you to replicate that functionality explicitly.
Edit
I don't understand your second comment. Your test should look like this:
def test_duplicate_job_in_same_event(self):
event = Event.objects.create(...)
job = Job.objects.create(event=event, ...)
response = self.client.post('/AdminUnit/job/',{'event' : event.id, ...})
Upvotes: 3
Reputation: 3364
Ad Daniel Roseman noticed, unique_together
should not be tested here. If it should be tested at all is a mater of opinion. However, the unique_together
is defined on model level, and luckily everything becomes much simpler when you unit-test it there:
class TestJob(TestCase):
def test_duplicate_in_same_event(self):
event = Event.objects.create(
eventName='test_event', noOfVolunteersRequired=10,
startDate='2014-05-05 05:05:05', endDate='2014-05-05 05:05:05')
Job.objects.create(
event=event,
jobName='test_jobName',jobDescription='test_jobDescription_1',
noOfVolunteersRequired=10, startDate='2014-05-05 05:05:05',
endDate='2014-05-05 05:05:05')
job = Job(
event=event,
jobName='test_jobName',jobDescription='test_jobDescription_1',
noOfVolunteersRequired=10, startDate='2014-05-05 05:05:05',
endDate='2014-05-05 05:05:05')
with self.assertRaises(ValidationError):
job.validate_unique()
Upvotes: 1