1# -*- coding: utf-8 -*-
2
3import shutil
4import subprocess
5import tempfile
6
7import pytest
8
9
10from chempy.util.table import rsys2tablines, rsys2table, rsys2pdf_table
11from .test_graph import _get_rsys
12from ..testing import skipif
13
14
15try:
16    pdflatex_missing = subprocess.call(["pdflatex", "--version"]) != 0
17except OSError:
18    pdflatex_missing = True
19
20
21def test_rsys2tablines():
22    assert rsys2tablines(_get_rsys(), tex=False) == ["1 & 2 A & -> & B & 3 & - & None"]
23
24
25def test_rsys2table():
26    assert rsys2table(_get_rsys()) == (
27        r"""
28\begin{table}
29\centering
30\label{tab:none}
31\caption[None]{None}
32\begin{tabular}{lllllll}
33\toprule
34Id. & Reactants &  & Products & {Rate constant} & Unit & Ref \\
35\midrule
361 & \ensuremath{2 \boldsymbol{A}} & \ensuremath{\rightarrow} &"""
37        + r""" \ensuremath{\boldsymbol{B}} & \ensuremath{3} & \ensuremath{-} & None \\
38\bottomrule
39\end{tabular}
40\end{table}"""
41    )
42
43
44@pytest.mark.parametrize("longtable", (True, False))
45@skipif(pdflatex_missing, reason="latex not installed? (pdflatex command missing)")
46def test_rsys2pdf_table(longtable):
47    rsys = _get_rsys()
48    tempdir = tempfile.mkdtemp()
49    try:
50        rsys2pdf_table(rsys, tempdir, longtable=longtable)
51    finally:
52        shutil.rmtree(tempdir)
53
54
55@pytest.mark.skipif(
56    pdflatex_missing, reason="latex not installed? (pdflatex command missing)"
57)
58def test_rsys2pdf_table_no_output_dir():
59    rsys = _get_rsys()
60    rsys2pdf_table(rsys, save=False)
61