| 1 | |
| 2 | import unittest |
| 3 | |
| 4 | from src.pricing import Usage, cost_usd, resolve, PRICING |
| 5 | |
| 6 | |
| 7 | class PricingTests(unittest.TestCase): |
| 8 | def test_cost_math_exact(self): |
| 9 | |
| 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 | |
| 25 | |
| 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 | |
| 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 | |
| 52 | if __name__ == "__main__": |
| 53 | unittest.main() |