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
import pytest
from sqlite3 import IntegrityError
from httpx import ConnectError
from conftest import MegaMillionsDraw
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:
"""test scraping game data and inserting it into the database"""
# 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))
# loop over the records returned
for key, value in raw_data.items():
# convert each record to a MegaMillionsDraw object
record = get_mega_millions_object(draw_date=key, draw_result=value)
# add the record to the database
db_session.add(record)
print(len(raw_data))
# loop over the records returned
for key, value in raw_data.items():
# convert each record to a MegaMillionsDraw object
record = get_mega_millions_object(draw_date=key, draw_result=value)
# add the record to the database
db_session.add(record)
# commit the changes
db_session.commit()
# commit the changes
db_session.commit()
# get all the records from the table
records = db_session.query(MegaMillionsDraw).all()
assert len(records) == len(raw_data)
# get all the records from the table
records = db_session.query(MegaMillionsDraw).all()
assert len(records) == len(raw_data)
except ConnectError as ce:
assert pytest.fail(f"Target scrape site is unavailable: {ce}")