I have an inline formset (TranslationFormSet), which edits all the translations associated with a particular Sense. It seems to be working well, both when adding new translations and editing existing ones. However, my unittests[1] are failing, because there are two items where there should be one: i.e., the edited item is a new item, and the old item is still there. This is only happening in the the tests, the actual form on the website is behaving as expecting. So it must be something wrong with how I've written the tests, but I can't figure out what. I modeled it largely off the Django docs section on inline formsets.
Here are the test and failure:
def test_edit_sense_successfully_edits_translations(self):
# set up required related objects
e = Entry.objects.create(hw='فحص')
p = PartOfSpeech.objects.create(pos=1, entry=e)
s = Sense.objects.create(number=1, pos=p, entry=e)
t = Translation.objects.create(tr='test')
# submit edited data through the form
self.client.post('/edit_sense/%d/%d/' % (p.id, s.id), {
'number': 2,
'pos': p,
'entry': e,
'translation_set-0-id': t.id,
'translation_set-0-tr': 'test1',
'translation_set-TOTAL_FORMS': '3',
'translation_set-INITIAL_FORMS': '1',
})
sense = Sense.objects.first()
self.assertEquals(sense.translation_set.count(), 1) # passes
self.assertEquals(Translation.objects.count(), 1) # fails
======================================================================
FAIL: test_edit_sense_successfully_edits_translations (dict.tests.add_sense_tests.EditSensePageView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/larapsodia/dict/dev/dict/tests/add_sense_tests.py", line 181, in test_edit_sense_successfully_edits_translations
self.assertEquals(Translation.objects.count(), 1)
AssertionError: 2 != 1
What am I doing wrong?
[1] Yeah, yeah, yeah, I know these are not technically unittests but integrated tests. Whatever.
Aucun commentaire:
Enregistrer un commentaire