单元测试 Django 模型保存功能
Unit Testing Django Model Save Function
我正在创建测试以检查自定义校准模型保存功能是否会更新资产记录(外键)(如果它是资产的最新校准记录)。保存功能在实时开发和生产服务器中完全按照预期执行,甚至在 django shell 中,但在测试期间似乎失败了...
models.py
class Asset(models.Model):
...
requires_calibration = models.BooleanField()
passed_calibration = models.BooleanField(default=False)
calibration_date_prev = models.DateField(null=True, blank=True)
calibration_date_next = models.DateField(null=True, blank=True)
class CalibrationRecord(models.Model):
calibration_record_id = models.AutoField(primary_key=True)
asset = models.ForeignKey(
"myapp.Asset",
on_delete=models.CASCADE,
limit_choices_to={"requires_calibration": True}
)
calibration_date = models.DateField(default=timezone.now)
calibration_date_next = models.DateField(null=True, blank=True)
calibration_outcome = models.CharField(max_length=10, default="Pass")
def save(self, *args, **kwargs):
super(CalibrationRecord, self).save(*args, **kwargs)
# Check if this is the latest calibration record for any asset, if so update asset.calibration_dates and status
latest_asset_calibration = CalibrationRecord.objects.filter(asset=self.asset.pk).order_by(
"-calibration_date", "-calibration_record_id")[0]
if self.pk == latest_asset_calibration.pk:
Asset.objects.filter(pk=self.asset.pk).update(calibration_date_prev=self.calibration_date)
if self.calibration_date_next:
Asset.objects.filter(pk=self.asset.pk).update(calibration_date_next=self.calibration_date_next)
else:
Asset.objects.filter(pk=self.asset.pk).update(calibration_date_next=None)
if self.calibration_outcome == "Pass":
Asset.objects.filter(pk=self.asset.pk).update(passed_calibration=True)
else:
Asset.objects.filter(pk=self.asset.pk).update(passed_calibration=False)
tests_models.py 测试失败示例
class CalibrationRecordTests(TestCase):
def test_calibration_record_updates_asset_cal_date_prev(self):
"""
All calibration records should update related Asset record's "calibration_date_prev" to calibration_date
"""
asset1 = Asset.objects.create(asset_description="Test Asset 2", requires_calibration=True)
self.assertIsNone(asset1.calibration_date_prev)
cal = CalibrationRecord.objects.create(asset=asset1, calibration_description="Test Calibration 2", calibration_date=timezone.now())
self.assertEqual(cal.calibration_date, asset1.calibration_date_prev)
错误日志
======================================================================
FAIL: test_calibration_record_updates_asset_cal_date_prev (assetregister.tests_models.CalibrationRecordTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\[path]\app\tests_models.py", line 159, in test_calibration_record_updates_asset_cal_date_prev
self.assertEqual(cal.calibration_date, asset1.calibration_date_prev)
AssertionError: datetime.datetime(2018, 2, 26, 12, 26, 34, 457513, tzinfo=<UTC>) != None
======================================================================
我所有与此自定义校准记录保存功能相关的测试似乎都失败了,因为相关资产记录没有在应该更新的时候更新。
知道为什么这在开发和生产期间有效但在测试期间无效吗?
尽管 .create() 方法应该自动执行 .save() 之后,我什至尝试在创建校准记录后手动执行 .save() ,但它似乎仍然失败。
已解决!
自定义保存函数正确更新数据库,但不是正在测试的模型实例!需要 刷新 模型实例以通过使用类似 [model].objects.get()
的方式再次调用它来获取任何更新
例如:
asset = Asset.objects.create(asset_description="Test Asset 2", requires_calibration=True)
self.assertIsNone(asset.calibration_date_prev)
CalibrationRecord.objects.create(asset=asset, calibration_description="Test Calibration 2",
calibration_date=timezone.now())
cal = CalibrationRecord.objects.get(calibration_description="Test Calibration 2")
asset = Asset.objects.get(asset_description="Test Asset 2")
self.assertEqual(cal.calibration_date, asset.calibration_date_prev)
如果您发现自己在这里,最好使用 Model.refresh_from_db(using=None, fields=None)
重新加载对象。参见 Django documentation
也结帐Reload django object from database
我正在创建测试以检查自定义校准模型保存功能是否会更新资产记录(外键)(如果它是资产的最新校准记录)。保存功能在实时开发和生产服务器中完全按照预期执行,甚至在 django shell 中,但在测试期间似乎失败了...
models.py
class Asset(models.Model):
...
requires_calibration = models.BooleanField()
passed_calibration = models.BooleanField(default=False)
calibration_date_prev = models.DateField(null=True, blank=True)
calibration_date_next = models.DateField(null=True, blank=True)
class CalibrationRecord(models.Model):
calibration_record_id = models.AutoField(primary_key=True)
asset = models.ForeignKey(
"myapp.Asset",
on_delete=models.CASCADE,
limit_choices_to={"requires_calibration": True}
)
calibration_date = models.DateField(default=timezone.now)
calibration_date_next = models.DateField(null=True, blank=True)
calibration_outcome = models.CharField(max_length=10, default="Pass")
def save(self, *args, **kwargs):
super(CalibrationRecord, self).save(*args, **kwargs)
# Check if this is the latest calibration record for any asset, if so update asset.calibration_dates and status
latest_asset_calibration = CalibrationRecord.objects.filter(asset=self.asset.pk).order_by(
"-calibration_date", "-calibration_record_id")[0]
if self.pk == latest_asset_calibration.pk:
Asset.objects.filter(pk=self.asset.pk).update(calibration_date_prev=self.calibration_date)
if self.calibration_date_next:
Asset.objects.filter(pk=self.asset.pk).update(calibration_date_next=self.calibration_date_next)
else:
Asset.objects.filter(pk=self.asset.pk).update(calibration_date_next=None)
if self.calibration_outcome == "Pass":
Asset.objects.filter(pk=self.asset.pk).update(passed_calibration=True)
else:
Asset.objects.filter(pk=self.asset.pk).update(passed_calibration=False)
tests_models.py 测试失败示例
class CalibrationRecordTests(TestCase):
def test_calibration_record_updates_asset_cal_date_prev(self):
"""
All calibration records should update related Asset record's "calibration_date_prev" to calibration_date
"""
asset1 = Asset.objects.create(asset_description="Test Asset 2", requires_calibration=True)
self.assertIsNone(asset1.calibration_date_prev)
cal = CalibrationRecord.objects.create(asset=asset1, calibration_description="Test Calibration 2", calibration_date=timezone.now())
self.assertEqual(cal.calibration_date, asset1.calibration_date_prev)
错误日志
======================================================================
FAIL: test_calibration_record_updates_asset_cal_date_prev (assetregister.tests_models.CalibrationRecordTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\[path]\app\tests_models.py", line 159, in test_calibration_record_updates_asset_cal_date_prev
self.assertEqual(cal.calibration_date, asset1.calibration_date_prev)
AssertionError: datetime.datetime(2018, 2, 26, 12, 26, 34, 457513, tzinfo=<UTC>) != None
======================================================================
我所有与此自定义校准记录保存功能相关的测试似乎都失败了,因为相关资产记录没有在应该更新的时候更新。
知道为什么这在开发和生产期间有效但在测试期间无效吗?
尽管 .create() 方法应该自动执行 .save() 之后,我什至尝试在创建校准记录后手动执行 .save() ,但它似乎仍然失败。
已解决!
自定义保存函数正确更新数据库,但不是正在测试的模型实例!需要 刷新 模型实例以通过使用类似 [model].objects.get()
例如:
asset = Asset.objects.create(asset_description="Test Asset 2", requires_calibration=True)
self.assertIsNone(asset.calibration_date_prev)
CalibrationRecord.objects.create(asset=asset, calibration_description="Test Calibration 2",
calibration_date=timezone.now())
cal = CalibrationRecord.objects.get(calibration_description="Test Calibration 2")
asset = Asset.objects.get(asset_description="Test Asset 2")
self.assertEqual(cal.calibration_date, asset.calibration_date_prev)
如果您发现自己在这里,最好使用 Model.refresh_from_db(using=None, fields=None)
重新加载对象。参见 Django documentation
也结帐Reload django object from database