Files
Lottery_Project/tests/test_database.py
T
2026-02-21 13:30:58 -05:00

55 lines
1.8 KiB
Python

from datetime import datetime
from pytest import fixture
from sqlalchemy.exc import IntegrityError
import sqlalchemy.orm
import data.database as db
@fixture
def db_session() -> db.dal.Session:
db.dal.connect()
return db.dal.Session()
def test_base() -> None:
"""Test to ensure db.Base() is a sqlalchemy DeclarativeBase object."""
assert isinstance(db.Base(), sqlalchemy.orm.DeclarativeBase)
def test_powerball_draw_by_date_blank(db_session) -> None:
"""
Test that a null record is returned when no date is provided and that no dateless
records can exist in the table.
"""
with db_session as session:
blank_results = session.query(db.PowerballDraw).filter_by(draw_date='').all()
assert blank_results == []
def test_powerball_draw_by_date(db_session) -> None:
"""Test to be sure we can find the first draw in the table by date."""
with db_session as session:
date = datetime(2010, 2, 3).date()
draw = db.PowerballDraw(draw_date=date, white_ball1=17, white_ball2=22, white_ball3=36, white_ball4=37,
white_ball5=52, powerball=24, power_play=2)
record = session.query(db.PowerballDraw).filter_by(draw_date=date).first()
assert record == draw
def test_powerball_draw_integrity(db_session) -> None:
"""Test that a proper error is received when a duplicate value insertion is attempted."""
with db_session as session:
date = datetime.strptime('02/03/2010','%m/%d/%Y').date()
try:
draw = db.PowerballDraw(draw_date=date, white_ball1=17, white_ball2=22, white_ball3=36, white_ball4=37,
white_ball5=52, powerball=24, power_play=2)
session.add(draw)
session.commit()
assert False
except IntegrityError:
assert True