test_id_card_age.py 722 B

123456789101112131415161718
  1. from datetime import date
  2. from app.core.id_card import calc_age_from_chinese_id_card
  3. def test_calc_age_from_18_digit_id():
  4. # 1990-05-15
  5. id_card = "340102199005151234"
  6. assert calc_age_from_chinese_id_card(id_card, today=date(2026, 5, 14)) == 35
  7. assert calc_age_from_chinese_id_card(id_card, today=date(2026, 5, 15)) == 36
  8. assert calc_age_from_chinese_id_card(id_card, today=date(2026, 8, 19)) == 36
  9. def test_calc_age_rejects_invalid():
  10. assert calc_age_from_chinese_id_card("") is None
  11. assert calc_age_from_chinese_id_card("34010219900515") is None
  12. assert calc_age_from_chinese_id_card("340102199013151234") is None
  13. assert calc_age_from_chinese_id_card("340102199002301234") is None