Browse files

steal kapil's mongodb plugin for nose

  • Loading branch information...
1 parent 1adfdfb commit 8734e0be0ad6511eee7d7d248d2538e0f794c5fd @reedobrien reedobrien committed Aug 19, 2011
Showing with 140 additions and 41 deletions.
  1. +4 −0 coverage
  2. +1 −0 lumin/tests/__init__.py
  3. +37 −0 lumin/tests/base.py
  4. +87 −0 lumin/tests/mongodb.py
  5. +3 −1 lumin/tests/test_config.py
  6. +4 −34 lumin/tests/test_node.py
  7. +3 −6 setup.py
  8. +1 −0 test
View
4 coverage
@@ -0,0 +1,4 @@
+#!/bin/sh
+# Use like "./coverage" or "sh coverage" to prevent the command interjecting.
+
+../../bin/nosetests --nologcapture --nocapture --with-coverage --cover-package=lumin --cover-erase --exclude=selenium --mongodb-bin=../../bin/mongod --mongodb-port=43000 $*
View
1 lumin/tests/__init__.py
@@ -0,0 +1 @@
+# a package
View
37 lumin/tests/base.py
@@ -0,0 +1,37 @@
+import os
+import unittest
+
+import pyramid.testing
+
+class BaseFunctionalTestCase(unittest.TestCase):
+ request_path = '/'
+
+ def setUp(self):
+ self.request = pyramid.testing.DummyRequest(
+ path=self.request_path,
+ )
+
+ self.config = pyramid.testing.setUp(
+ request=self.request,
+ settings={
+ 'secret': 'secret',
+ 'db_name': 'test',
+ })
+
+ self.config.include('lumin')
+
+ conn = self.config.register_mongodb(
+ 'mongodb://%s' % os.environ['TEST_MONGODB'])
+
+ # Drop and create test database
+ conn.drop_database('test')
+ db = conn['test']
+
+ # Create collections
+ db.create_collection('users')
+
+ # Fire new request event to set up environment
+ self.config.registry.handle(pyramid.events.NewRequest(self.request))
+
+ def tearDown(self):
+ pyramid.testing.tearDown()
View
87 lumin/tests/mongodb.py
@@ -0,0 +1,87 @@
+import os
+import subprocess
+import sys
+import tempfile
+
+from nose.plugins import Plugin
+
+
+class MongoDBPlugin(Plugin):
+ """A nose plugin that setups a test managed mongodb instance.
+ """
+
+ def __init__(self):
+ super(MongoDBPlugin, self).__init__()
+ self.mongodb_bin = None
+ self.db_port = None
+ self.db_path = None
+ self.process = None
+ self._running = False
+ self._enabled = False
+
+ def options(self, parser, env={}):
+ parser.add_option(
+ "--mongodb-bin", action="store", dest="mongodb_bin", default=None,
+ help="Setup a mongodb test instance using the specified binary.")
+ parser.add_option(
+ "--mongodb-port", action="store", dest="mongodb_port", type="int",
+ default=43000, help="Port to run the test mongodb instance on")
+
+ def configure(self, options, conf):
+ if options.mongodb_bin:
+ self.db_port = options.mongodb_port
+ self.mongodb_bin = os.path.abspath(
+ os.path.expanduser(os.path.expandvars(options.mongodb_bin)))
+
+ self.enabled = True
+ assert os.path.exists(self.mongodb_bin), "Invalid mongodb binary"
+
+ def begin(self):
+ """Start an instance of mongodb
+ """
+ self._running = False
+
+ if "TEST_MONGODB" in os.environ:
+ return
+
+ # Stores data here
+ self.db_path = tempfile.mkdtemp()
+ if not os.path.exists(self.db_path):
+ os.mkdir(self.db_path)
+
+ # Another option of possible use.
+ #"--noprealloc", # only needed on fs that don't sparse
+ # allocate, ie. not (ext4, btrfs, xfs)
+
+ self.process = subprocess.Popen(
+ args=[
+ self.mongodb_bin,
+ "--dbpath",
+ self.db_path,
+ "--port",
+ str(self.db_port),
+ "--quiet", # don't flood stdout, we're not reading it
+ "--nohttpinterface", # save the port
+ "--noscripting", # not used
+ "--nounixsocket", # not used
+ "--noprealloc",
+ ],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT
+ )
+ os.environ["TEST_MONGODB"] = "localhost:%s" % (self.db_port)
+ self._running = True
+
+ def finalize(self, result):
+ """Stop the mongodb instance.
+ """
+ if not self._running:
+ return
+
+ del os.environ["TEST_MONGODB"]
+ if sys.platform == 'darwin':
+ self.process.kill()
+ else:
+ self.process.terminate()
+ self.process.wait()
+ self._running = False
View
4 lumin/tests/test_config.py
@@ -1,3 +1,4 @@
+import os
import unittest
import pyramid.testing
@@ -18,7 +19,8 @@ def test_register_mongodb_directive(self):
self.assertTrue(connection is None)
# Now, let's register a database connection
- self.config.register_mongodb('mongodb://localhost/')
+ self.config.register_mongodb(
+ 'mongodb://%s' % os.environ['TEST_MONGODB'])
connection = self.config.registry.queryUtility(IMongoDBConnection)
self.assertTrue(connection is not None)
View
38 lumin/tests/test_node.py
@@ -1,37 +1,7 @@
-import lumin
-import unittest
-import pyramid.events
-import pyramid.testing
+from lumin.tests.base import BaseFunctionalTestCase
-class NodeTestCase(unittest.TestCase):
- def setUp(self):
- self.request = pyramid.testing.DummyRequest()
- self.config = pyramid.testing.setUp(
- request=self.request,
- settings={
- 'secret': 'secret',
- 'db_name': 'test',
- })
-
- self.config.include(lumin)
- conn = self.config.register_mongodb('mongodb://localhost/')
-
- # Drop and create test database
- conn.drop_database('test')
- db = conn['test']
-
- # Create collections
- db.create_collection('test')
-
- # Fire new request event to set up environment
- self.config.registry.handle(pyramid.events.NewRequest(self.request))
-
- def tearDown(self):
- pyramid.testing.tearDown()
-
-
-class CollectionTestCase(NodeTestCase):
+class CollectionTestCase(BaseFunctionalTestCase):
def test_collection_find(self):
# Insert item directly into collection
self.request.db['test'].insert({'_id': 'frobnitz'}, {})
@@ -80,7 +50,7 @@ def test_collection_insert(self):
self.assertEqual(result, 'frobnitz-1')
-class ContextByIdTestCase(NodeTestCase):
+class ContextByIdTestCase(BaseFunctionalTestCase):
def test_save(self):
# Insert item directly into collection
self.request.db['test'].insert(
@@ -136,7 +106,7 @@ def test_update_history(self):
-class ContextBySpecTestCase(NodeTestCase):
+class ContextBySpecTestCase(BaseFunctionalTestCase):
def test_unique(self):
# Insert item directly into collection
self.request.db['test'].insert(
View
9 setup.py
@@ -88,7 +88,7 @@ def run(self):
long_description=README + '\n\n' + CHANGES,
classifiers=[
"Topic :: Database",
- "Development Status :: 3 - Alpha",
+ "Development Status :: 4 - Beta",
"Framework :: BFG",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
@@ -109,11 +109,8 @@ def run(self):
test_suite="nose.collector",
cmdclass = {'doc' : doc },
entry_points = """\
- #[console_scripts]
- #addzope2user = lumin.scripts.adduser:main
-
- #[paste.filter_app_factory]
- #middleware = lumin:constructor
+ [nose.plugins.0.10]
+ mongodb = lumin.tests.mongodb:MongoDBPlugin
"""
)
View
1 test
@@ -0,0 +1 @@
+../../bin/nosetests --nologcapture --nocapture --mongodb-bin=../../bin/mongod --mongodb-port=43000 $*

0 comments on commit 8734e0b

Please sign in to comment.