Merge timings fix (DTF-WEB-9)
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 6m48s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m12s

This commit is contained in:
Cauê Faleiros
2026-11-12 15:34:41 -03:00
2 changed files with 37 additions and 6 deletions

View File

@@ -253,17 +253,31 @@ class Item(StrictModel):
raise ValueError('Resolution warning must be acknowledged')
return self
TIMING_MAX = {'page_ms': 600000, 'upload_ms': 3600000, 'upload_bytes': 10**12,
'analysis_ms': 3600000, 'montage_ms': 3600000}
class ClientTimings(StrictModel):
"""What the browser measured on the way to a quote (item 0.12).
They are only numbers for Números; a cart may send none of them, and the
server never uses them to price or accept an order.
server never uses them to price or accept an order: a value out of range
(a cart left open for hours, DTF-WEB-9) is dropped, never refused.
"""
page_ms: int | None = Field(default=None, ge=0, le=600000)
upload_ms: int | None = Field(default=None, ge=0, le=3600000)
upload_bytes: int | None = Field(default=None, ge=0, le=10**12)
analysis_ms: int | None = Field(default=None, ge=0, le=3600000)
montage_ms: int | None = Field(default=None, ge=0, le=3600000)
page_ms: int | None = None
upload_ms: int | None = None
upload_bytes: int | None = None
analysis_ms: int | None = None
montage_ms: int | None = None
@model_validator(mode='before')
@classmethod
def drop_out_of_range(cls, data):
if not isinstance(data, dict):
return data
return {name: value for name, value in data.items()
if name not in TIMING_MAX or (isinstance(value, int) and not isinstance(value, bool)
and 0 <= value <= TIMING_MAX[name])}
class QuoteRequest(StrictModel):

View File

@@ -31,5 +31,22 @@ class SpeedFiguresTests(unittest.TestCase):
self.assertTrue(all(row['average'] is None and row['samples'] == 0 for row in rows))
class ClientTimingsTests(unittest.TestCase):
def test_a_value_out_of_range_is_dropped_not_refused(self):
"""DTF-WEB-9: a cart left open for 6 h sent upload_ms 23556292 and
the customer could not pay."""
from app.core.models import ClientTimings
timings = ClientTimings.model_validate({'upload_ms': 23556292, 'analysis_ms': 5000,
'page_ms': -1, 'montage_ms': 1.5, 'upload_bytes': True})
self.assertEqual(timings.model_dump(), {'page_ms': None, 'upload_ms': None, 'upload_bytes': None,
'analysis_ms': 5000, 'montage_ms': None})
def test_an_unknown_field_is_still_refused(self):
from pydantic import ValidationError
from app.core.models import ClientTimings
with self.assertRaises(ValidationError):
ClientTimings.model_validate({'other_ms': 1})
if __name__ == '__main__':
unittest.main()