|
| 1 | +# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 | + |
| 3 | +# THIRD PARTY |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from astropy.cosmology._src.io.builtin.mrt import ( |
| 8 | + read_mrt, |
| 9 | + write_mrt, |
| 10 | +) |
| 11 | +from astropy.table import QTable, Table |
| 12 | + |
| 13 | +from .base import ReadWriteDirectTestBase, ReadWriteTestMixinBase |
| 14 | + |
| 15 | + |
| 16 | +class ReadWriteMRTTestMixin(ReadWriteTestMixinBase): |
| 17 | + """ |
| 18 | + Tests for a Cosmology[Read/Write] with ``format="mrt"``. |
| 19 | + This class will not be directly called by :mod:`pytest` since its name does |
| 20 | + not begin with ``Test``. To activate the contained tests this class must |
| 21 | + be inherited in a subclass. Subclasses must define a :func:`pytest.fixture` |
| 22 | + ``cosmo`` that returns/yields an instance of a |Cosmology|. |
| 23 | + See ``TestCosmology`` for an example. |
| 24 | + """ |
| 25 | + |
| 26 | + def test_to_mrt_bad_index(self, read, write, tmp_path): |
| 27 | + """Test if argument ``index`` is incorrect""" |
| 28 | + fp = tmp_path / "test_to_mrt_bad_index.mrt" |
| 29 | + |
| 30 | + write(fp, format="ascii.mrt") |
| 31 | + |
| 32 | + # single-row table and has a non-0/None index |
| 33 | + with pytest.raises(IndexError, match="index 2 out of range"): |
| 34 | + read(fp, index=2, format="ascii.mrt") |
| 35 | + |
| 36 | + # string index where doesn't match |
| 37 | + with pytest.raises(KeyError, match="No matches found for key"): |
| 38 | + read(fp, index="row 0", format="ascii.mrt") |
| 39 | + |
| 40 | + # ----------------------- |
| 41 | + |
| 42 | + def test_to_mrt_failed_cls(self, write, tmp_path): |
| 43 | + """Test failed table type.""" |
| 44 | + fp = tmp_path / "test_to_mrt_failed_cls.mrt" |
| 45 | + |
| 46 | + with pytest.raises(TypeError, match="'cls' must be"): |
| 47 | + write(fp, format="ascii.mrt", cls=list) |
| 48 | + |
| 49 | + # ----------------------- |
| 50 | + |
| 51 | + @pytest.mark.parametrize("tbl_cls", [QTable, Table]) |
| 52 | + def test_to_mrt_cls(self, write, tbl_cls, tmp_path): |
| 53 | + fp = tmp_path / "test_to_mrt_cls.mrt" |
| 54 | + write(fp, format="ascii.mrt", cls=tbl_cls) |
| 55 | + |
| 56 | + # ----------------------- |
| 57 | + |
| 58 | + def test_readwrite_mrt_instance(self, cosmo_cls, cosmo, read, write, tmp_path): |
| 59 | + """Test cosmology -> ascii.mrt -> cosmology.""" |
| 60 | + fp = tmp_path / "test_readwrite_mrt_instance.mrt" |
| 61 | + |
| 62 | + # ------------ |
| 63 | + # To Table |
| 64 | + |
| 65 | + write(fp, format="ascii.mrt") |
| 66 | + |
| 67 | + # some checks on the saved file |
| 68 | + tbl = Table.read(fp, format="ascii.mrt") |
| 69 | + assert tbl["name"] == cosmo.name |
| 70 | + |
| 71 | + # ------------ |
| 72 | + # From Table |
| 73 | + |
| 74 | + tbl["mismatching"] = "will error" |
| 75 | + tbl.write(fp, format="ascii.mrt", overwrite=True) |
| 76 | + |
| 77 | + # tests are different if the last argument is a **kwarg |
| 78 | + if tuple(cosmo._init_signature.parameters.values())[-1].kind == 4: |
| 79 | + got = read(fp, format="ascii.mrt") |
| 80 | + |
| 81 | + assert got.__class__ is cosmo_cls |
| 82 | + assert got.name == cosmo.name |
| 83 | + assert "mismatching" not in got.meta |
| 84 | + |
| 85 | + return # don't continue testing |
| 86 | + |
| 87 | + # read with mismatching parameters errors |
| 88 | + with pytest.raises(TypeError, match="there are unused parameters"): |
| 89 | + read(fp, format="ascii.mrt") |
| 90 | + |
| 91 | + # unless mismatched are moved to meta |
| 92 | + got = read(fp, format="ascii.mrt", move_to_meta=True) |
| 93 | + assert got == cosmo |
| 94 | + assert got.meta["mismatching"] == "will error" |
| 95 | + |
| 96 | + # it won't error if everything matches up |
| 97 | + tbl.remove_column("mismatching") |
| 98 | + tbl.write(fp, format="ascii.mrt", overwrite=True) |
| 99 | + got = read(fp, format="ascii.mrt") |
| 100 | + assert got == cosmo |
| 101 | + |
| 102 | + # ----------------------- |
| 103 | + |
| 104 | + def test_readwrite_mrt_subclass_partial_info( |
| 105 | + self, cosmo_cls, cosmo, read, write, tmp_path |
| 106 | + ): |
| 107 | + """ |
| 108 | + Test writing from an instance and reading from that class. |
| 109 | + This works with missing information. |
| 110 | + """ |
| 111 | + fp = tmp_path / "test_read_mrt_subclass_partial_info.mrt" |
| 112 | + |
| 113 | + # test write |
| 114 | + write(fp, format="ascii.mrt") |
| 115 | + |
| 116 | + # partial information |
| 117 | + tbl = Table.read(fp, format="ascii.mrt") |
| 118 | + del tbl["Tcmb0"] |
| 119 | + tbl.write(fp, format="ascii.mrt", overwrite=True) |
| 120 | + |
| 121 | + # read with the same class that wrote fills in the missing info with |
| 122 | + # the default value |
| 123 | + got = cosmo_cls.read(fp, format="ascii.mrt") |
| 124 | + got2 = read(fp, format="ascii.mrt", cosmology=cosmo_cls) |
| 125 | + |
| 126 | + assert got == got2 # internal consistency |
| 127 | + |
| 128 | + # not equal, because Tcmb0 is changed, which also changes m_nu |
| 129 | + assert got != cosmo |
| 130 | + assert got.Tcmb0 == cosmo_cls._init_signature.parameters["Tcmb0"].default |
| 131 | + assert got.clone(name=cosmo.name, Tcmb0=cosmo.Tcmb0, m_nu=cosmo.m_nu) == cosmo |
| 132 | + |
| 133 | + |
| 134 | +class WriteMRTTestMixin(ReadWriteTestMixinBase): |
| 135 | + def test_to_mrt_failed_cls(self, write, tmp_path): |
| 136 | + """Test failed table type.""" |
| 137 | + fp = tmp_path / "test_to_mrt_failed_cls.mrt" |
| 138 | + |
| 139 | + with pytest.raises(TypeError, match="'cls' must be"): |
| 140 | + write(fp, format="ascii.mrt", cls=list) |
| 141 | + |
| 142 | + @pytest.mark.parametrize("tbl_cls", [QTable, Table]) |
| 143 | + def test_to_mrt_cls(self, write, tbl_cls, tmp_path): |
| 144 | + fp = tmp_path / "test_to_mrt_cls.mrt" |
| 145 | + write(fp, format="ascii.mrt", cls=tbl_cls) |
| 146 | + |
| 147 | + def test_to_mrt_bad_index(self, read, write, tmp_path): |
| 148 | + """Test if argument ``index`` is incorrect""" |
| 149 | + fp = tmp_path / "test_to_mrt_bad_index.mrt" |
| 150 | + |
| 151 | + write(fp, format="ascii.mrt") |
| 152 | + |
| 153 | + # single-row table and has a non-0/None index |
| 154 | + with pytest.raises(IndexError, match="index 2 out of range"): |
| 155 | + read(fp, index=2, format="ascii.mrt") |
| 156 | + |
| 157 | + # string index where doesn't match |
| 158 | + with pytest.raises(KeyError, match="No matches found for key"): |
| 159 | + read(fp, index="row 0", format="ascii.mrt") |
| 160 | + |
| 161 | + def test_write_mrt_invalid_path(self, write): |
| 162 | + """Test passing an invalid path""" |
| 163 | + invalid_fp = "" |
| 164 | + with pytest.raises(FileNotFoundError, match="No such file or directory"): |
| 165 | + write(invalid_fp, format="ascii.mrt") |
| 166 | + |
| 167 | + def test_readwrite_mrt_instance(self, cosmo_cls, cosmo, read, write, tmp_path): |
| 168 | + fp = tmp_path / "test_readwrite_mrt_instance.mrt" |
| 169 | + write(fp, format="ascii.mrt") |
| 170 | + tb1 = Table.read(fp, format="ascii.mrt") |
| 171 | + assert tb1["name"] == cosmo.name |
| 172 | + |
| 173 | + |
| 174 | +class TestReadWriteMRT(ReadWriteDirectTestBase, WriteMRTTestMixin): |
| 175 | + """ |
| 176 | + Directly test ``read/write_mrt``. |
| 177 | + These are not public API and are discouraged from use, in favor of |
| 178 | + ``Cosmology.read/write(..., format="mrt")``, but should be |
| 179 | + tested regardless b/c they are used internally. |
| 180 | + """ |
| 181 | + |
| 182 | + def setup_class(self): |
| 183 | + self.functions = {"read": read_mrt, "write": write_mrt} |
0 commit comments