<s> import unittest <EOL> from flask import Flask , request , Response <EOL> from werkzeug . exceptions import NotFound <EOL> from flask . ext . jsontools import jsonapi , FlaskJsonClient , JsonResponse , make_json_response <EOL> class TestJsonApi ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> users = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT:a>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT:b>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT:c>' } , <EOL> ] <EOL> self . app = app = Flask ( __name__ ) <EOL> self . app . debug = self . app . testing = True <EOL> self . app . test_client_class = FlaskJsonClient <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ jsonapi <EOL> def list_users ( ) : <EOL> return users <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ jsonapi <EOL> def get_user ( id ) : <EOL> try : <EOL> return { '<STR_LIT:user>' : list_users ( ) [ id - <NUM_LIT:1> ] } <EOL> except IndexError : <EOL> raise NotFound ( '<STR_LIT>' . format ( id ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ jsonapi <EOL> def patch_user ( id ) : <EOL> if id == <NUM_LIT:1> : <EOL> return { '<STR_LIT:error>' : '<STR_LIT>' } , <NUM_LIT> <EOL> req = request . get_json ( ) <EOL> users [ id - <NUM_LIT:1> ] = req [ '<STR_LIT:user>' ] <EOL> return users [ id - <NUM_LIT:1> ] <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> def delete_user ( id ) : <EOL> if id == <NUM_LIT:1> : <EOL> return JsonResponse ( { '<STR_LIT:error>' : '<STR_LIT>' } , <NUM_LIT> ) <EOL> del users [ id - <NUM_LIT:1> ] <EOL> return make_json_response ( True ) <EOL> def testList ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app . test_client ( ) as c : <EOL> rv = c . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT:200> ) <EOL> self . assertIsInstance ( rv , JsonResponse ) <EOL> self . assertEqual ( rv . get_json ( ) , [ { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT:a>' } , { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT:b>' } , { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT:c>' } ] ) <EOL> def testGet ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app . test_client ( ) as c : <EOL> rv = c . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT:200> ) <EOL> self . assertIsInstance ( rv , JsonResponse ) <EOL> self . assertEqual ( rv . get_json ( ) , { '<STR_LIT:user>' : { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT:a>' } } ) <EOL> rv = c . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT> ) <EOL> self . assertIsInstance ( rv , Response ) <EOL> self . assertIn ( '<STR_LIT>' , rv . get_data ( ) ) <EOL> def testUpdate ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app . test_client ( ) as c : <EOL> rv = c . patch ( '<STR_LIT>' ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT> ) <EOL> self . assertIsInstance ( rv , JsonResponse ) <EOL> self . assertEqual ( rv . get_json ( ) , { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> rv = c . patch ( '<STR_LIT>' , { '<STR_LIT:user>' : { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT:200> ) <EOL> self . assertIsInstance ( rv , JsonResponse ) <EOL> self . assertEqual ( rv . get_json ( ) , { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assertRaises ( IndexError , c . patch , '<STR_LIT>' , { '<STR_LIT:user>' : { } } ) <EOL> def testDelete ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app . test_client ( ) as c : <EOL> rv = c . delete ( '<STR_LIT>' ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT> ) <EOL> self . assertIsInstance ( rv , JsonResponse ) <EOL> self . assertEqual ( rv . get_json ( ) , { '<STR_LIT:error>' : '<STR_LIT>' } ) <EOL> rv = c . delete ( '<STR_LIT>' ) <EOL> self . assertEqual ( rv . status_code , <NUM_LIT:200> ) <EOL> self . assertIsInstance ( rv , JsonResponse ) <EOL> self . assertEqual ( rv . get_json ( ) , True ) </s>
