"""Run from repository root: python3 -m unittest local.test_pricing -v.""" import json from pathlib import Path import subprocess import unittest from .pricing import price, TIERS class PricingTests(unittest.TestCase): def test_every_grade_against_actual_site_javascript(self): # The ladders 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('local/static/site-config.js').read_text() tiers = source.split('const FAIXAS=')[1].split('\n};',1)[0]+'\n}' calculator = source.split('const cobrar=')[1].split(';',1)[0] js = f'const FAIXAS={tiers};const MINIMO_M=1;const cobrar={calculator};' js += '''const results=[];for(const mode of Object.keys(FAIXAS))for(let grade=0;grade<=100;grade++) 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=FAIXAS[mode].find(x=>grade>=x[0])[1]; results.push([mode,String(metres),grade,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)) for mode,metres,grade,billed,unit,total in cases: 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_assembly_is_included_at_every_tier(self): for grade in range(101): self.assertEqual(price('avulsa','1',grade)['total_cents']-price('file','1',grade)['total_cents'],1000) self.assertEqual(price('uv','1',grade)['total_cents']-price('uvfile','1',grade)['total_cents'],1400) 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()