<s> from flask import Flask , Response , Request <EOL> import flask <EOL> from werkzeug . test import EnvironBuilder <EOL> from nose . tools import raises <EOL> from . resolver import Pushrod , pushrod_view <EOL> from . renderers . base import renderer , UnrenderedResponse , RendererNotFound <EOL> from . renderers . json import json_renderer <EOL> from . renderers . jinja2 import jinja2_renderer <EOL> from unittest import TestCase <EOL> import json <EOL> import logging <EOL> test_response_str = """<STR_LIT>""" <EOL> test_response = eval ( test_response_str ) <EOL> @ renderer ( '<STR_LIT>' , normalize = False ) <EOL> def repr_renderer ( unrendered , ** kwargs ) : <EOL> return unrendered . rendered ( repr ( unrendered . response ) , "<STR_LIT>" ) <EOL> def test_logging_no_app ( ) : <EOL> pushrod = Pushrod ( ) <EOL> assert pushrod . logger == logging <EOL> class PushrodTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> app = Flask ( __name__ , <EOL> template_folder = '<STR_LIT>' , <EOL> ) <EOL> app . testing = True <EOL> pushrod = Pushrod ( app ) <EOL> pushrod . register_renderer ( repr_renderer ) <EOL> pushrod . default_renderer = repr_renderer <EOL> self . app = app <EOL> self . pushrod = app . extensions [ '<STR_LIT>' ] <EOL> self . client = app . test_client ( ) <EOL> app_context = app . app_context ( ) <EOL> app_context . push ( ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . app_contexts = [ ] <EOL> self . app_contexts . append ( app_context ) <EOL> def tearDown ( self ) : <EOL> self . app_contexts . pop ( ) . pop ( ) <EOL> def setup_method ( self , method ) : <EOL> return self . setUp ( ) <EOL> def teardown_method ( self , method ) : <EOL> return self . tearDown ( ) <EOL> class PushrodResolverTestCase ( PushrodTestCase ) : <EOL> def test_raw_dict_response_data ( self ) : <EOL> @ self . app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def test_view_raw_dict ( ) : <EOL> return test_response <EOL> test_view_raw_dict_response = self . client . get ( "<STR_LIT>" ) <EOL> assert test_view_raw_dict_response . status_code == <NUM_LIT:200> <EOL> assert test_view_raw_dict_response . data == repr ( test_response ) <EOL> def test_response_data ( self ) : <EOL> @ self . app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def test_view_200_view ( ) : <EOL> return UnrenderedResponse ( test_response ) <EOL> @ self . app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def test_view_404_view ( ) : <EOL> return UnrenderedResponse ( test_response , <NUM_LIT> ) <EOL> test_view_200_response = self . client . get ( "<STR_LIT>" ) <EOL> assert test_view_200_response . status_code == <NUM_LIT:200> <EOL> assert test_view_200_response . data == repr ( test_response ) <EOL> test_view_404_response = self . client . get ( "<STR_LIT>" ) <EOL> assert test_view_404_response . status_code == <NUM_LIT> <EOL> assert test_view_404_response . data == repr ( test_response ) <EOL> def test_response_bypass ( self ) : <EOL> @ self . app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def test_raw_string_view ( ) : <EOL> return "<STR_LIT:test>" <EOL> @ self . app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def test_regular_response_view ( ) : <EOL> return Response ( "<STR_LIT:test>" ) <EOL> test_raw_string_response = self . client . get ( "<STR_LIT>" ) <EOL> assert test_raw_string_response . status_code == <NUM_LIT:200> <EOL> assert test_raw_string_response . data == "<STR_LIT:test>" <EOL> test_regular_response_response = self . client . get ( "<STR_LIT>" ) <EOL> assert test_regular_response_response . status_code == <NUM_LIT:200> <EOL> assert test_regular_response_response . data == "<STR_LIT:test>" <EOL> @ raises ( RendererNotFound ) <EOL> def test_renderer_not_found ( self ) : <EOL> @ self . app . route ( "<STR_LIT:/>" ) <EOL> @ pushrod_view ( ) <EOL> def test_renderer_not_found_view ( ) : <EOL> return { } <EOL> response = self . client . get ( "<STR_LIT>" ) <EOL> assert response . status_code == <NUM_LIT> , "<STR_LIT>" <EOL> with self . app . test_request_context ( "<STR_LIT>" ) : <EOL> self . pushrod . render_response ( test_renderer_not_found_view ( ) , "<STR_LIT:none>" ) <EOL> def test_constructor_renderer_registration ( self ) : <EOL> get_request_obj = lambda * args , ** kwargs : Request ( EnvironBuilder ( * args , ** kwargs ) . get_environ ( ) ) <EOL> pushrod = Pushrod ( renderers = [ '<STR_LIT>' ] ) <EOL> assert pushrod . default_renderer == None <EOL> assert '<STR_LIT:html>' not in pushrod . named_renderers <EOL> assert '<STR_LIT>' not in pushrod . mime_type_renderers <EOL> assert '<STR_LIT>' in pushrod . named_renderers <EOL> assert '<STR_LIT:application/json>' in pushrod . mime_type_renderers <EOL> assert pushrod . named_renderers [ '<STR_LIT>' ] == json_renderer <EOL> assert pushrod . mime_type_renderers [ '<STR_LIT:application/json>' ] == json_renderer <EOL> assert pushrod . get_renderers_for_request ( get_request_obj ( "<STR_LIT:/>" ) ) == [ ] <EOL> assert pushrod . get_renderers_for_request ( get_request_obj ( "<STR_LIT>" ) ) == [ json_renderer ] <EOL> pushrod = Pushrod ( renderers = [ repr_renderer ] ) <EOL> assert '<STR_LIT:html>' not in pushrod . named_renderers <EOL> assert '<STR_LIT>' not in pushrod . named_renderers <EOL> assert '<STR_LIT>' not in pushrod . named_renderers <EOL> assert '<STR_LIT:application/json>' not in pushrod . mime_type_renderers <EOL> assert '<STR_LIT>' in pushrod . named_renderers <EOL> assert pushrod . named_renderers [ '<STR_LIT>' ] == repr_renderer <EOL> assert pushrod . get_renderers_for_request ( get_request_obj ( "<STR_LIT:/>" ) ) == [ ] <EOL> assert pushrod . get_renderers_for_request ( get_request_obj ( "<STR_LIT>" ) ) == [ repr_renderer ] <EOL> pushrod = Pushrod ( renderers = [ repr_renderer ] , default_renderer = '<STR_LIT>' ) <EOL> assert pushrod . get_renderers_for_request ( get_request_obj ( "<STR_LIT:/>" ) ) == [ repr_renderer ] <EOL> with self . app . app_context ( ) : <EOL> Pushrod ( renderers = [ ] ) <EOL> Pushrod ( self . app , renderers = [ ] ) <EOL> Pushrod ( self . app , default_renderer = repr_renderer ) <EOL> @ raises ( TypeError ) <EOL> def test_register_invalid_renderer ( self ) : <EOL> def dummy ( ) : <EOL> pass <EOL> self . pushrod . register_renderer ( dummy ) <EOL> def test_render_tuple_response ( self ) : <EOL> response = self . pushrod . render_response ( ( <EOL> { u'<STR_LIT>' : u"<STR_LIT>" } , <EOL> <NUM_LIT> , <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } , <EOL> ) , json_renderer ) <EOL> assert response . status_code == <NUM_LIT> <EOL> assert '<STR_LIT>' in response . headers <EOL> assert response . headers [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> response_json = json . loads ( response . data ) <EOL> assert u'<STR_LIT>' in response_json <EOL> assert response_json [ u'<STR_LIT>' ] == u"<STR_LIT>" <EOL> class PushrodNormalizerTestCase ( PushrodTestCase ) : <EOL> def test_basestring_normalizer ( self ) : <EOL> from_unicode = self . pushrod . normalize ( u"<STR_LIT>" ) <EOL> from_str = self . pushrod . normalize ( "<STR_LIT>" ) <EOL> assert from_unicode == u"<STR_LIT>" <EOL> assert from_str == u"<STR_LIT>" <EOL> def test_number_normalizer ( self ) : <EOL> assert self . pushrod . normalize ( <NUM_LIT:10> ) == <NUM_LIT:10> <EOL> assert self . pushrod . normalize ( <NUM_LIT> L ) == <NUM_LIT> L <EOL> assert self . pushrod . normalize ( <NUM_LIT> ) == <NUM_LIT> <EOL> def test_bool_normalizer ( self ) : <EOL> assert self . pushrod . normalize ( True ) == True <EOL> def test_none_normalizer ( self ) : <EOL> assert self . pushrod . normalize ( None ) == None <EOL> def test_iterable_normalizer ( self ) : <EOL> in_list = [ "<STR_LIT>" , <NUM_LIT> ] <EOL> from_list = self . pushrod . normalize ( in_list ) <EOL> from_tuple = self . pushrod . normalize ( tuple ( in_list ) ) <EOL> assert from_list == in_list <EOL> assert from_tuple == in_list <EOL> def test_dict_normalizer ( self ) : <EOL> in_dict = { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> } <EOL> assert self . pushrod . normalize ( in_dict ) == in_dict <EOL> def test_delegate_normalizer ( self ) : <EOL> class MyClass ( object ) : <EOL> __pushrod_field__ = "<STR_LIT:value>" <EOL> def __init__ ( self ) : <EOL> self . value = <NUM_LIT> <EOL> my_instance = MyClass ( ) <EOL> assert self . pushrod . normalize ( my_instance ) == self . pushrod . normalize ( my_instance . value ) <EOL> def test_no_normalizer ( self ) : <EOL> class MyClass ( object ) : <EOL> pass <EOL> assert self . pushrod . normalize ( MyClass ( ) ) == NotImplemented <EOL> self . pushrod . normalizers [ MyClass ] = lambda x , pushrod : NotImplemented <EOL> assert self . pushrod . normalize ( MyClass ( ) ) == NotImplemented <EOL> def test_dict_value_no_normalizer ( self ) : <EOL> class MyClass ( object ) : <EOL> pass <EOL> assert self . pushrod . normalize ( { u"<STR_LIT>" : u"<STR_LIT>" , u"<STR_LIT>" : MyClass ( ) , u"<STR_LIT>" : <NUM_LIT:6> } ) == { u"<STR_LIT>" : u"<STR_LIT>" , u"<STR_LIT>" : <NUM_LIT:6> } <EOL> def test_normalizer_method ( self ) : <EOL> class MyClass ( object ) : <EOL> def __pushrod_normalize__ ( self , pushrod ) : <EOL> return u"<STR_LIT>" <EOL> assert self . pushrod . normalize ( MyClass ( ) ) == u"<STR_LIT>" <EOL> def test_normalizer_generated_dict ( self ) : <EOL> class MyClass ( object ) : <EOL> __pushrod_fields__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , one , two , three ) : <EOL> self . one = one <EOL> self . two = two <EOL> self . three = three <EOL> assert self . pushrod . normalize ( MyClass ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) == { u'<STR_LIT>' : u'<STR_LIT>' , u'<STR_LIT>' : u'<STR_LIT>' } <EOL> def test_normalizer_override ( self ) : <EOL> self . pushrod . normalizer_overrides [ int ] . append ( lambda x , pushrod : unicode ( x ) if x > <NUM_LIT:0> else NotImplemented ) <EOL> assert self . pushrod . normalize ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> assert self . pushrod . normalize ( <NUM_LIT:1> ) == u"<STR_LIT:1>" <EOL> class PushrodRendererTestCase ( PushrodTestCase ) : <EOL> def test_json_renderer ( self ) : <EOL> regular = json . dumps ( test_response ) <EOL> rendered = self . pushrod . render_response ( <EOL> test_response , json_renderer ) <EOL> assert regular == rendered . data <EOL> json . loads ( rendered . data ) <EOL> @ raises ( RendererNotFound ) <EOL> def test_jinja_renderer_no_template ( self ) : <EOL> self . pushrod . render_response ( <EOL> test_response , jinja2_renderer ) <EOL> def test_jinja_renderer_no_template_fallback ( self ) : <EOL> self . pushrod . render_response ( <EOL> test_response , [ jinja2_renderer , json_renderer ] ) <EOL> def test_jinja_renderer ( self ) : <EOL> regular = test_response_str <EOL> with self . app . test_request_context ( ) : <EOL> flask . current_app <EOL> rendered = self . pushrod . render_response ( <EOL> test_response , jinja2_renderer , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert regular == rendered . data , "<STR_LIT>" % ( rendered . data , regular ) <EOL> assert eval ( rendered . data ) == test_response </s>
