Skip to content

Commit

Permalink
Start testing the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Mar 26, 2024
1 parent bf3657e commit c520a2d
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# This file is part of astro_metadata_translator.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the LICENSE file at the top-level directory of this distribution
# for details of code ownership.
#
# Use of this source code is governed by a 3-clause BSD-style
# license that can be found in the LICENSE file.

import os.path
import traceback
import unittest

import click.testing
import yaml

from astro_metadata_translator.cli.astrometadata import main as astrometadata

TESTDIR = os.path.abspath(os.path.dirname(__file__))


def click_result_msg(result: click.testing.Result) -> str:
"""Get a standard assert message from a click result.
Parameters
----------
result : click.testing.Result
The result object returned from `click.testing.CliRunner.invoke`.
Returns
-------
msg : `str`
The message string.
"""
msg = f"""\noutput: {result.output}\nexception: {result.exception}"""
if result.exception:
msg += f"""\ntraceback: {"".join(traceback.format_tb(result.exception.__traceback__))}"""
return msg


class TestCLI(unittest.TestCase):
"""Test astrometadata command line."""

def setUp(self) -> None:
self.runner = click.testing.CliRunner()

def test_translate(self) -> None:
"""Test astrometadata translate."""
result = self.runner.invoke(
astrometadata, ["translate", os.path.join(TESTDIR, "data", "fitsheader-decam.yaml")]
)
self.assertEqual(result.exit_code, 0, click_result_msg(result))
self.assertIn("instrument: DECam", result.output, click_result_msg(result))

result = self.runner.invoke(
astrometadata,
["--traceback", "translate", os.path.join(TESTDIR, "data", "bad-sidecar.json")],
)
self.assertEqual(result.exit_code, 1, click_result_msg(result))
self.assertIn("ValueError", result.output, click_result_msg(result))

def test_dump(self) -> None:
"""Test that YAML is dumped."""
# Use warning log level to hide the INFO message.
result = self.runner.invoke(
astrometadata,
[
"--log-level",
"WARNING",
"dump",
os.path.join(TESTDIR, "data", "fitsheader-hsc-HSCA04090107.json"),
],
)
self.assertEqual(result.exit_code, 0, click_result_msg(result))
parsed = yaml.safe_load(result.output)
self.assertEqual(parsed["T_EFMN32"], 50)


if __name__ == "__main__":
unittest.main()

Check warning on line 82 in tests/test_cli.py

View check run for this annotation

Codecov / codecov/patch

tests/test_cli.py#L82

Added line #L82 was not covered by tests

0 comments on commit c520a2d

Please sign in to comment.