forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rag

Clone
★ 0

0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits

test_pricing.py53 lines, 2.4 KiBHistoryRawDownload
1"""Birim testleri — maliyet hesabı (src.pricing). Ağ/anahtar gerektirmez."""
2import unittest
3
4from src.pricing import Usage, cost_usd, resolve, PRICING
5
6
7class PricingTests(unittest.TestCase):
8 def test_cost_math_exact(self):
9 # deepseek-chat -> v4-flash: (6000*0.014 + 14000*0.44 + 3000*1.32)/1e6
10 u = Usage(input_cache_hit=6000, input_cache_miss=14000, output=3000)
11 self.assertAlmostEqual(cost_usd("deepseek-chat", u), 0.010204, places=6)
12
13 def test_offpeak_is_half(self):
14 u = Usage(input_cache_miss=10000, output=2000)
15 peak = cost_usd("deepseek-chat", u, "peak")
16 off = cost_usd("deepseek-chat", u, "offpeak")
17 self.assertAlmostEqual(off, peak * 0.5, places=9)
18
19 def test_alias_resolves(self):
20 self.assertEqual(resolve("deepseek-chat"), "deepseek-v4-flash")
21 self.assertEqual(resolve("deepseek-reasoner"), "deepseek-v4-pro")
22
23 def test_unknown_model_warns_and_returns_zero(self):
24 # AUDIT EXP-007 R2: bilinmeyen model artık KeyError DEĞİL — uyarı + 0.0 döner
25 # (model churn üretimi/costlog'u çökertmesin). Fiyat eksikse uyarı loglanır.
26 import warnings
27 with warnings.catch_warnings(record=True) as w:
28 warnings.simplefilter("always")
29 got = cost_usd("gpt-yok", Usage(output=1))
30 self.assertEqual(got, 0.0)
31 self.assertTrue(any("bilinmeyen model" in str(x.message) for x in w))
32
33 def test_usage_from_api_cache_split(self):
34 u = Usage.from_api({"prompt_cache_hit_tokens": 100,
35 "prompt_cache_miss_tokens": 900,
36 "completion_tokens": 200,
37 "completion_tokens_details": {"reasoning_tokens": 50}})
38 self.assertEqual((u.input_cache_hit, u.input_cache_miss, u.output, u.reasoning),
39 (100, 900, 200, 50))
40
41 def test_usage_from_api_no_cache_is_conservative(self):
42 # cache alanı yoksa tüm prompt cache-miss (en pahalı) sayılmalı
43 u = Usage.from_api({"prompt_tokens": 500, "completion_tokens": 100})
44 self.assertEqual((u.input_cache_hit, u.input_cache_miss), (0, 500))
45
46 def test_pricing_table_shape(self):
47 for m, p in PRICING.items():
48 self.assertLessEqual(p["in_hit"], p["in_miss"], f"{m}: hit>miss olmamalı")
49 self.assertLessEqual(p["in_miss"], p["out"], f"{m}: miss>out beklenir")
50
51
52if __name__ == "__main__":
53 unittest.main()