import pytest from estimator.domain.enums import Engagement, Role from estimator.domain.errors import MissingRateError from estimator.pricing import make_quote, price, price_with from tests.factories import DOC_EXAMPLE_TASKS, task TASKS = DOC_EXAMPLE_TASKS RATES_RU = {"analyst": 1500, "backend": 1800, "qa": 1200, "devops": 1800} RATES_EU = {"analyst": 75, "backend": 85, "qa": 60, "devops": 90} # ---------------------------------------------------------------- the worked example def test_doc_example_ru(): r = price(TASKS, RATES_RU, rounding_step=1000) assert r.hours == pytest.approx(144.7) assert r.sigma == pytest.approx(10.0) assert r.base_cost == pytest.approx(243_800) # the PERT reserve is 1.04 x 10 = 10.4 h, the 10% floor is 14.5 h: the floor wins assert r.pert_reserve_hours == pytest.approx(10.4) and r.floor_reserve_hours == pytest.approx(14.5) assert r.reserve_basis == "floor" and r.reserve_hours == pytest.approx(14.5) assert r.reserve_pct == pytest.approx(0.10, abs=1e-4) assert r.hours_with_reserve == pytest.approx(159.1) assert r.cost_expected == 336_000 # 243 800 x 1.15 x 1.20 = 336 444 assert r.cost_with_reserve == 370_000 # 336 444 x 1.10 = 370 088 assert r.reserve_cost == 34_000 def test_doc_example_eu(): r = price(TASKS, RATES_EU) assert r.base_cost == pytest.approx(11_680) assert r.cost_expected == 16_118 # 11 680 x 1.15 x 1.20 = 16 118.4 assert r.cost_with_reserve == 17_730 # x 1.10 = 17 730.2 assert r.reserve_cost == r.cost_with_reserve - r.cost_expected def test_no_double_reserve_the_old_number_is_gone(): """The previous model added a flat 10% and then scaled by the P85 hours again (397 000).""" r = price(TASKS, RATES_RU, rounding_step=1000) assert r.cost_with_reserve < 397_000 # ------------------------------------------------------------------- reserve policy def test_reserve_is_the_larger_of_pert_and_floor(): wide = [task("a", Role.BACKEND, 10, 30, 110), task("b", Role.BACKEND, 10, 30, 110)] r = price(wide, RATES_RU) # sigma per task 16.67, total 23.57, reserve 24.5 h against a 10% floor of ~8.7 h assert r.reserve_basis == "pert" and r.reserve_hours == r.pert_reserve_hours assert r.pert_reserve_hours > r.floor_reserve_hours assert r.reserve_pct > 0.10 def test_wide_spread_raises_the_price_and_tight_spread_does_not_lower_it_below_the_floor(): tight = price([task("a", Role.BACKEND, 20, 20, 20)], RATES_RU) # no spread at all assert tight.sigma == 0 and tight.reserve_basis == "floor" and tight.reserve_pct == pytest.approx(0.10) wide = price([task("a", Role.BACKEND, 5, 20, 80)], RATES_RU) assert wide.reserve_pct > tight.reserve_pct def test_floor_is_configurable(): base = price(TASKS, RATES_RU) higher = price(TASKS, RATES_RU, reserve_floor_pct=0.20) assert higher.reserve_pct == pytest.approx(0.20, abs=1e-3) assert higher.cost_with_reserve > base.cost_with_reserve assert higher.cost_expected == base.cost_expected # the reserve never changes the expected cost assert price(TASKS, RATES_RU, reserve_floor_pct=0).reserve_basis == "pert" def test_reserve_is_added_exactly_once(): # floor case: the price grows by exactly the floor, not by floor plus something else r = price(TASKS, RATES_RU) assert r.cost_with_reserve / r.cost_expected == pytest.approx(1.10, rel=1e-4) # PERT case: the price grows by z x sigma / hours and nothing else (the displayed hours are rounded) wide = price([task("a", Role.BACKEND, 10, 30, 110), task("b", Role.BACKEND, 10, 30, 110)], RATES_RU) assert wide.reserve_basis == "pert" assert wide.cost_with_reserve / wide.cost_expected == pytest.approx(1 + 1.04 * wide.sigma / wide.hours, rel=2e-3) def test_expected_never_exceeds_with_reserve(): for tasks in (TASKS, [task("a", Role.QA, 1, 2, 30)], [task("b", Role.DEVOPS, 5, 5, 5)]): r = price(tasks, RATES_RU) assert r.cost_expected <= r.cost_with_reserve and r.hours <= r.hours_with_reserve # ------------------------------------------------------------------ markup and margin def test_markup_is_not_margin(): r = price(TASKS, RATES_RU) assert r.implied_margin_pct == pytest.approx(0.1667, abs=1e-4) # 20% markup = 16.7% margin # selling price minus cost, as a share of the selling price, really is that margin work_cost = r.base_cost * (1 + r.inputs.pm_pct) margin = (r.cost_expected - work_cost) / r.cost_expected assert margin == pytest.approx(r.implied_margin_pct, abs=0.01) def test_billing_rate_includes_management_and_markup(): r = price(TASKS, RATES_RU) backend = next(x for x in r.by_role if x.role == Role.BACKEND) assert backend.rate == 1800 and backend.billing_rate == pytest.approx(1800 * 1.15 * 1.20) # 2 484 # billing the expected hours at the client rates reproduces the expected cost total = sum(x.hours * x.billing_rate for x in r.by_role) assert total == pytest.approx(r.cost_expected, rel=1e-3) def test_zero_markup_zero_pm(): r = price(TASKS, RATES_RU, pm_pct=0, markup_pct=0, reserve_floor_pct=0) assert r.cost_expected == pytest.approx(r.base_cost, abs=1) assert r.implied_margin_pct == 0 # ------------------------------------------------------------------------- misc def test_breakdown_by_role_sums_to_base(): r = price(TASKS, RATES_RU) assert sum(x.cost for x in r.by_role) == pytest.approx(r.base_cost, abs=0.05) def test_missing_rate_raises(): with pytest.raises(MissingRateError): price(TASKS, {"backend": 1800}) def test_empty_tasks(): r = price([], RATES_RU) assert r.hours == 0 and r.cost_expected == 0 and r.cost_with_reserve == 0 and r.reserve_pct == 0 def test_cost_grows_with_hours(): small = price([task("a", Role.BACKEND, 8, 10, 12)], RATES_RU) big = price([task("a", Role.BACKEND, 16, 20, 24)], RATES_RU) assert big.cost_with_reserve > small.cost_with_reserve def test_unordered_estimate_rejected(): with pytest.raises(ValueError): task("bad", Role.QA, 10, 5, 20) @pytest.mark.parametrize("kw", [{"pm_pct": -0.1}, {"markup_pct": -1}, {"reserve_floor_pct": -0.01}, {"reserve_z": -1}, {"rounding_step": 0}]) def test_invalid_parameters_rejected(kw): with pytest.raises(ValueError): price(TASKS, RATES_RU, **kw) @pytest.mark.parametrize("step,expected_with", [(1, 370_088), (1000, 370_000), (5000, 370_000), (10_000, 370_000)]) def test_rounding_step(step, expected_with): assert price(TASKS, RATES_RU, rounding_step=step).cost_with_reserve == expected_with # ------------------------------------------------------------- snapshot and repricing def test_snapshot_contains_everything_needed_to_reproduce_the_price(): r = price(TASKS, RATES_RU, markup_pct=0.25, reserve_floor_pct=0.15, rounding_step=1000) assert r.inputs.rates[Role.BACKEND] == 1800 assert (r.inputs.markup_pct, r.inputs.reserve_floor_pct, r.inputs.rounding_step) == (0.25, 0.15, 1000) again = price_with(TASKS, r.inputs) assert again == r def test_repricing_ignores_the_current_defaults(): old = price(TASKS, RATES_RU, markup_pct=0.30) changed_tasks = [*TASKS[:-1], task("deploy", Role.DEVOPS, 8, 16, 32)] new = price_with(changed_tasks, old.inputs) assert new.inputs.markup_pct == 0.30 # the original parameters, not today's defaults assert new.cost_expected > old.cost_expected # ------------------------------------------------------------------------ quotes def test_fixed_price_quote_includes_the_reserve(): r = price(TASKS, RATES_RU, rounding_step=1000) q = make_quote(r, Engagement.FIXED_PRICE) assert q.amount == 370_000 and q.cap is None and q.hours == pytest.approx(159.1) def test_time_and_materials_quote_is_expected_with_a_ceiling(): r = price(TASKS, RATES_RU, rounding_step=1000) q = make_quote(r, Engagement.TIME_AND_MATERIALS) assert q.amount == 336_000 and q.cap == 370_000 assert q.hours == pytest.approx(144.7) and q.hours_cap == pytest.approx(159.1) def test_both_quotes_come_from_the_same_numbers(): r = price(TASKS, RATES_RU) fixed, tm = make_quote(r, Engagement.FIXED_PRICE), make_quote(r, Engagement.TIME_AND_MATERIALS) assert fixed.amount == tm.cap and tm.amount == r.cost_expected # ----------------------------------------------------------------------- VAT def test_vat_is_added_on_top_and_nothing_else_changes(): r = price(TASKS, RATES_RU, rounding_step=1000) plain = make_quote(r, Engagement.FIXED_PRICE) vat = make_quote(r, Engagement.FIXED_PRICE, vat_rate=0.22) assert plain.vat_rate == 0 and plain.amount_gross == plain.amount == 370_000 assert vat.amount == 370_000 # the net price is untouched assert vat.amount_gross == 451_400 and vat.vat_amount == 81_400 and vat.vat_rate == 0.22 assert r == price(TASKS, RATES_RU, rounding_step=1000) # the calculation itself never sees VAT def test_time_and_materials_vat_covers_the_expected_amount_and_the_ceiling(): r = price(TASKS, RATES_RU, rounding_step=1000) q = make_quote(r, Engagement.TIME_AND_MATERIALS, vat_rate=0.22) assert (q.amount, q.cap) == (336_000, 370_000) assert q.amount_gross == pytest.approx(409_920) and q.cap_gross == pytest.approx(451_400) def test_vat_amounts_keep_kopecks(): r = price(TASKS, RATES_RU) # no rounding step: 370 088 q = make_quote(r, Engagement.FIXED_PRICE, vat_rate=0.22) assert q.amount_gross == pytest.approx(370_088 * 1.22, abs=0.01) def test_negative_vat_rejected(): with pytest.raises(ValueError): make_quote(price(TASKS, RATES_RU), Engagement.FIXED_PRICE, vat_rate=-0.1)