0

I have a test where I am mocking a filter call on a manager. the assert looks like this:

filter_mock.assert_called_once_with(type_id__in=[3, 4, 5, 6], finance=mock_finance, parent_transaction__date_posted=tran_date_posted)

and the code being tested looks like this:

agregates = Balance.objects.filter(
            finance=self.finance,type_id__in=self.balance_types,
            parent_transaction__date_posted__lte=self.transaction_date_posted
        )

I thought that since these are kwargs, order shouldn't matter, but the test is failing, even though the values for each pair DO match. below is the error I am seeing:

AssertionError: Expected call: filter(type_id__in=[3, 4, 5, 6], parent_transaction__date_posted=datetime.datetime(2015, 5, 29, 16, 22, 59, 532772), finance=) Actual call: filter(type_id__in=[3, 4, 5, 6], finance=, parent_transaction__date_posted__lte=datetime.datetime(2015, 5, 29, 16, 22, 59, 532772))

what the heck is going on? kwarg order should not matter, and even if I do order to match what the test is asserting, the test still fails.

Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91

1 Answers1

3

Your keys are not exactly the same. In your assert_called_with, you have the key parent_transaction__date_posted, but in your code you are using the key parent_transaction__date_posted__lte. That is what is causing your test to fail, not the bad sorting. Here is my own test as a proof of concept:

    >>> myobject.test(a=1, b=2)
    >>> mock_test.assert_called_with(b=2, a=1)
    OK
    >>> myobject.test(a=1, b__lte=2)
    >>> mock_test.assert_called_with(b=2, a=1)
    AssertionError: Expected call: test(a=1, b=2)
    Actual call: test(a=1, b__lte=2)

You will need to correct either your test or your code so that they match (include __lte or don't depending on your need)

Jake Griffin
  • 2,014
  • 12
  • 15