def test_dotted_path_base(self): """ Test defining a base for the registry as a dotted path to avoid the circular import problem. """ from appregister import Registry from appregister.base import InvalidOperation class MyRegistry(Registry): base = 'test_appregister.models.Question' registry = MyRegistry() from test_appregister.models import Question # Test the registry allows a valid registration and block an invalid. class MySubClass(Question): pass registry.register(MySubClass) class MyObject(object): pass with self.assertRaises(InvalidOperation): registry.register(MyObject)
def test_register(self): """ Test simple registration of a sublcass """ from test_appregister.models import Question, registry class MyQuestion(Question): pass registry.register(MyQuestion)
def test_clear_register(self): from test_appregister.models import Question, registry class MyQuestion(Question): pass registry.register(MyQuestion) self.assertIn(MyQuestion, registry.all()) registry.clear() self.assertNotIn(MyQuestion, registry.all())
def test_non_subclass_register(self): """ Test trying to register a non-sublcass, which is not allowed """ from appregister.base import InvalidOperation from test_appregister.models import registry class NonSubclass(object): pass with self.assertRaises(InvalidOperation): registry.register(NonSubclass)
def test_already_registered(self): """ Test attempting to register a subclass more than once. """ from appregister.base import AlreadyRegistered from test_appregister.models import Question, registry class MyQuestion2(Question): pass registry.register(MyQuestion2) with self.assertRaises(AlreadyRegistered): registry.register(MyQuestion2)
def test_doesnt_contain_base(self): """ Test the base class isn't registered. """ from test_appregister.models import Question, registry self.assertEqual(registry.all(), set()) class MyQuestion3(Question): pass registry.register(MyQuestion3) self.assertEqual(registry.all(), set([MyQuestion3, ]))
def test_unregister(self): """ Test the unregister process by registering, unregistering and registering again """ from test_appregister.models import Question, registry class MyQuestion3(Question): pass registry.register(MyQuestion3) self.assertIn(MyQuestion3, registry.all()) registry.unregister(MyQuestion3) self.assertNotIn(MyQuestion3, registry.all()) registry.register(MyQuestion3) self.assertIn(MyQuestion3, registry.all())
def test_basic_registry(self): from appregister import SortedRegistry from appregister.base import InvalidOperation, AlreadyRegistered from test_appregister.models import Question class MyRegistry(SortedRegistry): base = Question registry = MyRegistry() # Test the registry allows a valid registration and block an invalid. class MyTestSubClass(Question): pass registry.register(MyTestSubClass) with self.assertRaises(AlreadyRegistered): registry.register(MyTestSubClass) class MyObject(object): pass with self.assertRaises(InvalidOperation): registry.register(MyObject) self.assertIn(MyTestSubClass, registry) registry.unregister(MyTestSubClass) self.assertNotIn(MyTestSubClass, registry) self.assertEqual(len(registry), 0)
def test_basic_registry(self): from appregister import Registry from appregister.base import InvalidOperation from test_appregister.models import Question class MyRegistry(Registry): base = Question registry = MyRegistry() # Test the registry allows a valid registration and block an invalid. class MySubClass(Question): pass registry.register(MySubClass) class MyObject(object): pass with self.assertRaises(InvalidOperation): registry.register(MyObject)
def test_basic_registry(self): from appregister import NamedRegistry from appregister.base import InvalidOperation, AlreadyRegistered from test_appregister.models import Question class MyRegistry(NamedRegistry): base = Question registry = MyRegistry() # Test the registry allows a valid registration and block an invalid. class MyTestSubClass(Question): pass registry.register('first', MyTestSubClass) with self.assertRaises(AlreadyRegistered): registry.register('first', MyTestSubClass) registry.register('second', MyTestSubClass) class MyObject(object): pass with self.assertRaises(InvalidOperation): registry.register('invalid', MyObject) registry.unregister('first') with self.assertRaises(KeyError): registry['first'] self.assertEqual(registry['second'], MyTestSubClass) self.assertEqual(len(registry), 1) for i in registry: self.assertIn(i, registry.keys())
from test_appregister.models import Question, registry class MyAutoDiscoveredQuestion(Question): pass registry.register(MyAutoDiscoveredQuestion)