<s> from __future__ import ( absolute_import , unicode_literals ) <EOL> import sys <EOL> import os <EOL> import unittest <EOL> import tempfile <EOL> import shutil <EOL> from contextlib import contextmanager <EOL> from builtins import ( range , next , open ) <EOL> from six import string_types <EOL> sijax_path = os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> sys . path . append ( sijax_path ) <EOL> from sijax import Sijax <EOL> from sijax . response import BaseResponse , StreamingIframeResponse <EOL> from sijax . exception import SijaxError <EOL> from sijax . plugin . comet import register_comet_callback , register_comet_object , CometResponse <EOL> from sijax . plugin . upload import register_upload_callback , UploadResponse <EOL> from sijax . helper import init_static_path <EOL> @ contextmanager <EOL> def temporary_dir ( * args , ** kwargs ) : <EOL> path = tempfile . mkdtemp ( ) <EOL> try : <EOL> yield path <EOL> finally : <EOL> shutil . rmtree ( path ) <EOL> class SijaxMainTestCase ( unittest . TestCase ) : <EOL> def test_changing_incoming_data_works ( self ) : <EOL> inst = Sijax ( ) <EOL> self . assertTrue ( inst . get_data ( ) == { } ) <EOL> data = { "<STR_LIT:key>" : "<STR_LIT:value>" } <EOL> inst . set_data ( data ) <EOL> self . assertTrue ( inst . get_data ( ) == data ) <EOL> data = { "<STR_LIT:key>" : "<STR_LIT:value>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertTrue ( inst . get_data ( ) == { "<STR_LIT:key>" : "<STR_LIT:value>" } ) <EOL> inst . set_data ( data ) <EOL> self . assertTrue ( inst . get_data ( ) == data ) <EOL> def test_detecting_sijax_requests_works ( self ) : <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> self . assertFalse ( inst . is_sijax_request ) <EOL> self . assertTrue ( inst . requested_function is None ) <EOL> inst . set_data ( { "<STR_LIT:key>" : "<STR_LIT:value>" } ) <EOL> self . assertFalse ( inst . is_sijax_request ) <EOL> self . assertTrue ( inst . requested_function is None ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" } ) <EOL> self . assertFalse ( inst . is_sijax_request ) <EOL> self . assertEqual ( inst . requested_function , "<STR_LIT>" ) <EOL> inst . set_data ( { cls . PARAM_ARGS : "<STR_LIT>" } ) <EOL> self . assertFalse ( inst . is_sijax_request ) <EOL> self . assertTrue ( inst . requested_function is None ) <EOL> self . assertEqual ( [ ] , inst . request_args ) <EOL> inst . set_data ( { cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertFalse ( inst . is_sijax_request ) <EOL> self . assertTrue ( inst . requested_function is None ) <EOL> self . assertEqual ( [ ] , inst . request_args ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ "<STR_LIT>" , <NUM_LIT:5> ] , inst . request_args ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ <NUM_LIT> , <NUM_LIT:5> ] , inst . request_args ) <EOL> def test_events_have_default_handlers ( self ) : <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> events = [ getattr ( cls , attr ) for attr in dir ( cls ) if attr . startswith ( '<STR_LIT>' ) ] <EOL> for event in events : <EOL> self . assertTrue ( inst . has_event ( event ) , "<STR_LIT>" % event ) <EOL> def test_registering_custom_events_works ( self ) : <EOL> inst = Sijax ( ) <EOL> event_name = "<STR_LIT>" <EOL> event_callback = lambda obj_response : obj_response . alert ( "<STR_LIT>" ) <EOL> self . assertFalse ( inst . has_event ( event_name ) , "<STR_LIT>" ) <EOL> inst . register_event ( event_name , event_callback ) <EOL> self . assertTrue ( inst . has_event ( event_name ) , "<STR_LIT>" ) <EOL> def test_executing_regular_callbacks_works ( self ) : <EOL> calls_history = [ ] <EOL> call_args_history = [ ] <EOL> def event_before ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , BaseResponse ) ) <EOL> calls_history . append ( "<STR_LIT>" ) <EOL> obj_response . script ( "<STR_LIT>" ) <EOL> def event_after ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , BaseResponse ) ) <EOL> calls_history . append ( "<STR_LIT>" ) <EOL> obj_response . css ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def callback_main ( obj_response , arg1 , arg2 ) : <EOL> self . assertTrue ( isinstance ( obj_response , BaseResponse ) ) <EOL> calls_history . append ( "<STR_LIT>" ) <EOL> call_args_history . append ( arg1 ) <EOL> call_args_history . append ( arg2 ) <EOL> obj_response . alert ( "<STR_LIT>" ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_event ( cls . EVENT_BEFORE_PROCESSING , event_before ) <EOL> inst . register_event ( cls . EVENT_AFTER_PROCESSING , event_after ) <EOL> call_args = [ "<STR_LIT>" , <NUM_LIT:15> ] <EOL> response = inst . execute_callback ( call_args , callback = callback_main ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , calls_history ) <EOL> self . assertEqual ( call_args , call_args_history ) <EOL> self . assertTrue ( isinstance ( response , string_types ) ) <EOL> from sijax . helper import json <EOL> try : <EOL> commands = json . loads ( response ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> self . assertTrue ( isinstance ( commands , list ) ) <EOL> commands_history = [ ] <EOL> for cmd_params in commands : <EOL> self . assertTrue ( isinstance ( cmd_params , dict ) ) <EOL> self . assertTrue ( "<STR_LIT:type>" in cmd_params , "<STR_LIT>" ) <EOL> commands_history . append ( cmd_params [ "<STR_LIT:type>" ] ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , commands_history ) <EOL> def test_bad_callback_objects_raise_exception ( self ) : <EOL> inst = Sijax ( ) <EOL> try : <EOL> inst . execute_callback ( callback = '<STR_LIT>' , args = [ ] ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except SijaxError : <EOL> pass <EOL> def test_get_js_fails_with_missing_request_uri ( self ) : <EOL> inst = Sijax ( ) <EOL> try : <EOL> inst . get_js ( ) <EOL> self . fail ( ) <EOL> except SijaxError : <EOL> pass <EOL> def test_changing_uris_results_in_a_differnt_js_output ( self ) : <EOL> inst = Sijax ( ) <EOL> req_uri = "<STR_LIT>" <EOL> json_uri = "<STR_LIT>" <EOL> inst . set_request_uri ( req_uri ) <EOL> js = inst . get_js ( ) <EOL> self . assertTrue ( '<STR_LIT>' % req_uri in js ) <EOL> self . assertFalse ( '<STR_LIT>' in js ) <EOL> inst . set_json_uri ( json_uri ) <EOL> js = inst . get_js ( ) <EOL> self . assertTrue ( '<STR_LIT>' % req_uri in js ) <EOL> self . assertTrue ( '<STR_LIT>' % json_uri in js ) <EOL> def test_process_request_throws_exception_when_called_for_non_sijax_requests ( self ) : <EOL> inst = Sijax ( ) <EOL> try : <EOL> inst . process_request ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except SijaxError : <EOL> pass <EOL> def test_process_request_calls_invalid_request_event_for_invalid_requests ( self ) : <EOL> from sijax . helper import json <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ "<STR_LIT>" , <NUM_LIT:12> ] , inst . request_args ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , string_types ) ) <EOL> try : <EOL> commands = json . loads ( response ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> self . assertTrue ( isinstance ( commands , list ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( commands ) ) <EOL> command_data = commands . pop ( <NUM_LIT:0> ) <EOL> self . assertTrue ( "<STR_LIT:type>" in command_data ) <EOL> self . assertEqual ( "<STR_LIT>" , command_data [ "<STR_LIT:type>" ] ) <EOL> def test_process_request_calls_invalid_call_event_for_invalid_calls ( self ) : <EOL> from types import FunctionType <EOL> from sijax . helper import json <EOL> call_history = [ ] <EOL> def my_callback ( obj_response , arg1 , arg2 ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> def my_callback_with_defaults ( obj_response , arg1 = <NUM_LIT> , arg2 = <NUM_LIT:15> ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> def my_callback_raising_TypeError ( obj_response ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def my_callback_raising_TypeError2 ( obj_response ) : <EOL> def inner ( ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> inner ( ) <EOL> def invalid_call ( obj_response , failed_callback ) : <EOL> self . assertTrue ( isinstance ( failed_callback , FunctionType ) ) <EOL> call_history . append ( "<STR_LIT>" % failed_callback . __name__ ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_callback ( "<STR_LIT>" , my_callback ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ "<STR_LIT>" , <NUM_LIT:12> ] , inst . request_args ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , string_types ) ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ "<STR_LIT>" ] , inst . request_args ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , string_types ) ) <EOL> try : <EOL> commands = json . loads ( response ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> self . assertTrue ( isinstance ( commands , list ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( commands ) ) <EOL> command_data = commands . pop ( <NUM_LIT:0> ) <EOL> self . assertTrue ( "<STR_LIT:type>" in command_data ) <EOL> self . assertEqual ( "<STR_LIT>" , command_data [ "<STR_LIT:type>" ] ) <EOL> inst . register_event ( cls . EVENT_INVALID_CALL , invalid_call ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ ] , inst . request_args ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , string_types ) ) <EOL> inst . register_callback ( "<STR_LIT>" , my_callback_with_defaults ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , string_types ) ) <EOL> inst . register_callback ( "<STR_LIT>" , my_callback_raising_TypeError ) <EOL> try : <EOL> inst . process_request ( ) <EOL> except TypeError : <EOL> call_history . append ( '<STR_LIT>' ) <EOL> inst . register_callback ( "<STR_LIT>" , my_callback_raising_TypeError2 ) <EOL> try : <EOL> inst . process_request ( ) <EOL> except TypeError : <EOL> call_history . append ( '<STR_LIT>' ) <EOL> expected = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( expected , call_history ) <EOL> def test_new_callbacks_override_old_during_registering ( self ) : <EOL> call_history = [ ] <EOL> def callback_one ( obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> def callback_two ( obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . register_callback ( "<STR_LIT>" , callback_one ) <EOL> inst . process_request ( ) <EOL> inst . register_callback ( "<STR_LIT>" , callback_two ) <EOL> inst . process_request ( ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" ] , call_history ) <EOL> def test_mass_registering_from_a_class_works ( self ) : <EOL> call_history = [ ] <EOL> class SijaxHandler ( object ) : <EOL> @ staticmethod <EOL> def callback_one ( obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def callback_two ( obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def callback_three ( cls , obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> inst = Sijax ( ) <EOL> inst . register_object ( SijaxHandler ) <EOL> cls = inst . __class__ <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , call_history ) <EOL> def test_mass_registering_from_an_object_works ( self ) : <EOL> call_history = [ ] <EOL> class SijaxHandler ( object ) : <EOL> def callback_one ( self , obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> def callback_two ( self , obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> inst = Sijax ( ) <EOL> inst . register_object ( SijaxHandler ( ) ) <EOL> cls = inst . __class__ <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" ] , call_history ) <EOL> def test_the_response_class_could_be_changed_during_registration ( self ) : <EOL> call_history = [ ] <EOL> class CustomResponse ( BaseResponse ) : pass <EOL> def my_callback ( obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> self . assertFalse ( isinstance ( obj_response , CustomResponse ) ) <EOL> self . assertTrue ( isinstance ( obj_response , BaseResponse ) ) <EOL> def my_custom_callback ( obj_response ) : <EOL> call_history . append ( "<STR_LIT>" ) <EOL> self . assertTrue ( isinstance ( obj_response , CustomResponse ) ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_callback ( "<STR_LIT>" , my_callback ) <EOL> params_custom = { cls . PARAM_RESPONSE_CLASS : CustomResponse } <EOL> inst . register_callback ( "<STR_LIT>" , my_custom_callback , ** params_custom ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" ] , call_history ) <EOL> def test_args_extra_works_properly ( self ) : <EOL> call_history = [ ] <EOL> def callback ( obj_response , arg1_custom , arg2_custom , arg_regular ) : <EOL> self . assertTrue ( isinstance ( obj_response , BaseResponse ) ) <EOL> call_history . append ( arg1_custom ) <EOL> call_history . append ( arg2_custom ) <EOL> call_history . append ( arg_regular ) <EOL> def callback_basic ( obj_response , arg_regular ) : <EOL> self . assertTrue ( isinstance ( obj_response , BaseResponse ) ) <EOL> call_history . append ( arg_regular ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_callback ( "<STR_LIT>" , callback , args_extra = ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> inst . register_callback ( "<STR_LIT>" , callback_basic ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> inst . process_request ( ) <EOL> call_history_expected = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . assertEqual ( call_history_expected , call_history ) <EOL> def test_args_extra_expects_a_list_or_a_tuple ( self ) : <EOL> inst = Sijax ( ) <EOL> def try_args ( args_extra , should_succeed ) : <EOL> success = False <EOL> try : <EOL> inst . register_callback ( "<STR_LIT:name>" , lambda r : r , args_extra = args_extra ) <EOL> success = True <EOL> except SijaxError : <EOL> pass <EOL> self . assertEqual ( should_succeed , success , "<STR_LIT>" % repr ( args_extra ) ) <EOL> try_args ( [ ] , True ) <EOL> try_args ( ( ) , True ) <EOL> try_args ( None , True ) <EOL> try_args ( [ '<STR_LIT>' ] , True ) <EOL> try_args ( ( '<STR_LIT>' , ) , True ) <EOL> try_args ( [ '<STR_LIT>' , '<STR_LIT>' ] , True ) <EOL> try_args ( ( '<STR_LIT>' , '<STR_LIT>' ) , True ) <EOL> try_args ( [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT:list>" } ] , True ) <EOL> try_args ( ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , ) , True ) <EOL> try_args ( '<STR_LIT:string>' , False ) <EOL> try_args ( True , False ) <EOL> try_args ( False , False ) <EOL> try_args ( { "<STR_LIT>" : "<STR_LIT>" } , False ) <EOL> def test_regular_functions_that_yield_are_not_allowed ( self ) : <EOL> inst = Sijax ( ) <EOL> def callback ( obj_response ) : <EOL> yield obj_response <EOL> try : <EOL> inst . execute_callback ( [ ] , callback ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except SijaxError : <EOL> pass <EOL> def test_response_method_call_rejects_bad_args ( self ) : <EOL> inst = Sijax ( ) <EOL> def try_call ( args_value , should_succeed ) : <EOL> def callback ( obj_response ) : <EOL> obj_response . call ( '<STR_LIT>' , args_value ) <EOL> success = False <EOL> try : <EOL> inst . execute_callback ( [ ] , callback ) <EOL> success = True <EOL> except SijaxError : <EOL> pass <EOL> self . assertEqual ( should_succeed , success , "<STR_LIT>" % <EOL> repr ( args_value ) ) <EOL> try_call ( None , True ) <EOL> try_call ( [ ] , True ) <EOL> try_call ( [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:3> , <NUM_LIT:4> , '<STR_LIT>' ] , True ) <EOL> try_call ( ( ) , True ) <EOL> try_call ( '<STR_LIT>' , False ) <EOL> try_call ( <NUM_LIT:1> , False ) <EOL> try_call ( { '<STR_LIT>' : '<STR_LIT>' } , False ) <EOL> def test_init_static_path_helper_works ( self ) : <EOL> import os <EOL> import sijax <EOL> with temporary_dir ( ) as static_path : <EOL> init_static_path ( static_path ) <EOL> version_file = os . path . join ( static_path , '<STR_LIT>' ) <EOL> if not os . path . exists ( version_file ) : <EOL> self . fail ( '<STR_LIT>' % version_file ) <EOL> with open ( version_file ) as fp : <EOL> self . assertEqual ( fp . read ( ) , sijax . __version__ ) <EOL> core_js_file = os . path . join ( static_path , '<STR_LIT>' ) <EOL> with open ( core_js_file , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> init_static_path ( static_path ) <EOL> with open ( core_js_file ) as fp : <EOL> self . assertEqual ( fp . read ( ) , '<STR_LIT>' ) <EOL> with open ( version_file , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> new_file = os . path . join ( static_path , '<STR_LIT>' ) <EOL> self . assertFalse ( os . path . exists ( new_file ) ) <EOL> with open ( new_file , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . exists ( new_file ) ) <EOL> init_static_path ( static_path ) <EOL> with open ( core_js_file ) as fp : <EOL> self . assertNotEqual ( fp . read ( ) , '<STR_LIT>' ) <EOL> with open ( version_file ) as fp : <EOL> self . assertEqual ( fp . read ( ) , sijax . __version__ ) <EOL> self . assertFalse ( os . path . exists ( new_file ) ) <EOL> def test_static_path_helper_refuses_to_write_to_non_empty_paths ( self ) : <EOL> import os <EOL> def try_init ( path , should_succeed ) : <EOL> success = False <EOL> try : <EOL> init_static_path ( path ) <EOL> success = True <EOL> except SijaxError : <EOL> pass <EOL> self . assertEqual ( should_succeed , success ) <EOL> with temporary_dir ( ) as static_path : <EOL> try_init ( static_path , True ) <EOL> with temporary_dir ( ) as static_path : <EOL> with open ( os . path . join ( static_path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> try_init ( static_path , False ) <EOL> with temporary_dir ( ) as static_path : <EOL> with open ( os . path . join ( static_path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> with open ( os . path . join ( static_path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> try_init ( static_path , True ) <EOL> def test_response_classes_need_to_be_callable ( self ) : <EOL> inst = Sijax ( ) <EOL> def try_response_class ( response_class , should_succeed ) : <EOL> success = False <EOL> try : <EOL> inst . register_callback ( "<STR_LIT:name>" , lambda r : r , response_class = response_class ) <EOL> success = True <EOL> except SijaxError : <EOL> pass <EOL> self . assertEqual ( should_succeed , success , "<STR_LIT>" % repr ( response_class ) ) <EOL> try_response_class ( BaseResponse , True ) <EOL> class CustomResponse ( BaseResponse ) : pass <EOL> try_response_class ( CustomResponse , True ) <EOL> def response_factory ( * args , ** kwargs ) : <EOL> return CustomResponse ( * args , ** kwargs ) <EOL> try_response_class ( response_factory , True ) <EOL> try_response_class ( None , True ) <EOL> try_response_class ( '<STR_LIT>' , False ) <EOL> try_response_class ( <NUM_LIT> , False ) <EOL> class SijaxStreamingTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_streaming_functions_return_generators ( self ) : <EOL> from types import GeneratorType <EOL> call_history = [ ] <EOL> def callback_before ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> obj_response . html ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def callback_after ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> def callback_yielding ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> obj_response . alert ( "<STR_LIT:test>" ) <EOL> yield obj_response <EOL> obj_response . css ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def callback_normal ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> obj_response . script ( "<STR_LIT>" ) <EOL> def assert_generator_entries_length ( generator , length ) : <EOL> items = [ ] <EOL> try : <EOL> while True : <EOL> items . append ( next ( generator ) ) <EOL> except StopIteration : <EOL> pass <EOL> self . assertEqual ( length , len ( items ) ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_event ( cls . EVENT_BEFORE_PROCESSING , callback_before ) <EOL> inst . register_event ( cls . EVENT_AFTER_PROCESSING , callback_after ) <EOL> options = { cls . PARAM_RESPONSE_CLASS : StreamingIframeResponse } <EOL> response = inst . execute_callback ( [ ] , callback = callback_yielding , ** options ) <EOL> self . assertTrue ( isinstance ( response , GeneratorType ) ) <EOL> assert_generator_entries_length ( response , <NUM_LIT:3> ) <EOL> def callback_before_new ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> yield obj_response <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> obj_response . alert ( "<STR_LIT>" ) <EOL> yield obj_response <EOL> inst . register_event ( cls . EVENT_BEFORE_PROCESSING , callback_before_new ) <EOL> inst . register_event ( cls . EVENT_AFTER_PROCESSING , lambda r : r . alert ( "<STR_LIT>" ) ) <EOL> response = inst . execute_callback ( [ ] , callback = callback_normal , ** options ) <EOL> self . assertTrue ( isinstance ( response , GeneratorType ) ) <EOL> assert_generator_entries_length ( response , <NUM_LIT:4> ) <EOL> call_history_expected = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> self . assertEqual ( call_history_expected , call_history ) <EOL> def test_invalid_call_event_works ( self ) : <EOL> from types import GeneratorType , FunctionType <EOL> call_history = [ ] <EOL> def my_callback ( obj_response , arg1 , arg2 ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> def my_callback_raising_TypeError ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def my_callback_raising_TypeError2 ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> def inner ( ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> inner ( ) <EOL> def invalid_call ( obj_response , failed_callback ) : <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> self . assertTrue ( isinstance ( failed_callback , FunctionType ) ) <EOL> call_history . append ( "<STR_LIT>" % failed_callback . __name__ ) <EOL> def exhaust_generator ( gen ) : <EOL> self . assertTrue ( isinstance ( gen , GeneratorType ) ) <EOL> try : <EOL> while True : <EOL> next ( gen ) <EOL> except StopIteration : <EOL> pass <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> options = { cls . PARAM_RESPONSE_CLASS : StreamingIframeResponse } <EOL> inst . register_event ( cls . EVENT_INVALID_CALL , invalid_call ) <EOL> inst . register_callback ( "<STR_LIT>" , my_callback , ** options ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ "<STR_LIT>" , <NUM_LIT:12> ] , inst . request_args ) <EOL> response = inst . process_request ( ) <EOL> exhaust_generator ( response ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> self . assertEqual ( "<STR_LIT>" , inst . requested_function ) <EOL> self . assertEqual ( [ ] , inst . request_args ) <EOL> response = inst . process_request ( ) <EOL> exhaust_generator ( response ) <EOL> inst . register_callback ( "<STR_LIT>" , my_callback_raising_TypeError , ** options ) <EOL> try : <EOL> response = inst . process_request ( ) <EOL> exhaust_generator ( response ) <EOL> except TypeError : <EOL> call_history . append ( '<STR_LIT>' ) <EOL> inst . register_callback ( "<STR_LIT>" , my_callback_raising_TypeError2 , ** options ) <EOL> try : <EOL> response = inst . process_request ( ) <EOL> exhaust_generator ( response ) <EOL> except TypeError : <EOL> call_history . append ( '<STR_LIT>' ) <EOL> expected = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( expected , call_history ) <EOL> class SijaxCometTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_registering_helper_works ( self ) : <EOL> from types import GeneratorType <EOL> class CustomResponse ( CometResponse ) : pass <EOL> def process_request ( inst ) : <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , GeneratorType ) ) <EOL> for string in response : <EOL> pass <EOL> call_history = [ ] <EOL> def invalid_call ( obj_response , callback ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_event ( cls . EVENT_INVALID_CALL , invalid_call ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> def my_callback ( obj_response ) : <EOL> self . assertTrue ( isinstance ( obj_response , CometResponse ) ) <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> call_history . append ( type ( obj_response ) . __name__ ) <EOL> register_comet_callback ( inst , "<STR_LIT>" , my_callback ) <EOL> process_request ( inst ) <EOL> params_custom = { cls . PARAM_RESPONSE_CLASS : CustomResponse } <EOL> register_comet_callback ( inst , "<STR_LIT>" , my_callback , ** params_custom ) <EOL> process_request ( inst ) <EOL> class SijaxHandler ( object ) : <EOL> @ staticmethod <EOL> def callback_one ( obj_response , arg1 ) : <EOL> self . assertTrue ( isinstance ( obj_response , CustomResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> call_history . append ( type ( obj_response ) . __name__ ) <EOL> call_history . append ( arg1 ) <EOL> register_comet_object ( inst , SijaxHandler , ** params_custom ) <EOL> inst . set_data ( { cls . PARAM_REQUEST : "<STR_LIT>" , cls . PARAM_ARGS : '<STR_LIT>' } ) <EOL> process_request ( inst ) <EOL> call_history_expected = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> <EOL> ] <EOL> self . assertEqual ( call_history_expected , call_history ) <EOL> class SijaxUploadTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_registering_helper_works ( self ) : <EOL> from types import GeneratorType <EOL> from sijax . plugin . upload import func_name_by_form_id <EOL> class CustomResponse ( UploadResponse ) : pass <EOL> def process_request ( inst ) : <EOL> self . assertTrue ( inst . is_sijax_request ) <EOL> response = inst . process_request ( ) <EOL> self . assertTrue ( isinstance ( response , GeneratorType ) ) <EOL> for string in response : <EOL> pass <EOL> call_history = [ ] <EOL> def invalid_call ( obj_response , callback ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> inst = Sijax ( ) <EOL> cls = inst . __class__ <EOL> inst . register_event ( cls . EVENT_INVALID_CALL , invalid_call ) <EOL> def my_callback ( obj_response , files , form_values ) : <EOL> self . assertTrue ( isinstance ( obj_response , UploadResponse ) ) <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> call_history . append ( obj_response . form_id ) <EOL> call_history . append ( type ( obj_response ) . __name__ ) <EOL> call_history . append ( files [ "<STR_LIT:file>" ] ) <EOL> self . assertEqual ( { "<STR_LIT>" : "<STR_LIT:value>" , "<STR_LIT>" : <NUM_LIT:15> } , form_values ) <EOL> def my_callback2 ( obj_response , custom_arg1 , custom_arg2 , form_values ) : <EOL> self . assertTrue ( isinstance ( obj_response , UploadResponse ) ) <EOL> self . assertTrue ( isinstance ( obj_response , StreamingIframeResponse ) ) <EOL> call_history . append ( "<STR_LIT>" ) <EOL> call_history . append ( obj_response . form_id ) <EOL> call_history . append ( type ( obj_response ) . __name__ ) <EOL> call_history . append ( custom_arg1 ) <EOL> call_history . append ( custom_arg2 ) <EOL> self . assertEqual ( { "<STR_LIT>" : "<STR_LIT:value>" , "<STR_LIT>" : <NUM_LIT:15> } , form_values ) <EOL> form_id = "<STR_LIT>" <EOL> public_callback_name = func_name_by_form_id ( form_id ) <EOL> request_args_json = '<STR_LIT>' % form_id <EOL> post = { } <EOL> post [ cls . PARAM_REQUEST ] = public_callback_name <EOL> post [ cls . PARAM_ARGS ] = request_args_json <EOL> post [ "<STR_LIT>" ] = "<STR_LIT:value>" <EOL> post [ "<STR_LIT>" ] = <NUM_LIT:15> <EOL> inst . set_data ( post ) <EOL> files = { "<STR_LIT:file>" : "<STR_LIT:object>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> register_upload_callback ( inst , form_id , my_callback , args_extra = ( files , ) ) <EOL> process_request ( inst ) <EOL> params_custom = { cls . PARAM_RESPONSE_CLASS : CustomResponse } <EOL> register_upload_callback ( inst , form_id , my_callback2 , args_extra = ( "<STR_LIT>" , "<STR_LIT>" ) , ** params_custom ) <EOL> process_request ( inst ) <EOL> call_history_expected = [ <EOL> "<STR_LIT>" , form_id , "<STR_LIT>" , "<STR_LIT:object>" , <EOL> "<STR_LIT>" , form_id , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> self . assertEqual ( call_history_expected , call_history ) <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( SijaxMainTestCase ) ) <EOL> suite . addTest ( unittest . makeSuite ( SijaxStreamingTestCase ) ) <EOL> suite . addTest ( unittest . makeSuite ( SijaxCometTestCase ) ) <EOL> suite . addTest ( unittest . makeSuite ( SijaxUploadTestCase ) ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( defaultTest = '<STR_LIT>' ) </s>
