Init Project Flexly Estimator
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import pytest
|
||||
|
||||
from estimator.domain.enums import MarketVerdict
|
||||
from estimator.domain.errors import CurrencyMismatchError
|
||||
from estimator.domain.models import Benchmark
|
||||
from estimator.market import market_check
|
||||
|
||||
B = Benchmark(min=400_000, max=600_000, currency="RUB") # bounds with tol 0.25: 300k .. 750k
|
||||
|
||||
|
||||
def test_within():
|
||||
r = market_check(370_000, "RUB", B)
|
||||
assert r.verdict == MarketVerdict.WITHIN
|
||||
assert r.lower == 300_000 and r.upper == 750_000
|
||||
|
||||
|
||||
def test_below():
|
||||
assert market_check(299_999, "RUB", B).verdict == MarketVerdict.BELOW
|
||||
|
||||
|
||||
def test_above():
|
||||
assert market_check(750_001, "RUB", B).verdict == MarketVerdict.ABOVE
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cost", [300_000, 750_000])
|
||||
def test_bounds_inclusive(cost):
|
||||
assert market_check(cost, "RUB", B).verdict == MarketVerdict.WITHIN
|
||||
|
||||
|
||||
def test_no_benchmark():
|
||||
r = market_check(100, "RUB", None)
|
||||
assert r.verdict == MarketVerdict.NO_BENCHMARK and r.lower is None
|
||||
|
||||
|
||||
def test_currency_mismatch_raises():
|
||||
with pytest.raises(CurrencyMismatchError):
|
||||
market_check(17_730, "EUR", B)
|
||||
|
||||
|
||||
def test_zero_tolerance():
|
||||
r = market_check(399_999, "RUB", B, tolerance=0)
|
||||
assert r.verdict == MarketVerdict.BELOW
|
||||
|
||||
|
||||
def test_benchmark_with_vat_is_compared_without_vat():
|
||||
gross = Benchmark(min=488_000, max=732_000, currency="RUB", includes_vat=True, vat_rate=0.22)
|
||||
# 488 000 / 1.22 = 400 000 and 732 000 / 1.22 = 600 000: the same market as B, in net terms
|
||||
r = market_check(370_000, "RUB", gross)
|
||||
assert r.verdict == MarketVerdict.WITHIN and r.normalized_from_gross is True
|
||||
assert r.lower == pytest.approx(300_000) and r.upper == pytest.approx(750_000)
|
||||
assert market_check(299_000, "RUB", gross).verdict == MarketVerdict.BELOW
|
||||
|
||||
|
||||
def test_ignoring_the_vat_flag_would_have_given_a_wrong_answer():
|
||||
gross = Benchmark(min=488_000, max=732_000, currency="RUB", includes_vat=True, vat_rate=0.22)
|
||||
naive = Benchmark(min=488_000, max=732_000, currency="RUB") # the same numbers treated as net
|
||||
# a net price of 340 000 is inside the market (300 000 to 750 000 without VAT) ...
|
||||
assert market_check(340_000, "RUB", gross).verdict == MarketVerdict.WITHIN
|
||||
# ... but looks too cheap if the benchmark's VAT is forgotten (lower bound 366 000)
|
||||
assert market_check(340_000, "RUB", naive).verdict == MarketVerdict.BELOW
|
||||
|
||||
|
||||
def test_a_net_benchmark_is_not_converted():
|
||||
assert market_check(370_000, "RUB", B).normalized_from_gross is False
|
||||
|
||||
|
||||
def test_benchmark_including_vat_requires_a_rate():
|
||||
with pytest.raises(ValueError):
|
||||
Benchmark(min=1, max=2, currency="RUB", includes_vat=True)
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from estimator.domain.enums import Region, Segment
|
||||
from estimator.domain.errors import (
|
||||
MarketDataInvalid,
|
||||
MarketVersionNotFound,
|
||||
UnknownRegionError,
|
||||
)
|
||||
from estimator.market import MarketCatalog
|
||||
from estimator.pricing import price
|
||||
from tests.factories import DOC_EXAMPLE_TASKS
|
||||
|
||||
REPO_MARKET = Path(__file__).resolve().parents[3] / "config" / "market"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def catalog() -> MarketCatalog:
|
||||
return MarketCatalog(REPO_MARKET, "2026Q4")
|
||||
|
||||
|
||||
def _copy_with(tmp_path: Path, text: str, name: str = "2026Q4") -> MarketCatalog:
|
||||
(tmp_path / f"{name}.toml").write_text(text)
|
||||
return MarketCatalog(tmp_path, name)
|
||||
|
||||
|
||||
def test_loads_real_file(catalog):
|
||||
repo = catalog.active()
|
||||
assert repo.version == "2026Q4"
|
||||
assert repo.currency(Region.RU) == "RUB"
|
||||
assert repo.rates(Region.EU)["backend"] == 85
|
||||
b = repo.benchmark(Region.RU, Segment.MULTICHANNEL_AI)
|
||||
assert b is not None and b.min == 400_000 and b.currency == "RUB"
|
||||
|
||||
|
||||
def test_missing_benchmark_is_none(catalog):
|
||||
assert catalog.active().benchmark(Region.RU, Segment.MINI_APP) is None
|
||||
|
||||
|
||||
def test_channel_multiplier(catalog):
|
||||
repo = catalog.active()
|
||||
assert repo.channel_multiplier(1) == pytest.approx(1.0)
|
||||
assert repo.channel_multiplier(3) == pytest.approx(1.3)
|
||||
with pytest.raises(ValueError):
|
||||
repo.channel_multiplier(0)
|
||||
|
||||
|
||||
def test_versions_listed(catalog):
|
||||
assert "2026Q4" in catalog.versions()
|
||||
|
||||
|
||||
def test_unknown_version(catalog):
|
||||
with pytest.raises(MarketVersionNotFound):
|
||||
catalog.load("1999Q1")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad", ["../default", "..", "a/b", "a\\b", "", "x" * 40, "2026Q4.toml"])
|
||||
def test_path_traversal_rejected(catalog, bad):
|
||||
with pytest.raises(MarketVersionNotFound):
|
||||
catalog.load(bad)
|
||||
|
||||
|
||||
def test_version_must_match_filename(tmp_path):
|
||||
src = (REPO_MARKET / "2026Q4.toml").read_text().replace('version = "2026Q4"', 'version = "2099Q1"')
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, src).active()
|
||||
|
||||
|
||||
def test_unknown_key_in_benchmark_rejected(tmp_path):
|
||||
src = (REPO_MARKET / "2026Q4.toml").read_text() + '\nsurprise = 1\n'
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, src).active()
|
||||
|
||||
|
||||
def test_unknown_top_level_key_rejected(tmp_path):
|
||||
src = 'surprise = 1\n' + (REPO_MARKET / "2026Q4.toml").read_text()
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, src).active()
|
||||
|
||||
|
||||
def test_non_positive_rate_rejected(tmp_path):
|
||||
src = (REPO_MARKET / "2026Q4.toml").read_text().replace("backend = 1800", "backend = 0", 1)
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, src).active()
|
||||
|
||||
|
||||
def test_inverted_benchmark_rejected(tmp_path):
|
||||
src = (REPO_MARKET / "2026Q4.toml").read_text().replace("min = 400000", "min = 900000")
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, src).active()
|
||||
|
||||
|
||||
def test_unknown_role_rejected(tmp_path):
|
||||
src = (REPO_MARKET / "2026Q4.toml").read_text().replace("qa = 1200", "wizard = 1200")
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, src).active()
|
||||
|
||||
|
||||
def test_broken_toml_rejected(tmp_path):
|
||||
with pytest.raises(MarketDataInvalid):
|
||||
_copy_with(tmp_path, "this is = = not toml").active()
|
||||
|
||||
|
||||
def test_unknown_region_in_file(tmp_path):
|
||||
src = (REPO_MARKET / "2026Q4.toml").read_text()
|
||||
# drop the whole eu rate card
|
||||
start = src.index("[rates.eu]")
|
||||
end = src.index("[channel_coefficient]")
|
||||
repo = _copy_with(tmp_path, src[:start] + src[end:]).active()
|
||||
with pytest.raises(UnknownRegionError):
|
||||
repo.rates(Region.EU)
|
||||
|
||||
|
||||
def test_cache_returns_same_object(catalog):
|
||||
assert catalog.load("2026Q4") is catalog.load("2026Q4")
|
||||
|
||||
|
||||
def test_rates_feed_calculator_to_doc_example(catalog):
|
||||
repo = catalog.active()
|
||||
ru = price(DOC_EXAMPLE_TASKS, repo.rates(Region.RU), rounding_step=1000)
|
||||
eu = price(DOC_EXAMPLE_TASKS, repo.rates(Region.EU))
|
||||
assert ru.cost_expected == 336_000 and ru.cost_with_reserve == 370_000
|
||||
assert eu.cost_with_reserve == 17_730
|
||||
|
||||
|
||||
def test_second_version_isolated(tmp_path):
|
||||
shutil.copy(REPO_MARKET / "2026Q4.toml", tmp_path / "2026Q4.toml")
|
||||
q1 = (REPO_MARKET / "2026Q4.toml").read_text().replace("2026Q4", "2027Q1").replace("backend = 1800", "backend = 2000")
|
||||
(tmp_path / "2027Q1.toml").write_text(q1)
|
||||
cat = MarketCatalog(tmp_path, "2026Q4")
|
||||
assert cat.versions() == ["2026Q4", "2027Q1"]
|
||||
assert cat.load("2027Q1").rates(Region.RU)["backend"] == 2000
|
||||
assert cat.active().rates(Region.RU)["backend"] == 1800
|
||||
Reference in New Issue
Block a user