Home > Software design >  Pytest patch field's default attribute for inherited django model
Pytest patch field's default attribute for inherited django model

Time:10-12

I have the following model in common/models.py:

from django.db import models
from uuid import uuid4

class BaseModel(models.Model):
    guid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)

    class Meta:
       abstract = True

In app/models.py I have the following:

from django.db import models
from common.models import BaseModel

class Entity(BaseModel):
   name = models.CharField()

In tests I tried to patch uuid4 in the following ways:

def test_model_create(mocker):
   # Given
   mock_guid = mocker.patch("uuid.uuid4", return_value="some-guid")

   # When
   entity = Entity.objects.create(name="test_name")

   # Then
   mock_guid.assert_called_once()
   assert "some-guid" == entity.guid

mock_guid.assert_called_once() returns not called. What can be the issue here?

CodePudding user response:

I think the issue is that guid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False) references the uuid.uuid4 function directly at parsing time. When you set up your mock, it's too late to replace this pointer.

Changing it to something like guid = models.UUIDField(unique=True, default=lambda: uuid.uuid4(), editable=False) should fix your issue, because the value is returned at runtime, and your mock will be setup properly before the function is referenced and called.

  • Related