| 1 |
import unittest.mock as mk |
| 2 |
|
| 3 |
import asdf |
| 4 |
import pytest |
| 5 |
from astropy.table import Table |
| 6 |
|
| 7 |
from asdf_astropy.io.connect import read_table, write_table |
| 8 |
|
| 9 |
|
| 10 |
def make_table(): |
| 11 |
a = [1, 4, 5] |
| 12 |
b = [2.0, 5.0, 8.2] |
| 13 |
c = ["x", "y", "z"] |
| 14 |
return Table([a, b, c], names=("a", "b", "c"), meta={"name": "first table"}) |
| 15 |
|
| 16 |
|
| 17 |
def test_table_io(tmp_path): |
| 18 |
tmp_file = tmp_path / "table.asdf" |
| 19 |
|
| 20 |
table = make_table() |
| 21 |
|
| 22 |
table.write(tmp_file) |
| 23 |
|
| 24 |
# Simple sanity check using ASDF directly |
| 25 |
with asdf.open(tmp_file) as af: |
| 26 |
assert "data" in af |
| 27 |
assert isinstance(af["data"], Table) |
| 28 |
assert all(af["data"] == table) |
| 29 |
|
| 30 |
# Now test using the table reader |
| 31 |
new_t = Table.read(tmp_file) |
| 32 |
assert all(new_t == table) |
| 33 |
|
| 34 |
|
| 35 |
def test_table_io_custom_key(tmp_path): |
| 36 |
tmp_file = tmp_path / "table.asdf" |
| 37 |
|
| 38 |
table = make_table() |
| 39 |
|
| 40 |
table.write(tmp_file, data_key="something") |
| 41 |
|
| 42 |
# Simple sanity check using ASDF directly |
| 43 |
with asdf.open(tmp_file) as af: |
| 44 |
assert "something" in af |
| 45 |
assert "data" not in af |
| 46 |
assert isinstance(af["something"], Table) |
| 47 |
assert all(af["something"] == table) |
| 48 |
|
| 49 |
# Now test using the table reader |
| 50 |
with pytest.raises(KeyError): |
| 51 |
new_t = Table.read(tmp_file) |
| 52 |
|
| 53 |
new_t = Table.read(tmp_file, data_key="something") |
| 54 |
assert all(new_t == table) |
| 55 |
|
| 56 |
|
| 57 |
def test_table_io_custom_tree(tmp_path): |
| 58 |
tmp_file = tmp_path / "table.asdf" |
| 59 |
|
| 60 |
table = make_table() |
| 61 |
|
| 62 |
def make_custom_tree(tab): |
| 63 |
return {"foo": {"bar": tab}} |
| 64 |
|
| 65 |
table.write(tmp_file, make_tree=make_custom_tree) |
| 66 |
|
| 67 |
# Simple sanity check using ASDF directly |
| 68 |
with asdf.open(tmp_file) as af: |
| 69 |
assert "foo" in af |
| 70 |
assert "bar" in af["foo"] |
| 71 |
assert "data" not in af |
| 72 |
assert all(af["foo"]["bar"] == table) |
| 73 |
|
| 74 |
# Now test using table reader |
| 75 |
with pytest.raises(KeyError): |
| 76 |
new_t = Table.read(tmp_file) |
| 77 |
|
| 78 |
def find_table(asdffile): |
| 79 |
return asdffile["foo"]["bar"] |
| 80 |
|
| 81 |
new_t = Table.read(tmp_file, find_table=find_table) |
| 82 |
assert all(new_t == table) |
| 83 |
|
| 84 |
|
| 85 |
def test_read_table_error(tmp_path): |
| 86 |
file_name = tmp_path / "table.asdf" |
| 87 |
|
| 88 |
with pytest.raises(ValueError, match="Options 'data_key' and 'find_table' are not compatible"): |
| 89 |
read_table(file_name, data_key=mk.MagicMock(), find_table=mk.MagicMock()) |
| 90 |
|
| 91 |
|
| 92 |
def test_write_table_error(tmp_path): |
| 93 |
file_name = tmp_path / "table.asdf" |
| 94 |
|
| 95 |
with pytest.raises(ValueError, match="Options 'data_key' and 'make_tree' are not compatible"): |
| 96 |
write_table(mk.MagicMock(), file_name, data_key=mk.MagicMock(), make_tree=mk.MagicMock()) |