Coverage for tests/test_cli.py: 37%

77 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-25 10:20 -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 

32 

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

36 

37 

38class CliTestCase(unittest.TestCase): 

39 """Tests for CLI commands.""" 

40 

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

42 

43 def setUp(self) -> None: 

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

45 

46 def tearDown(self) -> None: 

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

48 

49 def test_basic_check(self) -> None: 

50 """Test for basic-check command.""" 

51 runner = CliRunner() 

52 result = runner.invoke(cli, ["basic-check", TEST_YAML], catch_exceptions=False) 

53 self.assertEqual(result.exit_code, 0) 

54 

55 def test_create_all(self) -> None: 

56 """Test for create command.""" 

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

58 

59 runner = CliRunner() 

60 result = runner.invoke( 

61 cli, 

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

63 catch_exceptions=False, 

64 ) 

65 self.assertEqual(result.exit_code, 0) 

66 

67 def test_create_all_dry_run(self) -> None: 

68 """Test for create --dry-run command.""" 

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

70 

71 runner = CliRunner() 

72 result = runner.invoke( 

73 cli, 

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

75 catch_exceptions=False, 

76 ) 

77 self.assertEqual(result.exit_code, 0) 

78 

79 def test_normalize(self) -> None: 

80 """Test for normalize command.""" 

81 runner = CliRunner() 

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

83 self.assertEqual(result.exit_code, 0) 

84 

85 def test_init_tap(self) -> None: 

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

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

88 runner = CliRunner() 

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

90 self.assertEqual(result.exit_code, 0) 

91 

92 def test_load_tap(self) -> None: 

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

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

95 # process. 

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

97 

98 # Need to run init-tap first. 

99 runner = CliRunner() 

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

101 self.assertEqual(result.exit_code, 0) 

102 

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

104 self.assertEqual(result.exit_code, 0) 

105 

106 def test_load_tap_mock(self) -> None: 

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

108 url = "postgresql+psycopg2://" 

109 

110 runner = CliRunner() 

111 result = runner.invoke( 

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

113 ) 

114 self.assertEqual(result.exit_code, 0) 

115 

116 def test_modify_tap(self) -> None: 

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

118 runner = CliRunner() 

119 

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

121 self.assertEqual(result.exit_code, 0) 

122 

123 def test_merge(self) -> None: 

124 """Test for merge command.""" 

125 runner = CliRunner() 

126 

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

128 self.assertEqual(result.exit_code, 0) 

129 

130 def test_validate_default(self) -> None: 

131 """Test validate command.""" 

132 runner = CliRunner() 

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

134 self.assertEqual(result.exit_code, 0) 

135 

136 def test_validate_default_with_require_description(self) -> None: 

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

138 runner = CliRunner() 

139 try: 

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

141 result = runner.invoke( 

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

143 ) 

144 except Exception as e: 

145 # Reraise exception. 

146 raise e 

147 

148 self.assertEqual(result.exit_code, 0) 

149 

150 def test_validate_rsp(self) -> None: 

151 """Test RSP schema type validation.""" 

152 runner = CliRunner() 

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

154 self.assertEqual(result.exit_code, 0) 

155 

156 

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

158 unittest.main()