Files
dtf-system/tests/test_sheet_gap.py
Cauê Faleiros 083441583a
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 8m24s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m14s
feat: each product's metres are summed and rounded once, and the 1 m minimum is once per order (Marcus, 2026-12-03)
Marcus: "o pedido mínimo de um metro deve ser somente para o primeiro
metro, após isso deve se somar a metragem de cada arquivo", "Soma tudo e
arredonda uma vez só", and for different products "Pode ser sim": each
product apart at its price, the minimum for the whole order. Order #246
(three ready sheets of 3,74, 0,24 and 0,22 m) was 5,80 m with 1 m per item;
it is now 4,20 m.

pricing.bill_order replaces the per-item minimum and bill_splits: per
product the exact lengths (a ready sheet without its gaps) are rounded once
with the 8 mm grace, the minimum's difference goes to the first item's
product, and each product's total is shared back to its items in 10 cm
steps (each its length rounded down, the missing steps to the largest
fractions, the minimum to the product's first item), for Tiny's 10 cm SKUs
and the cards. A share of 0 m adds no Tiny line. The print file's guard
allows an item one 10 cm step under its length. The Site cart shares the
same way (cobrarPedido), split sheets round once, the buy box says the
items of a product are summed and rounded once, and the Montador's
estimate rounds its sum once. Contract: output/revisao/contrato-minimo-pedido.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:46:40 -03:00

134 lines
6.3 KiB
Python

"""8 mm of clear film between the copies of a ready sheet (Marcus, 2026-11-26):
a new cart must carry it; a quote made before is still reviewed.
Run from the repository root: python3 -m unittest tests.test_sheet_gap -v.
"""
import unittest
from uuid import uuid4
from app.core.models import SHEET_GAP_CHANGED, Item, check_sheet_gaps
def sheet(copies=3, gap=0.8, mode='file'):
upload = str(uuid4())
width = 27 if mode == 'uvfile' else 57
length = 20
placements = [{'source_index': 0, 'copy_index': i, 'x_cm': 0, 'y_cm': round(i * (length + gap), 4),
'width_cm': width, 'length_cm': length, 'rotation_degrees': 0, 'mirrored': False}
for i in range(copies)]
height = round(copies * length + (copies - 1) * gap, 4)
return Item.model_validate({
'mode': mode, 'metres': str(height / 100), 'grade': 100, 'uploads': [upload],
'quality_status': 'ok', 'quality_acknowledged': False,
'production': {'version': 3, 'film_width_cm': width, 'height_cm': height,
'sources': [{'upload_id': upload, 'kind': 'sheet', 'width_cm': width,
'length_cm': length, 'copies': copies, 'measurement': 'file'}],
'placements': placements}})
class SheetGapTests(unittest.TestCase):
def test_the_print_file_takes_the_unbilled_gaps(self):
# Marcus, 2026-12-03: the gaps stay on the film but are not billed, so
# 10 copies of 20 cm (200 cm billed, 207,2 cm laid out) still print.
try:
from app.printfile import Unsupported, check_layout
except ImportError: # the host job has no imaging libraries
self.skipTest('app.printfile needs the runtime image')
item = sheet(copies=10).model_dump(mode='json')
item['billed_metres'] = '2'
check_layout(item, [(5700, 2000)])
item['billed_metres'] = '1.8' # more than one 10 cm step short
with self.assertRaises(Unsupported):
check_layout(item, [(5700, 2000)])
def test_copies_8_mm_apart_pass(self):
check_sheet_gaps([sheet()])
check_sheet_gaps([sheet(copies=1, gap=0)])
check_sheet_gaps([sheet(mode='uvfile')])
def test_copies_without_the_gap_are_refused_with_the_reason(self):
for gap in (0, 0.03, 0.5):
with self.assertRaises(ValueError) as caught:
check_sheet_gaps([sheet(gap=gap)])
self.assertEqual(str(caught.exception), SHEET_GAP_CHANGED)
class CentredSheetTests(unittest.TestCase):
"""A narrower ready sheet prints at its real size, centred on the film
(Marcus, 2026-12-02, contrato-folha-real.md V7)."""
def narrow_sheet(self, x_cm):
upload = str(uuid4())
width, film, length = 35.79, 57, 20
return Item.model_validate({
'mode': 'file', 'metres': str(length / 100), 'grade': 100, 'uploads': [upload],
'quality_status': 'ok', 'quality_acknowledged': False,
'production': {'version': 3, 'film_width_cm': film, 'height_cm': length,
'sources': [{'upload_id': upload, 'kind': 'sheet', 'width_cm': width,
'length_cm': length, 'copies': 1, 'measurement': 'file'}],
'placements': [{'source_index': 0, 'copy_index': 0, 'x_cm': x_cm,
'y_cm': 0, 'width_cm': width, 'length_cm': length,
'rotation_degrees': 0, 'mirrored': False}]}})
def test_a_narrow_sheet_is_centred(self):
self.narrow_sheet((57 - 35.79) / 2)
def test_a_narrow_sheet_away_from_the_centre_is_refused(self):
with self.assertRaises(ValueError):
self.narrow_sheet(0)
def loose(xs_ys):
upload = str(uuid4())
placements = [{'source_index': 0, 'copy_index': i, 'x_cm': x, 'y_cm': y, 'width_cm': 20, 'length_cm': 10,
'rotation_degrees': 0, 'mirrored': False} for i, (x, y) in enumerate(xs_ys)]
height = max(y for _, y in xs_ys) + 10
return {'mode': 'avulsa', 'metres': str(height / 100), 'grade': 100, 'uploads': [upload],
'quality_status': 'ok', 'quality_acknowledged': False,
'production': {'version': 3, 'film_width_cm': 57, 'height_cm': height,
'sources': [{'upload_id': upload, 'kind': 'artwork', 'width_cm': 20, 'length_cm': 10,
'copies': len(xs_ys), 'measurement': 'file'}],
'placements': placements}}
class OverlapTests(unittest.TestCase):
"""Pieces stacked on each other bill less film than they print on: the
server refuses them, whatever the browser sent."""
def test_pieces_apart_or_touching_pass(self):
Item.model_validate(loose([(0, 0), (20.5, 0), (0, 10.5), (20.5, 10.5)]))
Item.model_validate(loose([(0, 0), (20, 0), (0, 10)]))
def test_nested_rectangles_pass(self):
# The Site nests art by its shape: rectangles crossing over transparent
# parts are honest (Sentry DTF-WEB-2B, contrato-encaixe-forma.md K2).
Item.model_validate(loose([(0, 0), (10, 5)]))
Item.model_validate(loose([(0, 0), (19.9, 9.9)]))
def test_pieces_in_one_place_are_refused(self):
for layout in ([(0, 0), (0, 0)], [(0, 0)] * 100):
with self.assertRaises(ValueError) as caught:
Item.model_validate(loose(layout))
self.assertIn('uma sobre a outra', str(caught.exception))
def test_ready_sheets_never_cross(self):
# Sheets go one under the other, never nested (contrato-encaixe-forma.md K4).
with self.assertRaises(ValueError) as caught:
sheet(gap=-5)
self.assertIn('uma sobre a outra', str(caught.exception))
class ClosePiecesTests(unittest.TestCase):
"""The Site's count of pieces under 5 mm travels with the item to the
Kanban card (Marcus, 2026-11-26, #81)."""
def test_the_count_is_kept_and_defaults_to_none(self):
item = sheet()
self.assertEqual(item.close_pieces, 0)
data = item.model_dump(mode='json')
self.assertEqual(Item.model_validate({**data, 'close_pieces': 14}).close_pieces, 14)
with self.assertRaises(ValueError):
Item.model_validate({**data, 'close_pieces': -1})
if __name__ == '__main__':
unittest.main()