- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 Django 2.0 项目中,我的单元测试出现以下问题,但我找不到原因。
-- 更新:我正在使用 Postgres 10.1。当我切换到 sqlite3 时没有出现问题
我正在实现一个模型来跟踪另一个模型的任何变化
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Investment(models.Model):
"""the main model"""
status = models.IntegerField()
class InvestmentStatusTrack(models.Model):
"""track every change of status on an investment"""
investment = models.ForeignKey(Investment, on_delete=models.CASCADE)
status = models.IntegerField()
modified_on = models.DateTimeField(
blank=True, null=True, default=None, verbose_name=_('modified on'), db_index=True
)
modified_by = models.ForeignKey(
User, blank=True, null=True, default=None, verbose_name=_('modified by'), on_delete=models.CASCADE
)
class Meta:
ordering = ('-modified_on', )
def __str__(self):
return '{0} - {1}'.format(self.investment, self.status)
@receiver(post_save, sender=Investment)
def handle_status_track(sender, instance, created, **kwargs):
"""add a new track every time the investment status change"""
request = get_request() # a way to get the current request
modified_by = None
if request and request.user and request.user.is_authenticated:
modified_by = request.user
InvestmentStatusTrack.objects.create(
investment=instance, status=instance.status, modified_on=datetime.now(), modified_by=modified_by
)
我的大部分单元测试都因以下回溯而失败
Traceback (most recent call last):
File "/env/lib/python3.6/site-packages/django/test/testcases.py", line 209, in __call__
self._post_teardown()
File "/env/lib/python3.6/site-packages/django/test/testcases.py", line 893, in _post_teardown
self._fixture_teardown()
File "/env/lib/python3.6/site-packages/django/test/testcases.py", line 1041, in _fixture_teardown
connections[db_name].check_constraints()
File "/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 235, in check_constraints
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
File "/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "investments_investmentstatustrack" violates foreign key constraint "investments_investme_modified_by_id_3a12fb21_fk_auth_user"
DETAIL: Key (modified_by_id)=(1) is not present in table "auth_user".
知道如何解决这个问题吗?
-- 更新:显示问题的 2 个单元测试。
两者单独执行时都成功。看来问题出现在单元测试拆解上。此时外键约束失效,因为用户已被删除。
class TrackInvestmentStatusTest(ApiTestCase):
def login(self, is_staff=False):
password = "abc123"
self.user = mommy.make(User, is_staff=is_staff, is_active=True)
self.user.set_password(password)
self.user.save()
self.assertTrue(self.client.login(username=self.user.username, password=password))
def test_add_investment(self):
"""it should add a new investment and add a track"""
self.login()
url = reverse('investments:investments-list')
data = {}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(1, Investment.objects.count())
investment = Investment.objects.all()[0]
self.assertEqual(investment.status, Investment.STATUS_IN_PROJECT)
self.assertEqual(1, InvestmentStatusTrack.objects.count())
track = InvestmentStatusTrack.objects.all()[0]
self.assertEqual(track.status, investment.status)
self.assertEqual(track.investment, investment)
self.assertEqual(track.modified_by, self.user)
self.assertEqual(track.modified_on.date(), date.today())
def test_save_status(self):
"""it should modify the investment and add a track"""
self.login()
investment_status = Investment.STATUS_IN_PROJECT
investment = mommy.make(Investment, asset=asset, status=investment_status)
investment_id = investment.id
self.assertEqual(1, InvestmentStatusTrack.objects.count())
track = InvestmentStatusTrack.objects.all()[0]
self.assertEqual(track.status, investment.status)
self.assertEqual(track.investment, investment)
self.assertEqual(track.modified_by, None)
self.assertEqual(track.modified_on.date(), date.today())
url = reverse('investments:investments-detail', args=[investment.id])
data = {
'status': Investment.STATUS_ACCEPTED
}
response = self.client.patch(url, data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(1, Investment.objects.count())
investment = Investment.objects.all()[0]
self.assertEqual(investment.id, investment_id)
self.assertEqual(investment.status, Investment.STATUS_ACCEPTED)
self.assertEqual(2, InvestmentStatusTrack.objects.count())
track = InvestmentStatusTrack.objects.all()[0]
self.assertEqual(track.status, Investment.STATUS_ACCEPTED)
self.assertEqual(track.investment, investment)
self.assertEqual(track.modified_by, self.user)
self.assertEqual(track.modified_on.date(), date.today())
track = InvestmentStatusTrack.objects.all()[1]
self.assertEqual(track.status, Investment.STATUS_IN_PROJECT)
self.assertEqual(track.investment, investment)
self.assertEqual(track.modified_by, None)
self.assertEqual(track.modified_on.date(), date.today())
最佳答案
所以我调试了测试,发现问题发生在这里。
您用于捕获请求的中间件在 self.client.login
中不起作用。因为它永远不会被调用。在您的第一个测试中,您调用
response = self.client.post(url, data=data)
这会调用中间件并设置线程请求和当前用户。但是在你的下一个测试中你有一个
investment = mommy.make(Investment, status=investment_status)
这会触发 handle_status_track
,然后获取您之前测试遗留下来的旧请求,并将 id
的用户设为 1
.但是当前用户是 id=2
,id=1
用户是在测试 1 本身中创建和销毁的。
因此,在这种情况下,您用来欺骗和捕获请求的中间件基本上是罪魁祸首。
编辑-1
该问题只会发生在测试中,不会发生在生产中。避免这种情况的一个简单解决方法是在中间件中创建 set_user
方法
def set_user(user):
current_request = get_request()
if current_request:
current_request.user = user
然后更新你的登录方式到下面
def login(self, is_staff=False):
password = "abc123"
self.user = mommy.make(User, is_staff=is_staff, is_active=True)
self.user.set_password(password)
self.user.save()
self.assertTrue(self.client.login(username=self.user.username, password=password))
set_user(self.user)
这将确保每个测试都获得正确的中间件。
错误的异常堆栈跟踪
您的异常在 lin 以下
File "/env/lib/python3.6/site-packages/django/test/testcases.py", line 1041, in _fixture_teardown
connections[db_name].check_constraints()
现在,如果您查看该行的代码
def _fixture_teardown(self):
if not connections_support_transactions():
return super()._fixture_teardown()
try:
for db_name in reversed(self._databases_names()):
if self._should_check_constraints(connections[db_name]):
connections[db_name].check_constraints()
finally:
self._rollback_atomics(self.atomics)
有try block ,那怎么会出现异常呢? testcases.py
第188行,你有
def __call__(self, result=None):
"""
Wrapper around default __call__ method to perform common Django test
set up. This means that user-defined Test Cases aren't required to
include a call to super().setUp().
"""
testMethod = getattr(self, self._testMethodName)
skipped = (
getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False)
)
if not skipped:
try:
self._pre_setup()
except Exception:
result.addError(self, sys.exc_info())
return
super().__call__(result)
if not skipped:
try:
self._post_teardown()
except Exception:
result.addError(self, sys.exc_info())
return
result.addError(self, sys.exc_info())
捕获了已经被 self._post_teardown
处理的异常,所以你得到了错误的跟踪。不确定这是错误还是边缘情况,但我的分析
关于python - Django 应用程序 : unit tests fails because of django. db.utils.IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48831595/
我在 app_name 文件夹的根目录下的 django 应用程序中运行脚本;就像在 app_name>app_name 中一样。 在脚本的顶部,我有这个: import os import sys
我正在使用 sqlite 数据库,我将模型声明为: class User(db.Model): __tablename__ = 'users' id = db.Column(db.In
我正在使用 sqlite 数据库并且我在这个要点中声明了模型 https://gist.github.com/mmahesh/7245561 我添加了一个带有事务管理器的模型实例作为 with tra
我的网页的注册 View (如下)通过 Django 的身份验证模型 User 创建了一个新用户,然后使用该新创建的用户 ID 通过我自己的模型 account.Users 添加更多信息。我的问题是,
我正在尝试使用复合主键创建一个表。它有一个整数和两个字符串字段作为键。当我提交 session 时,它会引发 IntegrityError .我在做什么有什么问题? class Targets(db.
当我尝试将新条目保存到名为“config”的表中时遇到了这个问题, class Config(models.Model): ident = models.CharField(max_lengt
找不到解决方案。我现在传递一个元组列表,其中包含要插入的信息而不是从其他表中提取的信息,问题就消失了。 我目前正在编写将要编辑表格的代码。我有几行信息要插入,但无论顺序如何,我总是在第四个元素上收到
我有 python 线程应用程序 + Postgres。我正在使用 Django 的 ORM保存到 Postgres.. 我有并发保存调用。偶尔有 2 个线程保存导致问题的相同主键。 Postgres
有人可以向我解释如何正确测试 Postgres 数据库错误,尤其是 IntegrityError。例如我有下一个测试: class TestSlugs(TestCase): # This sl
我在尝试删除 User 时收到 IntegrityError: IntegrityError: (1451, 'Cannot delete or update a parent row: a fo
在 python 中,我正在使用 importmany 填充 SQLITE 数据库,因此我可以一次导入数万行数据。我的数据包含在元组列表中。我在数据库中设置了我想要的主键。 我遇到的问题是主键错误会引
每当尝试附加文件时,我的应用程序(本地)都会引发ActiveStorage::IntegrityError错误。我如何摆脱这个错误? 我只有一个has_one_attached,但我不知道该错误是如何
我想插入几个User数据库中的行。我真的不在乎插入是否成功,只要我得到通知,在这两种情况下我都能做到,那么哪一个在性能(主要是速度)方面更好? 始终插入行(通过调用模型的 save 方法)并捕获潜在的
我开始使用 factory_boy 包,所以我设置了一些工厂并想测试创建的对象不会引发任何验证错误。 这是我正在使用的 mixin,它基本上从模块中获取每个工厂,创建一个实例,然后测试 .full_c
我有一个模型 LucyGuide,它通过 OneToOneField 扩展了 Django 的 User 模型: class LucyGuide(models.Model): user = m
我有一个模型 LucyGuide,它通过 OneToOneField 扩展了 Django 的 User 模型: class LucyGuide(models.Model): user = m
我正在尝试在 Django 中创建配方/成分模型 在我的 models.py 中我得到了 class Ingredient(models.Model): name = models.CharF
我正在使用 Django 构建一个简单的博客应用程序。我想实现使用表单添加新博客的功能。出现一些问题。 这是我的 models.py from django.db import models from
我知道还有许多其他问题与完全相同的问题有关,但我已经尝试过他们的答案,但到目前为止没有一个有效。 我正在尝试从与其他表有关系的表中删除记录。这些表中的外键是 nullable=false ,因此尝试删
我正在与普通/ guest 用户一起处理我的结帐 View ,但很难解决完整性错误。想法是让访客用户仅使用电子邮件注册才能结帐,我需要将用户电子邮件设置为唯一。 模型.py from django.c
我是一名优秀的程序员,十分优秀!