Commits

Anonymous committed a7cf22a

tests broken, but refactoring started

Comments (0)

Files changed (1)

flaskext/testing.py

 import unittest
 import twill
 import simplejson
+import nose.tools
 
 from werkzeug import cached_property
 from werkzeug import LocalStack, LocalProxy
 
 from flask import Flask
 
-from nose import with_setup
+# add more nose  tools
+
+from nose.tools import ok_, eq_, make_decorator, raises, set_trace, \
+    timed, assert_equal, assert_not_equal
+
+__all__ = nose.tools.__all__
+__all__ += ["assert_200",]
+
 
 from alfajor import APIClient, WebBrowser
 
+def assert_context(name, value):
+    """
+    Checks if given name exists in the template context
+    and equals the given value.
+
+    :versionadded: 0.2
+    :param name: name of context variable
+    :param value: value to check against
+    """
+
+def assert_redirects(response, location):
+    """
+    Checks if response is an HTTP redirect to the 
+    given location.
+
+    :param response: Flask response
+    :param location: relative URL (i.e. without **http://localhost**)
+    """
+    assert response.status_code in (301, 302)
+    assert response.location == "http://localhost" + location
+ 
+def assert_status(response, status_code):
+    """
+    Helper method to check matching response status.
+    
+    :param response: Flask response
+    :param status_code: response status code (e.g. 200)
+    """
+    assert response.status_code == status_code
+
+def assert200(response):
+    """
+    Checks if response status code is 200 
+
+    :param response: Flask response
+    """
+
+    assert assert_status(response, 200)
+
+
+def assert403(self, response):
+    """
+    Checks if response status code is 403
+
+    :versionadded: 0.2
+    :param response: Flask response
+    """
+
+    self.assertStatus(response, 403)
+
+assert_403 = assert403
+
+def assert404(self, response):
+    """
+    Checks if response status code is 404
+
+    :param response: Flask response
+    """
+
+    self.assertStatus(response, 404)
+
+assert_404 = assert404
+
+def assert405(self, response):
+    """
+    Checks if response status code is 405
+
+    :versionadded: 0.2
+    :param response: Flask response
+    """
+
+    self.assertStatus(response, 405)
+
+assert_405 = assert405
+
+
 try:
     # we'll use signals for template-related tests if
     # available in this version of Flask
 
         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):
            
 class _TestModule(object):
 
-    def __init__(self, parent):
+    def __init__(self, parent=None):
 
         self._parent = parent
         self._setup_funcs = []
         @functools.wraps(func)
         def decorate():
 
-            app = self._parent.create_app()
+            if self._parent:
+                app = self._parent.create_app()
+            else:
+                raise RuntimeError, "Not parent node nor parent set"
 
-            _app_ctx = None
-            _test_ctx = None
+            _app_ctx = app.test_request_context()
+            _app_ctx.push()
 
-            def _setup():
+            _test_ctx = _TestContext(app)
+            _test_ctx.push()
 
-                global _app_ctx, _test_ctx
-
-                _app_ctx = app.test_request_context()
-                _app_ctx.push()
-
-                _test_ctx = _TestContext(app)
-                _test_ctx.push()
-
+            if self._parent:
                 for setup_func in self._parent._setup_funcs:
                     setup_func()
 
-                for setup_func in self._setup_funcs:
-                    setup_func()
+            for setup_func in self._setup_funcs:
+                setup_func()
 
-            def _teardown():
-                global _app_ctx, _test_ctx
+            try:
+                return func()
                 
+            finally:
                 for teardown_func in self._teardown_funcs:
                     teardown_func()
 
-                for teardown_func in self._parent._teardown_funcs:
-                    teardown_func()
+                if self._parent:
+                    for teardown_func in self._parent._teardown_funcs:
+                        teardown_func()
 
                 _test_ctx.pop()
                 _app_ctx.pop()
                 
-            _setup()
-            try:
-                func()
-            finally:
-                _teardown()
-                
         return decorate
 
+class Testing(_TestModule):
+
+    """
+    Works like this::
+
+        from flaskext.testing import testing, browser, client
+
+        testing = Testing(create_app)
+
+    or::
+        
+        testing = Testing()
+        testing.init_app(app)
+
+    uses decorators::
+
+        @testing.with_context
+        def test_index_page():
+        
+            response = client.get("/")
+            testing.assert_202(response)
+
+    or::
+        
+        @testing.setup
+        def create_db():
+            db.create_all()
+
+        @testing.teardown
+        def drop_db():
+            db.drop_all()
+            db.session.remove()
+
+        @testing.with_context
+        def test_index_page():
+
+            browser.open("/")
+            assert browser.status_code == 200
+            assert browser.document['#A'].tag == 'dl'
+            
+
+    """
+    def __init__(self, app):
+        
+        self.init_app(app)
+
+        super(Testing, self).__init__()
+
+    def init_app(self, app):
+
+        if isinstance(app, Flask):
+            self.create_app = lambda: app
+
+        else:
+            self.create_app = app
+
+    def register_module(self):
+        return _TestModule(self)
+
+# old unit-test based classes, for backward compatibility
+
 class TestCase(unittest.TestCase):
     
     def __call__(self, result=None):
                                  self.twill_port,
                                  url)
 
-class Testing(TestCase):
 
-    """
-    Works like this::
-
-        from flaskext.testing import testing, browser, client
-
-        testing = Testing(create_app)
-
-    or::
-        
-        testing = Testing()
-        testing.init_app(app)
-
-    uses decorators::
-
-        @testing.with_context
-        def test_index_page():
-        
-            response = client.get("/")
-            testing.assert_202(response)
-
-    or::
-        
-        @testing.setup
-        def create_db():
-            db.create_all()
-
-        @testing.teardown
-        def drop_db():
-            db.drop_all()
-            db.session.remove()
-
-        @testing.with_context
-        def test_index_page():
-
-            browser.open("/")
-            assert browser.status_code == 200
-            assert browser.document['#A'].tag == 'dl'
-            
-
-    """
-    def __init__(self, app):
-        
-        self.init_app(app)
-        self._setup_funcs = []
-        self._teardown_funcs = []
-        
-    def init_app(self, app):
-
-        if isinstance(app, Flask):
-            self.create_app = lambda: app
-
-        else:
-            self.create_app = app
-
-    def setup(self, func):
-        self._setup_funcs.append(func)
-        return func
-
-    def teardown(self, func):
-        self._teardown_funcs.append(func)
-        return func
-
-    def register_module(self):
-        return _TestModule(self)
-
-    def with_context(self, func):
-
-        @functools.wraps(func)
-        def decorate():
-
-            app = self.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._setup_funcs:
-                    setup_func()
-
-            def _teardown():
-                global _app_ctx, _test_ctx
-                
-                for teardown_func in self._teardown_funcs:
-                    teardown_func()
-
-                _test_ctx.pop()
-                _app_ctx.pop()
-                
-            _setup()
-            try:
-                func()
-            finally:
-                _teardown()
-                
-        return decorate
-