|
| 1 | +# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 | + |
| 3 | +# THIRD PARTY |
| 4 | +import pytest |
| 5 | + |
| 6 | +# LOCAL |
| 7 | +from astropy.cosmology.io.latex import _FORMAT_TABLE, write_latex |
| 8 | +from astropy.table import QTable, Table |
| 9 | + |
| 10 | +from .base import ReadWriteDirectTestBase, ReadWriteTestMixinBase |
| 11 | + |
| 12 | + |
| 13 | +class WriteLATEXTestMixin(ReadWriteTestMixinBase): |
| 14 | + """ |
| 15 | + Tests for a Cosmology[Write] with ``format="latex"``. |
| 16 | + This class will not be directly called by :mod:`pytest` since its name does |
| 17 | + not begin with ``Test``. To activate the contained tests this class must |
| 18 | + be inherited in a subclass. Subclasses must dfine a :func:`pytest.fixture` |
| 19 | + ``cosmo`` that returns/yields an instance of a |Cosmology|. |
| 20 | + See ``TestCosmology`` for an example. |
| 21 | + """ |
| 22 | + |
| 23 | + def test_to_latex_failed_cls(self, write, tmp_path): |
| 24 | + """Test failed table type.""" |
| 25 | + fp = tmp_path / "test_to_latex_failed_cls.tex" |
| 26 | + |
| 27 | + with pytest.raises(TypeError, match="'cls' must be"): |
| 28 | + write(fp, format="latex", cls=list) |
| 29 | + |
| 30 | + @pytest.mark.parametrize("tbl_cls", [QTable, Table]) |
| 31 | + def test_to_latex_cls(self, write, tbl_cls, tmp_path): |
| 32 | + fp = tmp_path / "test_to_latex_cls.tex" |
| 33 | + write(fp, format="latex", cls=tbl_cls) |
| 34 | + |
| 35 | + def test_latex_columns(self, write, tmp_path): |
| 36 | + fp = tmp_path / "test_rename_latex_columns.tex" |
| 37 | + write(fp, format="latex", latex_names=True) |
| 38 | + tbl = QTable.read(fp) |
| 39 | + # asserts each column name has not been reverted yet |
| 40 | + # For now, Cosmology class and name are stored in first 2 slots |
| 41 | + for column_name in tbl.colnames[2:]: |
| 42 | + assert column_name in _FORMAT_TABLE.values() |
| 43 | + |
| 44 | + def test_write_latex_invalid_path(self, write): |
| 45 | + """Test passing an invalid path""" |
| 46 | + invalid_fp = "" |
| 47 | + with pytest.raises(FileNotFoundError, match="No such file or directory"): |
| 48 | + write(invalid_fp, format="latex") |
| 49 | + |
| 50 | + def test_write_latex_false_overwrite(self, write, tmp_path): |
| 51 | + """Test to write a LaTeX file without overwriting an existing file""" |
| 52 | + # Test that passing an invalid path to write_latex() raises a IOError |
| 53 | + fp = tmp_path / "test_write_latex_false_overwrite.tex" |
| 54 | + write(fp, format="latex") |
| 55 | + with pytest.raises(OSError, match="overwrite=True"): |
| 56 | + write(fp, format="latex", overwrite=False) |
| 57 | + |
| 58 | + |
| 59 | +class TestReadWriteLaTex(ReadWriteDirectTestBase, WriteLATEXTestMixin): |
| 60 | + """ |
| 61 | + Directly test ``write_latex``. |
| 62 | + These are not public API and are discouraged from use, in favor of |
| 63 | + ``Cosmology.write(..., format="latex")``, but should be |
| 64 | + tested regardless b/c they are used internally. |
| 65 | + """ |
| 66 | + |
| 67 | + def setup_class(self): |
| 68 | + self.functions = {"write": write_latex} |
| 69 | + |
| 70 | + def test_rename_direct_latex_columns(self, write, tmp_path): |
| 71 | + """Tests renaming columns""" |
| 72 | + fp = tmp_path / "test_rename_latex_columns.tex" |
| 73 | + write(fp, format="latex", latex_names=True) |
| 74 | + tbl = QTable.read(fp) |
| 75 | + # asserts each column name has not been reverted yet |
| 76 | + for column_name in tbl.colnames[2:]: |
| 77 | + # for now, Cosmology as metadata and name is stored in first 2 slots |
| 78 | + assert column_name in _FORMAT_TABLE.values() |
0 commit comments