Coverage for tests/test_cli.py: 38%
64 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-10 02:14 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-10 02:14 -0800
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, Optional
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 TapLoadingVisitor class."""
41 schema_obj: Optional[MutableMapping[str, Any]] = 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_basic_check(self) -> None:
50 """Test for basic-check command"""
52 runner = CliRunner()
53 result = runner.invoke(cli, ["basic-check", TEST_YAML], catch_exceptions=False)
54 self.assertEqual(result.exit_code, 0)
56 def test_create_all(self) -> None:
57 """Test for create-all command"""
59 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
61 runner = CliRunner()
62 result = runner.invoke(
63 cli,
64 ["create-all", "--schema-name=main", f"--engine-url={url}", TEST_YAML],
65 catch_exceptions=False,
66 )
67 self.assertEqual(result.exit_code, 0)
69 def test_create_all_dry_run(self) -> None:
70 """Test for create-all --dry-run command"""
72 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
74 runner = CliRunner()
75 result = runner.invoke(
76 cli,
77 ["create-all", "--schema-name=main", f"--engine-url={url}", "--dry-run", TEST_YAML],
78 catch_exceptions=False,
79 )
80 self.assertEqual(result.exit_code, 0)
82 def test_normalize(self) -> None:
83 """Test for normalize command"""
85 runner = CliRunner()
86 result = runner.invoke(cli, ["normalize", TEST_YAML], catch_exceptions=False)
87 self.assertEqual(result.exit_code, 0)
89 def test_init_tap(self) -> None:
90 """Test for init-tap command"""
92 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
93 runner = CliRunner()
94 result = runner.invoke(cli, ["init-tap", url], catch_exceptions=False)
95 self.assertEqual(result.exit_code, 0)
97 def test_load_tap(self) -> None:
98 """Test for load-tap command"""
100 # Cannot use the same url for both init-tap and load-tap in the same
101 # process.
102 url = f"sqlite:///{self.tmpdir}/tap.sqlite3"
104 # Need to run init-tap first.
105 runner = CliRunner()
106 result = runner.invoke(cli, ["init-tap", url])
107 self.assertEqual(result.exit_code, 0)
109 result = runner.invoke(cli, ["load-tap", f"--engine-url={url}", TEST_YAML], catch_exceptions=False)
110 self.assertEqual(result.exit_code, 0)
112 def test_load_tap_mock(self) -> None:
113 """Test for load-tap --dry-run command"""
115 url = "postgresql+psycopg2://"
117 runner = CliRunner()
118 result = runner.invoke(
119 cli, ["load-tap", f"--engine-url={url}", "--dry-run", TEST_YAML], catch_exceptions=False
120 )
121 self.assertEqual(result.exit_code, 0)
123 def test_modify_tap(self) -> None:
124 """Test for modify-tap command"""
126 runner = CliRunner()
128 # Without --start-schema-at it makes an exception
129 with self.assertRaises(TypeError):
130 result = runner.invoke(cli, ["modify-tap", TEST_YAML], catch_exceptions=False)
132 result = runner.invoke(cli, ["modify-tap", "--start-schema-at=1", TEST_YAML], catch_exceptions=False)
133 self.assertEqual(result.exit_code, 0)
135 def test_merge(self) -> None:
136 """Test for merge command"""
138 runner = CliRunner()
140 result = runner.invoke(cli, ["merge", TEST_YAML, TEST_MERGE_YAML], catch_exceptions=False)
141 self.assertEqual(result.exit_code, 0)
144if __name__ == "__main__": 144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true
145 unittest.main()