Fix clean functions so unit tests pass

This commit is contained in:
Oliver Walters 2020-05-16 17:43:32 +10:00
parent 40735d66a1
commit a6ad263ee7
2 changed files with 23 additions and 12 deletions

View File

@ -60,11 +60,14 @@ class Build(MPTTModel):
super().clean() super().clean()
if self.part.trackable: try:
if not self.quantity == int(self.quantity): if self.part.trackable:
raise ValidationError({ if not self.quantity == int(self.quantity):
'quantity': _("Build quantity must be integer value for trackable parts") raise ValidationError({
}) 'quantity': _("Build quantity must be integer value for trackable parts")
})
except PartModels.Part.DoesNotExist:
pass
title = models.CharField( title = models.CharField(
verbose_name=_('Build Title'), verbose_name=_('Build Title'),

View File

@ -215,6 +215,8 @@ class StockItem(MPTTModel):
- Quantity must be 1 if the StockItem has a serial number - Quantity must be 1 if the StockItem has a serial number
""" """
super().clean()
if self.status == StockStatus.SHIPPED and self.sales_order is None: if self.status == StockStatus.SHIPPED and self.sales_order is None:
raise ValidationError({ raise ValidationError({
'sales_order': "SalesOrder must be specified as status is marked as SHIPPED", 'sales_order': "SalesOrder must be specified as status is marked as SHIPPED",
@ -227,14 +229,20 @@ class StockItem(MPTTModel):
'status': 'Status cannot be marked as ASSIGNED_TO_OTHER_ITEM if the belongs_to field is not set', 'status': 'Status cannot be marked as ASSIGNED_TO_OTHER_ITEM if the belongs_to field is not set',
}) })
if self.part.trackable: try:
# Trackable parts must have integer values for quantity field! if self.part.trackable:
if not self.quantity == int(self.quantity): # Trackable parts must have integer values for quantity field!
raise ValidationError({ if not self.quantity == int(self.quantity):
'quantity': _('Quantity must be integer value for trackable parts') raise ValidationError({
}) 'quantity': _('Quantity must be integer value for trackable parts')
})
except PartModels.Part.DoesNotExist:
# For some reason the 'clean' process sometimes throws errors because self.part does not exist
# It *seems* that this only occurs in unit testing, though.
# Probably should investigate this at some point.
pass
if self.quantity <= 0: if self.quantity < 0:
raise ValidationError({ raise ValidationError({
'quantity': _('Quantity must be greater than zero') 'quantity': _('Quantity must be greater than zero')
}) })