Coverage for .tox/coverage/lib/python3.11/site-packages/wuttjamaican/testing.py: 100%
40 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-08-27 19:14 -0500
« prev ^ index » next coverage.py v7.3.2, created at 2024-08-27 19:14 -0500
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("FileConfigTestCase.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("FileConfigTestCase.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 DataTestCase(FileTestCase):
116 """
117 Base class for test suites requiring a full (typical) database.
119 It inherits from :class:`FileTestCase` so also has the
120 file-related methods.
122 This uses a SQLite in-memory database and creates all tables for
123 the app model. The running test has these attributes:
125 .. attribute:: config
127 Reference to the config object.
129 .. attribute:: app
131 Reference to the app handler.
133 .. attribute:: session
135 Open session for the test DB.
137 .. note::
139 If you subclass this and need to override setup/teardown,
140 please be sure to call the corresponding methods for this
141 class.
143 However you do *not* need to call the file-related setup or
144 teardown methods, as this class handles that automatically.
145 """
147 def setUp(self):
148 """ """
149 self.setup_db()
151 def setup_db(self):
152 """
153 Perform config/app/db setup operations for the test.
154 """
155 self.setup_files()
156 self.config = self.make_config(defaults={
157 'wutta.db.default.url': 'sqlite://',
158 })
159 self.app = self.config.get_app()
161 # init db
162 model = self.app.model
163 model.Base.metadata.create_all(bind=self.config.appdb_engine)
164 self.session = self.app.make_session()
166 def tearDown(self):
167 """ """
168 self.teardown_db()
170 def teardown_db(self):
171 """
172 Perform config/app/db teardown operations for the test.
173 """
174 self.teardown_files()
176 def make_config(self, **kwargs):
177 """ """
178 return WuttaConfig(**kwargs)