<s> import os <EOL> import json <EOL> import shutil <EOL> import tempfile <EOL> import unittest <EOL> from nose import SkipTest <EOL> from six import BytesIO <EOL> from swift import gettext_ as _ <EOL> from swift . common . swob import Request , Response <EOL> try : <EOL> from swift . common . middleware import xprofile <EOL> from swift . common . middleware . xprofile import ProfileMiddleware <EOL> from swift . common . middleware . x_profile . exceptions import ( <EOL> MethodNotAllowed , NotFoundException , ODFLIBNotInstalled , <EOL> PLOTLIBNotInstalled ) <EOL> from swift . common . middleware . x_profile . html_viewer import ( <EOL> HTMLViewer , PLOTLIB_INSTALLED ) <EOL> from swift . common . middleware . x_profile . profile_model import ( <EOL> ODFLIB_INSTALLED , ProfileLog , Stats2 ) <EOL> except ImportError : <EOL> xprofile = None <EOL> class FakeApp ( object ) : <EOL> def __call__ ( self , env , start_response ) : <EOL> req = Request ( env ) <EOL> return Response ( request = req , body = '<STR_LIT>' ) ( <EOL> env , start_response ) <EOL> class TestXProfile ( unittest . TestCase ) : <EOL> def test_get_profiler ( self ) : <EOL> if xprofile is None : <EOL> raise SkipTest <EOL> self . assertTrue ( xprofile . get_profiler ( '<STR_LIT>' ) is not None ) <EOL> self . assertTrue ( xprofile . get_profiler ( '<STR_LIT>' ) <EOL> is not None ) <EOL> class TestProfilers ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if xprofile is None : <EOL> raise SkipTest <EOL> self . profilers = [ xprofile . get_profiler ( '<STR_LIT>' ) , <EOL> xprofile . get_profiler ( '<STR_LIT>' ) ] <EOL> def fake_func ( self , * args , ** kw ) : <EOL> return len ( args ) + len ( kw ) <EOL> def test_runcall ( self ) : <EOL> for p in self . profilers : <EOL> v = p . runcall ( self . fake_func , '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( v , <NUM_LIT:3> ) <EOL> def test_runctx ( self ) : <EOL> for p in self . profilers : <EOL> p . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> p . snapshot_stats ( ) <EOL> self . assertTrue ( p . stats is not None ) <EOL> self . assertTrue ( len ( p . stats . keys ( ) ) > <NUM_LIT:0> ) <EOL> class TestProfileMiddleware ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if xprofile is None : <EOL> raise SkipTest <EOL> self . got_statuses = [ ] <EOL> self . app = ProfileMiddleware ( FakeApp , { } ) <EOL> self . tempdir = os . path . dirname ( self . app . log_filename_prefix ) <EOL> self . pids = [ '<STR_LIT>' , '<STR_LIT>' , str ( os . getpid ( ) ) ] <EOL> profiler = xprofile . get_profiler ( '<STR_LIT>' ) <EOL> for pid in self . pids : <EOL> path = self . app . log_filename_prefix + pid <EOL> profiler . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> profiler . dump_stats ( path ) <EOL> profiler . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> profiler . dump_stats ( path + '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . tempdir , ignore_errors = True ) <EOL> def get_app ( self , app , global_conf , ** local_conf ) : <EOL> factory = xprofile . filter_factory ( global_conf , ** local_conf ) <EOL> return factory ( app ) <EOL> def start_response ( self , status , headers ) : <EOL> self . got_statuses = [ status ] <EOL> self . headers = headers <EOL> def test_combine_body_qs ( self ) : <EOL> body = ( b"<STR_LIT>" <EOL> b"<STR_LIT>" ) <EOL> wsgi_input = BytesIO ( body ) <EOL> environ = { '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : wsgi_input } <EOL> req = Request . blank ( '<STR_LIT>' , environ = environ ) <EOL> query_dict = self . app . _combine_body_qs ( req ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT:all>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT:time>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT:1>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( query_dict [ '<STR_LIT>' ] , [ '<STR_LIT:default>' ] ) <EOL> def test_call ( self ) : <EOL> body = b"<STR_LIT>" <EOL> wsgi_input = BytesIO ( body + b'<STR_LIT>' ) <EOL> environ = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : wsgi_input } <EOL> resp = self . app ( environ , self . start_response ) <EOL> self . assertTrue ( resp [ <NUM_LIT:0> ] . find ( '<STR_LIT>' ) > <NUM_LIT:0> , resp ) <EOL> self . assertEqual ( self . got_statuses , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> wsgi_input = BytesIO ( body + b'<STR_LIT>' ) <EOL> environ [ '<STR_LIT>' ] = wsgi_input <EOL> if PLOTLIB_INSTALLED : <EOL> resp = self . app ( environ , self . start_response ) <EOL> self . assertEqual ( self . got_statuses , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> else : <EOL> resp = self . app ( environ , self . start_response ) <EOL> self . assertEqual ( self . got_statuses , [ '<STR_LIT>' ] ) <EOL> wsgi_input = BytesIO ( body + '<STR_LIT>' ) <EOL> environ [ '<STR_LIT>' ] = wsgi_input <EOL> resp = self . app ( environ , self . start_response ) <EOL> self . assertEqual ( self . headers , [ ( '<STR_LIT>' , <EOL> HTMLViewer . format_dict [ '<STR_LIT:default>' ] ) ] ) <EOL> wsgi_input = BytesIO ( body + '<STR_LIT>' ) <EOL> environ [ '<STR_LIT>' ] = wsgi_input <EOL> resp = self . app ( environ , self . start_response ) <EOL> self . assertTrue ( self . headers == [ ( '<STR_LIT>' , <EOL> HTMLViewer . format_dict [ '<STR_LIT>' ] ) ] ) <EOL> env2 = environ . copy ( ) <EOL> env2 [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> resp = self . app ( env2 , self . start_response ) <EOL> self . assertEqual ( self . got_statuses , [ '<STR_LIT>' ] , resp ) <EOL> wsgi_input = BytesIO ( body + b'<STR_LIT>' ) <EOL> environ [ '<STR_LIT>' ] = wsgi_input <EOL> resp = self . app ( environ , self . start_response ) <EOL> self . assertEqual ( self . got_statuses , [ '<STR_LIT>' ] , resp ) <EOL> wsgi_input = BytesIO ( body + b'<STR_LIT>' ) <EOL> environ [ '<STR_LIT>' ] = wsgi_input <EOL> resp = self . app ( environ , self . start_response ) <EOL> if ODFLIB_INSTALLED : <EOL> self . assertEqual ( self . headers , [ ( '<STR_LIT>' , <EOL> HTMLViewer . format_dict [ '<STR_LIT>' ] ) ] ) <EOL> else : <EOL> self . assertEqual ( self . got_statuses , [ '<STR_LIT>' ] ) <EOL> def test_dump_checkpoint ( self ) : <EOL> self . app . dump_checkpoint ( ) <EOL> self . assertTrue ( self . app . last_dump_at is not None ) <EOL> def test_renew_profile ( self ) : <EOL> old_profiler = self . app . profiler <EOL> self . app . renew_profile ( ) <EOL> new_profiler = self . app . profiler <EOL> self . assertTrue ( old_profiler != new_profiler ) <EOL> class Test_profile_log ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if xprofile is None : <EOL> raise SkipTest <EOL> self . dir1 = tempfile . mkdtemp ( ) <EOL> self . log_filename_prefix1 = self . dir1 + '<STR_LIT>' <EOL> self . profile_log1 = ProfileLog ( self . log_filename_prefix1 , False ) <EOL> self . pids1 = [ '<STR_LIT>' , '<STR_LIT>' , str ( os . getpid ( ) ) ] <EOL> profiler1 = xprofile . get_profiler ( '<STR_LIT>' ) <EOL> for pid in self . pids1 : <EOL> profiler1 . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> self . profile_log1 . dump_profile ( profiler1 , pid ) <EOL> self . dir2 = tempfile . mkdtemp ( ) <EOL> self . log_filename_prefix2 = self . dir2 + '<STR_LIT>' <EOL> self . profile_log2 = ProfileLog ( self . log_filename_prefix2 , True ) <EOL> self . pids2 = [ '<STR_LIT>' , '<STR_LIT>' , str ( os . getpid ( ) ) ] <EOL> profiler2 = xprofile . get_profiler ( '<STR_LIT>' ) <EOL> for pid in self . pids2 : <EOL> profiler2 . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> self . profile_log2 . dump_profile ( profiler2 , pid ) <EOL> def tearDown ( self ) : <EOL> self . profile_log1 . clear ( '<STR_LIT:all>' ) <EOL> self . profile_log2 . clear ( '<STR_LIT:all>' ) <EOL> shutil . rmtree ( self . dir1 , ignore_errors = True ) <EOL> shutil . rmtree ( self . dir2 , ignore_errors = True ) <EOL> def test_get_all_pids ( self ) : <EOL> self . assertEqual ( self . profile_log1 . get_all_pids ( ) , <EOL> sorted ( self . pids1 , reverse = True ) ) <EOL> for pid in self . profile_log2 . get_all_pids ( ) : <EOL> self . assertTrue ( pid . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] in self . pids2 ) <EOL> def test_clear ( self ) : <EOL> self . profile_log1 . clear ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . exists ( self . log_filename_prefix1 + '<STR_LIT>' ) ) <EOL> self . profile_log1 . clear ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . exists ( self . log_filename_prefix1 + <EOL> str ( os . getpid ( ) ) ) ) <EOL> self . profile_log1 . clear ( '<STR_LIT:all>' ) <EOL> for pid in self . pids1 : <EOL> self . assertFalse ( os . path . exists ( self . log_filename_prefix1 + pid ) ) <EOL> self . profile_log2 . clear ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . exists ( self . log_filename_prefix2 + '<STR_LIT>' ) ) <EOL> self . profile_log2 . clear ( '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . exists ( self . log_filename_prefix2 + <EOL> str ( os . getpid ( ) ) ) ) <EOL> self . profile_log2 . clear ( '<STR_LIT:all>' ) <EOL> for pid in self . pids2 : <EOL> self . assertFalse ( os . path . exists ( self . log_filename_prefix2 + pid ) ) <EOL> def test_get_logfiles ( self ) : <EOL> log_files = self . profile_log1 . get_logfiles ( '<STR_LIT:all>' ) <EOL> self . assertEqual ( len ( log_files ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( log_files ) , len ( self . pids1 ) ) <EOL> log_files = self . profile_log1 . get_logfiles ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( log_files ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( log_files , [ self . log_filename_prefix1 <EOL> + str ( os . getpid ( ) ) ] ) <EOL> log_files = self . profile_log1 . get_logfiles ( self . pids1 [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( log_files ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( log_files , [ self . log_filename_prefix1 <EOL> + self . pids1 [ <NUM_LIT:0> ] ] ) <EOL> log_files = self . profile_log2 . get_logfiles ( '<STR_LIT:all>' ) <EOL> self . assertEqual ( len ( log_files ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( log_files ) , len ( self . pids2 ) ) <EOL> log_files = self . profile_log2 . get_logfiles ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( log_files ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( log_files [ <NUM_LIT:0> ] . find ( self . log_filename_prefix2 + <EOL> str ( os . getpid ( ) ) ) > - <NUM_LIT:1> ) <EOL> log_files = self . profile_log2 . get_logfiles ( self . pids2 [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( log_files ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( log_files [ <NUM_LIT:0> ] . find ( self . log_filename_prefix2 + <EOL> self . pids2 [ <NUM_LIT:0> ] ) > - <NUM_LIT:1> ) <EOL> def test_dump_profile ( self ) : <EOL> prof = xprofile . get_profiler ( '<STR_LIT>' ) <EOL> prof . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> prof . create_stats ( ) <EOL> pfn = self . profile_log1 . dump_profile ( prof , os . getpid ( ) ) <EOL> self . assertTrue ( os . path . exists ( pfn ) ) <EOL> os . remove ( pfn ) <EOL> pfn = self . profile_log2 . dump_profile ( prof , os . getpid ( ) ) <EOL> self . assertTrue ( os . path . exists ( pfn ) ) <EOL> os . remove ( pfn ) <EOL> class Test_html_viewer ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if xprofile is None : <EOL> raise SkipTest <EOL> self . app = ProfileMiddleware ( FakeApp , { } ) <EOL> self . log_files = [ ] <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . log_filename_prefix = self . tempdir + '<STR_LIT>' <EOL> self . profile_log = ProfileLog ( self . log_filename_prefix , False ) <EOL> self . pids = [ '<STR_LIT>' , '<STR_LIT>' , str ( os . getpid ( ) ) ] <EOL> profiler = xprofile . get_profiler ( '<STR_LIT>' ) <EOL> for pid in self . pids : <EOL> profiler . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> self . log_files . append ( self . profile_log . dump_profile ( profiler , pid ) ) <EOL> self . viewer = HTMLViewer ( '<STR_LIT>' , '<STR_LIT>' , <EOL> self . profile_log ) <EOL> body = ( b"<STR_LIT>" <EOL> b"<STR_LIT>" ) <EOL> wsgi_input = BytesIO ( body ) <EOL> environ = { '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : wsgi_input } <EOL> req = Request . blank ( '<STR_LIT>' , environ = environ ) <EOL> self . query_dict = self . app . _combine_body_qs ( req ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . tempdir , ignore_errors = True ) <EOL> def fake_call_back ( self ) : <EOL> pass <EOL> def test_get_param ( self ) : <EOL> query_dict = self . query_dict <EOL> get_param = self . viewer . _get_param <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT>' , True ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT:time>' ) , '<STR_LIT:time>' ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT:time>' , True ) , <EOL> [ '<STR_LIT:time>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , - <NUM_LIT:1> ) , <NUM_LIT:10> ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT:0>' ) , '<STR_LIT:1>' ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT:time>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( get_param ( query_dict , '<STR_LIT>' , '<STR_LIT:default>' ) , '<STR_LIT:default>' ) <EOL> def test_render ( self ) : <EOL> url = '<STR_LIT>' <EOL> path_entries = [ '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) , <EOL> '<STR_LIT>' . split ( '<STR_LIT:/>' ) ] <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:GET>' , path_entries [ <NUM_LIT:0> ] , <EOL> self . query_dict , None ) <EOL> self . assertTrue ( content is not None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:POST>' , path_entries [ <NUM_LIT:0> ] , <EOL> self . query_dict , None ) <EOL> self . assertTrue ( content is not None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> plot_dict = self . query_dict . copy ( ) <EOL> plot_dict [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> if PLOTLIB_INSTALLED : <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:POST>' , path_entries [ <NUM_LIT:0> ] , <EOL> plot_dict , None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> else : <EOL> self . assertRaises ( PLOTLIBNotInstalled , self . viewer . render , <EOL> url , '<STR_LIT:POST>' , path_entries [ <NUM_LIT:0> ] , plot_dict , None ) <EOL> clear_dict = self . query_dict . copy ( ) <EOL> clear_dict [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> del clear_dict [ '<STR_LIT>' ] <EOL> clear_dict [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:POST>' , path_entries [ <NUM_LIT:0> ] , <EOL> clear_dict , None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> download_dict = self . query_dict . copy ( ) <EOL> download_dict [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:POST>' , path_entries [ <NUM_LIT:0> ] , <EOL> download_dict , None ) <EOL> self . assertTrue ( headers == [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT:default>' ] ) ] ) <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:GET>' , path_entries [ <NUM_LIT:1> ] , <EOL> self . query_dict , None ) <EOL> self . assertTrue ( isinstance ( json . loads ( content ) , dict ) ) <EOL> for method in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . assertRaises ( MethodNotAllowed , self . viewer . render , url , <EOL> method , path_entries [ <NUM_LIT:10> ] , self . query_dict , None ) <EOL> for entry in path_entries [ <NUM_LIT:2> : ] : <EOL> download_dict [ '<STR_LIT>' ] = '<STR_LIT:default>' <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:GET>' , entry , <EOL> download_dict , None ) <EOL> self . assertTrue ( <EOL> ( '<STR_LIT>' , self . viewer . format_dict [ '<STR_LIT:default>' ] ) <EOL> in headers , entry ) <EOL> download_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> content , headers = self . viewer . render ( url , '<STR_LIT:GET>' , entry , <EOL> download_dict , None ) <EOL> self . assertTrue ( isinstance ( json . loads ( content ) , dict ) ) <EOL> def test_index ( self ) : <EOL> content , headers = self . viewer . index_page ( self . log_files [ <NUM_LIT:0> : <NUM_LIT:1> ] , <EOL> profile_id = '<STR_LIT>' ) <EOL> self . assertTrue ( content . find ( '<STR_LIT>' ) > - <NUM_LIT:1> ) <EOL> self . assertTrue ( headers == [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> def test_index_all ( self ) : <EOL> content , headers = self . viewer . index_page ( self . log_files , <EOL> profile_id = '<STR_LIT:all>' ) <EOL> for f in self . log_files : <EOL> self . assertTrue ( content . find ( f ) > <NUM_LIT:0> , content ) <EOL> self . assertTrue ( headers == [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> def test_download ( self ) : <EOL> content , headers = self . viewer . download ( self . log_files ) <EOL> self . assertTrue ( content is not None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT:default>' ] ) ] ) <EOL> content , headers = self . viewer . download ( self . log_files , sort = '<STR_LIT>' , <EOL> limit = <NUM_LIT:10> , nfl_filter = '<STR_LIT>' ) <EOL> self . assertTrue ( content is not None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT:default>' ] ) ] ) <EOL> content , headers = self . viewer . download ( self . log_files , <EOL> output_format = '<STR_LIT:default>' ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT:default>' ] ) ] ) <EOL> content , headers = self . viewer . download ( self . log_files , <EOL> output_format = '<STR_LIT>' ) <EOL> self . assertTrue ( isinstance ( json . loads ( content ) , dict ) ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT>' ] ) ] ) <EOL> content , headers = self . viewer . download ( self . log_files , <EOL> output_format = '<STR_LIT>' ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT>' ] ) ] ) <EOL> if ODFLIB_INSTALLED : <EOL> content , headers = self . viewer . download ( self . log_files , <EOL> output_format = '<STR_LIT>' ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT>' ] ) ] ) <EOL> else : <EOL> self . assertRaises ( ODFLIBNotInstalled , self . viewer . download , <EOL> self . log_files , output_format = '<STR_LIT>' ) <EOL> content , headers = self . viewer . download ( self . log_files , <EOL> nfl_filter = __file__ , <EOL> output_format = '<STR_LIT>' ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , <EOL> self . viewer . format_dict [ '<STR_LIT>' ] ) ] ) <EOL> def test_plot ( self ) : <EOL> if PLOTLIB_INSTALLED : <EOL> content , headers = self . viewer . plot ( self . log_files ) <EOL> self . assertTrue ( content is not None ) <EOL> self . assertEqual ( headers , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertRaises ( NotFoundException , self . viewer . plot , [ ] ) <EOL> else : <EOL> self . assertRaises ( PLOTLIBNotInstalled , self . viewer . plot , <EOL> self . log_files ) <EOL> def test_format_source_code ( self ) : <EOL> osfile = os . __file__ . rstrip ( '<STR_LIT:c>' ) <EOL> nfl_os = '<STR_LIT>' % ( osfile , <NUM_LIT> , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , self . viewer . format_source_code ( nfl_os ) ) <EOL> self . assertNotIn ( '<STR_LIT>' , self . viewer . format_source_code ( nfl_os ) ) <EOL> nfl_illegal = '<STR_LIT>' % osfile <EOL> self . assertIn ( _ ( '<STR_LIT>' ) , <EOL> self . viewer . format_source_code ( nfl_illegal ) ) <EOL> nfl_not_exist = '<STR_LIT>' % osfile <EOL> expected_msg = _ ( '<STR_LIT>' ) % osfile <EOL> self . assertIn ( expected_msg , <EOL> self . viewer . format_source_code ( nfl_not_exist ) ) <EOL> class TestStats2 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if xprofile is None : <EOL> raise SkipTest <EOL> self . profile_file = tempfile . mktemp ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . profilers = [ xprofile . get_profiler ( '<STR_LIT>' ) , <EOL> xprofile . get_profiler ( '<STR_LIT>' ) ] <EOL> for p in self . profilers : <EOL> p . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> p . dump_stats ( self . profile_file ) <EOL> self . stats2 = Stats2 ( self . profile_file ) <EOL> self . selections = [ [ '<STR_LIT>' ] , [ '<STR_LIT>' , - <NUM_LIT:1> ] , <EOL> [ '<STR_LIT>' , - <NUM_LIT:10> ] , [ '<STR_LIT>' , <NUM_LIT:0.1> ] ] <EOL> def tearDown ( self ) : <EOL> os . remove ( self . profile_file ) <EOL> def test_func_to_dict ( self ) : <EOL> func = [ '<STR_LIT>' , <NUM_LIT:100> , '<STR_LIT>' ] <EOL> self . assertEqual ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : <EOL> '<STR_LIT>' } , self . stats2 . func_to_dict ( func ) ) <EOL> func = [ '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ] <EOL> self . assertEqual ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <EOL> '<STR_LIT>' } , self . stats2 . func_to_dict ( func ) ) <EOL> def test_to_json ( self ) : <EOL> for selection in self . selections : <EOL> js = self . stats2 . to_json ( selection ) <EOL> self . assertTrue ( isinstance ( json . loads ( js ) , dict ) ) <EOL> self . assertTrue ( json . loads ( js ) [ '<STR_LIT>' ] is not None ) <EOL> self . assertTrue ( json . loads ( js ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] is not None ) <EOL> def test_to_ods ( self ) : <EOL> if ODFLIB_INSTALLED : <EOL> for selection in self . selections : <EOL> self . assertTrue ( self . stats2 . to_ods ( selection ) is not None ) <EOL> def test_to_csv ( self ) : <EOL> for selection in self . selections : <EOL> self . assertTrue ( self . stats2 . to_csv ( selection ) is not None ) <EOL> self . assertTrue ( '<STR_LIT>' in self . stats2 . to_csv ( selection ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
