0

The unit test fails with the following exception:

def test_question_form(self):
    question = Question(question_text='Dummy question', pub_date=timezone.now(
    ) + datetime.timedelta(days=1), allow_multiple_choices=True)
    question_form = QuestionForm(
        {'question_text': question.question_text, 'pub_date': question.pub_date, 'allow_multiple_choices': 'on' if question.allow_multiple_choices else 'off'})
    self.assertTrue(question_form.is_valid())
    self.assertEqual(question_form.save(commit=False), question)

AssertionError: <Question: Dummy question> != <Question: Dummy question>

After some manual assertion the object seem to be equal, what am I doing wrong?

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139

1 Answers1

1

Since your instances are not saved, the model instance returned by form.save with commit=False and the original unsaved object will never be equal (except you override the __eq__ method of your model to handle this):

From the docs:

The equality method is defined such that instances with the same primary key value and the same concrete class are considered equal, except that instances with a primary key value of None aren’t equal to anything except themselves

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • What would be a solution to this except of overriding the equal method? Do I have to manually assert all model members? –  Mar 14 '17 at 14:33
  • See [compare-object-instances-for-equality-by-their-attributes-in-python](http://stackoverflow.com/questions/1227121/compare-object-instances-for-equality-by-their-attributes-in-python) – Moses Koledoye Mar 14 '17 at 14:36