<s> import time <EOL> from nose . tools import * <EOL> import voltron <EOL> from voltron . core import * <EOL> from voltron . api import * <EOL> from voltron . plugin import * <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class APITestRequest ( APIRequest ) : <EOL> _fields = { '<STR_LIT>' : False , '<STR_LIT:address>' : False , '<STR_LIT:count>' : True } <EOL> _types = { '<STR_LIT>' : int , '<STR_LIT:address>' : int , '<STR_LIT:count>' : int } <EOL> target_id = <NUM_LIT:0> <EOL> address = None <EOL> count = None <EOL> class APITestResponse ( APISuccessResponse ) : <EOL> _fields = { '<STR_LIT>' : True } <EOL> class APITestPlugin ( APIPlugin ) : <EOL> request = '<STR_LIT:test>' <EOL> request_class = APITestRequest <EOL> response_class = APITestResponse <EOL> def setup ( ) : <EOL> voltron . setup_env ( ) <EOL> def teardown ( ) : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> def test_parent_message_validation_fail ( ) : <EOL> msg = APIMessage ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert exception <EOL> def test_parent_request_validation_fail ( ) : <EOL> msg = APIRequest ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert exception <EOL> def test_parent_request_type ( ) : <EOL> msg = APIRequest ( ) <EOL> assert msg . type == '<STR_LIT>' <EOL> def test_parent_request_request ( ) : <EOL> msg = APIRequest ( ) <EOL> assert msg . request is None <EOL> def test_parent_response_validation_fail ( ) : <EOL> msg = APIResponse ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert exception <EOL> def test_parent_response_type ( ) : <EOL> msg = APIResponse ( ) <EOL> assert msg . type == '<STR_LIT>' <EOL> def test_parent_response_status ( ) : <EOL> msg = APIResponse ( ) <EOL> assert msg . status is None <EOL> def test_success_response_validation_succeed ( ) : <EOL> msg = APISuccessResponse ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert not exception <EOL> def test_success_response_type ( ) : <EOL> msg = APISuccessResponse ( ) <EOL> assert msg . type == '<STR_LIT>' <EOL> def test_success_response_status ( ) : <EOL> msg = APISuccessResponse ( ) <EOL> assert msg . status == '<STR_LIT:success>' <EOL> def test_error_response_validation_fail ( ) : <EOL> msg = APIErrorResponse ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert exception <EOL> def test_error_response_type ( ) : <EOL> msg = APIErrorResponse ( ) <EOL> assert msg . type == '<STR_LIT>' <EOL> def test_error_response_status ( ) : <EOL> msg = APIErrorResponse ( ) <EOL> assert msg . status == '<STR_LIT:error>' <EOL> def test_invalid_request_error_response_validation_succeed ( ) : <EOL> msg = APIInvalidRequestErrorResponse ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert not exception <EOL> def test_invalid_request_error_response_type ( ) : <EOL> msg = APIInvalidRequestErrorResponse ( ) <EOL> assert msg . type == '<STR_LIT>' <EOL> def test_invalid_request_error_response_status ( ) : <EOL> msg = APIInvalidRequestErrorResponse ( ) <EOL> assert msg . status == '<STR_LIT:error>' <EOL> def test_test_request_validation_fail ( ) : <EOL> msg = APITestRequest ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert exception <EOL> def test_test_request_validation_fail_with_param ( ) : <EOL> msg = APITestRequest ( target_id = <NUM_LIT:0> ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert exception <EOL> def test_test_request_validation_succeed_with_param ( ) : <EOL> msg = api_request ( '<STR_LIT:test>' , count = <NUM_LIT:16> ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError : <EOL> exception = True <EOL> assert not exception <EOL> assert msg . count == <NUM_LIT:16> <EOL> def test_test_request_validation_succeed_with_data ( ) : <EOL> msg = APITestRequest ( '<STR_LIT>' ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> exception = True <EOL> assert not exception <EOL> assert msg . count == <NUM_LIT:16> <EOL> def test_test_request_validation_succeed_by_assign ( ) : <EOL> msg = APITestRequest ( ) <EOL> msg . count = <NUM_LIT:16> <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> exception = True <EOL> assert not exception <EOL> assert msg . count == <NUM_LIT:16> <EOL> def test_test_request_string ( ) : <EOL> msg = APITestRequest ( count = <NUM_LIT:16> ) <EOL> assert json . loads ( str ( msg ) ) == { "<STR_LIT>" : "<STR_LIT:test>" , "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : False , "<STR_LIT>" : <NUM_LIT:10> , <EOL> "<STR_LIT:data>" : { "<STR_LIT:count>" : <NUM_LIT:16> , "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT:address>" : None } } <EOL> def test_test_response_validation_fail ( ) : <EOL> msg = APITestResponse ( ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> exception = True <EOL> assert exception <EOL> def test_test_response_validation_fail_with_param ( ) : <EOL> msg = APITestResponse ( thing = <NUM_LIT:1> ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> exception = True <EOL> assert exception <EOL> def test_test_response_validation_succeed_with_param ( ) : <EOL> msg = APITestResponse ( disassembly = "<STR_LIT>" ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> exception = True <EOL> assert not exception <EOL> def test_test_response_validation_succeed_with_data ( ) : <EOL> msg = APITestResponse ( '<STR_LIT>' ) <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> exception = True <EOL> assert not exception <EOL> def test_test_response_validation_succeed_by_assign ( ) : <EOL> msg = APITestResponse ( ) <EOL> msg . disassembly = "<STR_LIT>" <EOL> exception = False <EOL> try : <EOL> msg . validate ( ) <EOL> except MissingFieldError as e : <EOL> print ( str ( e ) ) <EOL> exception = True <EOL> assert not exception <EOL> def test_test_response_string ( ) : <EOL> msg = APITestResponse ( disassembly = '<STR_LIT>' ) <EOL> assert json . loads ( str ( msg ) ) == { "<STR_LIT:status>" : "<STR_LIT:success>" , "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:data>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> class APIEncodeMsg ( APIMessage ) : <EOL> _fields = { '<STR_LIT>' : False } <EOL> _encode_fields = [ '<STR_LIT>' ] <EOL> def test_encode_fields ( ) : <EOL> msg = APIEncodeMsg ( ) <EOL> msg . enc = six . b ( '<STR_LIT>' ) . join ( [ six . int2byte ( x ) for x in range ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> assert msg . to_dict ( ) [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] == six . text_type ( '<STR_LIT>' ) <EOL> assert msg . to_json ( ) == '<STR_LIT>' <EOL> msg2 = APIEncodeMsg ( data = msg . to_json ( ) ) <EOL> assert msg . to_dict ( ) == msg2 . to_dict ( ) <EOL> assert msg . to_json ( ) == msg2 . to_json ( ) <EOL> assert msg2 . enc == msg . enc <EOL> msg3 = APIEncodeMsg ( ) <EOL> msg3 . enc = six . u ( '<STR_LIT>' ) <EOL> assert msg3 . to_dict ( ) == { '<STR_LIT:data>' : { '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT:type>' : None } <EOL> msg3 . enc = six . b ( '<STR_LIT>' ) <EOL> assert msg3 . to_dict ( ) == { '<STR_LIT:data>' : { '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT:type>' : None } <EOL> msg4 = APIEncodeMsg ( ) <EOL> msg4 . from_dict ( msg . to_dict ( ) ) <EOL> assert msg4 . to_dict ( ) == msg . to_dict ( ) </s>
