|
| 1 | +"""Tests for Tyajyam calculations.""" |
| 2 | + |
| 3 | +import pytz |
| 4 | + |
| 5 | +from tyajyam import ( |
| 6 | + NAKSHATRA_TYAJYAM_RATIO, |
| 7 | + VARA_TYAJYAM_NAZHIGAI, |
| 8 | + _AMRITADI_TABLE, |
| 9 | + _tithi_ratio, |
| 10 | + compute_nakshatra_tyajyam, |
| 11 | + compute_vara_tyajyam, |
| 12 | + compute_amritadi_yogam, |
| 13 | + compute_tyajyam, |
| 14 | +) |
| 15 | + |
| 16 | +TZ = pytz.timezone("Asia/Kolkata") |
| 17 | + |
| 18 | + |
| 19 | +def _iso(jd, tz): |
| 20 | + """Simplified JD->ISO for testing (minutes-level precision).""" |
| 21 | + from advanced_panchang import _iso as real_iso |
| 22 | + |
| 23 | + return real_iso(jd, tz) |
| 24 | + |
| 25 | + |
| 26 | +# ---- Table dimension tests ---- |
| 27 | + |
| 28 | + |
| 29 | +def test_nakshatra_ratio_table_has_27_entries(): |
| 30 | + assert len(NAKSHATRA_TYAJYAM_RATIO) == 27 |
| 31 | + |
| 32 | + |
| 33 | +def test_amritadi_table_has_27_rows_of_7_columns(): |
| 34 | + assert len(_AMRITADI_TABLE) == 27 |
| 35 | + for i, row in enumerate(_AMRITADI_TABLE): |
| 36 | + assert len(row) == 7, f"Row {i} has {len(row)} cols" |
| 37 | + |
| 38 | + |
| 39 | +def test_amritadi_table_valid_codes(): |
| 40 | + valid = {"A", "S", "M", "P"} |
| 41 | + for i, row in enumerate(_AMRITADI_TABLE): |
| 42 | + for j, ch in enumerate(row): |
| 43 | + assert ch in valid, f"Row {i} col {j}: invalid code '{ch}'" |
| 44 | + |
| 45 | + |
| 46 | +def test_vara_nazhigai_covers_all_weekdays(): |
| 47 | + for d in range(1, 8): |
| 48 | + assert d in VARA_TYAJYAM_NAZHIGAI |
| 49 | + |
| 50 | + |
| 51 | +# ---- Tithi ratio tests ---- |
| 52 | + |
| 53 | + |
| 54 | +def test_tithi_ratio_pratipada(): |
| 55 | + assert _tithi_ratio(1) == (2, 5) |
| 56 | + assert _tithi_ratio(16) == (2, 5) # Krishna Pratipada same base |
| 57 | + |
| 58 | + |
| 59 | +def test_tithi_ratio_dashami(): |
| 60 | + assert _tithi_ratio(10) == (11, 20) |
| 61 | + assert _tithi_ratio(25) == (11, 20) # Krishna Dashami |
| 62 | + |
| 63 | + |
| 64 | +def test_tithi_ratio_purnima(): |
| 65 | + assert _tithi_ratio(15) == (29, 60) |
| 66 | + |
| 67 | + |
| 68 | +def test_tithi_ratio_amavasya(): |
| 69 | + assert _tithi_ratio(30) == (1, 10) |
| 70 | + |
| 71 | + |
| 72 | +# ---- Spec example: Ashwini Nakshatra Tyajyam ---- |
| 73 | + |
| 74 | + |
| 75 | +def test_ashwini_tyajyam_matches_spec(): |
| 76 | + """Ashwini ratio 5/6, 12h span -> start at 10h, duration 96 min.""" |
| 77 | + base_jd = 2460000.0 |
| 78 | + span_jd = 12.0 / 24.0 # 12 hours |
| 79 | + naks = [{"nak_idx": 0, "start_jd": base_jd, "end_jd": base_jd + span_jd}] |
| 80 | + results = compute_nakshatra_tyajyam(naks, _iso, TZ) |
| 81 | + assert len(results) == 1 |
| 82 | + assert results[0]["nakshatra"] == "Ashwini" |
| 83 | + |
| 84 | + |
| 85 | +def test_nakshatra_tyajyam_skips_if_offset_past_end(): |
| 86 | + """If the ratio puts the start beyond the nakshatra end, skip it.""" |
| 87 | + base_jd = 2460000.0 |
| 88 | + # Give Chitra (ratio 14/15) only a 1-hour window - tyajyam starts at 56 min |
| 89 | + span_jd = 1.0 / 24.0 |
| 90 | + naks = [{"nak_idx": 13, "start_jd": base_jd, "end_jd": base_jd + span_jd}] |
| 91 | + results = compute_nakshatra_tyajyam(naks, _iso, TZ) |
| 92 | + # Chitra 14/15 of 60 min = 56 min offset, so start is at 56 min — within window |
| 93 | + assert len(results) == 1 |
| 94 | + |
| 95 | + |
| 96 | +# ---- Vara Tyajyam ---- |
| 97 | + |
| 98 | + |
| 99 | +def test_vara_tyajyam_friday(): |
| 100 | + """Friday: 21 nazhigai * 24 min = 504 min from sunrise, 90 min duration.""" |
| 101 | + base_jd = 2460000.0 |
| 102 | + result = compute_vara_tyajyam(base_jd, 5, _iso, TZ) |
| 103 | + assert result is not None |
| 104 | + assert "start" in result |
| 105 | + assert "end" in result |
| 106 | + |
| 107 | + |
| 108 | +def test_vara_tyajyam_none_without_sunrise(): |
| 109 | + assert compute_vara_tyajyam(None, 5, _iso, TZ) is None |
| 110 | + |
| 111 | + |
| 112 | +# ---- Amritadi Yogam ---- |
| 113 | + |
| 114 | + |
| 115 | +def test_amritadi_safe_nakshatras_never_marana(): |
| 116 | + """Punarvasu, Purva Phalguni, Swati, Uttara Bhadrapada never produce M or P.""" |
| 117 | + safe_indices = [6, 10, 14, 25] |
| 118 | + for idx in safe_indices: |
| 119 | + row = _AMRITADI_TABLE[idx] |
| 120 | + assert "M" not in row and "P" not in row, ( |
| 121 | + f"Nakshatra {idx} should never have Marana or Prabalarishta" |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def test_amritadi_yogam_returns_correct_yogam(): |
| 126 | + """Thursday (iso=4) + Ashwini -> column index 3 -> 'A' -> Amrita.""" |
| 127 | + base_jd = 2460000.0 |
| 128 | + naks = [{"nak_idx": 0, "start_jd": base_jd, "end_jd": base_jd + 0.5}] |
| 129 | + results = compute_amritadi_yogam(naks, 4, base_jd, base_jd + 1.0, _iso, TZ) |
| 130 | + assert len(results) == 1 |
| 131 | + assert results[0]["yogam"] == "Amrita" |
| 132 | + assert results[0]["nakshatra"] == "Ashwini" |
| 133 | + |
| 134 | + |
| 135 | +def test_amritadi_bharani_sunday_is_prabalarishta(): |
| 136 | + """Sunday (iso=7) + Bharani -> column index 6 -> 'P' -> Prabalarishta.""" |
| 137 | + base_jd = 2460000.0 |
| 138 | + naks = [{"nak_idx": 1, "start_jd": base_jd, "end_jd": base_jd + 0.5}] |
| 139 | + results = compute_amritadi_yogam(naks, 7, base_jd, base_jd + 1.0, _iso, TZ) |
| 140 | + assert len(results) == 1 |
| 141 | + assert results[0]["yogam"] == "Prabalarishta" |
| 142 | + |
| 143 | + |
| 144 | +# ---- Integration test ---- |
| 145 | + |
| 146 | + |
| 147 | +def test_compute_tyajyam_returns_all_keys(): |
| 148 | + base_jd = 2460000.0 |
| 149 | + naks = [{"nak_idx": 0, "start_jd": base_jd, "end_jd": base_jd + 0.5}] |
| 150 | + tithis = [ |
| 151 | + { |
| 152 | + "index": 1, |
| 153 | + "name": "Shukla Pratipada", |
| 154 | + "start_jd": base_jd, |
| 155 | + "ends_at_jd": base_jd + 0.5, |
| 156 | + } |
| 157 | + ] |
| 158 | + result = compute_tyajyam( |
| 159 | + nakshatras_with_bounds=naks, |
| 160 | + tithis_in_window=tithis, |
| 161 | + sunrise_jd=base_jd, |
| 162 | + next_sunrise_jd=base_jd + 1.0, |
| 163 | + weekday_iso=4, |
| 164 | + iso_fn=_iso, |
| 165 | + tz=TZ, |
| 166 | + ) |
| 167 | + assert "nakshatra_tyajyam" in result |
| 168 | + assert "tithi_tyajyam" in result |
| 169 | + assert "vara_tyajyam" in result |
| 170 | + assert "amritadi_yogam" in result |
| 171 | + |
| 172 | + |
| 173 | +def test_full_panchang_includes_tyajyam(): |
| 174 | + """End-to-end: compute_detailed_panchang returns tyajyam section.""" |
| 175 | + from advanced_panchang import compute_detailed_panchang |
| 176 | + |
| 177 | + result = compute_detailed_panchang( |
| 178 | + "2026-05-16", 49.888, -119.496, "America/Vancouver" |
| 179 | + ) |
| 180 | + assert "tyajyam" in result |
| 181 | + tyajyam = result["tyajyam"] |
| 182 | + assert "nakshatra_tyajyam" in tyajyam |
| 183 | + assert "tithi_tyajyam" in tyajyam |
| 184 | + assert "vara_tyajyam" in tyajyam |
| 185 | + assert "amritadi_yogam" in tyajyam |
| 186 | + # Should have at least one entry in most categories |
| 187 | + assert len(tyajyam["nakshatra_tyajyam"]) >= 1 |
| 188 | + assert len(tyajyam["amritadi_yogam"]) >= 1 |
0 commit comments