Testing the cancel order function 

The first method that we are going to test is the cancel_orders method. As the name says, it will cancel an order. There a few things we want to test in this method:

  • The first test is quite straightforward; we only want to test if we can cancel an order, setting its status to Cancelled
  • The second test is that it shouldn't be possible to cancel orders that have not been received; in other words, only the orders that have the current status set to Received can be canceled
  • We need to test if the correct exception is raised in case we pass an invalid argument to the cancel_order method

With that said, let's add our tests:

def test_cancel_order(self):    order = Order.objects.get(pk=1) self.assertIsNotNone(order) ...

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.