如何获得有关夹具未加载原因的更多详细信息?

发布于 2024-09-02 22:40:04 字数 2512 浏览 6 评论 0原文

我有一个测试用例似乎没有加载固定装置。

我在构建测试数据库时看到此错误:

No fixtures found.  
.............................................Problem installing fixture '/Users/Bryan/work/CNPROG/forum/fixtures/forum_fixtures.json': Traceback (most recent call last):  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle  
    obj.save(using=using)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save  
    models.Model.save_base(self.object, using=using, raw=True)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/base.py", line 543, in save_base  
    created=(not record_exists), raw=raw)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 162, in send  
    response = receiver(signal=self, sender=sender, **named)  
  File "/Users/Bryan/work/CNPROG/forum/models.py", line 656, in record_ask_event  
    activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/fields/related.py", line 302, in __get__  
    rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 341, in get  
    % self.model._meta.object_name)  
DoesNotExist: User matching query does not exist.  



class UserProfileTestCases(TestCase):  
    """These are tests to verify that refactoring of UserProfile is correct"""  
    fixtures = ['forum_fixtures.json'] # apparently this needs to be in fixtures/ directory.  
    def setUp(self):  
        self.client = Client()  
        if self.client.login(username='DickCheney', password='test'):   
            print "client.login DickCheney successful";    
        else:   
            print "client.login FAILED"  

由于某种原因,装置未加载。

该夹具位于:
forum/fixtures/forum_fixtures.json

如何输出夹具未加载的原因?

回溯表明这里发生了一些事情:
文件“/Users/Bryan/work/CNPROG/forum/models.py”,第 656 行,在 record_ask_event

但我无法想象为什么这会影响灯具的加载。 当我查看代码时,通过 post_save 事件调用 record_ask_events。
我能够成功 manage.py loaddata forum_fixtures 所以我相信我设置正确。

I have a TestCase that doesn't seem to load the fixtures.

I'm seeing this error as the test database is being built:

No fixtures found.  
.............................................Problem installing fixture '/Users/Bryan/work/CNPROG/forum/fixtures/forum_fixtures.json': Traceback (most recent call last):  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle  
    obj.save(using=using)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save  
    models.Model.save_base(self.object, using=using, raw=True)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/base.py", line 543, in save_base  
    created=(not record_exists), raw=raw)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 162, in send  
    response = receiver(signal=self, sender=sender, **named)  
  File "/Users/Bryan/work/CNPROG/forum/models.py", line 656, in record_ask_event  
    activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/fields/related.py", line 302, in __get__  
    rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 341, in get  
    % self.model._meta.object_name)  
DoesNotExist: User matching query does not exist.  



class UserProfileTestCases(TestCase):  
    """These are tests to verify that refactoring of UserProfile is correct"""  
    fixtures = ['forum_fixtures.json'] # apparently this needs to be in fixtures/ directory.  
    def setUp(self):  
        self.client = Client()  
        if self.client.login(username='DickCheney', password='test'):   
            print "client.login DickCheney successful";    
        else:   
            print "client.login FAILED"  

For some reason the fixtures are not loading.

The fixture is located at:
forum/fixtures/forum_fixtures.json

How can I output the reason that the fixture is not loading?

The Traceback suggests something is happening here:
File "/Users/Bryan/work/CNPROG/forum/models.py", line 656, in record_ask_event

But I can't imagine why that would affect the loading of the fixtures.
When I looked at the code, record_ask_events is called via post_save event.
I was able to successfully manage.py loaddata forum_fixtures so I believe I set them up correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-09-09 22:40:04

使用以下命令运行更详细的测试:
python manage.py test --verbosity=2

我通过重新排列灯具的顺序解决了这个问题。

如果您有相关对象,则需要确保具有最多关联的对象(例如 auth_user )在固定装置中首先列出,因为相关对象可能在保存期间被调用。

Django v1.2 仅查找名为“initial_data”的装置。没有一次搜索“forum_fixtures.json”,

我将夹具名称更改为“initial_data.json”,现在它可以工作了。

Run tests with greater detail with this command:
python manage.py test --verbosity=2

I fixed this by rearranging the order of the fixtures.

If you have Related objects, you need to make sure the objects that have the most associations ( e.g. auth_user ) is listed first in the fixture because the related objects may be called during a save.

Django v1.2 only looked for fixtures named 'initial_data'. Not once was there are search for 'forum_fixtures.json'

I changed the fixture name to 'initial_data.json' and it now works.

年少掌心 2024-09-09 22:40:04

我遇到了这个问题。我的应用程序包含在我的设置文件中的 INSTALLED_APPS 元组中。因此,根据 django 文档,它应该在应用程序目录下的装置目录中搜索我放置在 TestCase 类的“装置”属性中的任何文件名。不起作用。不想将我的装置命名为“initial_data.json”,因为我只希望在运行单元测试时使用它。

I'm having this exact problem. My app is included in my INSTALLED_APPS tuple in my settings file. So according to django docs, it should search the fixtures directory under the app directory for any filenames I place in the 'fixtures' attribute of my TestCase class. Doesn't work. Don't want to name my fixture 'initial_data.json' because I only want it to be used when I run unit tests.

烟若柳尘 2024-09-09 22:40:04

您需要更改 post_save 信号处理程序以尊重“原始”kwarg。如果您对信号处理程序执行以下操作,则在夹具加载期间不会导致错误:

@receiver(post_save, sender=MyModel, dispatch_uid='MyModelUID')
def handler(sender, *args, **kwargs):
    if kwargs.get('raw'):
        return
    else:
        ...stuff

You'll need to change your post_save signal handler to respect the 'raw' kwarg. Your signal handler would not cause errors during fixture loading if you did something like this with it:

@receiver(post_save, sender=MyModel, dispatch_uid='MyModelUID')
def handler(sender, *args, **kwargs):
    if kwargs.get('raw'):
        return
    else:
        ...stuff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文