| 1 |
import asdf |
| 2 |
import astropy.units as u |
| 3 |
import pytest |
| 4 |
from astropy.coordinates import Angle, representation |
| 5 |
from numpy.random import default_rng |
| 6 |
|
| 7 |
from asdf_astropy.testing.helpers import assert_representation_equal |
| 8 |
|
| 9 |
IGNORED_REPRESENTATION_CLASSES = [ |
| 10 |
"WGS84GeodeticRepresentation", |
| 11 |
"WGS72GeodeticRepresentation", |
| 12 |
"GRS80GeodeticRepresentation", |
| 13 |
] |
| 14 |
|
| 15 |
REPRESENTATION_CLASSES = [ |
| 16 |
getattr(representation, class_name) |
| 17 |
for class_name in representation.__all__ |
| 18 |
if "Base" not in class_name and class_name not in IGNORED_REPRESENTATION_CLASSES |
| 19 |
] |
| 20 |
|
| 21 |
|
| 22 |
def create_representation(rep_class): |
| 23 |
rng = default_rng(42) |
| 24 |
kwargs = {} |
| 25 |
for attr_name, attr_type in rep_class.attr_classes.items(): |
| 26 |
value = rng.random((100,)) * u.deg if issubclass(attr_type, Angle) else rng.random((100,)) * u.km |
| 27 |
kwargs[attr_name] = value |
| 28 |
|
| 29 |
return rep_class(**kwargs) |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize("rep_class", REPRESENTATION_CLASSES) |
| 33 |
def test_serialization(rep_class, tmp_path): |
| 34 |
rep = create_representation(rep_class) |
| 35 |
file_path = tmp_path / "test.asdf" |
| 36 |
|
| 37 |
with asdf.AsdfFile() as af: |
| 38 |
af["rep"] = rep |
| 39 |
af.write_to(file_path) |
| 40 |
|
| 41 |
with asdf.open(file_path) as af: |
| 42 |
assert_representation_equal(af["rep"], rep) |