Skip to content

Commit

Permalink
Split route module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Geoffroy committed Jul 4, 2014
1 parent 13c0421 commit 402e92d
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 378 deletions.
57 changes: 57 additions & 0 deletions tests/unit/test_callback_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ~/code/django-crucrudile/django_crucrudile/routes/callback.py

from nose.tools import assert_true, assert_raises, assert_equal
import mock

from django_crucrudile.routes import Route, CallbackRoute


class CallbackRouteTestCase:
route_class = CallbackRoute
mock_callback = None

def setUp(self):
self.route_class = type(
'CallbackRoute',
(self.route_class, ),
{'name': "test name"}
)
self.mock_callback = mock.Mock()
self.route = self.route_class(
callback=self.mock_callback, name="name"
)

def test_subclasses_route(self):
assert_true(
issubclass(self.route_class, Route)
)

def test_init_requires_callback(self):
assert_raises(
ValueError, # No ``callback``...
self.route_class
)

callback = mock.Mock()

assert_equal(
self.route_class(callback=callback).callback,
callback
)

_route_class = type(
'CallbackRoute',
(self.route_class, ),
{'callback': callback}
)

assert_equal(
_route_class().callback,
callback
)

def test_get_callback(self):
assert_equal(
self.route.get_callback(),
self.mock_callback
)
89 changes: 0 additions & 89 deletions tests/unit/test_entity_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,9 @@

from django_crucrudile.entities.store import(
EntityStoreMetaclass, EntityStore,
provides, register_instances, register_class
)


class DecoratorTestMixin:
decorator = None
decorator_blank_args = [None]
decorator_blank_kwargs = {}

def test_callable(self):
assert_true(callable(self.decorator))

def test_returns_callable(self):
assert_true(callable(self.decorator(
*self.decorator_blank_args,
**self.decorator_blank_kwargs
)))


class ProvidesTestCase(DecoratorTestMixin):
@property
def decorator(self):
return provides

def test_calls_register_class(self):
kwargs = {'key': 'value'}
mock_provided_arg = mock.Mock()
mock_router_class = mock.Mock()

self.decorator(mock_provided_arg,
**kwargs)(mock_router_class)

assert_true(
mock_router_class.register_class.called
)

called_args, called_kwargs = list(
mock_router_class.register_class.call_args
)

assert_equal(
called_args,
(mock_provided_arg,)
)
assert_equal(
called_kwargs,
kwargs
)


class RegisterInstancesTestCase(DecoratorTestMixin):
@property
def decorator(self):
return register_instances

def test_patches_constructor(self):
mock_store = mock.Mock()
mock_store.register = mock_register = mock.Mock()
mock_init = mock.Mock()
mock_entity_class = type(
'MockEntity',
(),
{'__init__': mock_init}
)

self.decorator(mock_store)(mock_entity_class)

mock_entity_class()

assert_true(mock_init.called)
assert_true(mock_register.called)


class RegisterClassTestCase(DecoratorTestMixin):
@property
def decorator(self):
return register_class

def test_calls_register_class(self):
mock_store_class = mock.Mock()
mock_reg_cls = mock_store_class.register_class
mock_entity_class = mock.Mock()

self.decorator(mock_store_class)(mock_entity_class)

assert_true(mock_reg_cls.called)
assert_equal(
list(mock_reg_cls.call_args),
[(mock_entity_class,), {}]
)


class EntityStoreMetaclassTestCase:
store_metaclass = EntityStoreMetaclass

Expand Down
94 changes: 94 additions & 0 deletions tests/unit/test_entity_store_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import mock
import abc
from nose.tools import assert_true, assert_false, assert_raises, assert_equal


from django_crucrudile.entities.store import(
provides, register_instances, register_class
)
class DecoratorTestMixin:
decorator = None
decorator_blank_args = [None]
decorator_blank_kwargs = {}

def test_callable(self):
assert_true(callable(self.decorator))

def test_returns_callable(self):
assert_true(callable(self.decorator(
*self.decorator_blank_args,
**self.decorator_blank_kwargs
)))


class ProvidesTestCase(DecoratorTestMixin):
@property
def decorator(self):
return provides

def test_calls_register_class(self):
kwargs = {'key': 'value'}
mock_provided_arg = mock.Mock()
mock_router_class = mock.Mock()

self.decorator(mock_provided_arg,
**kwargs)(mock_router_class)

assert_true(
mock_router_class.register_class.called
)

called_args, called_kwargs = list(
mock_router_class.register_class.call_args
)

assert_equal(
called_args,
(mock_provided_arg,)
)
assert_equal(
called_kwargs,
kwargs
)


class RegisterInstancesTestCase(DecoratorTestMixin):
@property
def decorator(self):
return register_instances

def test_patches_constructor(self):
mock_store = mock.Mock()
mock_store.register = mock_register = mock.Mock()
mock_init = mock.Mock()
mock_entity_class = type(
'MockEntity',
(),
{'__init__': mock_init}
)

self.decorator(mock_store)(mock_entity_class)

mock_entity_class()

assert_true(mock_init.called)
assert_true(mock_register.called)


class RegisterClassTestCase(DecoratorTestMixin):
@property
def decorator(self):
return register_class

def test_calls_register_class(self):
mock_store_class = mock.Mock()
mock_reg_cls = mock_store_class.register_class
mock_entity_class = mock.Mock()

self.decorator(mock_store_class)(mock_entity_class)

assert_true(mock_reg_cls.called)
assert_equal(
list(mock_reg_cls.call_args),
[(mock_entity_class,), {}]
)
Loading

0 comments on commit 402e92d

Please sign in to comment.