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
+48 -31

No files matched your search

+19 -13
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,19 +74,22 @@ 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
raw_data = scrape_game_history(game='mega-millions') try:
raw_data = scrape_game_history(game='mega-millions')
print(len(raw_data)) print(len(raw_data))
# loop over the records returned # loop over the records returned
for key, value in raw_data.items(): for key, value in raw_data.items():
# convert each record to a MegaMillionsDraw object # convert each record to a MegaMillionsDraw object
record = get_mega_millions_object(draw_date=key, draw_result=value) record = get_mega_millions_object(draw_date=key, draw_result=value)
# add the record to the database # add the record to the database
db_session.add(record) db_session.add(record)
# commit the changes # commit the changes
db_session.commit() db_session.commit()
# 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}")
+18 -12
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,18 +74,21 @@ 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
raw_data = scrape_game_history(game='powerball') try:
raw_data = scrape_game_history(game='powerball')
# loop over the records returned # loop over the records returned
for key, value in raw_data.items(): for key, value in raw_data.items():
# convert each record to a PowerballDraw object # convert each record to a PowerballDraw object
record = get_powerball_object(draw_date=key, draw_result=value) record = get_powerball_object(draw_date=key, draw_result=value)
# add the record to the database # add the record to the database
db_session.add(record) db_session.add(record)
# commit the changes # commit the changes
db_session.commit() db_session.commit()
# 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}")
+11 -6
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."""
scrape_data = sc.scrape_game_history(game, year=1996) try:
assert len(scrape_data) in [17, 104] scrape_data = sc.scrape_game_history(game, year=1996)
scrape_data = sc.scrape_game_history(game, year=2020) assert len(scrape_data) in [17, 104]
assert len(scrape_data) in [104, 105] scrape_data = sc.scrape_game_history(game, year=2020)
scrape_data = sc.scrape_game_history(game) assert len(scrape_data) in [104, 105]
assert len(scrape_data) in range(0, 105) scrape_data = sc.scrape_game_history(game)
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: