Package: testing.common.database
Abouttesting.common.databaseis utilities for testing.* package.InstallUse pip:$ pip install testing.common.databaseHelpersclass Database(object):Databaseis a base class for database testing packages.
To create your database testing class, inherit this class and override methods below.def initialize(self):Handler for initialize database object.def get_data_directory(self):Path to data directory of your databse.Example:def get_data_directory(self):
    return os.path.join(self.base_dir, 'data')def initialize_database(self):Handler to initialize your database.Example:def initialize_database(self):
   if not os.path.exists(os.path.join(self.base_dir, 'data', 'PG_VERSION')):
       args = ([self.initdb, '-D', os.path.join(self.base_dir, 'data'), '--lc-messages=C'] +
               self.settings['initdb_args'].split())

       try:
           p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
           output, err = p.communicate()
           if p.returncode != 0:
               raise RuntimeError("initdb failed: %r" % err)
       except OSError as exc:
           raise RuntimeError("failed to spawn initdb: %s" % exc)def get_server_commandline(self):Command line to invoke your database server.Example:def get_server_commandline(self):
    return (['postgres',
             '-p', str(self.settings['port']),
             '-D', os.path.join(self.base_dir, 'data'),
             '-k', os.path.join(self.base_dir, 'tmp')] +
            self.settings['postgres_args'].split())def prestart(self):Handler called before invoking your database server.def poststart(self):Hander called after invoking your database server.def is_server_available(self):Methods check your database server available.
TheDatabaseclass uses this method to check the server boots up.Example:try:
    with closing(pg8000.connect(**self.dsn(database='template1'))):
        pass
except pg8000.Error:
    return False
else:
    return Truedef is_alive(self):Methods check the database server is alive.@property
def server_pid(self):Process ID of the database server.class DatabaseFactory(object):DatabaseFactoryis a factory class for the database class.
To create your database factory class, inherit this class and settarget_classvariable:class PostgresqlFactory(DatabaseFactory):
    target_class = PostgresqlThe factory class should work like atarget_class:# The factory class generates like a ``target_class``, in this case, generates ``Postgresql`` class
Postgresql = PostgresqlFactory()

# The generated class works same as ``target_class``
with Postgresql() as pgsql:
    #
    # do any tests using the database ...
    #It can bypass parameters to thetarget_classon every instantiation:Postgresql = PostgresqlFactory(copy_data_from='/path/to/database')

with Postgresql() as pgsql:
    #
    # Test with ``copy_data_from`` parameter :-)
    #Also, it is able to cache the database generated atDatabase.initialize_database()withcache_initialized_dbparameter.
It avoids running database initialization on every tests:# Initialize database once
Postgresql = PostgresqlFactory(cache_initialized_db=True)

with Postgresql() as pgsql:
    # copy cached database for this test.If you want to fixtures to the database, useon_initializedparameter:def handler(pgsql):
    # inserting fixtures

# Initialize database once, and call ``on_initialized`` handler
Postgresql = PostgresqlFactory(cache_initialized_db=True,
                               on_initialized=handler)class SkipIfNotInstalledDecorator(object):Generates decorator that skips the testcase if database command not found.
To create decorator, inherit this class and setnamevariable and overridesearch_server()method.Example:class PostgresqlSkipIfNotInstalledDecorator(SkipIfNotInstalledDecorator):
    name = 'PostgreSQL'

    def search_server(self):
        find_program('postgres', ['bin'])  # raise exception if not found


skipIfNotFound = skipIfNotInstalled = PostgresqlSkipIfNotInstalledDecorator()

@skipIfNotFound
def test():
    # testcasedef get_unused_port():Get free TCP port.def get_path_of(name):Searchs command from search paths. It works likewhichcommand.RequirementsPython 2.7, 3.4, 3.5, 3.6LicenseApache License 2.0Release signaturesReleases are signed with following keys:498D6B9EHistory2.0.3 (2017-10-24)Fix a bug:Handle exceptions from get_path_of()2.0.2 (2017-10-08)Fix a bug:#18: Fix launch when using cache_initialized_db without init_handler2.0.1 (2017-07-15)#9: Database always gets instantiated with correct settings#10: Remove explicit path to which#11: Make database server kill-timeout more configurable2.0.0 (2016-08-20)Use subprocess.Popen() instead of fork & execSupport windows platform (experimental)#4: Add boot_timeout parameterFix bugs:Fix syntax errors for Python3Show error messages if rescue from GC failed (ref: #1)1.1.0 (2016-02-05)Add Database#server_pid to get pid of the database serverAdd Database#is_alive() to check server is aliveDefine BOOT_TIMEOUT as constantFix AttributeError if any exceptions are raised in bootstrap1.0.0 (2016-02-01)Initial release