Coverage for tests/test_cli.py: 40%

61 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-16 02:49 -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_create_all(self) -> None: 

50 """Test for create command.""" 

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

52 

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) 

60 

61 def test_create_all_dry_run(self) -> None: 

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

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

64 

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) 

72 

73 def test_init_tap(self) -> None: 

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

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

76 runner = CliRunner() 

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

78 self.assertEqual(result.exit_code, 0) 

79 

80 def test_load_tap(self) -> None: 

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

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

83 # process. 

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

85 

86 # Need to run init-tap first. 

87 runner = CliRunner() 

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

89 self.assertEqual(result.exit_code, 0) 

90 

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

92 self.assertEqual(result.exit_code, 0) 

93 

94 def test_load_tap_mock(self) -> None: 

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

96 url = "postgresql+psycopg2://" 

97 

98 runner = CliRunner() 

99 result = runner.invoke( 

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

101 ) 

102 self.assertEqual(result.exit_code, 0) 

103 

104 def test_validate_default(self) -> None: 

105 """Test validate command.""" 

106 runner = CliRunner() 

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

108 self.assertEqual(result.exit_code, 0) 

109 

110 def test_validate_default_with_require_description(self) -> None: 

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

112 runner = CliRunner() 

113 try: 

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

115 result = runner.invoke( 

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

117 ) 

118 except Exception as e: 

119 # Reraise exception. 

120 raise e 

121 

122 self.assertEqual(result.exit_code, 0) 

123 

124 def test_validate_rsp(self) -> None: 

125 """Test RSP schema type validation.""" 

126 runner = CliRunner() 

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

128 self.assertEqual(result.exit_code, 0) 

129 

130 

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

132 unittest.main()