1
0
Fork 0

Add common FileConfigTestCase class, in new testing module

hoping i do not regret this decision..  since the tests do not live in
a "real" package i think the only way to share code is to add some
things to the main package
This commit is contained in:
Lance Edgar 2023-11-20 16:19:22 -06:00
parent 427afc27fc
commit a0186b346e
4 changed files with 92 additions and 28 deletions

View file

@ -16,4 +16,5 @@
db.conf
db.sess
exc
testing
util

View file

@ -0,0 +1,6 @@
``wuttjamaican.testing``
========================
.. automodule:: wuttjamaican.testing
:members:

View file

@ -0,0 +1,82 @@
# -*- coding: utf-8; -*-
################################################################################
#
# WuttJamaican -- Base package for Wutta Framework
# Copyright © 2023 Lance Edgar
#
# This file is part of Wutta Framework.
#
# Wutta Framework is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Wutta Framework is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
WuttJamaican - test utilities
"""
import os
import shutil
import tempfile
from unittest import TestCase
class FileConfigTestCase(TestCase):
"""
Common base class for test suites which write temporary config
files, for sake of testing the config constructor etc.
This inherits from :class:`python:unittest.TestCase` and adds the
following features:
Creates a temporary folder on setup, and removes it on teardown.
Adds the :meth:`write_file()` method to help with creating
temporary config files etc.
.. attribute:: tempdir
Path to the temporary folder created during setup.
"""
def setUp(self):
self.setup_file_config()
def setup_file_config(self):
"""
Setup logic specific to the ``FileConfigTestCase``.
This creates the temporary folder.
"""
self.tempdir = tempfile.mkdtemp()
def tearDown(self):
self.teardown_file_config()
def teardown_file_config(self):
"""
Teardown logic specific to the ``FileConfigTestCase``.
This removes the temporary folder.
"""
shutil.rmtree(self.tempdir)
def write_file(self, filename, content):
"""
Write a new file (in temporary folder) with the given filename
and content, and return its full path. For instance::
myfilepath = self.write_file('my.conf', '<file contents>')
"""
path = os.path.join(self.tempdir, filename)
with open(path, 'wt') as f:
f.write(content)
return path

View file

@ -2,8 +2,6 @@
import configparser
import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch, MagicMock
@ -13,21 +11,10 @@ from wuttjamaican import conf
from wuttjamaican.exc import ConfigurationError
from wuttjamaican.db import Session
from wuttjamaican.app import AppHandler
from wuttjamaican.testing import FileConfigTestCase
class TestWuttaConfig(TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tempdir)
def write_file(self, filename, content):
path = os.path.join(self.tempdir, filename)
with open(path, 'wt') as f:
f.write(content)
return path
class TestWuttaConfig(FileConfigTestCase):
def test_contstructor_basic(self):
config = conf.WuttaConfig()
@ -430,24 +417,12 @@ class TestGenericDefaultFiles(TestCase):
self.assertEqual(len(files), 0)
class TestMakeConfig(TestCase):
class TestMakeConfig(FileConfigTestCase):
# nb. we use appname='wuttatest' in this suite to avoid any
# "valid" default config files, env vars etc. which may be present
# on the dev machine
def setUp(self):
self.tempdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tempdir)
def write_file(self, filename, content):
path = os.path.join(self.tempdir, filename)
with open(path, 'wt') as f:
f.write(content)
return path
def test_generic_default_files(self):
generic = self.write_file('generic.conf', '')