A simple ComponentRegistry to store components.

This commit is contained in:
Emil Stenström 2015-06-11 20:42:16 +02:00
parent 22b00a4a62
commit 313852ce21
4 changed files with 33 additions and 0 deletions

View file

View file

@ -0,0 +1,9 @@
class ComponentRegistry(object):
def __init__(self):
self._registry = {} # component name -> component_class mapping
def register(self, name=None, component=None):
self._registry[name] = component
# This variable represents the global component registry
registry = ComponentRegistry()

0
tests/__init__.py Normal file
View file

24
tests/test_registry.py Normal file
View file

@ -0,0 +1,24 @@
import unittest
from django_components import component
class MockComponent(object):
pass
class ComponentRegistryTest(unittest.TestCase):
def setUp(self):
self.registry = component.ComponentRegistry()
def test_simple_register(self):
self.registry.register(name="testcomponent", component=MockComponent)
self.assertEqual(
self.registry._registry.items(),
[("testcomponent", MockComponent)]
)
def test_register_two_components(self):
self.registry.register(name="testcomponent", component=MockComponent)
self.registry.register(name="testcomponent2", component=MockComponent)
self.assertEqual(
self.registry._registry.items(),
[("testcomponent", MockComponent), ("testcomponent2", MockComponent)]
)