odm/tests/test_alpha5.py
2026-08-18 22:03:52 +08:00

98 lines
3 KiB
Python

"""Tests for the ALPHA-5 NORAD catalog-number encoding."""
from __future__ import annotations
import pytest
from sgp4.api import Satrec
from odm import (
ALPHA5_MAX,
ALPHA5_MIN,
from_alpha5,
tle_catalog_field,
to_alpha5,
)
from odm import parse_tle, tle_from_gp
@pytest.mark.parametrize("value,expected", [
(100000, "A0000"),
(108493, "A8493"),
(180000, "J0000"), # skips I
(234567, "P4567"),
(270354, "T0354"),
(339999, "Z9999"),
])
def test_to_alpha5_known_values(value, expected):
assert to_alpha5(value) == expected
def test_to_alpha5_skips_i_and_o():
encoded = "".join(to_alpha5(v * 10000)[0] for v in range(10, 34))
assert "I" not in encoded and "O" not in encoded
assert len(set(encoded)) == 24 # all distinct
@pytest.mark.parametrize("value", [ALPHA5_MIN, 123456, 234567, ALPHA5_MAX])
def test_alpha5_round_trips(value):
assert from_alpha5(to_alpha5(value)) == value
@pytest.mark.parametrize("value", [99999, ALPHA5_MAX + 1, 0, -1])
def test_to_alpha5_rejects_out_of_range(value):
with pytest.raises(ValueError):
to_alpha5(value)
@pytest.mark.parametrize("field", ["", "ABCDE", "A123", "I0000", "O0000", "12345"])
def test_from_alpha5_rejects_invalid(field):
assert from_alpha5(field) is None
def test_tle_catalog_field_plain_numeric_unchanged():
assert tle_catalog_field("25544", alpha5=True) == "25544"
assert tle_catalog_field("25544", alpha5=False) == "25544"
def test_tle_catalog_field_alpha5_vs_temp():
assert tle_catalog_field("234567", alpha5=True) == "P4567"
temp = tle_catalog_field("234567", alpha5=False)
assert temp.isdigit() and 90000 <= int(temp) <= 99999
def test_tle_catalog_field_alphanumeric_always_pseudo():
for flag in (True, False):
field = tle_catalog_field("A1234", alpha5=flag)
assert field.isdigit() and 90000 <= int(field) <= 99999
def test_tle_from_gp_alpha5_round_trips_through_sgp4():
iss = parse_tle(
"ISS (ZARYA)",
"1 25544U 98067A 24070.50000000 .00012345 00000-0 22000-3 0 9991",
"2 25544 51.6400 200.0000 0001234 90.0000 270.0000 15.50000000400000",
)
gp = dict(iss.omm)
gp["NORAD_CAT_ID"] = "234567"
_, line1, line2 = tle_from_gp(gp, alpha5=True)
assert len(line1) == 69 and len(line2) == 69
assert line1[2:7] == "P4567"
assert line1[2:7] == line2[2:7]
# sgp4 must decode the ALPHA-5 field back to the real catalog number.
sat = Satrec.twoline2rv(line1, line2)
assert sat.satnum == 234567
def test_tle_from_gp_defaults_to_temp_id():
"""Without alpha5, a 6-digit numeric ID must not overflow the 5-char field."""
iss = parse_tle(
"ISS (ZARYA)",
"1 25544U 98067A 24070.50000000 .00012345 00000-0 22000-3 0 9991",
"2 25544 51.6400 200.0000 0001234 90.0000 270.0000 15.50000000400000",
)
gp = dict(iss.omm)
gp["NORAD_CAT_ID"] = "234567"
_, line1, line2 = tle_from_gp(gp)
assert len(line1) == 69 and line1[2:7].isdigit()
assert 90000 <= int(line1[2:7]) <= 99999