Commits

Anonymous committed 9a7ff0c

handling modules

  • Participants
  • Parent commits 6025cb4
  • Branches alfajor-integration

Comments (0)

Files changed (1)

File flaskext/testing.py

 
         self.app = app
         
-        #self.client = APIClient()
-        #self.client.configure_in_scope()
+        self.client = APIClient()
+        self.client.configure_in_scope()
         
         #self.browser = WebBrowser()
         #self.browser.configure_in_scope()
     
-        self.client = self.app.test_client()
+        #self.client = self.app.test_client()
         self.fixtures = _GlobalFixtures()
 
     def push(self):
             self.pop()
 
            
+class _TestModule(object):
+
+    def __init__(self, parent):
+
+        self._parent = parent
+        self._setup_funcs = []
+        self._teardown_funcs = []
+
+    def setup(self, func):
+        self._setup_funcs.append(func)
+        return func
+
+    def teardown(self, func):
+        self._teardown_funcs.append(func)
+        return func
+
+    def with_context(self, func):
+
+        @functools.wraps(func)
+        def decorate():
+
+            app = self._parent.create_app()
+
+            _app_ctx = None
+            _test_ctx = None
+
+            def _setup():
+
+                global _app_ctx, _test_ctx
+
+                _app_ctx = app.test_request_context()
+                _app_ctx.push()
+
+                _test_ctx = _TestContext(app)
+                _test_ctx.push()
+
+                for setup_func in self._parent._setup_funcs:
+                    setup_func()
+
+                for setup_func in self._setup_funcs:
+                    setup_func()
+
+            def _teardown():
+                global _app_ctx, _test_ctx
+                
+                for teardown_func in self._teardown_funcs:
+                    teardown_func()
+
+                for teardown_func in self._parent._teardown_funcs:
+                    teardown_func()
+
+                _test_ctx.pop()
+                _app_ctx.pop()
+                
+            _setup()
+            try:
+                func()
+            finally:
+                _teardown()
+                
+        return decorate
+
 class TestCase(unittest.TestCase):
     
     def __call__(self, result=None):
         self._teardown_funcs.append(func)
         return func
 
+    def register_module(self):
+        return _TestModule(self)
+
     def with_context(self, func):
 
         @functools.wraps(func)