@@ -700,3 +700,98 @@ async def test_async_validation_passes(self) -> None:
700700 market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000"
701701 )
702702 assert isinstance (result , OrderCreated )
703+
704+
705+ # ── TestMinOrderCache ──────────────────────────────────────────────────
706+
707+
708+ class TestMinOrderCache :
709+ def test_cache_hit_skips_get_chance (self ) -> None :
710+ chance_calls = 0
711+
712+ def handler (request : httpx .Request ) -> httpx .Response :
713+ nonlocal chance_calls
714+ if request .url .path == "/v1/orders/chance" :
715+ chance_calls += 1
716+ return _multi_handler (request )
717+
718+ transport = _make_transport (handler )
719+ api = OrdersAPI (transport , CREDENTIALS , validate_min_order = True )
720+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
721+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
722+ assert chance_calls == 1
723+
724+ def test_cache_expires_after_ttl (self ) -> None :
725+ chance_calls = 0
726+
727+ def handler (request : httpx .Request ) -> httpx .Response :
728+ nonlocal chance_calls
729+ if request .url .path == "/v1/orders/chance" :
730+ chance_calls += 1
731+ return _multi_handler (request )
732+
733+ transport = _make_transport (handler )
734+ api = OrdersAPI (
735+ transport , CREDENTIALS , validate_min_order = True , min_total_ttl = - 1.0
736+ )
737+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
738+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
739+ assert chance_calls == 2
740+
741+ def test_cache_is_per_market (self ) -> None :
742+ chance_calls = 0
743+
744+ def handler (request : httpx .Request ) -> httpx .Response :
745+ nonlocal chance_calls
746+ if request .url .path == "/v1/orders/chance" :
747+ chance_calls += 1
748+ return _multi_handler (request )
749+
750+ transport = _make_transport (handler )
751+ api = OrdersAPI (transport , CREDENTIALS , validate_min_order = True )
752+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
753+ api .create (market = "KRW-ETH" , side = "bid" , ord_type = "price" , price = "6000" )
754+ assert chance_calls == 2
755+
756+ def test_cache_hit_still_validates (self ) -> None :
757+ transport = _make_transport (_multi_handler )
758+ api = OrdersAPI (transport , CREDENTIALS , validate_min_order = True )
759+ # First call populates the cache
760+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
761+ # Second call should use cache but still raise for low total
762+ with pytest .raises (ValidationError ) as exc_info :
763+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "3000" )
764+ assert exc_info .value .min_total == "5000"
765+
766+ def test_cache_populated_on_validation_failure (self ) -> None :
767+ chance_calls = 0
768+
769+ def handler (request : httpx .Request ) -> httpx .Response :
770+ nonlocal chance_calls
771+ if request .url .path == "/v1/orders/chance" :
772+ chance_calls += 1
773+ return _multi_handler (request )
774+
775+ transport = _make_transport (handler )
776+ api = OrdersAPI (transport , CREDENTIALS , validate_min_order = True )
777+ with pytest .raises (ValidationError ):
778+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "3000" )
779+ with pytest .raises (ValidationError ):
780+ api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "3000" )
781+ assert chance_calls == 1
782+
783+ @pytest .mark .asyncio
784+ async def test_async_cache_hit_skips_get_chance (self ) -> None :
785+ chance_calls = 0
786+
787+ async def handler (request : httpx .Request ) -> httpx .Response :
788+ nonlocal chance_calls
789+ if request .url .path == "/v1/orders/chance" :
790+ chance_calls += 1
791+ return _multi_handler (request )
792+
793+ transport = _make_async_transport (handler )
794+ api = AsyncOrdersAPI (transport , CREDENTIALS , validate_min_order = True )
795+ await api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
796+ await api .create (market = "KRW-BTC" , side = "bid" , ord_type = "price" , price = "6000" )
797+ assert chance_calls == 1
0 commit comments