|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import json |
| 4 | + |
| 5 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
| 6 | + |
| 7 | +import pytest |
| 8 | +from fastapi.testclient import TestClient |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +from main import app |
| 12 | +from models import Resort |
| 13 | + |
| 14 | + |
| 15 | +# Helper to build a minimal Resort with the fields we care about |
| 16 | +def make_resort(resort_id, name, blackout_dates=None, ltt_blackout_dates=None, ltt_available=False): |
| 17 | + return Resort( |
| 18 | + resort_id=resort_id, |
| 19 | + name=name, |
| 20 | + region='West', |
| 21 | + reservation_status='Not Required', |
| 22 | + indy_page=f'https://example.com/{resort_id}', |
| 23 | + ltt_available=ltt_available, |
| 24 | + blackout_all_dates=json.dumps(blackout_dates or []), |
| 25 | + ltt_blackout_all_dates=json.dumps(ltt_blackout_dates or []), |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +FAKE_RESORTS = [ |
| 30 | + make_resort( |
| 31 | + 'id-1', |
| 32 | + 'Alpine Peak', |
| 33 | + blackout_dates=['2025-12-25', '2025-12-26', '2026-01-01'], |
| 34 | + ltt_blackout_dates=['2025-12-25'], |
| 35 | + ltt_available=True, |
| 36 | + ), |
| 37 | + make_resort( |
| 38 | + 'id-2', |
| 39 | + 'Nordic Valley', |
| 40 | + blackout_dates=['2026-02-14', '2026-02-15'], |
| 41 | + ltt_blackout_dates=[], |
| 42 | + ltt_available=True, |
| 43 | + ), |
| 44 | + make_resort( |
| 45 | + 'id-3', |
| 46 | + 'Powder Ridge', |
| 47 | + blackout_dates=[], |
| 48 | + ltt_blackout_dates=[], |
| 49 | + ltt_available=False, |
| 50 | + ), |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture(autouse=True) |
| 55 | +def patch_resorts(): |
| 56 | + with patch('main._resorts', FAKE_RESORTS): |
| 57 | + yield |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def client(): |
| 62 | + return TestClient(app) |
| 63 | + |
| 64 | + |
| 65 | +# --- blackout_dates --- |
| 66 | + |
| 67 | + |
| 68 | +def test_blackout_dates_excludes_blacked_out_resorts(client): |
| 69 | + # Christmas day blacks out Alpine Peak |
| 70 | + response = client.get('/resorts?blackout_dates=2025-12-25') |
| 71 | + assert response.status_code == 200 |
| 72 | + names = {r['name'] for r in response.json()} |
| 73 | + assert 'Alpine Peak' not in names |
| 74 | + assert 'Nordic Valley' in names |
| 75 | + assert 'Powder Ridge' in names |
| 76 | + |
| 77 | + |
| 78 | +def test_blackout_dates_multiple_dates(client): |
| 79 | + # Dec 26 hits Alpine Peak; Feb 14 hits Nordic Valley |
| 80 | + response = client.get('/resorts?blackout_dates=2025-12-26,2026-02-14') |
| 81 | + names = {r['name'] for r in response.json()} |
| 82 | + assert names == {'Powder Ridge'} |
| 83 | + |
| 84 | + |
| 85 | +def test_blackout_dates_no_overlap_returns_all(client): |
| 86 | + response = client.get('/resorts?blackout_dates=2025-07-04') |
| 87 | + assert len(response.json()) == 3 |
| 88 | + |
| 89 | + |
| 90 | +def test_blackout_dates_ignores_whitespace(client): |
| 91 | + response = client.get('/resorts?blackout_dates=2025-12-25, 2026-02-14') |
| 92 | + names = {r['name'] for r in response.json()} |
| 93 | + assert names == {'Powder Ridge'} |
| 94 | + |
| 95 | + |
| 96 | +def test_blackout_dates_resort_with_empty_list_always_passes(client): |
| 97 | + response = client.get('/resorts?blackout_dates=2025-12-25') |
| 98 | + names = {r['name'] for r in response.json()} |
| 99 | + assert 'Powder Ridge' in names |
| 100 | + |
| 101 | + |
| 102 | +# --- ltt_dates --- |
| 103 | + |
| 104 | + |
| 105 | +def test_ltt_dates_excludes_ltt_blacked_out_resorts(client): |
| 106 | + # Dec 25 is in Alpine Peak's LTT blackout list |
| 107 | + response = client.get('/resorts?ltt_dates=2025-12-25') |
| 108 | + names = {r['name'] for r in response.json()} |
| 109 | + assert 'Alpine Peak' not in names |
| 110 | + assert 'Nordic Valley' in names |
| 111 | + assert 'Powder Ridge' in names |
| 112 | + |
| 113 | + |
| 114 | +def test_ltt_dates_no_overlap_returns_all(client): |
| 115 | + response = client.get('/resorts?ltt_dates=2025-07-04') |
| 116 | + assert len(response.json()) == 3 |
| 117 | + |
| 118 | + |
| 119 | +def test_ltt_dates_resort_with_empty_ltt_blackout_always_passes(client): |
| 120 | + # Nordic Valley has empty ltt_blackout — should never be excluded by ltt_dates |
| 121 | + response = client.get('/resorts?ltt_dates=2025-12-25') |
| 122 | + names = {r['name'] for r in response.json()} |
| 123 | + assert 'Nordic Valley' in names |
| 124 | + |
| 125 | + |
| 126 | +# --- ltt_available --- |
| 127 | + |
| 128 | + |
| 129 | +def test_ltt_available_true(client): |
| 130 | + response = client.get('/resorts?ltt_available=true') |
| 131 | + names = {r['name'] for r in response.json()} |
| 132 | + assert names == {'Alpine Peak', 'Nordic Valley'} |
| 133 | + |
| 134 | + |
| 135 | +def test_ltt_available_false(client): |
| 136 | + response = client.get('/resorts?ltt_available=false') |
| 137 | + names = {r['name'] for r in response.json()} |
| 138 | + assert names == {'Powder Ridge'} |
| 139 | + |
| 140 | + |
| 141 | +def test_ltt_available_in_response_shape(client): |
| 142 | + response = client.get('/resorts') |
| 143 | + assert all('ltt_available' in r for r in response.json()) |
| 144 | + |
| 145 | + |
| 146 | +# --- composability --- |
| 147 | + |
| 148 | + |
| 149 | +def test_ltt_available_and_ltt_dates_combined(client): |
| 150 | + # Only LTT resorts, but not blacked out on Dec 25 |
| 151 | + response = client.get('/resorts?ltt_available=true<t_dates=2025-12-25') |
| 152 | + names = {r['name'] for r in response.json()} |
| 153 | + # Alpine Peak is LTT but blacked out on Dec 25 via ltt_dates |
| 154 | + assert names == {'Nordic Valley'} |
| 155 | + |
| 156 | + |
| 157 | +def test_blackout_and_ltt_dates_combined(client): |
| 158 | + response = client.get('/resorts?blackout_dates=2025-12-25<t_dates=2025-12-25') |
| 159 | + names = {r['name'] for r in response.json()} |
| 160 | + assert names == {'Nordic Valley', 'Powder Ridge'} |
| 161 | + |
| 162 | + |
| 163 | +def test_all_filter_types_combined(client): |
| 164 | + # LTT available, not blacked out on Dec 25 (regular), not blacked out on Feb 14 (LTT) |
| 165 | + response = client.get( |
| 166 | + '/resorts?ltt_available=true&blackout_dates=2025-12-25<t_dates=2026-02-14' |
| 167 | + ) |
| 168 | + # Alpine Peak: LTT available ✓, but blacked out Dec 25 via blackout_dates ✗ |
| 169 | + # Nordic Valley: LTT available ✓, not blacked out Dec 25 ✓, but Feb 14 is only in |
| 170 | + # its regular blackout list (not ltt_blackout) — so ltt_dates=2026-02-14 passes ✓ |
| 171 | + names = {r['name'] for r in response.json()} |
| 172 | + assert names == {'Nordic Valley'} |
0 commit comments