| 1 |
import asdf |
| 2 |
import pytest |
| 3 |
from astropy import units |
| 4 |
from astropy.table import Column |
| 5 |
|
| 6 |
|
| 7 |
def create_builtin_units(): |
| 8 |
return {u for u in list(units.__dict__.values()) if isinstance(u, units.MagUnit)} |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize("unit", create_builtin_units()) |
| 12 |
@pytest.mark.filterwarnings("ignore::astropy.units.core.UnitsWarning") |
| 13 |
def test_builtin_serialization(unit, tmp_path): |
| 14 |
file_path = tmp_path / "test.asdf" |
| 15 |
with asdf.AsdfFile() as af: |
| 16 |
af["unit"] = unit |
| 17 |
af.write_to(file_path) |
| 18 |
|
| 19 |
with asdf.open(file_path) as af: |
| 20 |
assert af["unit"].is_equivalent(unit) |
| 21 |
|
| 22 |
with file_path.open() as f: |
| 23 |
assert "tag:astropy.org:astropy/units/magunit-" in f.read() |
| 24 |
|
| 25 |
|
| 26 |
def create_magunits(): |
| 27 |
magunits = [] |
| 28 |
for u in units.__dict__.values(): |
| 29 |
if isinstance(u, units.UnitBase) and not isinstance(u, units.MagUnit): |
| 30 |
try: |
| 31 |
magunit = units.mag(u) |
| 32 |
except units.UnitConversionError: |
| 33 |
pass |
| 34 |
else: |
| 35 |
magunits.append(magunit) |
| 36 |
|
| 37 |
return frozenset(magunits) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize("unit", create_magunits()) |
| 41 |
@pytest.mark.filterwarnings("ignore::astropy.units.core.UnitsWarning") |
| 42 |
def test_magunit_serialization(unit, tmp_path): |
| 43 |
file_path = tmp_path / "test.asdf" |
| 44 |
with asdf.AsdfFile() as af: |
| 45 |
af["unit"] = unit |
| 46 |
af.write_to(file_path) |
| 47 |
|
| 48 |
with asdf.open(file_path) as af: |
| 49 |
assert af["unit"].is_equivalent(unit) |
| 50 |
|
| 51 |
with file_path.open() as f: |
| 52 |
assert "tag:astropy.org:astropy/units/magunit-" in f.read() |
| 53 |
|
| 54 |
|
| 55 |
def test_magunit_in_column(tmp_path): |
| 56 |
column = Column([1, 2, 3], unit=units.function.ABmag, name="foo") |
| 57 |
|
| 58 |
file_path = tmp_path / "test.asdf" |
| 59 |
with asdf.AsdfFile(version="1.6.0") as af: |
| 60 |
af["column"] = column |
| 61 |
af.write_to(file_path) |
| 62 |
|
| 63 |
with asdf.open(file_path) as af: |
| 64 |
# the column unit tests already cover the other attributes |
| 65 |
assert af["column"].unit == column.unit |