[testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 31 Mar 2010 12:26:07 +0200
branchstable
changeset 5102 04c84959cd85
parent 5101 0d01acee1f0a
child 5103 2b242604fc42
[testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
devtools/testlib.py
--- a/devtools/testlib.py	Wed Mar 31 12:21:39 2010 +0200
+++ b/devtools/testlib.py	Wed Mar 31 12:26:07 2010 +0200
@@ -12,6 +12,7 @@
 import re
 from urllib import unquote
 from math import log
+from contextlib import contextmanager
 
 import simplejson
 
@@ -354,6 +355,15 @@
     def entity(self, rql, args=None, eidkey=None, req=None):
         return self.execute(rql, args, eidkey, req=req).get_entity(0, 0)
 
+    @contextmanager
+    def temporary_appobjects(self, *appobjects):
+        self.vreg._loadedmods.setdefault(self.__module__, {})
+        for obj in appobjects:
+            self.vreg.register(obj)
+        yield
+        for obj in appobjects:
+            self.vreg.unregister(obj)
+
     # vregistry inspection utilities ###########################################
 
     def pviews(self, req, rset):