pythonmockingpython-mock

Why is python mock assert_any_call not matching


Given the doc for assert_any_call

enter image description here

I have a log statement I want to assert against

...
logger.warning('Backup quantity is 0, supplied uuids %s, matched machines: %s', uuids, machines)
...

see the last 3 lines of my unit test (first 2 lines for investigation purpose only)

# deliberate fail so it prints what it expects
logger.warning.assert_not_called()

# prints the params used in the official assert
print('Backup quantity is 0, supplied uuids %s, matched machines: %s', [vm3_uuid], VirtualMachine.objects.none())

# actual official assert
logger.warning.assert_any_call('Backup quantity is 0, supplied uuids %s, matched machines: %s', [vm3_uuid], VirtualMachine.objects.none())

The output for the test assert_not_called was:

AssertionError: Expected 'warning' to not have been called. Called 2 times.
Calls: [call('Backup quantity is 0, supplied uuids %s, matched machines: %s', ['232d7937-975c-457b-8a11-ac473d0e04a0'], <QuerySet []>),
 call('%s %s cannot find proxmox UUID from BIS', 'DEF-456', 'vm4')]

The param print was

Backup quantity is 0, supplied uuids %s, matched machines: %s ['232d7937-975c-457b-8a11-ac473d0e04a0'] <QuerySet []>

The offical assert failed

AssertionError: warning('Backup quantity is 0, supplied uuids %s, matched machines: %s', ['232d7937-975c-457b-8a11-ac473d0e04a0'], <QuerySet []>) call not found

Unless I missed something very obvious, I don't know why the assert_any_call would fail?


Solution

  • VirtualMachine.objects.none() may return an empty QuerySet, but that doesn't mean it's going to compare equal to another empty QuerySet. The value you pass to assert_any_call isn't the same object that was passed to the actual warning call, and isn't equal to that original object either.

    For your assertion to pass, the querysets would have to compare equal with ==.