<s> import datetime <EOL> from decimal import Decimal <EOL> import json <EOL> import sys <EOL> from mock import Mock <EOL> import pytest <EOL> from ubersmith . api import METHODS , BaseResponse <EOL> from ubersmith . calls import generate_generic_calls <EOL> from ubersmith . clean import _CLEANERS <EOL> from ubersmith . exceptions import ValidationError <EOL> from ubersmith import uber , order <EOL> def setup_module ( module ) : <EOL> """<STR_LIT>""" <EOL> bases = set ( method . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] for method in METHODS ) <EOL> for base in bases : <EOL> mod_name = '<STR_LIT:.>' . join ( [ '<STR_LIT>' , base ] ) <EOL> if not sys . modules . get ( mod_name ) : <EOL> __import__ ( mod_name ) <EOL> def make_base_response ( data ) : <EOL> response = Mock ( ) <EOL> response . headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> } <EOL> response . json . return_value = { <EOL> "<STR_LIT:status>" : True , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : data , <EOL> } <EOL> return BaseResponse ( response ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , METHODS ) <EOL> def test_method_exists ( method ) : <EOL> base , call = method . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> mod_name = '<STR_LIT:.>' . join ( [ '<STR_LIT>' , base ] ) <EOL> assert hasattr ( sys . modules [ mod_name ] , call ) <EOL> class DescribeGenerateGenericCalls : <EOL> def it_creates_call_funcs ( self ) : <EOL> base = '<STR_LIT>' <EOL> namespace = { } <EOL> generate_generic_calls ( base , namespace ) <EOL> base_funcs = ( m . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] for m in METHODS if m . startswith ( base ) ) <EOL> assert sorted ( namespace . keys ( ) ) == list ( sorted ( base_funcs ) ) <EOL> def it_adds_func_names_to_all ( self ) : <EOL> base = '<STR_LIT>' <EOL> namespace = { '<STR_LIT>' : [ ] } <EOL> generate_generic_calls ( base , namespace ) <EOL> base_funcs = ( m . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] for m in METHODS if m . startswith ( base ) ) <EOL> assert sorted ( namespace [ '<STR_LIT>' ] ) == list ( sorted ( base_funcs ) ) <EOL> def it_adds_doc_string_to_funcs_that_already_exist ( self ) : <EOL> func = lambda : None <EOL> base = '<STR_LIT>' <EOL> namespace = { '<STR_LIT>' : func } <EOL> generate_generic_calls ( base , namespace ) <EOL> assert namespace [ '<STR_LIT>' ] . __doc__ == METHODS [ '<STR_LIT>' ] <EOL> def it_does_not_add_doc_string_to_funcs_that_already_have_them ( self ) : <EOL> func = lambda : None <EOL> func . __doc__ = '<STR_LIT>' <EOL> base = '<STR_LIT>' <EOL> namespace = { '<STR_LIT>' : func } <EOL> generate_generic_calls ( base , namespace ) <EOL> assert namespace [ '<STR_LIT>' ] . __doc__ == '<STR_LIT>' <EOL> def test_json_call ( ) : <EOL> handler = Mock ( ) <EOL> data = { "<STR_LIT:test>" : "<STR_LIT>" } <EOL> handler . process_request . return_value = make_base_response ( data ) <EOL> assert dict ( uber . method_list . handler ( handler ) ( ) ) == { "<STR_LIT:test>" : "<STR_LIT>" } <EOL> def test_group_call ( ) : <EOL> handler = Mock ( ) <EOL> data = { "<STR_LIT:1>" : { "<STR_LIT:test>" : "<STR_LIT>" } } <EOL> handler . process_request . return_value = make_base_response ( data ) <EOL> assert dict ( order . list . handler ( handler ) ( ) ) == { <NUM_LIT:1> : { "<STR_LIT:test>" : "<STR_LIT>" } } <EOL> def test_file_call ( ) : <EOL> handler = Mock ( ) <EOL> response = Mock ( ) <EOL> response . content = '<STR_LIT>' <EOL> response . headers = { } <EOL> handler . process_request . return_value = BaseResponse ( response ) <EOL> uber_file = uber . documentation . handler ( handler ) ( ) <EOL> assert uber_file . data == '<STR_LIT>' <EOL> def test_calls_validates_required_fields ( ) : <EOL> with pytest . raises ( ValidationError ) : <EOL> order . queue_list . handler ( '<STR_LIT>' ) ( ) <EOL> def test_calls_validates_or_required_fields ( ) : <EOL> with pytest . raises ( ValidationError ) : <EOL> order . get . handler ( '<STR_LIT>' ) ( ) <EOL> def dict_zip ( * dicts ) : <EOL> for key in set ( dicts [ <NUM_LIT:0> ] ) . intersection ( * dicts [ <NUM_LIT:1> : ] ) : <EOL> yield tuple ( d [ key ] for d in dicts ) <EOL> @ pytest . mark . parametrize ( [ '<STR_LIT>' , '<STR_LIT:value>' , '<STR_LIT:result>' ] , dict_zip ( _CLEANERS , { <EOL> '<STR_LIT:bool>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:int>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:float>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> } , { <EOL> '<STR_LIT:bool>' : True , <EOL> '<STR_LIT:int>' : <NUM_LIT> , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT:float>' : <NUM_LIT> , <EOL> '<STR_LIT>' : datetime . datetime . fromtimestamp ( float ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : { b'<STR_LIT:test>' : <NUM_LIT> } , <EOL> } ) ) <EOL> def test_cleaners ( cleaner , value , result ) : <EOL> assert cleaner ( value ) == result </s>
