<s> from __future__ import unicode_literals <EOL> import json <EOL> import warnings <EOL> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> try : <EOL> from mock import Mock , patch <EOL> except : <EOL> from unittest . mock import Mock , patch <EOL> from contextlib import contextmanager <EOL> from flask import Flask <EOL> from nose . tools import assert_equal , assert_raises <EOL> try : <EOL> from nose . tools import assert_equal , assert_raises , assert_in , assert_not_in , assert_is_none <EOL> except : <EOL> class Dummy ( unittest . TestCase ) : <EOL> def nop ( ) : <EOL> pass <EOL> dummy = Dummy ( '<STR_LIT>' ) <EOL> assertions = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> for local_name , unittest_name in assertions . items ( ) : <EOL> vars ( ) [ local_name ] = getattr ( dummy , unittest_name ) <EOL> del Dummy <EOL> del dummy <EOL> def assert_data_equal ( tested , expected ) : <EOL> '''<STR_LIT>''' <EOL> tested = json . loads ( json . dumps ( tested ) ) <EOL> expected = json . loads ( json . dumps ( expected ) ) <EOL> assert_equal ( tested , expected ) <EOL> class TestCase ( unittest . TestCase ) : <EOL> '''<STR_LIT>''' <EOL> def setUp ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . app = Flask ( __name__ ) <EOL> @ contextmanager <EOL> def context ( self , ** kwargs ) : <EOL> with self . app . test_request_context ( '<STR_LIT:/>' , ** kwargs ) : <EOL> yield <EOL> @ contextmanager <EOL> def settings ( self , ** settings ) : <EOL> '''<STR_LIT>''' <EOL> original = { } <EOL> for key , value in settings . items ( ) : <EOL> original [ key ] = self . app . config . get ( key ) <EOL> self . app . config [ key ] = value <EOL> yield <EOL> for key , value in original . items ( ) : <EOL> self . app . config [ key ] = value <EOL> @ contextmanager <EOL> def assert_warning ( self , category = Warning ) : <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> yield <EOL> self . assertGreaterEqual ( len ( w ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> warning = w [ <NUM_LIT:0> ] <EOL> self . assertEqual ( warning . category , category , '<STR_LIT>' . format ( category . __name__ ) ) <EOL> def get ( self , url , ** kwargs ) : <EOL> with self . app . test_client ( ) as client : <EOL> return client . get ( url , ** kwargs ) <EOL> def post ( self , url , ** kwargs ) : <EOL> with self . app . test_client ( ) as client : <EOL> return client . post ( url , ** kwargs ) <EOL> def get_json ( self , url , status = <NUM_LIT:200> , ** kwargs ) : <EOL> response = self . get ( url , ** kwargs ) <EOL> self . assertEqual ( response . status_code , status ) <EOL> self . assertEqual ( response . content_type , '<STR_LIT:application/json>' ) <EOL> return json . loads ( response . data . decode ( '<STR_LIT:utf8>' ) ) <EOL> def get_specs ( self , prefix = '<STR_LIT>' , status = <NUM_LIT:200> , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return self . get_json ( '<STR_LIT>' . format ( prefix ) , status = status , ** kwargs ) <EOL> def assertDataEqual ( self , tested , expected ) : <EOL> '''<STR_LIT>''' <EOL> assert_data_equal ( tested , expected ) </s>
