diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2013-01-14 14:08:48 +0100 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2013-01-14 14:08:48 +0100 |
commit | c0308a32ff35d921e75c5cff68df14aeacd4b9f9 (patch) | |
tree | c059bfd7c55fb16971e355dba4f98ec224b7e37f /test | |
parent | b2c601c590f1b54916ea8eab8280b0118c1a7e6a (diff) | |
download | logilab-common-c0308a32ff35d921e75c5cff68df14aeacd4b9f9.tar.gz |
[registry] introduce RegistrableObject and RegistrableInstance base classes. Closes #98742
and make them mandatory *for automatic registration*. Cleanup automatic registration
code accordingly.
Instances are now registrable, and automatically registered provided they inherit
from RegistrableInstance.
Diffstat (limited to 'test')
-rw-r--r-- | test/data/regobjects.py | 22 | ||||
-rw-r--r-- | test/data/regobjects2.py | 8 | ||||
-rw-r--r-- | test/unittest_registry.py | 44 |
3 files changed, 72 insertions, 2 deletions
diff --git a/test/data/regobjects.py b/test/data/regobjects.py new file mode 100644 index 0000000..6cea558 --- /dev/null +++ b/test/data/regobjects.py @@ -0,0 +1,22 @@ +"""unittest_registry data file""" +from logilab.common.registry import yes, RegistrableObject, RegistrableInstance + +class Proxy(object): + """annoying object should that not be registered, nor cause error""" + def __getattr__(self, attr): + return 1 + +trap = Proxy() + +class AppObjectClass(RegistrableObject): + __registry__ = 'zereg' + __regid__ = 'appobject1' + __select__ = yes() + +class AppObjectInstance(RegistrableInstance): + __registry__ = 'zereg' + __select__ = yes() + def __init__(self, regid): + self.__regid__ = regid + +appobject2 = AppObjectInstance('appobject2') diff --git a/test/data/regobjects2.py b/test/data/regobjects2.py new file mode 100644 index 0000000..5c28b51 --- /dev/null +++ b/test/data/regobjects2.py @@ -0,0 +1,8 @@ +from logilab.common.registry import RegistrableObject, RegistrableInstance, yes + +class MyRegistrableInstance(RegistrableInstance): + __regid__ = 'appobject3' + __select__ = yes() + __registry__ = 'zereg' + +instance = MyRegistrableInstance() diff --git a/test/unittest_registry.py b/test/unittest_registry.py index b84f672..a15fe98 100644 --- a/test/unittest_registry.py +++ b/test/unittest_registry.py @@ -1,4 +1,4 @@ -# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of Logilab-Common. @@ -19,10 +19,17 @@ from __future__ import with_statement import gc +import logging +import os.path as osp +import sys from operator import eq, lt, le, gt +from contextlib import contextmanager + +logging.basicConfig(level=logging.ERROR) + from logilab.common.testlib import TestCase, unittest_main -from logilab.common.registry import Predicate, AndPredicate, OrPredicate, wrap_predicates +from logilab.common.registry import * class _1_(Predicate): @@ -159,5 +166,38 @@ class SelectorsTC(TestCase): self.assertEqual(s3(None), 0) self.assertEqual(self.count, 8) +@contextmanager +def prepended_syspath(path): + sys.path.insert(0, path) + yield + sys.path = sys.path[1:] + +class RegistryStoreTC(TestCase): + + def test_autoload(self): + store = RegistryStore() + store.setdefault('zereg') + with prepended_syspath(self.datadir): + store.register_objects([self.datapath('regobjects.py'), + self.datapath('regobjects2.py')]) + self.assertEqual(['zereg'], store.keys()) + self.assertEqual(set(('appobject1', 'appobject2', 'appobject3')), + set(store['zereg'])) + + +class RegistrableInstanceTC(TestCase): + + def test_instance_modulename(self): + # no inheritance + obj = RegistrableInstance() + self.assertEqual(obj.__module__, 'unittest_registry') + # with inheritance from another python file + with prepended_syspath(self.datadir): + from regobjects2 import instance, MyRegistrableInstance + instance2 = MyRegistrableInstance() + self.assertEqual(instance.__module__, 'regobjects2') + self.assertEqual(instance2.__module__, 'unittest_registry') + + if __name__ == '__main__': unittest_main() |