File: test_io.py

package info (click to toggle)
asdf-astropy 0.3.0%2Bgithub-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 556 kB
  • sloc: python: 3,703; makefile: 109
file content (100 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import unittest.mock as mk

import asdf
import pytest
from astropy.table import Table

from asdf_astropy.io.connect import read_table, write_table


def make_table():

    a = [1, 4, 5]
    b = [2.0, 5.0, 8.2]
    c = ["x", "y", "z"]
    return Table([a, b, c], names=("a", "b", "c"), meta={"name": "first table"})


def test_table_io(tmpdir):

    tmpfile = str(tmpdir.join("table.asdf"))

    table = make_table()

    table.write(tmpfile)

    # Simple sanity check using ASDF directly
    with asdf.open(tmpfile) as af:
        assert "data" in af.keys()
        assert isinstance(af["data"], Table)
        assert all(af["data"] == table)

    # Now test using the table reader
    new_t = Table.read(tmpfile)
    assert all(new_t == table)


def test_table_io_custom_key(tmpdir):

    tmpfile = str(tmpdir.join("table.asdf"))

    table = make_table()

    table.write(tmpfile, data_key="something")

    # Simple sanity check using ASDF directly
    with asdf.open(tmpfile) as af:
        assert "something" in af.keys()
        assert "data" not in af.keys()
        assert isinstance(af["something"], Table)
        assert all(af["something"] == table)

    # Now test using the table reader
    with pytest.raises(KeyError):
        new_t = Table.read(tmpfile)

    new_t = Table.read(tmpfile, data_key="something")
    assert all(new_t == table)


def test_table_io_custom_tree(tmpdir):

    tmpfile = str(tmpdir.join("table.asdf"))

    table = make_table()

    def make_custom_tree(tab):
        return dict(foo=dict(bar=tab))

    table.write(tmpfile, make_tree=make_custom_tree)

    # Simple sanity check using ASDF directly
    with asdf.open(tmpfile) as af:
        assert "foo" in af.keys()
        assert "bar" in af["foo"]
        assert "data" not in af.keys()
        assert all(af["foo"]["bar"] == table)

    # Now test using table reader
    with pytest.raises(KeyError):
        new_t = Table.read(tmpfile)

    def find_table(asdffile):
        return asdffile["foo"]["bar"]

    new_t = Table.read(tmpfile, find_table=find_table)
    assert all(new_t == table)


def test_read_table_error(tmp_path):
    file_name = tmp_path / "table.asdf"

    with pytest.raises(ValueError, match="Options 'data_key' and 'find_table' are not compatible"):
        read_table(file_name, data_key=mk.MagicMock(), find_table=mk.MagicMock())


def test_write_table_error(tmp_path):
    file_name = tmp_path / "table.asdf"

    with pytest.raises(ValueError, match="Options 'data_key' and 'make_tree' are not compatible"):
        write_table(mk.MagicMock(), file_name, data_key=mk.MagicMock(), make_tree=mk.MagicMock())