2016-09-16 74 views
0

我創建YAML夾具夾具處理但未加載(繼承的模型)

test.yaml:

- model: myapp.person 
    pk: 1 
    fields: 
    email: [email protected] 
    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_跟蹤(),並檢查了Person.objects.all()

(Pdb) Person 
<class 'myapp.models.Person'> 
(Pdb) Person.objects.all() 
<QuerySet []> 

所以我認爲,燈具不填充數據庫。

所以我檢查夾具文件是否被讀取。我改變參數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' 

所以這是證明,測試讀取文件,並與數據庫檢查。那麼爲什麼人是空的?

UPDATE

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

問題在於繼承並使用sqlite作爲測試數據庫。當我將測試數據庫更改爲postgresql時,日誌顯示問題。 所以我在燈具必須創建兩個對象:

- model: myapp.parent 
    pk : 1 
    fields: 
    params: [] 
- model: myapp.person 
    pk : 1 
    fields: 
    parent_ptr_id: 1 
    email: [email protected] 

而現在一切正常。