Files
dtf-system/tests/test_sheet_trim.py
Cauê Faleiros bb6fb14b1a
Some checks failed
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Publish images (push) Has been cancelled
Build and deploy / Secret scan and release gate (push) Has been cancelled
Build and deploy / Integration suite on a real stack (push) Has been cancelled
fix: the server refuses stacked pieces and files laid out at other proportions, and never cuts a strip with ink
The price came from the layout the browser sent, checked only in part:
- Pieces on top of each other passed: 100 copies stacked in one place bill
  10 cm and print over each other. The layout validator now refuses any
  overlap.
- A file laid out at proportions it does not have (a 5 m sheet sent as
  10 cm) was found only by the print file, after payment. The quote now
  checks each raster file's proportions from the bytes it already reads.
- The empty strips the Site cuts from a ready sheet were cut on its word.
  The print file now reads them: a strip with ink sends the item to hand
  preparation with the reason, instead of printing the art cut off.

Test fixtures that laid real images out at impossible proportions now keep
the image's own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 15:56:00 -03:00

90 lines
4.0 KiB
Python

"""Empty strips cut from a ready sheet (Marcus, 2026-11-18): only a measured
finished sheet may carry them, and they never change what is placed.
Run from the repository root: python3 -m unittest tests.test_sheet_trim -v.
"""
import unittest
from uuid import uuid4
from pydantic import ValidationError
from app.core.models import Item
def sheet_item(mode='file', kind='sheet', measurement='file', top=10, bottom=40):
upload = str(uuid4())
width = 27 if mode in ('uvfile', 'uv') else 57
return {'mode': mode, 'metres': '4.6', 'grade': 100, 'uploads': [upload],
'quality_status': 'ok', 'quality_acknowledged': False,
'production': {'version': 3, 'film_width_cm': width, 'height_cm': '453.8',
'sources': [{'upload_id': upload, 'kind': kind, 'width_cm': width,
'length_cm': '453.8', 'copies': 1, 'measurement': measurement,
'trim_top_cm': top, 'trim_bottom_cm': bottom}],
'placements': [{'source_index': 0, 'copy_index': 0, 'x_cm': 0, 'y_cm': 0,
'width_cm': width, 'length_cm': '453.8',
'rotation_degrees': 0, 'mirrored': False}]}}
class SheetTrimTests(unittest.TestCase):
def test_a_measured_ready_sheet_carries_its_cut_strips(self):
item = Item.model_validate(sheet_item(mode='uvfile'))
source = item.production.sources[0]
self.assertEqual((source.trim_top_cm, source.trim_bottom_cm), (10, 40))
def test_no_strips_by_default(self):
data = sheet_item()
del data['production']['sources'][0]['trim_top_cm'], data['production']['sources'][0]['trim_bottom_cm']
source = Item.model_validate(data).production.sources[0]
self.assertEqual((source.trim_top_cm, source.trim_bottom_cm), (0, 0))
def test_only_a_measured_ready_sheet_is_trimmed(self):
with self.assertRaises(ValidationError):
Item.model_validate(sheet_item(measurement='customer'))
with self.assertRaises(ValidationError):
Item.model_validate(sheet_item(mode='avulsa', kind='artwork'))
with self.assertRaises(ValidationError):
Item.model_validate(sheet_item(top=-1))
class CutStripsAreEmptyTests(unittest.TestCase):
"""The print file cuts the strips only when they hold no ink: on a measure
the file does not bear out, artwork would leave the print unseen."""
def source(self, ink_row=None, alpha=255, fmt='PNG'):
import os
import tempfile
from PIL import Image
from app.printpng import Source
folder = tempfile.mkdtemp()
image = Image.new('RGBA', (60, 1200), (0, 0, 0, 0))
image.paste((200, 30, 30, 255), (0, 300, 60, 900)) # the art, rows 300-899
if ink_row is not None:
image.putpixel((30, ink_row), (0, 0, 0, alpha))
path = os.path.join(folder, 'folha.' + fmt.lower())
image.save(path, fmt)
return Source(path, 'folha.' + fmt.lower())
# 1200 rows for 120 cm: 20 cm (200 rows) cut at each end.
spec = {'length_cm': 80, 'trim_top_cm': 20, 'trim_bottom_cm': 20}
def test_clear_strips_are_cut(self):
for fmt in ('PNG', 'TIFF'):
source = self.source(fmt=fmt)
source.trim(self.spec)
self.assertEqual(source.cut, (200, 200), fmt)
# A residue the Site does not count as ink does not hold the cut either.
self.source(ink_row=50, alpha=10, fmt=fmt).trim(self.spec)
def test_ink_in_a_cut_strip_goes_to_hand_preparation(self):
from app.printfile import Unsupported
for fmt in ('PNG', 'TIFF'):
for row, where in ((150, 'do começo'), (1100, 'do fim')):
with self.assertRaises(Unsupported) as caught:
self.source(ink_row=row, fmt=fmt).trim(self.spec)
self.assertIn(where, str(caught.exception), (fmt, row))
if __name__ == '__main__':
unittest.main()