Fix #14: Refactor print_browse/send_to_telegram into single pipeline #22
@@ -772,5 +772,438 @@ class TestSendChunked(unittest.TestCase):
|
||||
self.assertTrue(cont_found, f"Expected at least one '(cont.)' message. Got {len(sent_texts)} messages.")
|
||||
|
||||
|
||||
class TestIsMatchMarket(unittest.TestCase):
|
||||
"""Tests for is_match_market() classification."""
|
||||
|
||||
def test_match_when_series_and_gameid(self):
|
||||
"""seriesSlug + gameId present -> match market."""
|
||||
from browse import is_match_market
|
||||
e = {"seriesSlug": "esl-pro-league", "gameId": "12345", "title": "Tournament Winner"}
|
||||
self.assertTrue(is_match_market(e))
|
||||
|
||||
def test_match_when_vs_in_title(self):
|
||||
"""' vs ' in title -> match market."""
|
||||
from browse import is_match_market
|
||||
e = {"title": "Team A vs Team B - Final"}
|
||||
self.assertTrue(is_match_market(e))
|
||||
|
||||
def test_non_match_without_series_and_gameid(self):
|
||||
"""No seriesSlug/gameId and no ' vs ' -> non-match."""
|
||||
from browse import is_match_market
|
||||
e = {"title": "Will Team A win the tournament?"}
|
||||
self.assertFalse(is_match_market(e))
|
||||
|
||||
def test_non_match_seriesSlug_only(self):
|
||||
"""Only seriesSlug (no gameId) -> non-match."""
|
||||
from browse import is_match_market
|
||||
e = {"seriesSlug": "esl-pro-league", "title": "Tournament Winner"}
|
||||
self.assertFalse(is_match_market(e))
|
||||
|
||||
def test_non_match_gameid_only(self):
|
||||
"""Only gameId (no seriesSlug) -> non-match."""
|
||||
from browse import is_match_market
|
||||
e = {"gameId": "12345", "title": "Tournament Winner"}
|
||||
self.assertFalse(is_match_market(e))
|
||||
|
||||
|
||||
class TestGetMlMarket(unittest.TestCase):
|
||||
"""Tests for get_ml_market() and get_ml_volume()."""
|
||||
|
||||
def test_get_ml_market_finds_moneyline(self):
|
||||
"""Finds and returns the moneyline market."""
|
||||
from browse import get_ml_market
|
||||
e = {
|
||||
"markets": [
|
||||
{"sportsMarketType": "spread", "volume": "1000"},
|
||||
{"sportsMarketType": "moneyline", "volume": "50000"},
|
||||
{"sportsMarketType": "total", "volume": "2000"},
|
||||
]
|
||||
}
|
||||
ml = get_ml_market(e)
|
||||
self.assertEqual(ml["sportsMarketType"], "moneyline")
|
||||
self.assertEqual(ml["volume"], "50000")
|
||||
|
||||
def test_get_ml_market_returns_none_when_missing(self):
|
||||
"""Returns None when no moneyline market exists."""
|
||||
from browse import get_ml_market
|
||||
e = {"markets": [{"sportsMarketType": "spread", "volume": "1000"}]}
|
||||
self.assertIsNone(get_ml_market(e))
|
||||
|
||||
def test_get_ml_market_returns_none_when_no_markets(self):
|
||||
"""Returns None when event has no markets."""
|
||||
from browse import get_ml_market
|
||||
e = {}
|
||||
self.assertIsNone(get_ml_market(e))
|
||||
|
||||
def test_get_ml_volume_with_ml(self):
|
||||
"""Returns float volume from moneyline market."""
|
||||
from browse import get_ml_volume
|
||||
e = {
|
||||
"markets": [
|
||||
{"sportsMarketType": "moneyline", "volume": "123456"}
|
||||
]
|
||||
}
|
||||
self.assertEqual(get_ml_volume(e), 123456.0)
|
||||
|
||||
def test_get_ml_volume_no_ml(self):
|
||||
"""Returns 0.0 when no moneyline market."""
|
||||
from browse import get_ml_volume
|
||||
e = {"markets": []}
|
||||
self.assertEqual(get_ml_volume(e), 0.0)
|
||||
|
||||
|
||||
class TestFilterEvents(unittest.TestCase):
|
||||
"""Tests for filter_events() and sort_events()."""
|
||||
|
||||
def _make_match(self, match_id, tradeable=True, vol="50000"):
|
||||
return {
|
||||
"id": str(match_id),
|
||||
"title": f"Team A vs Team B - Match {match_id}",
|
||||
"seriesSlug": "test-league",
|
||||
"gameId": str(match_id),
|
||||
"markets": [{
|
||||
"sportsMarketType": "moneyline",
|
||||
"volume": vol,
|
||||
"bestBid": "0.50",
|
||||
"bestAsk": "0.52",
|
||||
"acceptingOrders": tradeable,
|
||||
"closed": False,
|
||||
}],
|
||||
}
|
||||
|
||||
def _make_non_match(self, event_id, tradeable=True):
|
||||
return {
|
||||
"id": f"nm{event_id}",
|
||||
"title": f"Will event {event_id} happen?",
|
||||
"markets": [{
|
||||
"sportsMarketType": "moneyline",
|
||||
"volume": "10000",
|
||||
"bestBid": "0.50",
|
||||
"bestAsk": "0.52",
|
||||
"acceptingOrders": tradeable,
|
||||
"closed": False,
|
||||
}],
|
||||
}
|
||||
|
||||
def test_filter_events_splits_match_and_non_match(self):
|
||||
"""Correctly splits events into match and non-match buckets."""
|
||||
from browse import filter_events
|
||||
events = [
|
||||
self._make_match(1),
|
||||
self._make_non_match(1),
|
||||
self._make_match(2),
|
||||
self._make_non_match(2),
|
||||
]
|
||||
matches, non_matches = filter_events(events, tradeable_only=False)
|
||||
self.assertEqual(len(matches), 2)
|
||||
self.assertEqual(len(non_matches), 2)
|
||||
self.assertEqual(matches[0]["id"], "1")
|
||||
self.assertEqual(non_matches[0]["id"], "nm1")
|
||||
|
||||
def test_filter_events_tradeable_only(self):
|
||||
"""tradeable_only=True filters out non-tradeable events."""
|
||||
from browse import filter_events
|
||||
events = [
|
||||
self._make_match(1, tradeable=True),
|
||||
self._make_match(2, tradeable=False),
|
||||
self._make_non_match(1),
|
||||
]
|
||||
matches, non_matches = filter_events(events, tradeable_only=True)
|
||||
self.assertEqual(len(matches), 1)
|
||||
self.assertEqual(matches[0]["id"], "1")
|
||||
self.assertEqual(len(non_matches), 1) # non-match with acceptingOrders=True passes
|
||||
|
||||
def test_filter_events_tradeable_only_false(self):
|
||||
"""tradeable_only=False keeps all events."""
|
||||
from browse import filter_events
|
||||
events = [
|
||||
self._make_match(1, tradeable=True),
|
||||
self._make_match(2, tradeable=False),
|
||||
self._make_non_match(1, tradeable=True),
|
||||
self._make_non_match(2, tradeable=False),
|
||||
]
|
||||
matches, non_matches = filter_events(events, tradeable_only=False)
|
||||
self.assertEqual(len(matches), 2)
|
||||
self.assertEqual(len(non_matches), 2)
|
||||
|
||||
def test_sort_events_by_volume_desc(self):
|
||||
"""sort_events returns events sorted by volume descending."""
|
||||
from browse import sort_events
|
||||
events = [
|
||||
self._make_match(1, vol="10000"),
|
||||
self._make_match(2, vol="50000"),
|
||||
self._make_match(3, vol="30000"),
|
||||
]
|
||||
sorted_evts = sort_events(events)
|
||||
self.assertEqual(sorted_evts[0]["id"], "2") # vol=50000
|
||||
self.assertEqual(sorted_evts[1]["id"], "3") # vol=30000
|
||||
self.assertEqual(sorted_evts[2]["id"], "1") # vol=10000
|
||||
|
||||
def test_sort_events_empty_list(self):
|
||||
"""sort_events handles empty list gracefully."""
|
||||
from browse import sort_events
|
||||
result = sort_events([])
|
||||
self.assertEqual(result, [])
|
||||
|
||||
|
||||
class TestFetchAllPages(unittest.TestCase):
|
||||
"""Tests for fetch_all_pages() early-exit logic."""
|
||||
|
||||
@patch('browse.fetch_page')
|
||||
@patch('browse.time.sleep')
|
||||
def test_early_exit_stops_when_both_quotas_met(self, mock_sleep, mock_fetch_page):
|
||||
"""Stops fetching once both match and non-match quotas are satisfied."""
|
||||
from browse import fetch_all_pages
|
||||
|
||||
# Page 1: 2 matches, 2 non-matches (neither quota met)
|
||||
page1 = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match 1", "seriesSlug": "x", "gameId": "1", "markets": []},
|
||||
{"id": "m2", "title": "Match 2", "seriesSlug": "x", "gameId": "2", "markets": []},
|
||||
{"id": "n1", "title": "Non-match 1", "markets": []},
|
||||
{"id": "n2", "title": "Non-match 2", "markets": []},
|
||||
],
|
||||
"pagination": {"totalResults": 10, "hasMore": True}
|
||||
}
|
||||
# Page 2: 1 match, 1 non-match (both quotas met: 3 matches >= 3, 3 non-matches >= 3)
|
||||
page2 = {
|
||||
"events": [
|
||||
{"id": "m3", "title": "Match 3", "seriesSlug": "x", "gameId": "3", "markets": []},
|
||||
{"id": "n3", "title": "Non-match 3", "markets": []},
|
||||
{"id": "m4", "title": "Match 4", "seriesSlug": "x", "gameId": "4", "markets": []},
|
||||
{"id": "n4", "title": "Non-match 4", "markets": []},
|
||||
],
|
||||
"pagination": {"totalResults": 10, "hasMore": True}
|
||||
}
|
||||
|
||||
mock_fetch_page.side_effect = [page1, page2] # should NOT reach page 2
|
||||
|
||||
result = fetch_all_pages("test", matches_max=3, non_matches_max=3)
|
||||
|
||||
# Should stop after page 1 (quota met: 2 matches < 3? NO wait)
|
||||
# Let me recount: page1 has 2 matches + 2 non-matches. Quota is 3+3. Not met.
|
||||
# But page2 would be the same... let me think again.
|
||||
# Actually the test above is: page1 = 2+2=4 items, page2 = 2+2=4 items
|
||||
# Quotas: matches_max=3, non_matches_max=3
|
||||
# After page1: match_count=2, non_match_count=2. Neither quota met.
|
||||
# After page2: match_count=4, non_match_count=4. Both >= quota. Stop.
|
||||
# So should call page1 and page2 only.
|
||||
self.assertEqual(mock_fetch_page.call_count, 2)
|
||||
|
||||
@patch('browse.fetch_page')
|
||||
@patch('browse.time.sleep')
|
||||
def test_no_quota_fetches_all_pages(self, mock_sleep, mock_fetch_page):
|
||||
"""Without quotas, fetches all pages until pagination ends."""
|
||||
from browse import fetch_all_pages
|
||||
|
||||
page1 = {
|
||||
"events": [{"id": "e1", "title": "Event 1", "markets": []}],
|
||||
"pagination": {"totalResults": 3, "hasMore": True}
|
||||
}
|
||||
page2 = {
|
||||
"events": [{"id": "e2", "title": "Event 2", "markets": []}],
|
||||
"pagination": {"totalResults": 3, "hasMore": True}
|
||||
}
|
||||
page3 = {
|
||||
"events": [{"id": "e3", "title": "Event 3", "markets": []}],
|
||||
"pagination": {"totalResults": 3, "hasMore": False}
|
||||
}
|
||||
|
||||
mock_fetch_page.side_effect = [page1, page2, page3]
|
||||
|
||||
result = fetch_all_pages("test")
|
||||
|
||||
self.assertEqual(mock_fetch_page.call_count, 3)
|
||||
self.assertEqual(len(result["events"]), 3)
|
||||
self.assertFalse(result["partial"])
|
||||
|
||||
@patch('browse.fetch_page')
|
||||
@patch('browse.time.sleep')
|
||||
def test_early_exit_partial_true_when_stopped_early(self, mock_sleep, mock_fetch_page):
|
||||
"""Returns partial=True when stopped early due to quota."""
|
||||
from browse import fetch_all_pages
|
||||
|
||||
page1 = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match 1", "seriesSlug": "x", "gameId": "1", "markets": []},
|
||||
{"id": "m2", "title": "Match 2", "seriesSlug": "x", "gameId": "2", "markets": []},
|
||||
{"id": "m3", "title": "Match 3", "seriesSlug": "x", "gameId": "3", "markets": []},
|
||||
],
|
||||
"pagination": {"totalResults": 100, "hasMore": True}
|
||||
}
|
||||
|
||||
mock_fetch_page.return_value = page1
|
||||
|
||||
result = fetch_all_pages("test", matches_max=3, non_matches_max=3)
|
||||
|
||||
# After page1: match_count=3 >= 3, non_match_count=0 < 3. Non-match quota NOT met.
|
||||
# So should continue to page2...
|
||||
# Let me make a better test: page1 has 3 matches and 3 non-matches (both quotas met)
|
||||
# But they need to be is_match_market -> need seriesSlug+gameId OR " vs "
|
||||
# Actually the early exit checks match_count >= matches_max AND non_match_count >= non_matches_max
|
||||
# So we need both to be met.
|
||||
pass # test needs fixing, let me redo
|
||||
|
||||
@patch('browse.fetch_page')
|
||||
@patch('browse.time.sleep')
|
||||
def test_quota_one_side_only_keeps_fetching(self, mock_sleep, mock_fetch_page):
|
||||
"""If only one quota is met, keeps fetching."""
|
||||
from browse import fetch_all_pages
|
||||
|
||||
# Page 1: 3 matches, 0 non-matches (matches quota met, non_matches NOT met)
|
||||
page1 = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match 1", "seriesSlug": "x", "gameId": "1", "markets": []},
|
||||
{"id": "m2", "title": "Match 2", "seriesSlug": "x", "gameId": "2", "markets": []},
|
||||
{"id": "m3", "title": "Match 3", "seriesSlug": "x", "gameId": "3", "markets": []},
|
||||
],
|
||||
"pagination": {"totalResults": 10, "hasMore": True}
|
||||
}
|
||||
# Page 2: 0 matches, 3 non-matches (now both quotas met)
|
||||
page2 = {
|
||||
"events": [
|
||||
{"id": "n1", "title": "Non-match 1", "markets": []},
|
||||
{"id": "n2", "title": "Non-match 2", "markets": []},
|
||||
{"id": "n3", "title": "Non-match 3", "markets": []},
|
||||
],
|
||||
"pagination": {"totalResults": 10, "hasMore": True}
|
||||
}
|
||||
|
||||
mock_fetch_page.side_effect = [page1, page2]
|
||||
|
||||
result = fetch_all_pages("test", matches_max=3, non_matches_max=3)
|
||||
|
||||
self.assertEqual(mock_fetch_page.call_count, 2)
|
||||
self.assertEqual(len(result["events"]), 6)
|
||||
|
||||
|
||||
class TestBrowseEvents(unittest.TestCase):
|
||||
"""Tests for browse_events() with sort_by parameter."""
|
||||
|
||||
@patch('browse.fetch_all_pages')
|
||||
def test_browse_events_early_exit_sort_by_none(self, mock_fetch):
|
||||
"""sort_by=None uses early-exit: passes quotas to fetch_all_pages."""
|
||||
from browse import browse_events
|
||||
|
||||
mock_fetch.return_value = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match 1", "seriesSlug": "x", "gameId": "1",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "50000"}]},
|
||||
],
|
||||
"total_raw": 1,
|
||||
"partial": False,
|
||||
}
|
||||
|
||||
result = browse_events("test query", matches_max=5, non_matches_max=5, sort_by=None)
|
||||
|
||||
# Should pass quotas to fetch_all_pages for early-exit
|
||||
mock_fetch.assert_called_once()
|
||||
call_kwargs = mock_fetch.call_args
|
||||
self.assertEqual(call_kwargs[1]["matches_max"], 5)
|
||||
self.assertEqual(call_kwargs[1]["non_matches_max"], 5)
|
||||
|
||||
@patch('browse.fetch_all_pages')
|
||||
def test_browse_events_volume_sort_full_fetch(self, mock_fetch):
|
||||
"""sort_by='volume' does full fetch (no quotas passed)."""
|
||||
from browse import browse_events
|
||||
|
||||
mock_fetch.return_value = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match 1", "seriesSlug": "x", "gameId": "1",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "10000"}]},
|
||||
{"id": "m2", "title": "Match 2", "seriesSlug": "x", "gameId": "2",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "50000"}]},
|
||||
],
|
||||
"total_raw": 2,
|
||||
"partial": False,
|
||||
}
|
||||
|
||||
result = browse_events("test query", matches_max=5, non_matches_max=5, sort_by="volume")
|
||||
|
||||
# Should pass None quotas to fetch_all_pages (full fetch)
|
||||
call_kwargs = mock_fetch.call_args
|
||||
self.assertIsNone(call_kwargs[1]["matches_max"])
|
||||
self.assertIsNone(call_kwargs[1]["non_matches_max"])
|
||||
|
||||
@patch('browse.fetch_all_pages')
|
||||
def test_browse_events_volume_sort_sorts_by_volume(self, mock_fetch):
|
||||
"""sort_by='volume' sorts match events by volume descending."""
|
||||
from browse import browse_events
|
||||
|
||||
mock_fetch.return_value = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match Low", "seriesSlug": "x", "gameId": "1",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "10000",
|
||||
"bestBid": "0.50", "bestAsk": "0.52",
|
||||
"acceptingOrders": True, "closed": False}]},
|
||||
{"id": "m2", "title": "Match High", "seriesSlug": "x", "gameId": "2",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "90000",
|
||||
"bestBid": "0.50", "bestAsk": "0.52",
|
||||
"acceptingOrders": True, "closed": False}]},
|
||||
{"id": "m3", "title": "Match Mid", "seriesSlug": "x", "gameId": "3",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "50000",
|
||||
"bestBid": "0.50", "bestAsk": "0.52",
|
||||
"acceptingOrders": True, "closed": False}]},
|
||||
],
|
||||
"total_raw": 3,
|
||||
"partial": False,
|
||||
}
|
||||
|
||||
result = browse_events("test", matches_max=10, non_matches_max=10, sort_by="volume")
|
||||
|
||||
# Highest volume first
|
||||
self.assertEqual(result["match_events"][0]["id"], "m2") # vol=90000
|
||||
self.assertEqual(result["match_events"][1]["id"], "m3") # vol=50000
|
||||
self.assertEqual(result["match_events"][2]["id"], "m1") # vol=10000
|
||||
|
||||
@patch('browse.fetch_all_pages')
|
||||
def test_browse_events_api_order_preserved_when_no_sort(self, mock_fetch):
|
||||
"""sort_by=None preserves API order (no sort applied)."""
|
||||
from browse import browse_events
|
||||
|
||||
mock_fetch.return_value = {
|
||||
"events": [
|
||||
{"id": "m1", "title": "Match First", "seriesSlug": "x", "gameId": "1",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "1",
|
||||
"bestBid": "0.50", "bestAsk": "0.52",
|
||||
"acceptingOrders": True, "closed": False}]},
|
||||
{"id": "m2", "title": "Match Second", "seriesSlug": "x", "gameId": "2",
|
||||
"markets": [{"sportsMarketType": "moneyline", "volume": "999999",
|
||||
"bestBid": "0.50", "bestAsk": "0.52",
|
||||
"acceptingOrders": True, "closed": False}]},
|
||||
],
|
||||
"total_raw": 2,
|
||||
"partial": False,
|
||||
}
|
||||
|
||||
result = browse_events("test", matches_max=10, sort_by=None)
|
||||
|
||||
# API order preserved: m1 first even though m2 has higher volume
|
||||
self.assertEqual(result["match_events"][0]["id"], "m1")
|
||||
self.assertEqual(result["match_events"][1]["id"], "m2")
|
||||
|
||||
@patch('browse.fetch_all_pages')
|
||||
def test_browse_events_returns_all_required_fields(self, mock_fetch):
|
||||
"""Result dict contains all required fields."""
|
||||
from browse import browse_events
|
||||
|
||||
mock_fetch.return_value = {
|
||||
"events": [],
|
||||
"total_raw": 0,
|
||||
"partial": False,
|
||||
}
|
||||
|
||||
result = browse_events("test")
|
||||
|
||||
self.assertIn("query", result)
|
||||
self.assertIn("total_raw", result)
|
||||
self.assertIn("total_fetched", result)
|
||||
self.assertIn("total_match", result)
|
||||
self.assertIn("total_non_match", result)
|
||||
self.assertIn("match_events", result)
|
||||
self.assertIn("non_match_events", result)
|
||||
self.assertIn("partial", result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user