Skip to main content

About

testing.common.database is utilities for testing.* package.

https://travis-ci.org/tk0miya/testing.common.database.svg?branch=master https://codeclimate.com/github/tk0miya/testing.common.database/badges/gpa.svg

Install

Use pip:

$ pip install testing.common.database

Helpers

class Database(object):

Database is 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. The Database class 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 True

class DatabaseFactory(object):

DatabaseFactory is a factory class for the database class. To create your database factory class, inherit this class and set target_class variable:

class PostgresqlFactory(DatabaseFactory):
    target_class = Postgresql

The factory class should work like a target_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 the target_class on 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 at Database.initialize_database() with cache_initialized_db parameter. 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, use on_initialized parameter:

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 set name variable and override search_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():
    # testcase

def get_unused_port():

Get free TCP port.

def get_path_of(name):

Searchs command from search paths. It works like which command.

Requirements

  • Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5

License

Apache License 2.0

History

1.0.0 (2016-02-01)

  • Initial release

Metadata

Release files for testing.common.database 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for testing.common.database 1.0.0
File Size Uploaded Provenance Actions
testing.common.database-1.0.0.tar.gz 10.3 kB Without provenance Details for testing.common.database-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for testing.common.database 1.0.0
File Interpreter ABI Platform Actions
testing.common.database-1.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for testing.common.database-1.0.0-py2.py3-none-any.whl

Total release size: 19.2 kB

Release files / testing.common.database-1.0.0.tar.gz

Download URL testing.common.database-1.0.0.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f563c4e1b3c319eaa3fd8b0e64abbaeb3024eee4f489142b2906f2ae4d3d485a
BLAKE2b-256 checksum
How to use checksums
001cb80bf5a989120f90a8a9b32304ef5fbb2df0db298cae0f8bc4e699f7a57b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / testing.common.database-1.0.0-py2.py3-none-any.whl

Download URL testing.common.database-1.0.0-py2.py3-none-any.whl
Size 8.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
13db45bd8b1e6e1918908408291e98de35ba3c82ae339062812c362e1e111e2e
BLAKE2b-256 checksum
How to use checksums
e7ff9808cc0acecbfa72578e1bfcf5f9bf1cad73bf60c1467d59ee1fa51feff8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for testing.common.database 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page