"""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 app import catalog from app.cart_pricing import price_items from app.core import pricing 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': []} 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)) if __name__ == '__main__': unittest.main()