Wrap db.Manager to a function arg for automated tests

This commit is contained in:
Martin Zibricky 2011-09-03 16:21:36 +02:00
parent 0de5d5e09a
commit 4cf750ce9d
3 changed files with 73 additions and 21 deletions

View File

@ -228,26 +228,29 @@ def main(args=None):
help='Set the Qt4 style (passed directly to Qt4).')
parser.add_option('--testing', dest='testing',
action='store_true', help='Run by testing framework')
# Set up logging
log_path = AppLocation.get_directory(AppLocation.CacheDir)
check_directory_exists(log_path)
filename = os.path.join(log_path, u'openlp.log')
logfile = logging.FileHandler(filename, u'w')
logfile.setFormatter(logging.Formatter(
u'%(asctime)s %(name)-55s %(levelname)-8s %(message)s'))
log.addHandler(logfile)
logging.addLevelName(15, u'Timer')
# Parse command line options and deal with them.
# Use args supplied programatically if possible.
(options, args) = parser.parse_args(args) if args else parser.parse_args()
# Set up logging
# In test mode it is skipped
if not options.testing:
log_path = AppLocation.get_directory(AppLocation.CacheDir)
check_directory_exists(log_path)
filename = os.path.join(log_path, u'openlp.log')
logfile = logging.FileHandler(filename, u'w')
logfile.setFormatter(logging.Formatter(
u'%(asctime)s %(name)-55s %(levelname)-8s %(message)s'))
log.addHandler(logfile)
logging.addLevelName(15, u'Timer')
if options.loglevel.lower() in ['d', 'debug']:
log.setLevel(logging.DEBUG)
print 'Logging to:', filename
elif options.loglevel.lower() in ['w', 'warning']:
log.setLevel(logging.WARNING)
else:
log.setLevel(logging.INFO)
# Deal with other command line options.
qt_args = []
if options.loglevel.lower() in ['d', 'debug']:
log.setLevel(logging.DEBUG)
print 'Logging to:', filename
elif options.loglevel.lower() in ['w', 'warning']:
log.setLevel(logging.WARNING)
else:
log.setLevel(logging.INFO)
if options.style:
qt_args.extend(['-style', options.style])
# Throw the rest of the arguments at Qt, just in case.

View File

@ -159,7 +159,7 @@ class Manager(object):
Provide generic object persistence management
"""
def __init__(self, plugin_name, init_schema, db_file_name=None,
upgrade_mod=None):
db_file_path=None, upgrade_mod=None):
"""
Runs the initialisation process that includes creating the connection
to the database and the tables if they don't exist.
@ -176,6 +176,10 @@ class Manager(object):
``db_file_name``
The file name to use for this database. Defaults to None resulting
in the plugin_name being used.
``db_file_path``
The path to sqlite file to use for this database. This is useful
for testing purposes.
"""
settings = QtCore.QSettings()
settings.beginGroup(plugin_name)
@ -184,7 +188,11 @@ class Manager(object):
db_type = unicode(
settings.value(u'db type', QtCore.QVariant(u'sqlite')).toString())
if db_type == u'sqlite':
if db_file_name:
# For automated tests we need to supply file_path directly
if db_file_path:
self.db_url = u'sqlite:///%s' % os.path.normpath(
os.path.abspath(db_file_path))
elif db_file_name:
self.db_url = u'sqlite:///%s/%s' % (
AppLocation.get_section_data_path(plugin_name),
db_file_name)

View File

@ -30,16 +30,57 @@
Configuration file for pytest framework.
"""
import logging
import random
import string
from PyQt4 import QtCore
from sqlalchemy.orm import clear_mappers
from openlp.core import main as openlp_main
from openlp.core.lib.db import Manager
from openlp.plugins.songs.lib.db import init_schema
# set up logging to stderr (console)
_handler = logging.StreamHandler(stream=None)
_handler.setFormatter(logging.Formatter(
u'%(asctime)s %(name)-55s %(levelname)-8s %(message)s'))
logging.addLevelName(15, u'Timer')
log = logging.getLogger()
log.addHandler(_handler)
log.setLevel(logging.DEBUG)
# Test function argument to make openlp gui instance persistent for all tests.
# All test cases have to access the same instance. To allow create multiple
# Test cases in module have to access the same instance. To allow creating
# multiple
# instances it would be necessary use diffrent configuraion and data files.
# Created instance will use your OpenLP settings.
def pytest_funcarg__openlpapp(request):
def setup():
return openlp_main(['--testing'])
def teardown(app):
pass
return request.cached_setup(setup=setup, teardown=teardown, scope='session')
# sqlalchemy allows to map classess to only one database at a time
clear_mappers()
return request.cached_setup(setup=setup, teardown=teardown, scope='module')
# Test function argument to make openlp gui instance persistent for all tests.
def pytest_funcarg__empty_dbmanager(request):
def setup():
tmpdir = request.getfuncargvalue('tmpdir')
db_file_path = tmpdir.join('songs.sqlite')
print db_file_path
unique = ''.join(random.choice(string.letters + string.digits)
for i in range(8))
plugin_name = 'test_songs_%s' % unique
settings = QtCore.QSettings()
settings.beginGroup(plugin_name)
settings.setValue(u'db type', QtCore.QVariant(u'sqlite'))
settings.endGroup()
manager = Manager(plugin_name, init_schema,
db_file_path=db_file_path.strpath)
return manager
def teardown(manager):
clear_mappers()
return request.cached_setup(setup=setup, teardown=teardown, scope='function')