Coverage for tests/test_cli.py: 37%
73 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 02:43 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 02:43 -0700
1# This file is part of felis.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import os
23import shutil
24import tempfile
25import unittest
26from collections.abc import MutableMapping
27from typing import Any
29from click.testing import CliRunner
31from felis.cli import cli
33TESTDIR = os.path.abspath(os.path.dirname(__file__))
34TEST_YAML = os.path.join(TESTDIR, "data", "test.yml")
35TEST_MERGE_YAML = os.path.join(TESTDIR, "data", "test-merge.yml")
38class CliTestCase(unittest.TestCase):
39 """Tests for CLI commands."""
41 schema_obj: MutableMapping[str, Any] | None = None
43 def setUp(self) -> None:
44 self.tmpdir = tempfile.mkdtemp(dir=TESTDIR)
46 def tearDown(self) -> None:
47 shutil.rmtree(self.tmpdir, ignore_errors=True)
49 def test_create_all(self) -> None:
50 """Test for create command."""
51 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
53 runner = CliRunner()
54 result = runner.invoke(
55 cli,
56 ["create", "--schema-name=main", f"--engine-url={url}", TEST_YAML],
57 catch_exceptions=False,
58 )
59 self.assertEqual(result.exit_code, 0)
61 def test_create_all_dry_run(self) -> None:
62 """Test for create --dry-run command."""
63 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
65 runner = CliRunner()
66 result = runner.invoke(
67 cli,
68 ["create", "--schema-name=main", f"--engine-url={url}", "--dry-run", TEST_YAML],
69 catch_exceptions=False,
70 )
71 self.assertEqual(result.exit_code, 0)
73 def test_normalize(self) -> None:
74 """Test for normalize command."""
75 runner = CliRunner()
76 result = runner.invoke(cli, ["normalize", TEST_YAML], catch_exceptions=False)
77 self.assertEqual(result.exit_code, 0)
79 def test_init_tap(self) -> None:
80 """Test for init-tap command."""
81 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
82 runner = CliRunner()
83 result = runner.invoke(cli, ["init-tap", url], catch_exceptions=False)
84 self.assertEqual(result.exit_code, 0)
86 def test_load_tap(self) -> None:
87 """Test for load-tap command."""
88 # Cannot use the same url for both init-tap and load-tap in the same
89 # process.
90 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
92 # Need to run init-tap first.
93 runner = CliRunner()
94 result = runner.invoke(cli, ["init-tap", url])
95 self.assertEqual(result.exit_code, 0)
97 result = runner.invoke(cli, ["load-tap", f"--engine-url={url}", TEST_YAML], catch_exceptions=False)
98 self.assertEqual(result.exit_code, 0)
100 def test_load_tap_mock(self) -> None:
101 """Test for load-tap --dry-run command."""
102 url = "postgresql+psycopg2://"
104 runner = CliRunner()
105 result = runner.invoke(
106 cli, ["load-tap", f"--engine-url={url}", "--dry-run", TEST_YAML], catch_exceptions=False
107 )
108 self.assertEqual(result.exit_code, 0)
110 def test_modify_tap(self) -> None:
111 """Test for modify-tap command."""
112 runner = CliRunner()
114 result = runner.invoke(cli, ["modify-tap", "--start-schema-at=1", TEST_YAML], catch_exceptions=False)
115 self.assertEqual(result.exit_code, 0)
117 def test_merge(self) -> None:
118 """Test for merge command."""
119 runner = CliRunner()
121 result = runner.invoke(cli, ["merge", TEST_YAML, TEST_MERGE_YAML], catch_exceptions=False)
122 self.assertEqual(result.exit_code, 0)
124 def test_validate_default(self) -> None:
125 """Test validate command."""
126 runner = CliRunner()
127 result = runner.invoke(cli, ["validate", TEST_YAML], catch_exceptions=False)
128 self.assertEqual(result.exit_code, 0)
130 def test_validate_default_with_require_description(self) -> None:
131 """Test validate command with description required."""
132 runner = CliRunner()
133 try:
134 # Wrap this in a try/catch in case an exception is thrown.
135 result = runner.invoke(
136 cli, ["validate", "--require-description", TEST_YAML], catch_exceptions=False
137 )
138 except Exception as e:
139 # Reraise exception.
140 raise e
142 self.assertEqual(result.exit_code, 0)
144 def test_validate_rsp(self) -> None:
145 """Test RSP schema type validation."""
146 runner = CliRunner()
147 result = runner.invoke(cli, ["validate", "-s", "RSP", TEST_YAML], catch_exceptions=False)
148 self.assertEqual(result.exit_code, 0)
151if __name__ == "__main__": 151 ↛ 152line 151 didn't jump to line 152, because the condition on line 151 was never true
152 unittest.main()