Coverage for .tox/coverage/lib/python3.11/site-packages/wuttjamaican/testing.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-11-24 10:30 -0600

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# WuttJamaican -- Base package for Wutta Framework 

5# Copyright © 2023-2024 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24WuttJamaican - test utilities 

25""" 

26 

27import os 

28import shutil 

29import tempfile 

30import warnings 

31from unittest import TestCase 

32 

33from wuttjamaican.conf import WuttaConfig 

34 

35 

36class FileTestCase(TestCase): 

37 """ 

38 Base class for test suites which (may) write temporary files, for 

39 sake of testing the config constructor etc. It inherits from 

40 :class:`python:unittest.TestCase`. 

41 

42 This class creates a temporary folder on setup, and removes it on 

43 teardown. See below for features exposed to work with the folder. 

44 

45 .. attribute:: tempdir 

46 

47 Path to the temporary folder created during setup. 

48 

49 .. note:: 

50 

51 If you subclass this and need to override setup/teardown, 

52 please be sure to call the corresponding methods for this 

53 class. 

54 """ 

55 

56 def setUp(self): 

57 """ """ 

58 self.setup_files() 

59 

60 def setup_files(self): 

61 """ 

62 This creates the temporary folder. 

63 """ 

64 self.tempdir = tempfile.mkdtemp() 

65 

66 def setup_file_config(self): # pragma: no cover 

67 """ """ 

68 warnings.warn("FileTestCase.setup_file_config() is deprecated; " 

69 "please use setup_files() instead", 

70 DeprecationWarning, stacklevel=2) 

71 self.setup_files() 

72 

73 def tearDown(self): 

74 """ """ 

75 self.teardown_files() 

76 

77 def teardown_files(self): 

78 """ 

79 This removes the temporary folder. 

80 """ 

81 shutil.rmtree(self.tempdir) 

82 

83 def teardown_file_config(self): # pragma: no cover 

84 """ """ 

85 warnings.warn("FileTestCase.teardown_file_config() is deprecated; " 

86 "please use teardown_files() instead", 

87 DeprecationWarning, stacklevel=2) 

88 self.teardown_files() 

89 

90 def write_file(self, filename, content): 

91 """ 

92 Write a new file (in temporary folder) with the given filename 

93 and content, and return its full path. For instance:: 

94 

95 myconf = self.write_file('my.conf', '<file contents>') 

96 """ 

97 path = os.path.join(self.tempdir, filename) 

98 with open(path, 'wt') as f: 

99 f.write(content) 

100 return path 

101 

102 def mkdir(self, dirname): 

103 """ 

104 Make a new temporary folder and return its path. 

105 

106 Note that this will be created *underneath* :attr:`tempdir`. 

107 """ 

108 return tempfile.mkdtemp(dir=self.tempdir) 

109 

110 

111# TODO: deprecate / remove this 

112FileConfigTestCase = FileTestCase 

113 

114 

115class ConfigTestCase(FileTestCase): 

116 """ 

117 Base class for test suites requiring a config object. 

118 

119 It inherits from :class:`FileTestCase` so also has the 

120 file-related methods. 

121 

122 The running test has these attributes: 

123 

124 .. attribute:: config 

125 

126 Reference to the config object. 

127 

128 .. attribute:: app 

129 

130 Reference to the app handler. 

131 

132 .. note:: 

133 

134 If you subclass this directly and need to override 

135 setup/teardown, please be sure to call the corresponding 

136 methods for this class. 

137 """ 

138 

139 def setUp(self): 

140 """ """ 

141 self.setup_config() 

142 

143 def setup_config(self): 

144 """ 

145 Perform config setup operations for the test. 

146 """ 

147 self.setup_files() 

148 self.config = self.make_config() 

149 self.app = self.config.get_app() 

150 

151 def tearDown(self): 

152 """ """ 

153 self.teardown_config() 

154 

155 def teardown_config(self): 

156 """ 

157 Perform config teardown operations for the test. 

158 """ 

159 self.teardown_files() 

160 

161 def make_config(self, **kwargs): 

162 """ """ 

163 return WuttaConfig(**kwargs) 

164 

165 

166# TODO: this should inherit from ConfigTestCase 

167class DataTestCase(FileTestCase): 

168 """ 

169 Base class for test suites requiring a full (typical) database. 

170 

171 It inherits from :class:`FileTestCase` so also has the 

172 file-related methods. 

173 

174 This uses a SQLite in-memory database and creates all tables for 

175 the app model. The running test has these attributes: 

176 

177 .. attribute:: config 

178 

179 Reference to the config object. 

180 

181 .. attribute:: app 

182 

183 Reference to the app handler. 

184 

185 .. attribute:: session 

186 

187 Open session for the test DB. 

188 

189 .. note:: 

190 

191 If you subclass this and need to override setup/teardown, 

192 please be sure to call the corresponding methods for this 

193 class. 

194 

195 However you do *not* need to call the file-related setup or 

196 teardown methods, as this class handles that automatically. 

197 """ 

198 

199 def setUp(self): 

200 """ """ 

201 self.setup_db() 

202 

203 def setup_db(self): 

204 """ 

205 Perform config/app/db setup operations for the test. 

206 """ 

207 self.setup_files() 

208 self.config = self.make_config(defaults={ 

209 'wutta.db.default.url': 'sqlite://', 

210 }) 

211 self.app = self.config.get_app() 

212 

213 # init db 

214 model = self.app.model 

215 model.Base.metadata.create_all(bind=self.config.appdb_engine) 

216 self.session = self.app.make_session() 

217 

218 def tearDown(self): 

219 """ """ 

220 self.teardown_db() 

221 

222 def teardown_db(self): 

223 """ 

224 Perform config/app/db teardown operations for the test. 

225 """ 

226 self.teardown_files() 

227 

228 def make_config(self, **kwargs): 

229 """ """ 

230 return WuttaConfig(**kwargs)