Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Pytest patch field's default attribute for inherited django model

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda