@@ -695,6 +695,74 @@ def test_empty_events_returns_empty(
695695 result = client .search_markets ("bitcoin" )
696696 assert result == []
697697
698+ def test_malformed_market_skipped (
699+ self , client : PolymarketClient , httpx_mock
700+ ):
701+ """Markets that fail to parse are skipped (KeyError/TypeError)."""
702+ # outcomes as an int triggers TypeError in json.loads / iteration
703+ bad_market = {"condition_id" : "0xbad" , "outcomes" : 999 , "outcomePrices" : 999 }
704+ httpx_mock .add_response (json = {
705+ "events" : [{"markets" : [bad_market , SAMPLE_GAMMA_MARKET ]}],
706+ "pagination" : {},
707+ })
708+ result = client .search_markets ("bitcoin" )
709+ assert len (result ) == 1
710+ assert result [0 ].slug == "will-bitcoin-hit-100k"
711+
712+ def test_market_without_condition_id_skipped (
713+ self , client : PolymarketClient , httpx_mock
714+ ):
715+ """Markets without condition_id are skipped."""
716+ no_cid = {** SAMPLE_GAMMA_MARKET , "condition_id" : "" , "conditionId" : "" }
717+ httpx_mock .add_response (json = {
718+ "events" : [{"markets" : [no_cid ]}],
719+ "pagination" : {},
720+ })
721+ result = client .search_markets ("bitcoin" )
722+ assert result == []
723+
724+
725+ # ---------------------------------------------------------------------------
726+ # get_categories tests
727+ # ---------------------------------------------------------------------------
728+
729+ class TestGetCategories :
730+ def test_get_categories (self , client : PolymarketClient , httpx_mock ):
731+ httpx_mock .add_response (json = [
732+ {"label" : "Politics" , "slug" : "politics" },
733+ {"label" : "Sports" , "slug" : "sports" , "parentCategory" : "top" },
734+ ])
735+ result = client .get_categories (top_level_only = True )
736+ assert len (result ) == 1
737+ assert result [0 ]["label" ] == "Politics"
738+
739+ def test_get_categories_all (self , client : PolymarketClient , httpx_mock ):
740+ httpx_mock .add_response (json = [
741+ {"label" : "Politics" , "slug" : "politics" },
742+ {"label" : "Sports" , "slug" : "sports" , "parentCategory" : "top" },
743+ ])
744+ result = client .get_categories (top_level_only = False )
745+ assert len (result ) == 2
746+
747+ def test_get_categories_empty (self , client : PolymarketClient , httpx_mock ):
748+ httpx_mock .add_response (json = [])
749+ result = client .get_categories ()
750+ assert result == []
751+
752+ def test_get_categories_non_list (self , client : PolymarketClient , httpx_mock ):
753+ httpx_mock .add_response (json = {"error" : "bad" })
754+ result = client .get_categories ()
755+ assert result == []
756+
757+ def test_get_categories_cached (self , client : PolymarketClient , httpx_mock ):
758+ cats = [{"label" : "Politics" , "slug" : "politics" }]
759+ httpx_mock .add_response (json = cats )
760+ result1 = client .get_categories ()
761+ assert len (result1 ) == 1
762+ # Second call should use cache (no extra HTTP request)
763+ result2 = client .get_categories ()
764+ assert len (result2 ) == 1
765+
698766
699767# ---------------------------------------------------------------------------
700768# Line 213: get_tick_size cache hit path
0 commit comments