Skip to content

Commit

Permalink
passing all db, son and module tests
Browse files Browse the repository at this point in the history
In both python 2 and 3
  • Loading branch information
Reed O'Brien committed Nov 9, 2012
1 parent b1ebe80 commit 6c2c8d4
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 93 deletions.
26 changes: 23 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
lumin Changelog
==========================

0.0 (unreleased)
----------------
0.5.0 (unreleased) refactoring release
--------------------------------------

This is a full refactoring, starting in a blank repo

Refactor Goals
++++++++++++++

- rename context/node to resource
- use pbkdf2 instead of sha512
- use mock instead of actual DB
- 100% test coverage
- just use object dict, not data
- ????
- simplify
- reduce dependencies on pyramid

0.0.0 - cam-0.3.2
-----------------

These were the original experiments and code, there are a couple apps
with years of production time. However they dated back to repoze.bfg < 1,
were crufty, partially tested and the turd needed some polishing...

- Initial release.
5 changes: 1 addition & 4 deletions lumin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# from .db import register_memcached
from lumin.db import register_mongodb


def includeme(config):
""" Function meant to be included via
:meth:`pyramid.config.Configurator.include`, which sets up the
Expand All @@ -9,7 +9,4 @@ def includeme(config):
config.add_directive('register_mongodb',
register_mongodb,
action_wrap=True)
# config.add_directive('register_memcached',
# register_memcached,
# action_wrap=True)
config.scan('lumin')
23 changes: 0 additions & 23 deletions lumin/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from pyramid.events import subscriber
from pyramid.interfaces import INewRequest
from pyramid.threadlocal import get_current_registry

from lumin.son import ColanderNullTransformer

Expand All @@ -32,25 +31,3 @@ def register_mongodb(config, db_uri, slave_okay=False):
conn = pymongo.Connection(db_uri, slave_okay=slave_okay)
config.registry.registerUtility(conn, IMongoDBConnection)
return conn


### XXX DO I really want memcached
#class IMemcachedClient(Interface):
# pass


#def get_memcached():
# reg = get_current_registry()
# return reg.queryUtility(IMemcachedClient)

#@subscriber(INewRequest)
#def add_memcached(event):
# mc = get_memcached()
# if mc:
# event.request.mc = mc

#def register_memcached(config, mc_host):
# import memcache

# mc_conn = memcache.Client([mc_host])
# config.registry.registerUtility(mc_conn, IMemcachedClient)
56 changes: 40 additions & 16 deletions lumin/son.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def transform_incoming(self, son, collection):
for (k, v) in son.items():
if v is colander.null:
son[k] = SENTINEL
continue
if isinstance(v, dict):
self._recursive_in(v)
return son
Expand All @@ -44,10 +43,10 @@ def transform_outgoing(self, son, collection):
serialized sentinel value to ``colander.null``.
"""
for (k, v) in son.items():
if isinstance(v, dict):
if isinstance(v, dict): # pragma: no branch
if v != SENTINEL:
self._recursive_out(v)
elif v == SENTINEL:
elif v == SENTINEL: # pragma: no branch
son[k] = colander.null
return son

Expand All @@ -58,7 +57,6 @@ def _recursive_in(self, subson):
for (k, v) in subson.items():
if v is colander.null:
subson[k] = SENTINEL
continue
if isinstance(v, dict):
for (key, value) in v.items():
if value is colander.null:
Expand Down Expand Up @@ -94,28 +92,58 @@ class DecimalTransformer(SONManipulator):
"""
def transform_incoming(self, son, collection):
"""
sets any DecimalTransformer to a serializable value.
sets any Decimal to a serializable value.
"""
for (k, v) in son.items():
if isinstance(v, Decimal):
son[k] = {'_type': 'decimal', 'value': v}
elif isinstance(v, dict):
son[k] = self.transform_incoming(v, collection)
son[k] = {'_type': 'decimal', 'value': str(v)}
if isinstance(v, dict):
self._recursive_in(v)
return son

def _recursive_in(self, subson):
"""
called by transform_incoming to provide recursive transformation
"""
for (k, v) in subson.items():
if isinstance(v, Decimal):
subson[k] = {'_type': 'decimal', 'value': str(v)}
if isinstance(v, dict):
for (key, value) in v.items():
if isinstance(value, Decimal):
v[key] = {'_type': 'decimal',
'value': str(value)}
if isinstance(value, dict):
self._recursive_in(value)

def transform_outgoing(self, son, collection):
"""
Sets any top level serialized Decimal to a Decimal. This is
not recursive.
"""
for (k, v) in son.items():
if isinstance(v, dict):
if isinstance(v, dict): # pragma: no branch
if "_type" in v and v["_type"] == "decimal":
son[k] = Decimal(v['value'])
else:
son[k] = self.transform_outgoing(v, collection)
if isinstance(v, dict): # pragma: no branch
self._recursive_out(v)
return son

def _recursive_out(self, subson):
"""
called by transform_outgoing to provide recursive transformation
"""
for (k, v) in subson.items():
if isinstance(v, dict):
if "_type" in v and v["_type"] == "decimal":
subson[k] = Decimal(v['value'])
else:
for (key, value) in v.items():
if "_type" in value and value["_type"] == "decimal":
value[key] = Decimal(value['value'])
if isinstance(value, dict): # pragma: no branch
self._recursive_out(v)


class DeNull:
"""
Expand All @@ -137,7 +165,6 @@ def _transform(self, son):
for (k, v) in son.items():
if v is colander.null:
son[k] = ''
continue
if isinstance(v, dict):
self._recurse(v)
return son
Expand All @@ -146,7 +173,6 @@ def _recurse(self, subson):
for (k, v) in subson.items():
if v is colander.null:
subson[k] = ''
continue
if isinstance(v, dict):
for (key, value) in v.items():
if value is colander.null:
Expand Down Expand Up @@ -179,16 +205,14 @@ def _transform(self, son):
for (k, v) in son.items():
if v is SENTINEL:
son[k] = ''
continue
if isinstance(v, dict):
if isinstance(v, dict): # pragma: no branch
self._recurse(v)
return son

def _recurse(self, subson):
for (k, v) in subson.items():
if v is SENTINEL:
subson[k] = ''
continue
if isinstance(v, dict):
for (key, value) in v.items():
if value is SENTINEL:
Expand Down
43 changes: 29 additions & 14 deletions lumin/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
# import os
import unittest
import pyramid.testing

Expand All @@ -10,32 +10,47 @@ def setUp(self):
def tearDown(self):
pyramid.testing.tearDown()

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

def _registerdb(self):
self.config.registry.settings['db_name'] = 'frozznob'
self.config.register_mongodb('mongodb://localhost')

def test_register_mongodb_directive(self):
self._includelumin()

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://%s' % os.environ['TEST_MONGODB'])
self._registerdb()
connection = self.config.registry.queryUtility(IMongoDBConnection)
self.assertTrue(connection is not None)

# def test_register_memcached_directive(self):
# import lumin
# self.config.include(lumin)
def test_get_mongodb(self):
self._includelumin()
self._registerdb()

from lumin.db import get_mongodb
from pymongo.database import Database

# from lumin.db import IMemcachedClient
# client = self.config.registry.queryUtility(IMemcachedClient)
# self.assertTrue(client is None)
conn = get_mongodb(self.config.registry)
self.assertTrue(isinstance(conn, Database))

# # Now, let's register a memcached client
# self.config.register_memcached('127.0.0.1:11211')
# client = self.config.registry.queryUtility(IMemcachedClient)
# self.assertTrue(client is not None)
def test_add_mongodb(self):
self._includelumin()
self._registerdb()
import pyramid
request = pyramid.testing.DummyRequest(
path='/',
)
import pyramid
# fire an event to call add_mongodb
self.config.registry.handle(pyramid.events.NewRequest(request))
self.assertTrue(hasattr(request, 'db'))


class TestIncludeMe(unittest.TestCase):
Expand Down
Loading

0 comments on commit 6c2c8d4

Please sign in to comment.