Skip to content

Commit

Permalink
Add includeme support and attach registration directives to confi…
Browse files Browse the repository at this point in the history
…gurator.
  • Loading branch information
malthe committed Aug 2, 2011
1 parent 0d4ce41 commit 9d2b331
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lumin/__init__.py
@@ -1 +1,11 @@
#a package
from .db import register_mongodb
from .db import register_memcached

def includeme(config):
""" Function meant to be included via
:meth:`pyramid.config.Configurator.include`, which sets up the
Configurator with a ``register_path`` method."""

config.add_directive('register_mongodb', register_mongodb, action_wrap=False)
config.add_directive('register_memcached', register_memcached, action_wrap=False)
config.scan('lumin')
6 changes: 5 additions & 1 deletion lumin/db.py
Expand Up @@ -58,7 +58,11 @@ def add_memcached(event):
if mc:
event.request.mc = mc

def register_memchached(config, mc_host):
def register_memcached(config, mc_host):
if memcache is None:
# Raise import error exception
import memcached

mc_conn = memcache.Client(mc_host)
config.registerUtility(mc_conn, IMemcachedClient)

Expand Down
36 changes: 36 additions & 0 deletions lumin/tests/test_config.py
@@ -0,0 +1,36 @@
import unittest
import pyramid.testing


class ConfigurationTest(unittest.TestCase):
def setUp(self):
self.config = pyramid.testing.setUp()

def tearDown(self):
pyramid.testing.tearDown()

def test_register_mongodb_directive(self):
import lumin
self.config.include(lumin)

from lumin.db import IMongoDBConnection
connection = self.config.registry.queryUtility(IMongoDBConnection)
self.assertTrue(connection is None)

# Now, let's register a database connection
self.config.register_mongodb('mongodb://localhost/')
connection = self.config.registry.queryUtility(IMongoDBConnection)
self.assertTrue(connection is not None)

def test_register_memcached_directive(self):
import lumin
self.config.include(lumin)

from lumin.db import IMemcachedClient
client = self.config.registry.queryUtility(IMemcachedClient)
self.assertTrue(client is None)

# Now, let's register a memcached client
self.config.register_memcached('http://localhost/')
client = self.config.registry.queryUtility(IMemcachedClient)
self.assertTrue(client is not None)

0 comments on commit 9d2b331

Please sign in to comment.