Add try/except to catch error when site is unavailable

This commit is contained in:
chris committed 2026-03-04 20:41:58 -05:00
1 parent 97635dd3f2
commit 08491297e3
3 files changed
+17

No files matched your search

+6
View File
@@ -1,5 +1,8 @@
from datetime import datetime from datetime import datetime
import pytest
from sqlite3 import IntegrityError from sqlite3 import IntegrityError
from httpx import ConnectError
from conftest import MegaMillionsDraw from conftest import MegaMillionsDraw
from util.drawing import get_mega_millions_object from util.drawing import get_mega_millions_object
@@ -71,6 +74,7 @@ def test_mega_millions_draw_eq_repr_and_str(db_session) -> None:
def test_mega_millions_scrape_and_insert(db_session) -> None: def test_mega_millions_scrape_and_insert(db_session) -> None:
"""test scraping game data and inserting it into the database""" """test scraping game data and inserting it into the database"""
# scrape the current year history for mega millions # scrape the current year history for mega millions
try:
raw_data = scrape_game_history(game='mega-millions') raw_data = scrape_game_history(game='mega-millions')
print(len(raw_data)) print(len(raw_data))
@@ -87,3 +91,5 @@ def test_mega_millions_scrape_and_insert(db_session) -> None:
# get all the records from the table # get all the records from the table
records = db_session.query(MegaMillionsDraw).all() records = db_session.query(MegaMillionsDraw).all()
assert len(records) == len(raw_data) assert len(records) == len(raw_data)
except ConnectError as ce:
assert pytest.fail(f"Target scrape site is unavailable: {ce}")
+6
View File
@@ -1,5 +1,8 @@
from datetime import datetime from datetime import datetime
import pytest
from sqlite3 import IntegrityError from sqlite3 import IntegrityError
from httpx import ConnectError
from conftest import PowerballDraw from conftest import PowerballDraw
from util.drawing import get_powerball_object from util.drawing import get_powerball_object
@@ -71,6 +74,7 @@ def test_powerball_draw_eq_repr_and_str(db_session) -> None:
def test_powerball_scrape_and_insert(db_session) -> None: def test_powerball_scrape_and_insert(db_session) -> None:
"""test scraping game data and inserting it into the database""" """test scraping game data and inserting it into the database"""
# scrape the current year history for powerball # scrape the current year history for powerball
try:
raw_data = scrape_game_history(game='powerball') raw_data = scrape_game_history(game='powerball')
# loop over the records returned # loop over the records returned
@@ -86,3 +90,5 @@ def test_powerball_scrape_and_insert(db_session) -> None:
# get all the records from the table # get all the records from the table
records = db_session.query(PowerballDraw).all() records = db_session.query(PowerballDraw).all()
assert len(records) == len(raw_data) assert len(records) == len(raw_data)
except ConnectError as ce:
pytest.fail(f"Target scrape site is unavailable: {ce}")
+5
View File
@@ -1,6 +1,8 @@
"""Tests for the `util` module.""" """Tests for the `util` module."""
from datetime import datetime from datetime import datetime
from httpx import ConnectError
import pytest import pytest
import util.convert as uc import util.convert as uc
import util.drawing as ud import util.drawing as ud
@@ -51,12 +53,15 @@ def test_get_next_draw_date_specified_from_date() -> None:
@pytest.mark.parametrize("game", ['powerball', 'mega-millions']) @pytest.mark.parametrize("game", ['powerball', 'mega-millions'])
def test_scrape_game_history(game) -> None: def test_scrape_game_history(game) -> None:
"""Test scraping the powerball result page for 1996, 2020, and current year.""" """Test scraping the powerball result page for 1996, 2020, and current year."""
try:
scrape_data = sc.scrape_game_history(game, year=1996) scrape_data = sc.scrape_game_history(game, year=1996)
assert len(scrape_data) in [17, 104] assert len(scrape_data) in [17, 104]
scrape_data = sc.scrape_game_history(game, year=2020) scrape_data = sc.scrape_game_history(game, year=2020)
assert len(scrape_data) in [104, 105] assert len(scrape_data) in [104, 105]
scrape_data = sc.scrape_game_history(game) scrape_data = sc.scrape_game_history(game)
assert len(scrape_data) in range(0, 105) assert len(scrape_data) in range(0, 105)
except ConnectError as ce:
pytest.fail(f"Target scrape site is unavailable: {ce}")
def test_calculate_odds() -> None: def test_calculate_odds() -> None: