22
33import pytest
44
5- from astropy .cosmology ._src .io .builtin .latex import _FORMAT_TABLE , write_latex
5+ from astropy .cosmology ._src .io .builtin .latex import (
6+ _FORMAT_TABLE ,
7+ write_latex ,
8+ read_latex
9+ )
610from astropy .io .registry .base import IORegistryError
711from astropy .table import QTable , Table
812
913from .base import ReadWriteDirectTestBase , ReadWriteTestMixinBase
1014
1115
12- class WriteLATEXTestMixin (ReadWriteTestMixinBase ):
16+ class ReadWriteLATEXTestMixin (ReadWriteTestMixinBase ):
1317 """
14- Tests for a Cosmology[Write] with ``format="latex"``.
18+ Tests for a Cosmology[Read/ Write] with ``format="ascii. latex"``.
1519 This class will not be directly called by :mod:`pytest` since its name does
1620 not begin with ``Test``. To activate the contained tests this class must
1721 be inherited in a subclass. Subclasses must define a :func:`pytest.fixture`
1822 ``cosmo`` that returns/yields an instance of a |Cosmology|.
1923 See ``TestCosmology`` for an example.
2024 """
2125
26+ def test_to_latex_bad_index (self , read , write , tmp_path ):
27+ """Test if argument ``index`` is incorrect"""
28+
29+ fp = tmp_path / "test_to_latex_bad_index.tex"
30+
31+ write (fp , format = "ascii.latex" )
32+
33+ # single-row table and has a non-0/None index
34+ with pytest .raises (IndexError , match = "index 2 out of range" ):
35+ read (fp , index = 2 , format = "ascii.latex" )
36+
37+ # string index where doesn't match
38+ with pytest .raises (KeyError , match = "No matches found for key" ):
39+ read (fp , index = "row 0" , format = "ascii.latex" )
40+
41+ def test_read_latex_invalid_path (self , read ):
42+ """Test passing an invalid or non-existent path"""
43+
44+ #using "blabla" makes it platform independent
45+ invalid_fp = "blabla.tex"
46+
47+ with pytest .raises (FileNotFoundError , match = "No such file or directory" ):
48+ read (invalid_fp , format = "ascii.latex" )
49+
50+ def test_latex_column_mnu (self , read , write , tmp_path ):
51+ """Test for table column m_nu to have a numpy array, essential for proper cosmology conversion"""
52+
53+ fp = tmp_path / "test_rename_latex_columns.tex"
54+
55+ write (fp , latex_names = True )
56+
57+ tbl = QTable .read (fp )
58+
59+ import numpy as np
60+ assert isinstance (tbl ["$m_{nu}$" ].value , np .ndarray )
61+
62+ # -----------------------
63+
2264 def test_to_latex_failed_cls (self , write , tmp_path ):
2365 """Test failed table type."""
2466 fp = tmp_path / "test_to_latex_failed_cls.tex"
@@ -64,18 +106,18 @@ def test_write_latex_unsupported_format(self, write, tmp_path):
64106 write (fp , format = invalid_format )
65107
66108
67- class TestReadWriteLaTex (ReadWriteDirectTestBase , WriteLATEXTestMixin ):
109+ class TestReadWriteLaTex (ReadWriteDirectTestBase , ReadWriteLATEXTestMixin ):
68110 """
69- Directly test ``write_latex``.
111+ Directly test ``read/ write_latex``.
70112 These are not public API and are discouraged from use, in favor of
71- ``Cosmology.write(..., format="latex")``, but should be
113+ ``Cosmology.read/ write(..., format="latex")``, but should be
72114 tested regardless b/c they are used internally.
73115 """
74116
75117 def setup_class (self ):
76- self .functions = {"write" : write_latex }
118+ self .functions = {"write" : write_latex , "read" : read_latex }
77119
78- def test_rename_direct_latex_columns (self , write , tmp_path ):
120+ def test_rename_direct_latex_columns (self , read , write , tmp_path ):
79121 """Tests renaming columns"""
80122 fp = tmp_path / "test_rename_latex_columns.tex"
81123 write (fp , latex_names = True )
@@ -84,3 +126,11 @@ def test_rename_direct_latex_columns(self, write, tmp_path):
84126 for column_name in tbl .colnames [2 :]:
85127 # for now, Cosmology as metadata and name is stored in first 2 slots
86128 assert column_name in _FORMAT_TABLE .values ()
129+
130+ cosmo = read (fp , format = "ascii.latex" )
131+ converted_tbl = cosmo .to_format ("astropy.table" )
132+
133+ # asserts each column name has been reverted
134+ for column_name in converted_tbl .colnames [1 :]:
135+ # for now now, metadata is still stored in first slot
136+ assert column_name in _FORMAT_TABLE .keys ()
0 commit comments