带固定装置的DJANGO测试可为外国竞争或诚信

发布于 2025-02-07 02:32:22 字数 972 浏览 1 评论 0原文

我正在尝试为我们拥有的Django Restapi编写测试用例,但我对固定装置的加载有问题。当我只有一个测试柜时,一切正常,但是当我在第二个django应用中添加第二个测试柜时,我会得到django.db.utils.integrityerror。我的最初目的是创建一个通用测试用柜,在该测试柜中我在setuptestdata函数中设置了最常用的对象,并使其他测试从该功能继承。

我尝试过的事情:

  • 使用rest_framework和django的testcase中的apitestcase
  • 不使用继承,并且使用django中的testCase使用
  • setup方法使用两个文件,而不是
  • 使用django.core.core.core.core.core..core..cormand.come..come..come..come..come..come..come..command。类变量中的类
  • 仅在测试柜中声明固定装置的首先执行的固定装置(这使得另一个测试箱具有空的db,因此清楚地,对于每个testcase Info重新创建了每个testcase信息)
  • 在固定灯变量中更改了文件的顺序

。我评论了另一个作品的测试文件之一,反之亦然。当我将call_command使用带有冗长=

django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table "preference_questions" violates foreign key constraint "preference_questi_preference_questi_64f61c66_fk_prefer" DETAIL:  Key (preference_question_category_id)=(2) is not present in table "preference_question_category"

2上面提到的。

I am trying to write test cases for the Django RestAPI that we have but I have an issue with the fixtures loading. Everything works correctly when I have only one TestCase but when I add a second TestCase in a second django app I get django.db.utils.IntegrityError. My original intention was to create a general TestCase where I set up the most used objects in the setUpTestData function and make the other tests inherit from that one.

Things I have tried:

  • Using APITestCase from rest_framework and TestCase from django
  • Not using inheritance, and having both files using TestCase from django
  • Using setUp method instead of setUpTestData
  • Using call_command from django.core.management in the setUpTestData method instead of creating the fixtures in each class in the class variable
  • Declaring the fixtures only in the TestCase that is executed first (this makes the other TestCase have an empty DB, so it it clear that for each TestCase info is recreated)
  • Changed the order of the files in the fixtures variable

When I comment one of the test files the other works and vice-versa. When I used the call_command with verbose=2, the fixtures in the first file executed work perfectly, and it breaks just when trying to install the first fixture of the second file, with the error:

django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table "preference_questions" violates foreign key constraint "preference_questi_preference_questi_64f61c66_fk_prefer" DETAIL:  Key (preference_question_category_id)=(2) is not present in table "preference_question_category"

Sometimes it gives ForeignKeyViolation depending on the case of the above mentioned.

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

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

发布评论

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

评论(1

夜光 2025-02-14 02:32:23

事实证明,问题是固定装置上有ID,因为对象之间有关系,因此无法删除这些ID。在这种情况下,我找到了两个选项:

  • 使用此 post ,这使您可以为天然钥匙创建新方法,并
  • 在setuptestdata中手动创建对象(劳动密集型但不会给您任何问题),

具体取决于您的情况。使用一个或另一个更有趣。但是,第一个可能是最好的。

Turns out the issue was that the fixtures had IDs on them, those IDs cannot be removed because objects had relations between them. In this case, there are two options I found:

  • Use natural keys as explained in this post, this makes you create new methods for the natural keys and dump your DB
  • Create the objects manually in the setUpTestData (labor intensive but won't give you any problems)

Depending on your case it may be more interesting to use one or the other. Nonetheless, the first one is the best probably.

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