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
« 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"""
27import os
28import shutil
29import tempfile
30import warnings
31from unittest import TestCase
33from wuttjamaican.conf import WuttaConfig
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`.
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.
45 .. attribute:: tempdir
47 Path to the temporary folder created during setup.
49 .. note::
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 """
56 def setUp(self):
57 """ """
58 self.setup_files()
60 def setup_files(self):
61 """
62 This creates the temporary folder.
63 """
64 self.tempdir = tempfile.mkdtemp()
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()
73 def tearDown(self):
74 """ """
75 self.teardown_files()
77 def teardown_files(self):
78 """
79 This removes the temporary folder.
80 """
81 shutil.rmtree(self.tempdir)
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()
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::
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
102 def mkdir(self, dirname):
103 """
104 Make a new temporary folder and return its path.
106 Note that this will be created *underneath* :attr:`tempdir`.
107 """
108 return tempfile.mkdtemp(dir=self.tempdir)
111# TODO: deprecate / remove this
112FileConfigTestCase = FileTestCase
115class ConfigTestCase(FileTestCase):
116 """
117 Base class for test suites requiring a config object.
119 It inherits from :class:`FileTestCase` so also has the
120 file-related methods.
122 The running test has these attributes:
124 .. attribute:: config
126 Reference to the config object.
128 .. attribute:: app
130 Reference to the app handler.
132 .. note::
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 """
139 def setUp(self):
140 """ """
141 self.setup_config()
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()
151 def tearDown(self):
152 """ """
153 self.teardown_config()
155 def teardown_config(self):
156 """
157 Perform config teardown operations for the test.
158 """
159 self.teardown_files()
161 def make_config(self, **kwargs):
162 """ """
163 return WuttaConfig(**kwargs)
166# TODO: this should inherit from ConfigTestCase
167class DataTestCase(FileTestCase):
168 """
169 Base class for test suites requiring a full (typical) database.
171 It inherits from :class:`FileTestCase` so also has the
172 file-related methods.
174 This uses a SQLite in-memory database and creates all tables for
175 the app model. The running test has these attributes:
177 .. attribute:: config
179 Reference to the config object.
181 .. attribute:: app
183 Reference to the app handler.
185 .. attribute:: session
187 Open session for the test DB.
189 .. note::
191 If you subclass this and need to override setup/teardown,
192 please be sure to call the corresponding methods for this
193 class.
195 However you do *not* need to call the file-related setup or
196 teardown methods, as this class handles that automatically.
197 """
199 def setUp(self):
200 """ """
201 self.setup_db()
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()
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()
218 def tearDown(self):
219 """ """
220 self.teardown_db()
222 def teardown_db(self):
223 """
224 Perform config/app/db teardown operations for the test.
225 """
226 self.teardown_files()
228 def make_config(self, **kwargs):
229 """ """
230 return WuttaConfig(**kwargs)