#python #django #pytest
Вопрос:
Я использую mixer
модуль для создания тестовых объектов. Ниже приведен класс модели.
class ContactQueue(models.Model):
arn = models.CharField(max_length=255)
queue_id = models.CharField(max_length=50)
queue_type = models.CharField(max_length=25)
name = models.CharField(max_length=25, null=True)
description = models.TextField(null=True)
outbound_caller_config = models.JSONField()
hours_of_operation_id = models.CharField(max_length=50)
status = models.CharField(
max_length=10,
choices=(
('ENABLED', 'ENABLED'),
('DISABLED', 'DISABLED'),
)
)
Это тестовый пример, который я написал.
class TestContactQueue:
def test_model(self):
obj = mixer.blend("dashboard.ContactQueue")
assert obj.pk == 1, "Should create a ContactQueue instance"
Во время выполнения тестов я получил эту ошибку.
__________________________________________________________________ TestContactQueue.test_model ___________________________________________________________________
self = <dashboard.tests.test_models.TestContactQueue object at 0x000002329C1C8548>
def test_model(self):
> obj = mixer.blend("dashboard.ContactQueue")
dashboardteststest_models.py:21:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..venvlibsite-packagesmixermain.py:568: in blend
return type_mixer.blend(**values)
..venvlibsite-packagesmixermain.py:116: in blend
for name, value in defaults.items()
..venvlibsite-packagesmixermain.py:113: in <genexpr>
value.gen_value(self, name, value)
..venvlibsite-packagesmixermix_types.py:229: in gen_value
return type_mixer.gen_field(field)
..venvlibsite-packagesmixerbackenddjango.py:273: in gen_field
return super(TypeMixer, self).gen_field(field)
..venvlibsite-packagesmixermain.py:193: in gen_field
return self.gen_value(field.name, field, unique=unique)
..venvlibsite-packagesmixermain.py:238: in gen_value
fab = self.get_fabric(field, field_name, fake=fake)
..venvlibsite-packagesmixermain.py:282: in get_fabric
self.__fabrics[key] = self.make_fabric(field.scheme, field_name, fake)
..venvlibsite-packagesmixerbackenddjango.py:330: in make_fabric
fcls, field_name=fname, fake=fake, kwargs=kwargs)
..venvlibsite-packagesmixermain.py:301: in make_fabric
factory=self.__factory).blend, **kwargs)
..venvlibsite-packagesmixermain.py:55: in __call__
cls_type, mixer=mixer, factory=factory, fake=fake)
..venvlibsite-packagesmixermain.py:88: in __init__
self.__fields = _.OrderedDict(self.__load_fields())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <TypeMixer <class 'django.db.models.fields.json.JSONField'>>
def __load_fields(self):
> private_fields = getattr(self.__scheme._meta, 'private_fields', [])
E AttributeError: Mixer (dashboard.ContactQueue): type object 'JSONField' has no attribute '_meta'
..venvlibsite-packagesmixerbackenddjango.py:389: AttributeError
----------------------------------------------------------------------- Captured log call ------------------------------------------------------------------------
ERROR mixer:main.py:574 Traceback (most recent call last):
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 568, in blend
return type_mixer.blend(**values)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 116, in blend
for name, value in defaults.items()
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 113, in <genexpr>
value.gen_value(self, name, value)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermix_types.py", line 229, in gen_value
return type_mixer.gen_field(field)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixerbackenddjango.py", line 273, in gen_field
return super(TypeMixer, self).gen_field(field)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 193, in gen_field
return self.gen_value(field.name, field, unique=unique)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 238, in gen_value
fab = self.get_fabric(field, field_name, fake=fake)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 282, in get_fabric
self.__fabrics[key] = self.make_fabric(field.scheme, field_name, fake)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixerbackenddjango.py", line 330, in make_fabric
fcls, field_name=fname, fake=fake, kwargs=kwargs)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 301, in make_fabric
factory=self.__factory).blend, **kwargs)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 55, in __call__
cls_type, mixer=mixer, factory=factory, fake=fake)
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixermain.py", line 88, in __init__
self.__fields = _.OrderedDict(self.__load_fields())
File "c:usersamandesktopmy_projectvenvlibsite-packagesmixerbackenddjango.py", line 389, in __load_fields
private_fields = getattr(self.__scheme._meta, 'private_fields', [])
AttributeError: Mixer (dashboard.ContactQueue): type object 'JSONField' has no attribute '_meta'
Ответ №1:
Вы должны явно задать все поля JSON, которые у вас есть в модели, blend повторит их и проверит их свойство _meta, если вы этого не сделаете