<s> import time <EOL> import unittest <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . http import HttpResponse <EOL> from django . test import TestCase , RequestFactory <EOL> from django . utils import simplejson <EOL> from django . views . generic import View , TemplateView , RedirectView <EOL> class SimpleView ( View ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> class SimplePostView ( SimpleView ) : <EOL> post = SimpleView . get <EOL> class CustomizableView ( SimpleView ) : <EOL> parameter = { } <EOL> def decorator ( view ) : <EOL> view . is_decorated = True <EOL> return view <EOL> class DecoratedDispatchView ( SimpleView ) : <EOL> @ decorator <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( DecoratedDispatchView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class AboutTemplateView ( TemplateView ) : <EOL> def get ( self , request ) : <EOL> return self . render_to_response ( { } ) <EOL> def get_template_names ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> class AboutTemplateAttributeView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request ) : <EOL> return self . render_to_response ( context = { } ) <EOL> class InstanceView ( View ) : <EOL> def get ( self , request ) : <EOL> return self <EOL> class ViewTest ( unittest . TestCase ) : <EOL> rf = RequestFactory ( ) <EOL> def _assert_simple ( self , response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . content , '<STR_LIT>' ) <EOL> def test_no_init_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> view = SimpleView ( key = '<STR_LIT:value>' ) . as_view ( ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> def test_no_init_args ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> view = SimpleView . as_view ( '<STR_LIT:value>' ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except TypeError : <EOL> pass <EOL> def test_pathological_http_method ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( SimpleView . as_view ( ) ( <EOL> self . rf . get ( '<STR_LIT:/>' , REQUEST_METHOD = '<STR_LIT>' ) <EOL> ) . status_code , <NUM_LIT> ) <EOL> def test_get_only ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _assert_simple ( SimpleView . as_view ( ) ( self . rf . get ( '<STR_LIT:/>' ) ) ) <EOL> self . assertEqual ( SimpleView . as_view ( ) ( self . rf . post ( '<STR_LIT:/>' ) ) . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( SimpleView . as_view ( ) ( <EOL> self . rf . get ( '<STR_LIT:/>' , REQUEST_METHOD = '<STR_LIT>' ) <EOL> ) . status_code , <NUM_LIT> ) <EOL> def test_get_and_post ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _assert_simple ( SimplePostView . as_view ( ) ( self . rf . get ( '<STR_LIT:/>' ) ) ) <EOL> self . _assert_simple ( SimplePostView . as_view ( ) ( self . rf . post ( '<STR_LIT:/>' ) ) ) <EOL> self . assertEqual ( SimplePostView . as_view ( ) ( <EOL> self . rf . get ( '<STR_LIT:/>' , REQUEST_METHOD = '<STR_LIT>' ) <EOL> ) . status_code , <NUM_LIT> ) <EOL> def test_invalid_keyword_argument ( self ) : <EOL> """<STR_LIT>""" <EOL> for method in SimpleView . http_method_names : <EOL> kwargs = dict ( ( ( method , "<STR_LIT:value>" ) , ) ) <EOL> self . assertRaises ( TypeError , SimpleView . as_view , ** kwargs ) <EOL> CustomizableView . as_view ( parameter = "<STR_LIT:value>" ) <EOL> self . assertRaises ( TypeError , CustomizableView . as_view , foobar = "<STR_LIT:value>" ) <EOL> def test_calling_more_than_once ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . rf . get ( '<STR_LIT:/>' ) <EOL> view = InstanceView . as_view ( ) <EOL> self . assertNotEqual ( view ( request ) , view ( request ) ) <EOL> def test_class_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( SimpleView . __doc__ , SimpleView . as_view ( ) . __doc__ ) <EOL> self . assertEqual ( SimpleView . __name__ , SimpleView . as_view ( ) . __name__ ) <EOL> self . assertEqual ( SimpleView . __module__ , SimpleView . as_view ( ) . __module__ ) <EOL> def test_dispatch_decoration ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( DecoratedDispatchView . as_view ( ) . is_decorated ) <EOL> class TemplateViewTest ( TestCase ) : <EOL> urls = '<STR_LIT>' <EOL> rf = RequestFactory ( ) <EOL> def _assert_about ( self , response ) : <EOL> response . render ( ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_get ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _assert_about ( AboutTemplateView . as_view ( ) ( self . rf . get ( '<STR_LIT>' ) ) ) <EOL> def test_get_template_attribute ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _assert_about ( AboutTemplateAttributeView . as_view ( ) ( self . rf . get ( '<STR_LIT>' ) ) ) <EOL> def test_get_generic_template ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _assert_about ( TemplateView . as_view ( template_name = '<STR_LIT>' ) ( self . rf . get ( '<STR_LIT>' ) ) ) <EOL> def test_template_name_required ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( ImproperlyConfigured , self . client . get , '<STR_LIT>' ) <EOL> def test_template_params ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> def test_extra_template_params ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> self . assertEqual ( response . context [ '<STR_LIT:key>' ] , '<STR_LIT:value>' ) <EOL> def test_cached_views ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> time . sleep ( <NUM_LIT:1.0> ) <EOL> response2 = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response2 . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . content , response2 . content ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> response2 = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response2 . status_code , <NUM_LIT:200> ) <EOL> self . assertNotEqual ( response . content , response2 . content ) <EOL> class RedirectViewTest ( unittest . TestCase ) : <EOL> rf = RequestFactory ( ) <EOL> def test_no_url ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( ) ( self . rf . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_permanaent_redirect ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_temporary_redirect ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' , permanent = False ) ( self . rf . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_include_args ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' , query_string = True ) ( self . rf . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_parameter_substitution ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . get ( '<STR_LIT>' ) , object_id = <NUM_LIT> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_redirect_POST ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . post ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_redirect_HEAD ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . head ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_redirect_OPTIONS ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . options ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_redirect_PUT ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . put ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_redirect_DELETE ( self ) : <EOL> "<STR_LIT>" <EOL> response = RedirectView . as_view ( url = '<STR_LIT>' ) ( self . rf . delete ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) </s>
