Saturday 3 October 2020

Mock out a model field validator in Django

According to the patch documentation to mock out a certain function. We have to mock it from the module in which it is being used/called.

a.py
def function_to_mock(x):
   print('Called function to mock')

b.py
from a import function_to_mock

def function_to_test(some_param):
    function_to_mock(some_param)

# According to the documentation 
#if we want to mock out function_to_mock in a
# test we have to patch it from the b.py module because that is where 
# it is called from

class TestFunctionToTest(TestCase):

    @patch('b.function_to_mock')
    def test_function_to_test(self, mock_for_function_to_mock):
        function_to_test()
        mock_for_function_to_mock.assert_called_once()

# this should mock out function to mock and the assertion should work

I got myself in a situation where I cant tell exactly how to mock the function in question. Here is the situation.

# some application
validators.py
def validate_a_field(value):
    # do your validation here.

models.py
from .validators import validate_a_field

class ModelClass(models.Model):
      a_field = models.CharField(max_length=25, validators=[validate_a_field])

forms.py
class ModelClassModelForm(forms.ModelForm):
      class Meta:
           model = ModelClass
           fields = ['a_field',]

Finally in my tests.py
tests.py

class TestModelClassModelForm(TestCase):
      @patch('models.validate_a_field') <<< What to put here ???
      def test_valid_data_validates(self, validate_a_field_mock):
           data = {'a_field':'Some Text'}
           validate_a_field_mock.return_value = True

           form = ModelClassModelForm(data=data)
           is_valid = form.is_valid()
           validate_a_field_mock.assert_called_once() <<< This is failing

From my little understanding even though validate_a_field is called in models.py. It is never used from there when validation takes place. It therefore is not mocked when I patch as models.validate_a_field

My best guess is that it is called somewhere in django.forms.field. But I dont know how or where.

Does anyone know how to resolve this conundrum. I do have to mock out validate_a_field because it does call external apis which is more of an integration test. I want to write a unittest.



from Mock out a model field validator in Django

No comments:

Post a Comment