测试时 Django Fixtures 加载顺序是否错误?

发布于 2024-09-16 02:30:24 字数 1640 浏览 17 评论 0原文

我正在测试我的应用程序,但遇到了问题,我不知道为什么。我正在加载用于测试的装置,并且这些装置具有相互依赖的外键。它们必须按特定顺序加载,否则将无法工作。

我正在加载的装置是:

["test_company_data", "test_rate_index", 'test_rate_description']

公司数据是第一个。 test_rate_index 具有公司的外键,test_rate_description 具有 test_rate_index 中声明的模型的外键。 (顺便说一句,不同的测试需要不同的装置,这就是为什么我不只是把所有东西都推到一个)

如果我使用 django 的标准程序来加载测试,测试不会以正确的顺序加载。

class TestPackages(test.TestCase):
    fixtures = ["test_company_data", "test_rate_index", "test_rate_description",]

我收到消息

DoesNotExist: RateDescription matching query does not exist.

但是,如果我颠倒固定装置的顺序(这没有意义),它就会起作用:

fixtures = ["test_rate_description", "test_company_data", "test_rate_index",]

Django 的文档 指出固定装置按照声明的顺序加载,但情况似乎并非如此。

作为一种解决方法,我没有使用 django,而是

    call_command('loaddata', *fixtures, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                         })

在 setUp 方法中使用不同的函数,一次加载一个灯具。

def load_fixtures(fixtures):
    for fixture in fixtures:
        call_command('loaddata', fixture, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                            })

在尝试使用标准方法时,是否有我做错或不理解的事情导致我的灯具无法按正确的顺序加载?

I am testing my application and I am running into an issue and I'm not sure why. I'm loading fixtures for my tests and the fixtures have foreign keys that rely on each other. They must be loaded in a certain order or it won't work.

The fixtures I'm loading are:

["test_company_data", "test_rate_index", 'test_rate_description']

Company data is the first one. test_rate_index has a foreign key to company, and test_rate_description has a foreign key to a model declared in test_rate_index. (as an aside, different test need different fixtures which is why I'm not just shoving everything in one)

If I use django's standard procedure for loading tests, the tests do not load in the proper order.

class TestPackages(test.TestCase):
    fixtures = ["test_company_data", "test_rate_index", "test_rate_description",]

I get the message

DoesNotExist: RateDescription matching query does not exist.

But if I reverse the order of my fixtures (which makes no sense) it works:

fixtures = ["test_rate_description", "test_company_data", "test_rate_index",]

Django's documentation states that the fixtures load in the order they are declared, but this doesn't seem to be the case.

As a workaround, instead of using django's

    call_command('loaddata', *fixtures, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                         })

I'm using a different function in the setUp method that loads the fixtures one at a time.

def load_fixtures(fixtures):
    for fixture in fixtures:
        call_command('loaddata', fixture, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                            })

Is there something I'm doing incorrectly or not understanding that is causing my fixtures not to be loaded in the proper order when trying to use the standard method?

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-09-23 02:30:24

Django 的文档指出,装置按照声明的顺序加载,但情况似乎并非如此。

这当然很奇怪。当我测试我的一个项目(Django 1.2.1、Python 2.6.2、Postgresql 8.3.11)时,装置会以正确的顺序加载。

这是我要解决的问题。

DoesNotExist:RateDescription 匹配查询不存在。

  1. 您在加载夹具或执行测试时是否遇到此错误?你能找到引起这个问题的装置/代码吗?如果需要,请增加详细程度。

  2. 您可以尝试从命令行触发 loaddata 命令吗?调用它三次,并以正确的预期顺序为每次调用传递一个灯具的名称。并查看灯具是否已加载。

  3. 我知道您可能已经这样做了,但是您能否确保第一个和第二个灯具不包含任何 RateDescription 数据?

Django's documentation states that the fixtures load in the order they are declared, but this doesn't seem to be the case.

This is certainly strange. Fixtures are getting loaded in the proper order when I tested one of my projects (Django 1.2.1, Python 2.6.2, Postgresql 8.3.11).

Here is what I'd do to troubleshoot.

DoesNotExist: RateDescription matching query does not exist.

  1. Are you getting this error when loading a fixture or when executing a test? Can you find the fixture/code that is raising this? Increase verbosity if need be.

  2. Can you try firing a loaddata command from the command line? Call it three times, passing the name of one fixture for each call in the proper expected sequence. And see if the fixtures get loaded.

  3. I know you'd probably have done this already but can you make sure that the first and second fixtures do not contain any RateDescription data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文