Form and Field comparison always fails
|Reported by:||gcc1||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
It would be nice to be able to unit test that a particular view returns a correctly configured Form object in its response context. But this seems to be hard, because Form and Field objects don't compare properly.
For example, take the following view:
def decision_add_page(request): decision_form = DecisionForm() return render_to_response('decision_add.html', RequestContext(request, dict(decision_form=decision_form)))
I try to test it like this:
def get(self, view_function, **view_args): return self.client.get(reverse(view_function, kwargs=view_args)) def test_decision_add_page(self): response = self.client.get(reverse(openconsent.publicweb.views.decision_add_page)) self.assertEqual(DecisionForm(), response.context['decision_form'])
And it fails because it can't compare the two objects:
File "/home/chris/projects/openconsent/django/openconsent/publicweb/tests.py", line 37, in test_decision_add_page self.assertEqual(DecisionForm(), response.context['decision_form']) AssertionError: <openconsent.publicweb.forms.DecisionForm object at 0xa55cc4c> != <openconsent.publicweb.forms.DecisionForm object at 0xa55ce6c>
Then I tried to wrap both in list(), hoping that at least comparison on field objects was implemented:
But I get the same kind of error:
File "/home/chris/projects/openconsent/django/openconsent/publicweb/tests.py", line 38, in test_decision_add_page list(response.context['decision_form'])) AssertionError: Lists differ: [<django.forms.forms.BoundFiel... != [<django.forms.forms.BoundFiel... First differing element 0: <input id="id_short_name" type="text" name="short_name" maxlength="255" /> <input id="id_short_name" type="text" name="short_name" maxlength="255" /> Diff is 1761 characters long. Set self.maxDiff to None to see it.
How would one go about implementing equality sensibly for forms and fields?