Files
dtf-system/tests/test_cart_pricing.py
Cauê Faleiros 38f947d632 feat: Ajuste Fino Redesenho in the cart (Marcus 12/11, slice R1)
Under each art the Prumo warns about, the customer can choose Ajuste Fino
or Ajuste Fino Redesenho (vectorize or redraw, per art, +48 h, no batch
price). It is a disabled product until Gestão prices it in the Site tab;
the server charges it per chosen art, Tiny gets a DTFIMP.AJF.RED line and
two more business days, the card shows 'Redesenho · N artes' and the
designer gate applies. The preview loop is slice R2.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-12 11:05:17 -03:00

250 lines
13 KiB
Python

"""One pricing module: the same arithmetic for every cart (plan 003).
Run from repository root: python3 -m unittest tests.test_cart_pricing -v.
old_way() reproduces today's approval arithmetic with the functions that were
already there; the matrix pins price, split minimums, bands and the manual
extra. app/cart_pricing.price_items must return exactly the same list.
"""
import unittest
from decimal import Decimal
from uuid import uuid4
from app import catalog
from app.cart_pricing import (designer_hours, needs_designer, price_items,
redesign_price, treatment_price)
from app.core import pricing
try:
from pydantic import ValidationError
from app.core.models import QuoteRequest
except ImportError:
# The validate job runs this file on the host, without the runtime image's
# dependencies; the runtime suites exercise the model there.
QuoteRequest = ValidationError = None
BASE = {'file': 1990, 'avulsa': 2190, 'uvfile': 7990, 'uv': 8990}
UV_BANDS = [{'from_metres': '5', 'unit_cents': 8000},
{'from_metres': '20', 'unit_cents': 7000}]
def catalog_now(bands=None, prices=None):
prices = prices or BASE
bands = bands or {}
now = {mode: {'enabled': True, 'unit_cents': prices[mode],
'bands': bands.get(mode, [])} for mode in BASE}
now['tratamento'] = {'enabled': True, 'unit_cents': 1000, 'bands': []}
now['redesenho'] = {'enabled': True, 'unit_cents': 2990, 'bands': []}
return now
def item(mode, metres, grade=90, uid='u1', measurement='file', split=None):
kind = 'sheet' if mode in ('file', 'uvfile') else 'artwork'
data = {'mode': mode, 'metres': str(metres), 'grade': grade, 'uploads': [uid],
'production': {
'version': 3,
'film_width_cm': 27 if mode in ('uvfile', 'uv') else 57,
'height_cm': float(Decimal(str(metres)) * 100),
'sources': [{'upload_id': uid, 'kind': kind, 'width_cm': 10,
'length_cm': 10, 'copies': 1, 'rotation_degrees': 0,
'mirrored': False, 'measurement': measurement}],
'placements': []},
'quality_status': 'ok', 'quality_acknowledged': False}
if split:
data['split'] = split
return data
def old_way(items, names, catalog_now, manual_extra):
"""Today's approve arithmetic (app/quote_review.py before plan 003)."""
priced = []
for item in items:
extra = manual_extra if pricing.is_manual(item, names) else 0
priced.append({**pricing.price(item['mode'], str(item['metres']), item['grade'], extra,
minimum=Decimal('0') if item.get('split') else Decimal('1'),
unit_cents=catalog_now[item['mode']]['unit_cents']),
**({'split': item['split']} if item.get('split') else {})})
pricing.bill_splits(priced)
totals = {}
for item in priced:
totals[item['mode']] = totals.get(item['mode'], Decimal('0')) + Decimal(item['billed_metres'])
banded = {}
for mode, total in totals.items():
product = catalog_now[mode]
price_for_total = catalog.band_price(product['unit_cents'], product['bands'], total)
if price_for_total != product['unit_cents']:
banded[mode] = price_for_total
pricing.apply_bands(priced, banded)
return priced
def carts():
plain = {'u1': 'ARTE.PNG'}
manual = {'u1': 'ARTE.CDR'}
split = {'group': 'g', 'part': 1, 'of': 2}
split2 = {'group': 'g', 'part': 2, 'of': 2}
return {
'file-one': {'items': [item('file', '1')], 'names': plain,
'catalog': catalog_now(), 'manual': 0},
'avulsa-one': {'items': [item('avulsa', '2.75')], 'names': plain,
'catalog': catalog_now(), 'manual': 0},
'uvfile-one': {'items': [item('uvfile', '1.01')], 'names': plain,
'catalog': catalog_now(), 'manual': 0},
'uv-one': {'items': [item('uv', '1')], 'names': plain,
'catalog': catalog_now(), 'manual': 0},
'same-mode-band-together': {
'items': [item('uv', '2.5', uid='u1'), item('uv', '2.5', uid='u2')],
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
'catalog': catalog_now(bands={'uv': UV_BANDS}), 'manual': 0},
'same-mode-below-band': {
'items': [item('uv', '2.4', uid='u1'), item('uv', '2.5', uid='u2')],
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
'catalog': catalog_now(bands={'uv': UV_BANDS}), 'manual': 0},
'split-under-one-metre': {
'items': [item('avulsa', '0.3', uid='u1', split=split),
item('avulsa', '0.2', uid='u2', split=split2)],
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
'catalog': catalog_now(), 'manual': 0},
'split-and-band': {
'items': [item('avulsa', '0.3', uid='u1', split=split),
item('avulsa', '0.2', uid='u2', split=split2)],
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
'catalog': catalog_now(bands={'avulsa': [{'from_metres': '1',
'unit_cents': 1990}]}),
'manual': 0},
'manual-cdr-extra': {'items': [item('file', '1', split=None)],
'names': manual, 'catalog': catalog_now(), 'manual': 200},
'manual-customer-measure': {
'items': [item('file', '1.01', measurement='customer')],
'names': plain, 'catalog': catalog_now(), 'manual': 200},
'mixed-modes': {
'items': [item('file', '1', uid='u1'), item('avulsa', '2', uid='u2'),
item('uvfile', '0.5', uid='u3'), item('uv', '3', uid='u4')],
'names': {'u1': 'A.PNG', 'u2': 'B.PNG', 'u3': 'C.PNG', 'u4': 'D.PNG'},
'catalog': catalog_now(), 'manual': 0},
'no-bands': {'items': [item('avulsa', '1.2'), item('avulsa', '1.3')],
'names': plain, 'catalog': catalog_now(bands={}), 'manual': 0},
'band-exact-boundary': {
'items': [item('uv', '5')], 'names': plain,
'catalog': catalog_now(bands={'uv': UV_BANDS}), 'manual': 0},
'band-floating-metres': {
'items': [item('uv', '2.8000000000000003')], 'names': plain,
'catalog': catalog_now(bands={'uv': [{'from_metres': '2.8', 'unit_cents': 8500}]}),
'manual': 0},
'manual-plus-band': {
'items': [item('file', '2.5')], 'names': manual,
'catalog': catalog_now(bands={'file': [{'from_metres': '2', 'unit_cents': 1890}]}),
'manual': 200},
'mixed-modes-with-bands': {
'items': [item('file', '2', uid='u1'), item('file', '3', uid='u2'),
item('uv', '2', uid='u3')],
'names': {'u1': 'A.PNG', 'u2': 'B.PNG', 'u3': 'C.PNG'},
'catalog': catalog_now(bands={'file': [{'from_metres': '5', 'unit_cents': 1790}],
'uv': UV_BANDS}),
'manual': 0},
}
class CartPricingCharacterizationTest(unittest.TestCase):
def test_price_items_matches_todays_approval(self):
for name, cart in carts().items():
with self.subTest(cart=name):
self.assertEqual(
price_items(cart['items'], cart['names'], cart['catalog'], cart['manual']),
old_way(cart['items'], cart['names'], cart['catalog'], cart['manual']))
def test_the_matrix_is_anchored_to_the_table_prices(self):
def priced(name):
cart = carts()[name]
return price_items(cart['items'], cart['names'], cart['catalog'], cart['manual'])
self.assertEqual(priced('file-one')[0]['total_cents'], 1990)
self.assertEqual(priced('uv-one')[0]['total_cents'], 8990)
self.assertEqual([p['unit_cents'] for p in priced('same-mode-band-together')],
[8000, 8000])
split = priced('split-under-one-metre')
self.assertEqual([(p['billed_metres'], p['total_cents']) for p in split],
[('0.8', 1752), ('0.2', 438)])
floating = priced('band-floating-metres')[0]
self.assertEqual((floating['billed_metres'], floating['total_cents']), ('2.9', 24650))
def test_ajuste_fino_is_per_art_with_the_band(self):
# Front 1a: 1 to 9 arts at R$ 9,90, from 10 at R$ 6,90, all arts charged
# at the band. `true` keeps meaning every art of the cart.
offer = {'enabled': True, 'unit_cents': 990,
'bands': [{'from_metres': '10', 'unit_cents': 690}]}
for arts, unit in ((1, 990), (9, 990), (10, 690), (12, 690)):
items = [item('avulsa', '1', uid=f'u{index}') for index in range(arts)]
self.assertEqual(treatment_price(items, True, offer),
{'cents': unit * arts, 'hours': 24, 'arts': arts, 'unit_cents': unit})
items = [item('file', '1', uid='u1'), item('file', '1', uid='u2')]
self.assertEqual(treatment_price(items, [[1, 0]], offer),
{'cents': 990, 'hours': 24, 'arts': 1, 'unit_cents': 990})
self.assertEqual(treatment_price(items, [], offer), {})
self.assertEqual(treatment_price(items, True, {**offer, 'enabled': False}), {})
def test_redesenho_is_per_art_never_a_band(self):
# Front 1b: R$ 29,90 per art, and the basic batch band never applies.
offer = {'enabled': True, 'unit_cents': 2990,
'bands': [{'from_metres': '10', 'unit_cents': 690}]}
items = [item('file', '1', uid='u1'), item('file', '1', uid='u2')]
self.assertEqual(redesign_price(items, [[0, 0], [1, 0]], offer),
{'cents': 5980, 'hours': 48, 'arts': 2, 'unit_cents': 2990})
self.assertEqual(redesign_price(items, [], offer), {})
self.assertEqual(redesign_price(items, [[0, 0]], {**offer, 'enabled': False}), {})
self.assertEqual(redesign_price(items, [[0, 0]], {**offer, 'unit_cents': 0}), {})
def test_the_basic_band_counts_only_basic_arts(self):
offer = {'enabled': True, 'unit_cents': 990,
'bands': [{'from_metres': '10', 'unit_cents': 690}]}
items = [item('avulsa', '1', uid=f'u{index}') for index in range(10)]
basic = [[index, 0] for index in range(9)]
self.assertEqual(treatment_price(items, basic, offer),
{'cents': 8910, 'hours': 24, 'arts': 9, 'unit_cents': 990})
redesign = redesign_price(items, [[9, 0]], offer={**offer, 'unit_cents': 2990})
self.assertEqual(redesign['arts'], 1)
def test_designer_helpers_read_both_services(self):
self.assertEqual(designer_hours({}), 0)
self.assertEqual(designer_hours({'treatment': {'cents': 990}}), 24)
self.assertEqual(designer_hours({'redesign': {'cents': 2990}}), 48)
self.assertEqual(designer_hours({'treatment': {'cents': 990},
'redesign': {'cents': 2990}}), 48)
self.assertFalse(needs_designer({}))
self.assertTrue(needs_designer({'treatment': {'cents': 990}}))
self.assertTrue(needs_designer({'redesign': {'cents': 2990}}))
@unittest.skipUnless(QuoteRequest, 'pydantic is only in the runtime image')
def test_a_pair_cannot_be_basic_and_redesenho(self):
def quote(treatment=None, redesign=None, items=1):
entries = []
for _ in range(items):
uid = str(uuid4())
data = item('avulsa', '1', uid=uid)
data['production']['placements'] = [{
'source_index': 0, 'copy_index': 0, 'x_cm': 0, 'y_cm': 0,
'width_cm': 10, 'length_cm': 10, 'rotation_degrees': 0, 'mirrored': False}]
entries.append(data)
payload = {'request_key': str(uuid4()),
'customer': {'cnpj': '11222333000181', 'zap': '11999999999',
'mail': 'arte@example.test'},
'items': entries, 'freight': {'service': 'pickup'}}
if treatment is not None:
payload['treatment'] = treatment
if redesign is not None:
payload['redesign'] = redesign
return payload
QuoteRequest.model_validate(quote(treatment=[[0, 0]], redesign=[[1, 0]], items=2))
with self.assertRaises(ValidationError):
QuoteRequest.model_validate(quote(treatment=[[0, 0]], redesign=[[0, 0]]))
with self.assertRaises(ValidationError):
QuoteRequest.model_validate(quote(treatment=True, redesign=[[0, 0]]))
with self.assertRaises(ValidationError):
QuoteRequest.model_validate(quote(redesign=[[0, 0], [0, 0]]))
with self.assertRaises(ValidationError):
QuoteRequest.model_validate(quote(redesign=[[1, 0]]))
if __name__ == '__main__':
unittest.main()