我创建了yaml fixture
test.yaml:
- model: myapp.person
pk: 1
fields:
email: test@example.com
params: []
tests.py:
class MyTests(TestCase):
fixtures = ['test.yaml']
def test_1(self):
# import pdb; pdb.set_trace()
person = Person.objects.get(pk=1)
测试结果是
Traceback (most recent call last):
File "/home/django/myapp/tests.py", line 99, in test_part_count
person = Person.objects.get(pk=1)
File "/usr/lib/python3/dist-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python3/dist-packages/django/db/models/query.py", line 385, in get self.model._meta.object_name
myapp.models.DoesNotExist: Person matching query does not exist.
如果我取消注释pdb.set_trace()并检查Person.objects.all()
得到
(Pdb) Person
<class 'myapp.models.Person'>
(Pdb) Person.objects.all()
<QuerySet []>
所以我认为,这些装置并没有填满数据库。
所以我检查了夹具文件是否被读取。我将params改为paramz并得到了
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/django/db/models/options.py", line 617, in get_field
return self.fields_map[field_name]
KeyError: 'paramz'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/django/core/serializers/pyyaml.py", line 78, in Deserializer
for obj in PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options):
File "/usr/lib/python3/dist-packages/django/core/serializers/python.py", line 129, in Deserializer
field = Model._meta.get_field(field_name)
File "/usr/lib/python3/dist-packages/django/db/models/options.py", line 619, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Person has no field named 'paramz'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/django/test/testcases.py", line 1019, in setUpClass
'database': db_name,
File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 130, in call_command
return command.execute(*args, **defaults)
File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/usr/lib/python3/dist-packages/django/core/management/commands/loaddata.py", line 64, in handle
self.loaddata(fixture_labels)
File "/usr/lib/python3/dist-packages/django/core/management/commands/loaddata.py", line 104, in loaddata
self.load_label(fixture_label)
File "/usr/lib/python3/dist-packages/django/core/management/commands/loaddata.py", line 161, in load_label
for obj in objects:
File "/usr/lib/python3/dist-packages/django/core/serializers/pyyaml.py", line 84, in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3/dist-packages/django/core/serializers/pyyaml.py", line 78, in Deserializer
for obj in PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options):
File "/usr/lib/python3/dist-packages/django/core/serializers/python.py", line 129, in Deserializer
field = Model._meta.get_field(field_name)
File "/usr/lib/python3/dist-packages/django/db/models/options.py", line 619, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/excursor/frontoffice/fixtures/test.yaml': Person has no field named 'paramz'
所以这证明,该测试读取文件,并检查数据库。那么为什么人是空的?
更新
def setUp(self):
call_command('loaddata', 'test.yaml', verbosity=2)
我将此添加到tests.py并获得更多详细信息:
Loading 'test' fixtures...
Checking '/home/django/myapp/fixtures' for fixtures...
Checking '/home/django/fixtures' for fixtures...
No fixture 'test' in '/home/django/fixtures'.
Checking '/home/django' for fixtures...
No fixture 'test' in '/home/django'.
Installing yaml fixture 'test' from '/home/django/myapp/fixtures'.
Installed 1 object(s) from 1 fixture(s)
但:
(Pdb) Person
<class 'myapp.models.Person'>
(Pdb) Person.objects.all()
<QuerySet []>
发生了什么......
答案 0 :(得分:0)
问题在于继承并使用sqlite作为测试数据库。当我将测试数据库更改为postgresql时,日志显示问题。 所以我在夹具中你必须创建两个对象:
- model: myapp.parent
pk : 1
fields:
params: []
- model: myapp.person
pk : 1
fields:
parent_ptr_id: 1
email: test@example.com
现在一切正常。