pytest-couchdbkit / pytest_couchdbkit / __init__.py

import pytest
import os
from .dumper import dump_db
from .utils import server_from_config, dbname_from_config, \
        maybe_destroy_and_create

def pytest_addoption(parser):
    parser.addini('couchdbkit_backend',
                  'socketpool backend we should use\n'
                  'defaults to $COUCHDBKIT_BACKEND or thread',
                  default=os.environ.get("COUCHDBKIT_BACKEND", 'thread'))
    parser.addini('couchdbkit_suffix', 'database name suffix')
    parser.addoption('--couchdb-no-push', action='store_true',
                     help='disable initial app push')


def pytest_addhooks(pluginmanager):
    from . import hookspec
    pluginmanager.addhooks(hookspec)

def pytest_sessionstart(session):
    if session.config.option.couchdb_no_push:
        return
    slaveinput = getattr(session.config, 'slaveinput', None)
    if slaveinput is not None:
        return
    try:
        dbname = dbname_from_config(session.config, 'pytest_%s_couchapp_source')
    except pytest.xfail.Exception:
        pass # we are not configured
    else:
        server = server_from_config(session.config)
        session.config.hook.pytest_couchdbkit_push_app(
                server=server,
                dbname=dbname)
        if dbname in server:
            server[dbname].compact()

def pytest_funcarg__couchdb_server(request):
    return server_from_config(request.config)


def pytest_funcarg__couchdb(request):
    server = request.getfuncargvalue('couchdb_server')
    tmpdir = request.getfuncargvalue('tmpdir')

    dbname = dbname_from_config(request.config, 'pytest_%s')
    db_source = dbname_from_config(request.config, 'pytest_%s_couchapp_source')
    slaveinput = getattr(request.config, 'slaveinput', None)
    if slaveinput is not None:
        dbname = '%s_%s' % (dbname, slaveinput['slaveid'])
    db = maybe_destroy_and_create(server, dbname)
    if db_source in server.all_dbs():
        server.replicate(db_source, dbname)
    
    def finalize_db():
        with tmpdir.join('couchdb.dump').open('w') as fp:
            dump_db(db, fp)
    request.addfinalizer(finalize_db)
    return db
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.