File diff 8b3c91575b79 → f03d215e52f1
conntrackt/tests/helpers.py
Show inline comments
 
@@ -184,3 +184,55 @@ class FakeMessages(object):
 
        """
 

	
 
        self.messages.append(message)
 

	
 

	
 
class RenderTestMixin(object):
 
    """
 
    Mixin class for testing that response rendering works correctly.
 

	
 
    In order to use this mixin, add it the left side of the class list the test
 
    is inheriting from, and configure it providing the following class options:
 

	
 
    view_class - Class used for the CBV that will be tested.
 
    view_function - View function that will be tested.
 
    render_test_view_args - Positional arguments to pass to the view.
 
    render_test_view_kwargs - Keyword arguments to pass to the view.
 
    """
 

	
 
    view_class = None
 
    view_function = None
 
    render_test_view_args = ()
 
    render_test_view_kwargs = {}
 

	
 
    def __init__(self, *args, **kwargs):
 
        """
 
        Initialises the mixin. Takes care of some basic validation of
 
        passed configuration options.
 
        """
 

	
 
        super(RenderTestMixin, self).__init__(*args, **kwargs)
 

	
 
        if self.view_class is None and self.view_function is None:
 
            raise ValueError("Render test mixin configured improperly - no CBV class or function was supplied via parameters 'view_class' or 'view_function'.")
 

	
 
        if type(self.render_test_view_kwargs) is not dict:
 
            raise ValueError("Render test mixin configured improperly - parameter 'render_test_view_kwargs' must be a dictionary.")
 

	
 
        if type(self.render_test_view_args) is not tuple:
 
            raise ValueError("Render test mixin configured improperly - parameter 'render_test_view_args' must be a tuple.")
 

	
 
    def test_no_render_exception_thrown_on_GET(self):
 

	
 
        # Get the view.
 
        if self.view_class is not None:
 
            view = self.view_class.as_view()
 
        elif self.view_function is not None:
 
            view = self.view_function
 

	
 
        # Grab the response.
 
        args = self.render_test_view_args
 
        kwargs = self.render_test_view_kwargs
 
        response = generate_get_response(view, *args, **kwargs)
 

	
 
        # Should not raise.
 
        response.render()