Files
dtf-system/tests/test_pricing.py
Cauê Faleiros c88d81588e feat: products and prices from the database, edited in the Kanban's Site tab (front 8.1)
Gestão and the administrators switch each product on and off and set its
price from a start date; a price that has started is history and cannot
change, a scheduled one can be cancelled. The Site and the server read
the current price; with no row the code's prices and TREATMENT_CENTS
still apply, so nothing changes until someone sets one. A switched-off
product disappears from the Site and new quotes with it are refused.
Approved quotes and paid orders keep their price.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-09 13:50:45 -03:00

100 lines
5.4 KiB
Python

"""Run from repository root: python3 -m unittest tests.test_pricing -v."""
import json
import os
from pathlib import Path
from unittest.mock import patch
import subprocess
import unittest
from app.core.pricing import 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]
js = f'const PRECO={prices};const MINIMO_M=1;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]){
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_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.
self.assertEqual(PRICES, {'file': 1990, 'avulsa': 2190, 'uvfile': 7990, 'uv': 8990})
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_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()