Skip to content

Commit

Permalink
Merge pull request pytest-dev#46 from ahlmss/reorganise_testing
Browse files Browse the repository at this point in the history
Reorganise testing
  • Loading branch information
eeaston committed Apr 16, 2014
2 parents 0e81738 + c49adb9 commit f761f8b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
86 changes: 81 additions & 5 deletions pkglib-testing/pkglib_testing/fixtures/server/rethink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

import pytest
import rethinkdb
import uuid
import logging
import time

from pkglib_testing import CONFIG

from .base import TestServer
from ..util import requires_config

log_format = ("%(levelname)-5s %(asctime)s.%(msecs)03d " +
"module:%(module)s %(message)s")
logging.basicConfig(format=log_format, datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO)
log = logging.getLogger(__name__)


def _rethink_server(request):
""" This does the actual work - there are several versions of this used
Expand Down Expand Up @@ -35,6 +44,71 @@ def rethink_server_sess(request):
return _rethink_server(request)


@pytest.yield_fixture(scope="function")
def rethink_unique_db(rethink_server_sess):
""" Starts up a session-scoped server, and returns a connection to
a unique database for the life of a single test, and drops it after
"""
dbid = uuid.uuid4().hex
conn = rethink_server_sess.conn
rethinkdb.db_create(dbid).run(conn)
conn.use(dbid)
yield conn
rethinkdb.db_drop(dbid).run(conn)


@pytest.yield_fixture(scope="module")
def rethink_module_db(rethink_server_sess):
""" Starts up a session-scoped server, and returns a connection to
a unique database for all the tests in one module.
Drops the database after module tests are complete.
"""
dbid = uuid.uuid4().hex
conn = rethink_server_sess.conn
log.info("Making database")
rethinkdb.db_create(dbid).run(conn)
conn.use(dbid)
yield conn
log.info("Dropping database")
rethinkdb.db_drop(dbid).run(conn)


@pytest.fixture(scope="module")
def rethink_make_tables(request, rethink_module_db):
reqd_table_list = getattr(request.module, 'FIXTURE_TABLES')
log.debug("Do stuff before all module tests with {}"
.format(reqd_table_list))
conn = rethink_module_db
for table_name, primary_key in reqd_table_list:
try:
rethinkdb.db(conn.db).table_create(table_name,
primary_key=primary_key,
).run(conn)
log.info('Made table "{}" with key "{}"'
.format(table_name, primary_key))
except RqlRuntimeError, err:
log.debug('Table "{}" not made: {}'.format(table_name,
err.message))


@pytest.yield_fixture(scope="function")
def rethink_empty_db(request, rethink_module_db, rethink_make_tables):
""" Given a module scoped database, we need to empty all the tables
for each test to ensure no interaction between test table content.
This is a useful approach, because of the long time taken to
create a new RethinkDB table, compared to the time to empty one.
"""
tables_to_emptied = (table[0] for table
in getattr(request.module, 'FIXTURE_TABLES'))
conn = rethink_module_db

for table_name in tables_to_emptied:
rethinkdb.db(conn.db).table(table_name).delete().run(conn)
log.debug('Emptied "{}" before test'.format(table_name))
yield conn


class RethinkDBServer(TestServer):
random_port = True

Expand All @@ -56,10 +130,12 @@ def run_cmd(self):

def check_server_up(self):
"""Test connection to the server."""
print("Connecting to RethinkDB at %s:%s" % (self.hostname, self.port))
log.info("Connecting to RethinkDB at {}:{}".format(
self.hostname, self.port))
try:
self.conn = rethinkdb.connect(host=self.hostname, port=self.port, db='test')
self.conn = rethinkdb.connect(host=self.hostname,
port=self.port, db='test')
return True
except rethinkdb.RqlDriverError as e:
print(e)
return False
except rethinkdb.RqlDriverError as err:
log.warn(err)
return False
20 changes: 19 additions & 1 deletion pkglib-testing/tests/integration/test_rethink_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,22 @@

def test_rethink_server(rethink_server):
assert rethink_server.check_server_up()
assert rethink_server.conn.db == 'test'
assert rethink_server.conn.db == 'test'


FIXTURE_TABLES = [('tbl_foo', 'code'), ('tbl_bar', 'id')]

# Lots of tests needed here!

def test_rethink_empty_db(rethink_empty_db):
pass

def test_foo_1(rethink_empty_db):
assert 1

def test_foo_2(rethink_empty_db):
assert 2

def test_foo_3(rethink_empty_db):
assert 3

0 comments on commit f761f8b

Please sign in to comment.