pytest-couchdbkit / testing / test_hooks.py

import pytest_couchdbkit
from pytest_couchdbkit.utils import maybe_destroy_and_create
import couchdbkit
import mock

settings = {'couchdbkit_suffix': 'test', 'couchdbkit_backend': 'thread'}

def funcargs(name, request):
    if name == 'couchdb_server':
        return couchdbkit.Server()
    print 'get funcarg', name
    return getattr(request, name)

def pytest_funcarg__request(request):
    tmpdir = request.getfuncargvalue('tmpdir')
    request = mock.MagicMock()
    request.config.getini.side_effect = settings.get
    request.getfuncargvalue.side_effect = lambda name: funcargs(name, request)
    request.tmpdir = tmpdir
    return request


def test_server_funcarg(request):
    server = pytest_couchdbkit.pytest_funcarg__couchdb_server(request)
    print server.info()

def test_database_dumping(request, tmpdir):
    db = pytest_couchdbkit.pytest_funcarg__couchdb(request)
    print db.info()
    db.save_doc({'_id': 'test'}, force_update=True)
    finalizer = request.addfinalizer.call_args[0][0]
    assert not tmpdir.join('couchdb.dump').check()
    finalizer()
    assert tmpdir.join('couchdb.dump').check()


def test_sessionstart_handles_missing_dbconfig_gracefull():
    session = mock.Mock()
    session.config.getini.side_effect = {}.get
    pytest_couchdbkit.pytest_sessionstart(session)


def test_sessionstart_with_config_calls_hook():
    settings = {'couchdbkit_suffix':'test'}
    session = mock.Mock()
    session.config.getini.side_effect = settings.get
    pytest_couchdbkit.pytest_sessionstart(session)
    call_args = session.config.hook.pytest_couchdbkit_push_app.call_args
    assert call_args[1]['dbname'] == 'pytest_test_couchapp_source'


def test_replication(request, tmpdir):
    server = pytest_couchdbkit.pytest_funcarg__couchdb_server(request)
    db_source = maybe_destroy_and_create(server, 'pytest_test_couchapp_source')
    db_source.save_doc({'_id': 'test'})


    db = pytest_couchdbkit.pytest_funcarg__couchdb(request)
    assert 'test' in 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.