today_str and transit_reference_date already followed transit_date, then today, then now. Narayana's current period read only args.today, and the nakshatra-dasha age and the annual fallback age used datetime.now(). A dated report therefore mixed two dates. The annual fallback age also used days // 365, which moves to the next year of age early. All three now take the same observation date. The fallback age counts calendar birthdays. Tests are ported from upstream test_full_reading_observation_date.py and test_tajika_report_age.py and adapted to cmd_full_reading. Same-day web consultations are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyeZrYn6ryNcSe5Y2Y
110 lines
4.4 KiB
Python
110 lines
4.4 KiB
Python
"""A dated full reading uses one observation date everywhere (BUG-1542).
|
|
|
|
Ported from upstream yinduzhanxing 933f254e tests/test_full_reading_observation_date.py
|
|
and tests/test_tajika_report_age.py (sync 10). The expressions are taken from
|
|
scripts/jyotish_engine.py by name, so the real code is exercised without
|
|
generating a report.
|
|
"""
|
|
import ast
|
|
from datetime import datetime
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
ENGINE = Path(__file__).resolve().parents[1] / 'scripts/jyotish_engine.py'
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _tree():
|
|
return ast.parse(ENGINE.read_text(encoding='utf-8'))
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def clock_expressions():
|
|
found = {}
|
|
for node in ast.walk(_tree()):
|
|
if not isinstance(node, ast.Assign):
|
|
continue
|
|
names = [t.id for t in node.targets if isinstance(t, ast.Name)]
|
|
for name in ('today_str', 'transit_reference_date', 'today_dt', 'ref_today'):
|
|
if name in names:
|
|
assert name not in found, f'{name} assigned twice; the test would read only one'
|
|
found[name] = node.value
|
|
return {k: compile(ast.Expression(v), '<report-clock>', 'eval') for k, v in found.items()}
|
|
|
|
|
|
class _Clock(datetime):
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
return cls(2030, 6, 7)
|
|
|
|
|
|
@pytest.mark.parametrize('arguments,expected', [
|
|
({'transit_date': '2001-02-03', 'today': '2020-04-05'}, '2001-02-03'),
|
|
({'transit_date': '2001-02-03'}, '2001-02-03'),
|
|
({'today': '2020-04-05'}, '2020-04-05'),
|
|
({}, '2030-06-07'),
|
|
])
|
|
def test_all_report_periods_observe_the_same_requested_date(arguments, expected):
|
|
values = {k: eval(code, {'args': SimpleNamespace(**arguments), 'datetime': _Clock})
|
|
for k, code in clock_expressions().items()}
|
|
values = {k: v.strftime('%Y-%m-%d') if isinstance(v, datetime) else v for k, v in values.items()}
|
|
assert values == dict.fromkeys(('today_str', 'transit_reference_date', 'today_dt', 'ref_today'), expected)
|
|
|
|
|
|
def _block_after(predicate, length):
|
|
for function in _tree().body:
|
|
if not isinstance(function, ast.FunctionDef) or function.name not in ('_cmd_full_reading', 'cmd_full_reading'):
|
|
continue
|
|
for node in ast.walk(function):
|
|
body = getattr(node, 'body', None)
|
|
if not isinstance(body, list):
|
|
continue
|
|
for index, child in enumerate(body):
|
|
if predicate(child):
|
|
return compile(ast.Module(body=body[index:index + length], type_ignores=[]), '<block>', 'exec')
|
|
raise AssertionError('block not found in cmd_full_reading')
|
|
|
|
|
|
def _assigns_args_age(node, target):
|
|
return (isinstance(node, ast.Assign) and [getattr(t, 'id', None) for t in node.targets] == [target]
|
|
and isinstance(node.value, ast.Attribute) and node.value.attr == 'age'
|
|
and isinstance(node.value.value, ast.Name) and node.value.value.id == 'args')
|
|
|
|
|
|
@pytest.mark.parametrize('extra,expected', [
|
|
({'today': '2020-12-30'}, 20),
|
|
({'today': '2021-01-01'}, 21),
|
|
({'today': '2021-01-01', 'transit_date': '2020-12-30'}, 20),
|
|
({'today': '2020-12-30', 'age': 18}, 18),
|
|
])
|
|
def test_fallback_annual_age_changes_at_the_calendar_birthday(extra, expected):
|
|
code = _block_after(lambda n: _assigns_args_age(n, 'age'), 2)
|
|
args = SimpleNamespace(**{'year': 2000, 'month': 1, 'day': 1, 'age': None, **extra})
|
|
state = {'args': args, 'datetime': datetime, 'report': {'warnings': []}}
|
|
exec(code, state)
|
|
assert state['age'] == expected
|
|
|
|
|
|
def test_fallback_annual_age_is_not_days_over_365():
|
|
# 2000-01-01 to 2039-12-25 is 14603 days; 14603 // 365 = 40, but the person is 39.
|
|
code = _block_after(lambda n: _assigns_args_age(n, 'age'), 2)
|
|
args = SimpleNamespace(year=2000, month=1, day=1, age=None, today='2039-12-25')
|
|
state = {'args': args, 'datetime': datetime, 'report': {'warnings': []}}
|
|
exec(code, state)
|
|
assert state['age'] == 39
|
|
|
|
|
|
@pytest.mark.parametrize('extra,expected', [
|
|
({'transit_date': '2010-01-01', 'today': '2020-01-01'}, 3653 / 365.25),
|
|
({'today': '2020-01-01'}, 7305 / 365.25),
|
|
])
|
|
def test_nakshatra_dasha_age_uses_the_observation_date(extra, expected):
|
|
code = _block_after(lambda n: _assigns_args_age(n, 'nak_age'), 2)
|
|
args = SimpleNamespace(**{'year': 2000, 'month': 1, 'day': 1, 'age': None, **extra})
|
|
state = {'args': args, 'datetime': _Clock}
|
|
exec(code, state)
|
|
assert state['nak_age'] == pytest.approx(expected)
|