-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||