Coverage for tests/test_cli.py: 36%

79 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-14 10:16 -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/>. 

21 

22import os 

23import shutil 

24import tempfile 

25import unittest 

26from collections.abc import MutableMapping 

27from typing import Any 

28 

29from click.testing import CliRunner 

30 

31from felis.cli import cli 

32from felis.datamodel import Schema 

33 

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

35TEST_YAML = os.path.join(TESTDIR, "data", "test.yml") 

36TEST_MERGE_YAML = os.path.join(TESTDIR, "data", "test-merge.yml") 

37 

38 

39class CliTestCase(unittest.TestCase): 

40 """Tests for CLI commands.""" 

41 

42 schema_obj: MutableMapping[str, Any] | None = None 

43 

44 def setUp(self) -> None: 

45 self.tmpdir = tempfile.mkdtemp(dir=TESTDIR) 

46 

47 def tearDown(self) -> None: 

48 shutil.rmtree(self.tmpdir, ignore_errors=True) 

49 

50 def test_basic_check(self) -> None: 

51 """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) 

55 

56 def test_create_all(self) -> None: 

57 """Test for create-all command.""" 

58 url = f"sqlite:///{self.tmpdir}/tap.sqlite3" 

59 

60 runner = CliRunner() 

61 result = runner.invoke( 

62 cli, 

63 ["create-all", "--schema-name=main", f"--engine-url={url}", TEST_YAML], 

64 catch_exceptions=False, 

65 ) 

66 self.assertEqual(result.exit_code, 0) 

67 

68 def test_create_all_dry_run(self) -> None: 

69 """Test for create-all --dry-run command.""" 

70 url = f"sqlite:///{self.tmpdir}/tap.sqlite3" 

71 

72 runner = CliRunner() 

73 result = runner.invoke( 

74 cli, 

75 ["create-all", "--schema-name=main", f"--engine-url={url}", "--dry-run", TEST_YAML], 

76 catch_exceptions=False, 

77 ) 

78 self.assertEqual(result.exit_code, 0) 

79 

80 def test_normalize(self) -> None: 

81 """Test for normalize command.""" 

82 runner = CliRunner() 

83 result = runner.invoke(cli, ["normalize", TEST_YAML], catch_exceptions=False) 

84 self.assertEqual(result.exit_code, 0) 

85 

86 def test_init_tap(self) -> None: 

87 """Test for init-tap command.""" 

88 url = f"sqlite:///{self.tmpdir}/tap.sqlite3" 

89 runner = CliRunner() 

90 result = runner.invoke(cli, ["init-tap", url], catch_exceptions=False) 

91 self.assertEqual(result.exit_code, 0) 

92 

93 def test_load_tap(self) -> None: 

94 """Test for load-tap command.""" 

95 # Cannot use the same url for both init-tap and load-tap in the same 

96 # process. 

97 url = f"sqlite:///{self.tmpdir}/tap.sqlite3" 

98 

99 # Need to run init-tap first. 

100 runner = CliRunner() 

101 result = runner.invoke(cli, ["init-tap", url]) 

102 self.assertEqual(result.exit_code, 0) 

103 

104 result = runner.invoke(cli, ["load-tap", f"--engine-url={url}", TEST_YAML], catch_exceptions=False) 

105 self.assertEqual(result.exit_code, 0) 

106 

107 def test_load_tap_mock(self) -> None: 

108 """Test for load-tap --dry-run command.""" 

109 url = "postgresql+psycopg2://" 

110 

111 runner = CliRunner() 

112 result = runner.invoke( 

113 cli, ["load-tap", f"--engine-url={url}", "--dry-run", TEST_YAML], catch_exceptions=False 

114 ) 

115 self.assertEqual(result.exit_code, 0) 

116 

117 def test_modify_tap(self) -> None: 

118 """Test for modify-tap command.""" 

119 runner = CliRunner() 

120 

121 result = runner.invoke(cli, ["modify-tap", "--start-schema-at=1", TEST_YAML], catch_exceptions=False) 

122 self.assertEqual(result.exit_code, 0) 

123 

124 def test_merge(self) -> None: 

125 """Test for merge command.""" 

126 runner = CliRunner() 

127 

128 result = runner.invoke(cli, ["merge", TEST_YAML, TEST_MERGE_YAML], catch_exceptions=False) 

129 self.assertEqual(result.exit_code, 0) 

130 

131 def test_validate_default(self) -> None: 

132 """Test validate command.""" 

133 runner = CliRunner() 

134 result = runner.invoke(cli, ["validate", TEST_YAML], catch_exceptions=False) 

135 self.assertEqual(result.exit_code, 0) 

136 

137 def test_validate_default_with_require_description(self) -> None: 

138 """Test validate command with description required.""" 

139 runner = CliRunner() 

140 try: 

141 # Wrap this in a try/catch in case an exception is thrown. 

142 result = runner.invoke( 

143 cli, ["validate", "--require-description", TEST_YAML], catch_exceptions=False 

144 ) 

145 except Exception as e: 

146 # Reraise exception. 

147 raise e 

148 finally: 

149 # Turn the flag off so it does not effect subsequent tests. 

150 Schema.require_description(False) 

151 

152 self.assertEqual(result.exit_code, 0) 

153 

154 def test_validate_rsp(self) -> None: 

155 """Test RSP schema type validation.""" 

156 runner = CliRunner() 

157 result = runner.invoke(cli, ["validate", "-s", "RSP", TEST_YAML], catch_exceptions=False) 

158 self.assertEqual(result.exit_code, 0) 

159 

160 

161if __name__ == "__main__": 161 ↛ 162line 161 didn't jump to line 162, because the condition on line 161 was never true

162 unittest.main()