Coverage for tests/test_cli.py: 38%

64 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-15 02: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, Optional 

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 TapLoadingVisitor class.""" 

40 

41 schema_obj: Optional[MutableMapping[str, Any]] = 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 

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 

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

60 

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) 

68 

69 def test_create_all_dry_run(self) -> None: 

70 """Test for create-all --dry-run command""" 

71 

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

73 

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) 

81 

82 def test_normalize(self) -> None: 

83 """Test for normalize command""" 

84 

85 runner = CliRunner() 

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

87 self.assertEqual(result.exit_code, 0) 

88 

89 def test_init_tap(self) -> None: 

90 """Test for init-tap command""" 

91 

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) 

96 

97 def test_load_tap(self) -> None: 

98 """Test for load-tap command""" 

99 

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" 

103 

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) 

108 

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

110 self.assertEqual(result.exit_code, 0) 

111 

112 def test_load_tap_mock(self) -> None: 

113 """Test for load-tap --dry-run command""" 

114 

115 url = "postgresql+psycopg2://" 

116 

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) 

122 

123 def test_modify_tap(self) -> None: 

124 """Test for modify-tap command""" 

125 

126 runner = CliRunner() 

127 

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) 

131 

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

133 self.assertEqual(result.exit_code, 0) 

134 

135 def test_merge(self) -> None: 

136 """Test for merge command""" 

137 

138 runner = CliRunner() 

139 

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

141 self.assertEqual(result.exit_code, 0) 

142 

143 

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

145 unittest.main()