"""
open/DurusWorks/qp/lib/qtest.py
"""
from qp.pub.common import get_publisher
from qp.lib.site import Site
from sancho.utest import UTest
import os


class QTest (UTest):
    """
    QTest is useful for UTests that use a DurusPublisher and 
    utilize a database connection.
    """

    def __init__(self, site_name=None):
        if not get_publisher():
            Site(site_name or os.environ.get('SITE')).get_publisher()
        UTest.__init__(self)

    def _pre(self):
        if get_publisher():
            get_publisher().get_connection().abort()

    def _post(self):
        if get_publisher():
            get_publisher().get_connection().abort()
