From 232917907065627bc48c8b4b34b9ea8e4b97d9d5 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 9 Jul 2024 21:49:15 +1000 Subject: [PATCH] Parameter value bug (#7601) (#7602) * Handle out-of range numerical values * Add unit test (cherry picked from commit 84d076848a901a1f40e636c6c593ca0004f1883e) Co-authored-by: Oliver --- src/backend/InvenTree/part/models.py | 7 +++++++ src/backend/InvenTree/part/test_param.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/backend/InvenTree/part/models.py b/src/backend/InvenTree/part/models.py index b0e020efa3..cfa609ac19 100644 --- a/src/backend/InvenTree/part/models.py +++ b/src/backend/InvenTree/part/models.py @@ -5,6 +5,7 @@ from __future__ import annotations import decimal import hashlib import logging +import math import os import re from datetime import datetime, timedelta @@ -3757,6 +3758,12 @@ class PartParameter(InvenTree.models.InvenTreeMetadataModel): 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, diff --git a/src/backend/InvenTree/part/test_param.py b/src/backend/InvenTree/part/test_param.py index 314fe11eb5..ad1cc609a3 100644 --- a/src/backend/InvenTree/part/test_param.py +++ b/src/backend/InvenTree/part/test_param.py @@ -47,6 +47,25 @@ class TestParams(TestCase): 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]: