All checks were successful
Build and deploy / Validate source (push) Successful in 14s
Build and deploy / Integration suite on a real stack (push) Successful in 8m42s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m10s
Marcus: a length that passes a 10 cm step by up to 8 mm is billed at that
step; by more, at the next. One rule for every product, on the server
(pricing.billed_length), the Site (arredonda10) and the Montador's estimate;
the print file accepts the 8 mm past the billed length instead of sending
the sheet to hand preparation.
The Site showed four numbers that did not add up (0,88 m, 4,42 m, 4,45 m,
4,50 m), and the team read 4,40 m. The copies line now gives the whole
sum ("5 × 88,4 cm + 4 espaços de 8 mm = 445,2 cm"), and the buy box says
why the billed metres are what they are, as a benefit ("os 4 mm a mais
ficam por nossa conta"). The buy box's parts note said 20 m; parts are
10 m since 18/11.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
12 KiB
Python
197 lines
12 KiB
Python
"""Run from repository root: python3 -m unittest tests.test_pricing -v."""
|
|
import json
|
|
import os
|
|
from decimal import Decimal
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
import subprocess
|
|
import unittest
|
|
from app.catalog import band_price
|
|
from app.core.pricing import apply_bands, bill_splits, is_manual, price, PRICES, treatment_cents
|
|
|
|
class PricingTests(unittest.TestCase):
|
|
def test_every_length_against_actual_site_javascript(self):
|
|
# The prices live with the rest of the Site's behaviour; this test exists
|
|
# to prove the server agrees with whatever the customer is shown.
|
|
source = Path('web/site-config.js').read_text()
|
|
prices = source.split('const PRECO=')[1].split(';',1)[0]
|
|
calculator = source.split('const cobrar=')[1].split(';',1)[0]
|
|
# Every 10 cm with 8 mm of grace (Marcus, 2026-11-27): the Site's own rounding.
|
|
grace = source.split('const TOLERANCIA_M=')[1].split(';',1)[0]
|
|
rounding = source.split('const arredonda10=')[1].split(';',1)[0]
|
|
js = (f'const PRECO={prices};const MINIMO_M=1;const TOLERANCIA_M={grace};'
|
|
f'const arredonda10={rounding};const cobrar={calculator};')
|
|
js += '''const results=[];for(const mode of Object.keys(PRECO))
|
|
for(const metres of [0.01,0.99,1,1.01,1.1,2.75,2.8,2.8000000000000003,19.99,60,12000,
|
|
1.008,1.0081,4.452,6.001,6.008,6.0081,6.009,5.992]){
|
|
const unit=PRECO[mode];
|
|
results.push([mode,String(metres),cobrar(metres),Math.round(unit*100),Math.round(cobrar(metres)*unit*100)]);
|
|
}process.stdout.write(JSON.stringify(results));'''
|
|
cases = json.loads(subprocess.check_output(['node','-e',js],text=True))
|
|
self.assertEqual({mode for mode, *_ in cases}, set(PRICES))
|
|
for mode,metres,billed,unit,total in cases:
|
|
for grade in (0, 39, 40, 75, 90, 100):
|
|
with self.subTest(mode=mode,metres=metres,grade=grade):
|
|
result=price(mode,metres,grade)
|
|
self.assertEqual(float(result['billed_metres']),billed)
|
|
self.assertEqual(result['unit_cents'],unit)
|
|
self.assertEqual(result['total_cents'],total)
|
|
|
|
def test_every_10_cm_with_8_mm_in_the_customers_favour(self):
|
|
# Marcus, 2026-11-27: up to 8 mm past a step stays in it, more goes on.
|
|
from app.core.pricing import billed_length
|
|
for metres, billed in (('6.001', '6'), ('6.008', '6'), ('6.0081', '6.1'), ('6.009', '6.1'),
|
|
('4.452', '4.5'), ('4.42', '4.5'), ('2.75', '2.8'), ('0.005', '0.1')):
|
|
with self.subTest(metres=metres):
|
|
self.assertEqual(billed_length(metres), Decimal(billed))
|
|
# Marcus's two examples: 3 copies of ~199,5 cm with 8 mm between them,
|
|
# and 5 copies of 88,4 cm.
|
|
self.assertEqual(price('file', '6.001', 100)['billed_metres'], '6')
|
|
self.assertEqual(price('file', '4.452', 100)['billed_metres'], '4.5')
|
|
|
|
def test_one_price_per_product(self):
|
|
# Dropstar, 2026-10-23: textile R$ 19,90 ready sheet and R$ 21,90 loose
|
|
# art; UV R$ 79,90 and R$ 89,90. Premium R$ 39,90 (2026-11-10).
|
|
self.assertEqual(PRICES, {'file': 1990, 'avulsa': 2190, 'uvfile': 7990, 'uv': 8990,
|
|
'premium': 3990})
|
|
self.assertEqual(price('avulsa', '2.75', 90)['total_cents'], 6132)
|
|
|
|
def test_the_grade_never_changes_the_price(self):
|
|
for mode in PRICES:
|
|
charges = {(p['unit_cents'], p['total_cents']) for p in (price(mode, '2.75', g) for g in range(101))}
|
|
self.assertEqual(len(charges), 1, mode)
|
|
self.assertNotIn('discount_cents', price(mode, '1', 100))
|
|
# The grade is still kept with the item, for the Kanban and Tiny.
|
|
self.assertEqual(price('file', '1', 92)['grade'], 92)
|
|
|
|
def test_manual_file_extra_is_per_metre(self):
|
|
plain, extra = price('file', '1.01', 0), price('file', '1.01', 0, 500)
|
|
self.assertEqual(extra['unit_cents'], plain['unit_cents'] + 500)
|
|
self.assertEqual(extra['total_cents'], plain['total_cents'] + 550)
|
|
# On top of the single price, whatever grade the item carries.
|
|
self.assertEqual(price('file', '1', 100, 200)['unit_cents'], 2190)
|
|
self.assertEqual(extra['manual_extra_cents'], 500)
|
|
self.assertNotIn('manual_extra_cents', plain)
|
|
for bad in (-1, True, 1.5):
|
|
with self.assertRaises(ValueError):price('file', '1', 0, bad)
|
|
|
|
def test_the_catalog_price_replaces_the_table_price(self):
|
|
# The Kanban's Site tab sets the price in the database (front 8); the
|
|
# minimum and the ten-centimetre rounding stay the Site's.
|
|
priced = price('avulsa', '2.75', 90, unit_cents=1234)
|
|
self.assertEqual(price('avulsa', '2.75', 90)['unit_cents'], PRICES['avulsa'])
|
|
self.assertEqual(priced['unit_cents'], 1234)
|
|
self.assertEqual(priced['billed_metres'], '2.8')
|
|
self.assertEqual(priced['total_cents'], 3455)
|
|
# A hand-opened sheet still pays the extra on top of the new price.
|
|
extra = price('file', '1.01', 0, 500, unit_cents=1234)
|
|
self.assertEqual(extra['unit_cents'], 1734)
|
|
self.assertEqual(extra['billed_metres'], '1.1')
|
|
self.assertEqual(extra['total_cents'], 1907)
|
|
|
|
def test_band_price_below_at_between_and_above(self):
|
|
# Front 8.2: the highest band reached wins, and it charges the whole
|
|
# quantity, not a staircase.
|
|
bands = [{'from_metres': '5', 'unit_cents': 1990},
|
|
{'from_metres': '20', 'unit_cents': 1890}]
|
|
self.assertEqual(band_price(2190, bands, Decimal('4.9')), 2190)
|
|
self.assertEqual(band_price(2190, bands, Decimal('5')), 1990)
|
|
self.assertEqual(band_price(2190, bands, Decimal('19.9')), 1990)
|
|
self.assertEqual(band_price(2190, bands, Decimal('20')), 1890)
|
|
self.assertEqual(band_price(2190, bands, Decimal('12000')), 1890)
|
|
# No bands, or a number from the session, is the base price.
|
|
self.assertEqual(band_price(2190, [], Decimal('5')), 2190)
|
|
self.assertEqual(band_price(2190, [{'from_metres': 5, 'unit_cents': 1990}], Decimal('5')), 1990)
|
|
|
|
def test_apply_bands_prices_the_whole_product_together(self):
|
|
# Two items of the same mode reach the band together: both pay it.
|
|
priced = [{'mode': 'file', 'billed_metres': '2.5', 'unit_cents': 2190, 'total_cents': 5475},
|
|
{'mode': 'file', 'billed_metres': '2.5', 'unit_cents': 2190, 'total_cents': 5475}]
|
|
apply_bands(priced, {'file': 1990})
|
|
self.assertEqual([item['unit_cents'] for item in priced], [1990, 1990])
|
|
self.assertEqual([item['total_cents'] for item in priced], [4975, 4975])
|
|
# A split group counts its billed metres once, after bill_splits.
|
|
priced = [{'mode': 'avulsa', 'billed_metres': '0.2', 'unit_cents': 2190, 'total_cents': 438,
|
|
'split': {'group': 'g', 'part': 1, 'of': 2}},
|
|
{'mode': 'avulsa', 'billed_metres': '0.3', 'unit_cents': 2190, 'total_cents': 657,
|
|
'split': {'group': 'g', 'part': 2, 'of': 2}}]
|
|
bill_splits(priced)
|
|
self.assertEqual(priced[0]['billed_metres'], '0.7')
|
|
apply_bands(priced, {'avulsa': 1890})
|
|
self.assertEqual(priced[0]['total_cents'], 1323)
|
|
self.assertEqual(priced[1]['total_cents'], 567)
|
|
# The manual-file extra stays on top of the band price.
|
|
priced = [{'mode': 'file', 'billed_metres': '2', 'unit_cents': 2190,
|
|
'manual_extra_cents': 200, 'total_cents': 4380}]
|
|
apply_bands(priced, {'file': 1890})
|
|
self.assertEqual(priced[0]['unit_cents'], 2090)
|
|
self.assertEqual(priced[0]['total_cents'], 4180)
|
|
# Another mode is untouched.
|
|
uv = {'mode': 'uv', 'billed_metres': '1', 'unit_cents': 8990, 'total_cents': 8990}
|
|
priced = [{'mode': 'file', 'billed_metres': '1', 'unit_cents': 2190, 'total_cents': 2190}, uv]
|
|
apply_bands(priced, {'file': 1990})
|
|
self.assertEqual((uv['unit_cents'], uv['total_cents']), (8990, 8990))
|
|
|
|
def test_band_prices_against_actual_site_javascript(self):
|
|
# The Site must show the same band price the server charges: run its
|
|
# own precoNaMetragem for the same table and totals.
|
|
source = Path('web/site-config.js').read_text()
|
|
prices = source.split('const PRECO=')[1].split(';', 1)[0]
|
|
body = source.split('function precoNaMetragem', 1)[1].split('\n}', 1)[0]
|
|
bands = [{'from_metres': 5, 'unit_cents': 1990},
|
|
{'from_metres': 20, 'unit_cents': 1890}]
|
|
js = ('const PRECO=' + prices + ';const FAIXAS=' + json.dumps({'file': bands}) + ';'
|
|
'function precoNaMetragem' + body + '\n}'
|
|
';const results=[];for(const m of ["4.9","5","19.9","20","60"])'
|
|
'results.push([m,Math.round(precoNaMetragem("file",Number(m))*100)]);'
|
|
'process.stdout.write(JSON.stringify(results));')
|
|
cases = json.loads(subprocess.check_output(['node', '-e', js], text=True))
|
|
for metres, cents in cases:
|
|
with self.subTest(metres=metres):
|
|
self.assertEqual(cents, band_price(PRICES['file'], bands, Decimal(metres)))
|
|
|
|
def test_ajuste_fino_band_against_actual_site_javascript(self):
|
|
# Ajuste Fino counts its band in arts: the Site helper must charge the
|
|
# same unit price as catalog.band_price.
|
|
source = Path('web/site-config.js').read_text()
|
|
body = source.split('function precoAjuste', 1)[1].split('\n}', 1)[0]
|
|
bands = [{'from_metres': 10, 'unit_cents': 690}]
|
|
js = ('const TAXA_TRATAMENTO=9.9;const FAIXAS=' + json.dumps({'tratamento': bands}) + ';'
|
|
'function precoAjuste' + body + '\n}'
|
|
';const results=[];for(const n of [1,9,10,12])'
|
|
'results.push([n,Math.round(precoAjuste(n)*100)]);'
|
|
'process.stdout.write(JSON.stringify(results));')
|
|
cases = json.loads(subprocess.check_output(['node', '-e', js], text=True))
|
|
for arts, cents in cases:
|
|
with self.subTest(arts=arts):
|
|
self.assertEqual(cents, band_price(990, bands, Decimal(arts)))
|
|
|
|
def test_which_items_are_opened_by_hand(self):
|
|
def item(kind, measurement, uid='u1'):
|
|
return {'production': {'sources': [{'kind': kind, 'measurement': measurement, 'upload_id': uid}]}}
|
|
self.assertTrue(is_manual(item('sheet', 'customer'), {'u1': 'folha.png'}))
|
|
self.assertTrue(is_manual(item('sheet', 'file'), {'u1': 'FOLHA.CDR'}))
|
|
self.assertTrue(is_manual(item('sheet', 'file'), {'u1': 'folha.tif'}))
|
|
self.assertFalse(is_manual(item('sheet', 'file'), {'u1': 'folha.pdf'}))
|
|
# Loose artwork is assembled by the Site; it never pays this.
|
|
self.assertFalse(is_manual(item('artwork', 'customer'), {'u1': 'arte.cdr'}))
|
|
|
|
def test_treatment_is_optional_and_configured_by_the_environment(self):
|
|
with patch.dict(os.environ, {'TREATMENT_CENTS': '990'}):
|
|
self.assertEqual(treatment_cents(), 990)
|
|
with patch.dict(os.environ, {'TREATMENT_CENTS': '0'}):
|
|
self.assertEqual(treatment_cents(), 0)
|
|
with patch.dict(os.environ, {'TREATMENT_CENTS': '-1'}):
|
|
with self.assertRaises(ValueError):
|
|
treatment_cents()
|
|
|
|
def test_invalid_inputs(self):
|
|
for value in ('0','-1','NaN','Infinity','12001'):
|
|
with self.assertRaises(ValueError):price('file',value,90)
|
|
for grade in (-1,101,True,89.5):
|
|
with self.assertRaises(ValueError):price('file','1',grade)
|
|
with self.assertRaises(ValueError):price('other','1',90)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|