So this is quite a 'complex' test case i want to tackle.
I have a Django Model, named Store and that store has many methods and fields. I want to test a specific method of the Store Model, beging get_orders(self)
That get_orders(self)
method calls on an external API trough an instance of module.
So we have the MarketPlace module. That Marketplace module has several methods like, get_orders(), create_order(), etc.
The Store model has an attribute that is an instance of the Marketplace module and via that route the method is called. How do i test the Store.get_orders(self)
?
#/app/stores/models.py
class Store(models.Model):
store_name = models.CharField(max_length=100, blank=False)
marketplace_api_key = models.CharField(max_length=100, blank=False)
@property
def marketplace_instance(self):
markets = MarketPlace(api_key=self.marketplace_api_key )
return market
def get_orders(self):
# this is the method i want to mock
open_orders = self.marketplace_instance.get_open_orders()
#/app/controllers/MarketPlace/MarketPlace.py
class MarketPlace(models.Model):
api_key = models.CharField(max_length=100, blank=False)
def get_open_orders(self):
url = f'https://marketplace.com/?apikey={self.api_key}'
r = requests.get(url, headers=headers)
if r.status_code == 200:
return r.json()
else:
return None
#/app/stores/tests/test_models.py
class StoreTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.store = StoreFactory()
cls.mock_open_order_patcher = patch('controllers.MarketPlace.Marketplace.MarketPlace.get_open_orders')
cls.mock_open_order = cls.mock_open_order_patcher .start()
@classmethod
def tearDown(cls) -> None:
cls.mock_open_order_patcher .stop()
def test_get_orders_marketplace(self):
orders_response = {}
self.mock_open_order.return_value = MagicMock(ok=True)
self.mock_open_order.return_value.json.return_value = orders_response
assert self.store.get_orders()
So it worked...
By doing the test like this:
@patch.object(Store, 'get_orders')
@patch.object(Marketplace, 'get_open_orders')
def test_get_orders_marketplace(self, mock_get_id, mock_get):
....