<s> from tastypie . test import ResourceTestCase <EOL> from core . interface . static_intr . models import StaticIntrKeyValue <EOL> import simplejson as json <EOL> from mozdns . tests . utils import random_label <EOL> API_VERSION = '<STR_LIT:1>' <EOL> class KVAPITests ( object ) : <EOL> object_list_url = "<STR_LIT>" <EOL> object_url = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> super ( KVAPITests , self ) . setUp ( ) <EOL> def test_create ( self ) : <EOL> resp , post_data = self . generic_create ( self . post_data ( ) ) <EOL> _ , _ , ( _ , new_object_url ) = resp . items ( ) <EOL> new_resp = self . api_client . get ( new_object_url , format = '<STR_LIT>' ) <EOL> self . assertValidJSONResponse ( new_resp ) <EOL> new_obj_data = json . loads ( new_resp . content ) <EOL> for key in post_data . keys ( ) : <EOL> self . assertEqual ( post_data [ key ] , new_obj_data [ key ] ) <EOL> def test_update ( self ) : <EOL> resp , post_data = self . generic_create ( self . post_data ( ) ) <EOL> _ , _ , ( _ , new_object_url ) = resp . items ( ) <EOL> update_resp , patch_data = self . generic_update ( new_object_url , <EOL> self . post_data ( ) ) <EOL> patch_resp = self . api_client . get ( new_object_url , format = '<STR_LIT>' ) <EOL> self . assertValidJSONResponse ( patch_resp ) <EOL> patch_obj_data = json . loads ( patch_resp . content ) <EOL> for key in patch_data . keys ( ) : <EOL> self . assertEqual ( patch_data [ key ] , patch_obj_data [ key ] ) <EOL> def test_delete ( self ) : <EOL> obj_count = self . test_type . objects . count ( ) <EOL> resp , post_data = self . generic_create ( self . post_data ( ) ) <EOL> _ , _ , ( _ , new_object_url ) = resp . items ( ) <EOL> self . assertEqual ( self . test_type . objects . count ( ) , obj_count + <NUM_LIT:1> ) <EOL> resp = self . api_client . delete ( new_object_url , format = '<STR_LIT>' ) <EOL> self . assertHttpAccepted ( resp ) <EOL> self . assertEqual ( self . test_type . objects . count ( ) , obj_count ) <EOL> def generic_update ( self , patch_url , patch_data ) : <EOL> obj_count = self . test_type . objects . count ( ) <EOL> resp = self . api_client . patch ( patch_url , format = '<STR_LIT>' , <EOL> data = patch_data ) <EOL> self . assertHttpAccepted ( resp ) <EOL> self . assertEqual ( self . test_type . objects . count ( ) , obj_count ) <EOL> return resp , patch_data <EOL> def generic_create ( self , post_data ) : <EOL> obj_count = self . test_type . objects . count ( ) <EOL> create_url = self . object_list_url . format ( <EOL> API_VERSION , str ( self . test_type . __name__ ) . lower ( ) ) <EOL> resp = self . api_client . post ( create_url , format = '<STR_LIT>' , data = post_data ) <EOL> self . assertHttpCreated ( resp ) <EOL> self . assertEqual ( self . test_type . objects . count ( ) , obj_count + <NUM_LIT:1> ) <EOL> return resp , post_data <EOL> def test_bad_value_create ( self ) : <EOL> post_data = self . bad_post_data ( ) <EOL> obj_count = self . test_type . objects . count ( ) <EOL> create_url = self . object_list_url . format ( <EOL> API_VERSION , str ( self . test_type . __name__ ) . lower ( ) ) <EOL> resp = self . api_client . post ( create_url , format = '<STR_LIT>' , data = post_data ) <EOL> self . assertHttpBadRequest ( resp ) <EOL> self . assertEqual ( self . test_type . objects . count ( ) , obj_count ) <EOL> def test_bad_value_update ( self ) : <EOL> good_post_data = self . good_post_data ( ) <EOL> resp , post_data = self . generic_create ( good_post_data ) <EOL> self . assertHTTPCreated ( resp ) <EOL> _ , _ , ( _ , new_object_url ) = resp . items ( ) <EOL> resp = self . api_client . patch ( new_object_url , format = '<STR_LIT>' , <EOL> data = self . bad_post_data ( ) ) <EOL> self . assertHttpBadRequest ( resp ) <EOL> new_resp = self . api_client . get ( new_object_url , format = '<STR_LIT>' ) <EOL> self . assertValidJSONResponse ( new_resp ) <EOL> new_obj_data = json . loads ( new_resp . content ) <EOL> self . assertEqual ( good_post_data , new_obj_data ) <EOL> class StaticIntrKVAPITests ( KVAPITests , ResourceTestCase ) : <EOL> test_type = StaticIntrKeyValue <EOL> def post_data ( self ) : <EOL> return { <EOL> '<STR_LIT:key>' : random_label ( ) , <EOL> '<STR_LIT:value>' : random_label ( ) , <EOL> } <EOL> def bad_post_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:key>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' <EOL> } <EOL> def good_post_data ( self ) : <EOL> return { <EOL> '<STR_LIT:key>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' <EOL> } </s>
