""" tests/test_history.py --------------------- Tests for the History screen data layer — get_draws_with_game(). UI widget instantiation is tested with a hidden Tk root (skipped on headless). """ import pytest from db.models import ( get_draws_with_game, get_game_by_name, insert_draw, ) # ── get_draws_with_game ─────────────────────────────────────────────────────── def test_returns_empty_for_fresh_db(tmp_db): rows = get_draws_with_game() assert rows == [] def test_returns_all_games_when_no_filter(tmp_db): pb = get_game_by_name("Powerball") mm = get_game_by_name("Mega Millions") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7, source="powerball_ny") insert_draw(mm["id"], "2024-01-02", [7, 11, 22, 29, 38], bonus=4, source="megamillions_ny") rows = get_draws_with_game() assert len(rows) == 2 def test_game_name_is_included(tmp_db): pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7, source="powerball_ny") rows = get_draws_with_game() assert rows[0]["game_name"] == "Powerball" def test_filters_by_game_id(tmp_db): pb = get_game_by_name("Powerball") mm = get_game_by_name("Mega Millions") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7, source="powerball_ny") insert_draw(mm["id"], "2024-01-02", [7, 11, 22, 29, 38], bonus=4, source="megamillions_ny") rows = get_draws_with_game(game_id=pb["id"]) assert len(rows) == 1 assert rows[0]["game_name"] == "Powerball" def test_filters_by_date_from(tmp_db): pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7) insert_draw(pb["id"], "2024-06-01", [5, 10, 20, 30, 40], bonus=3) rows = get_draws_with_game(date_from="2024-06-01") assert len(rows) == 1 assert rows[0]["draw_date"] == "2024-06-01" def test_filters_by_date_to(tmp_db): pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7) insert_draw(pb["id"], "2024-06-01", [5, 10, 20, 30, 40], bonus=3) rows = get_draws_with_game(date_to="2024-01-31") assert len(rows) == 1 assert rows[0]["draw_date"] == "2024-01-01" def test_filters_by_date_range(tmp_db): pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7) insert_draw(pb["id"], "2024-03-15", [5, 10, 20, 30, 40], bonus=3) insert_draw(pb["id"], "2024-12-01", [9, 18, 27, 36, 45], bonus=15) rows = get_draws_with_game(date_from="2024-02-01", date_to="2024-06-30") assert len(rows) == 1 assert rows[0]["draw_date"] == "2024-03-15" def test_default_order_is_desc(tmp_db): pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7) insert_draw(pb["id"], "2024-06-01", [5, 10, 20, 30, 40], bonus=3) rows = get_draws_with_game() assert rows[0]["draw_date"] == "2024-06-01" assert rows[1]["draw_date"] == "2024-01-01" def test_order_asc(tmp_db): pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7) insert_draw(pb["id"], "2024-06-01", [5, 10, 20, 30, 40], bonus=3) rows = get_draws_with_game(order="ASC") assert rows[0]["draw_date"] == "2024-01-01" def test_limit(tmp_db): pb = get_game_by_name("Powerball") for day in range(1, 6): insert_draw(pb["id"], f"2024-01-{day:02d}", [day, 2, 3, 4, 5], bonus=day) rows = get_draws_with_game(limit=3) assert len(rows) == 3 def test_combined_filters(tmp_db): pb = get_game_by_name("Powerball") mm = get_game_by_name("Mega Millions") insert_draw(pb["id"], "2024-01-01", [1, 2, 3, 4, 5], bonus=7) insert_draw(pb["id"], "2024-03-01", [5, 10, 20, 30, 40], bonus=3) insert_draw(mm["id"], "2024-03-01", [7, 11, 22, 29, 38], bonus=4) rows = get_draws_with_game(game_id=pb["id"], date_from="2024-02-01") assert len(rows) == 1 assert rows[0]["game_name"] == "Powerball" assert rows[0]["draw_date"] == "2024-03-01" # ── UI smoke test (skipped on headless) ─────────────────────────────────────── def _has_display(): try: import tkinter as tk root = tk.Tk() root.withdraw() root.destroy() return True except Exception: return False @pytest.mark.skipif(not _has_display(), reason="no display available") def test_history_screen_instantiates(tmp_db): import tkinter as tk from ui.history import HistoryScreen root = tk.Tk() root.withdraw() try: screen = HistoryScreen(root) assert screen.winfo_exists() finally: root.destroy() @pytest.mark.skipif(not _has_display(), reason="no display available") def test_history_screen_shows_inserted_rows(tmp_db): import tkinter as tk from ui.history import HistoryScreen pb = get_game_by_name("Powerball") insert_draw(pb["id"], "2024-01-01", [1, 13, 36, 61, 69], bonus=7, source="powerball_ny") insert_draw(pb["id"], "2024-03-01", [5, 20, 35, 50, 65], bonus=15, source="powerball_ny") root = tk.Tk() root.withdraw() try: screen = HistoryScreen(root) children = screen._tree.get_children() assert len(children) == 2 finally: root.destroy()