Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter value bug #7601

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/backend/InvenTree/part/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import decimal
import hashlib
import logging
import math
import os
import re
from datetime import timedelta
Expand Down Expand Up @@ -3820,6 +3821,12 @@ def calculate_numeric_value(self):
except ValueError:
self.data_numeric = None

if self.data_numeric is not None and type(self.data_numeric) is float:
# Prevent out of range numbers, etc
# Ref: https://github.com/inventree/InvenTree/issues/7593
if math.isnan(self.data_numeric) or math.isinf(self.data_numeric):
self.data_numeric = None

part = models.ForeignKey(
Part,
on_delete=models.CASCADE,
Expand Down
19 changes: 19 additions & 0 deletions src/backend/InvenTree/part/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ def test_validate(self):
t3.full_clean()
t3.save() # pragma: no cover

def test_invalid_numbers(self):
"""Test that invalid floating point numbers are correctly handled."""
p = Part.objects.first()
t = PartParameterTemplate.objects.create(name='Yaks')

valid_floats = ['-12', '1.234', '17', '3e45', '-12e34']

for value in valid_floats:
param = PartParameter(part=p, template=t, data=value)
param.full_clean()
self.assertIsNotNone(param.data_numeric)

invalid_floats = ['88E6352', 'inf', '-inf', 'nan', '3.14.15', '3eee3']

for value in invalid_floats:
param = PartParameter(part=p, template=t, data=value)
param.full_clean()
self.assertIsNone(param.data_numeric)

def test_metadata(self):
"""Unit tests for the metadata field."""
for model in [PartParameterTemplate]:
Expand Down
Loading