Init Project Flexly Estimator
This commit is contained in:
@@ -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