<s> from flask import Flask <EOL> from flask . ext . testing import TestCase as FlaskTestCase <EOL> from flask . ext . sqlalchemy import SQLAlchemy <EOL> from flask . ext . snooze import Snooze , SqlAlchemyEndpoint <EOL> from sqlalchemy . orm import object_mapper <EOL> from datetime import datetime <EOL> import re <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> """<STR_LIT>""" <EOL> def print_tb ( response ) : <EOL> from traceback import format_list <EOL> try : <EOL> response = response . json <EOL> print response [ '<STR_LIT:message>' ] <EOL> print <EOL> print '<STR_LIT:\n>' . join ( format_list ( response [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> except ( TypeError , KeyError ) : <EOL> pass <EOL> def data_model ( db ) : <EOL> class Book ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> created = db . Column ( db . DateTime ( timezone = False ) , default = datetime . utcnow ( ) ) <EOL> title = db . Column ( db . String ( <NUM_LIT> ) , unique = True , nullable = False ) <EOL> def __iter__ ( self ) : <EOL> self . _i = iter ( object_mapper ( self ) . columns ) <EOL> return self <EOL> def next ( self ) : <EOL> n = self . _i . next ( ) . name <EOL> a = getattr ( self , n ) <EOL> if type ( a ) == datetime : <EOL> a = str ( a ) <EOL> return n , a <EOL> return dict ( Book = Book ) <EOL> class TestSnoozeHttp ( FlaskTestCase ) : <EOL> """<STR_LIT>""" <EOL> def create_app ( self ) : <EOL> """<STR_LIT>""" <EOL> self . app = Flask ( __name__ ) <EOL> self . app . config [ '<STR_LIT>' ] = True <EOL> self . db = SQLAlchemy ( self . app ) <EOL> return self . app <EOL> def setUp ( self ) : <EOL> self . Book = data_model ( self . db ) [ '<STR_LIT>' ] <EOL> self . db . create_all ( ) <EOL> def create_mgr ( self ) : <EOL> """<STR_LIT>""" <EOL> return Snooze ( self . app ) <EOL> def assert_not_4xx ( self , response ) : <EOL> code = response . status_code <EOL> self . assertFalse ( str ( code ) . startswith ( '<STR_LIT:4>' ) , <EOL> "<STR_LIT>" % code ) <EOL> def test_rule_options ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> self . assert_not_4xx ( self . client . open ( '<STR_LIT>' , method = '<STR_LIT>' ) ) <EOL> def test_rule_list ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> print [ ( r . rule , r . methods , r . endpoint ) for r in self . app . url_map . iter_rules ( ) ] <EOL> self . assert_not_4xx ( self . client . get ( '<STR_LIT>' ) ) <EOL> def test_rule_post ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) , methods = ( '<STR_LIT:POST>' , '<STR_LIT>' ) ) <EOL> self . assert_not_4xx ( self . client . post ( '<STR_LIT>' ) ) <EOL> def test_rule_get ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> self . assert_404 ( self . client . get ( '<STR_LIT>' ) ) <EOL> def test_rule_head ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> self . assert_404 ( self . client . head ( '<STR_LIT>' ) ) <EOL> def test_rule_put ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> self . assert_not_4xx ( self . client . put ( '<STR_LIT>' ) ) <EOL> def test_rule_patch ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> r = self . client . patch ( '<STR_LIT>' ) <EOL> self . assert_404 ( r ) <EOL> def test_rule_delete ( self ) : <EOL> """<STR_LIT>""" <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> self . assert_404 ( self . client . delete ( '<STR_LIT>' ) ) <EOL> def test_verb_options ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> response = self . client . open ( '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> self . assert_200 ( response ) <EOL> options = json . loads ( response . data ) <EOL> print repr ( options ) <EOL> self . assertEqual ( set ( ( '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' ) ) , set ( options [ '<STR_LIT>' ] ) ) <EOL> print [ ( r . rule , r . methods , r . endpoint ) for r in self . app . url_map . iter_rules ( ) ] <EOL> self . assertEqual ( set ( ( '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) , set ( options [ '<STR_LIT>' ] ) ) <EOL> def test_verb_list ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> b1 = self . Book ( ) <EOL> b2 = self . Book ( ) <EOL> b3 = self . Book ( ) <EOL> b1 . title = '<STR_LIT>' <EOL> b2 . title = '<STR_LIT>' <EOL> b3 . title = '<STR_LIT>' <EOL> self . db . session . add ( b1 ) <EOL> self . db . session . add ( b2 ) <EOL> self . db . session . add ( b3 ) <EOL> self . db . session . flush ( ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> print_tb ( response ) <EOL> lst = json . loads ( response . data ) <EOL> self . assertEqual ( set ( [ b . id for b in ( b1 , b2 , b3 ) ] ) , set ( lst ) ) <EOL> def test_verb_post ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> title = "<STR_LIT>" <EOL> data_in = dict ( title = title ) <EOL> response = self . client . post ( '<STR_LIT>' , data = json . dumps ( data_in ) ) <EOL> print_tb ( response ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , response . headers ) <EOL> book_id = re . sub ( '<STR_LIT>' , '<STR_LIT>' , response . headers [ '<STR_LIT>' ] ) <EOL> book = self . Book . query . filter_by ( id = int ( book_id ) ) . first ( ) <EOL> self . assertEqual ( title , book . title ) <EOL> def test_verb_post_location ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> title = "<STR_LIT>" <EOL> data_in = dict ( title = title ) <EOL> response = self . client . post ( '<STR_LIT>' , data = json . dumps ( data_in ) ) <EOL> self . assertIn ( '<STR_LIT>' , response . headers ) <EOL> print "<STR_LIT>" , response . headers [ '<STR_LIT>' ] <EOL> self . assertEqual ( response . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_verb_post_500 ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> data_in = dict ( title = '<STR_LIT:title>' ) <EOL> response = self . client . post ( '<STR_LIT>' , data = json . dumps ( data_in ) ) <EOL> response = self . client . post ( '<STR_LIT>' , data = json . dumps ( data_in ) ) <EOL> print response . data <EOL> self . assertEqual ( response . status , '<STR_LIT>' ) <EOL> data_out = json . loads ( response . data ) <EOL> self . assertIn ( '<STR_LIT:type>' , data_out ) <EOL> self . assertIn ( '<STR_LIT:message>' , data_out ) <EOL> def test_verb_get ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> book = self . Book ( ) <EOL> book . title = '<STR_LIT:title>' <EOL> self . db . session . add ( book ) <EOL> self . db . session . flush ( ) <EOL> response = self . client . get ( '<STR_LIT>' % book . id ) <EOL> print_tb ( response ) <EOL> data = json . loads ( response . data ) <EOL> self . assertEqual ( data [ '<STR_LIT:title>' ] , book . title ) <EOL> def test_verb_get_404 ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> book = self . Book ( ) <EOL> book . title = '<STR_LIT:title>' <EOL> self . db . session . add ( book ) <EOL> self . db . session . flush ( ) <EOL> response = self . client . get ( '<STR_LIT>' % '<STR_LIT>' ) <EOL> print response . data <EOL> self . assert_404 ( response ) <EOL> data = json . loads ( response . data ) <EOL> print repr ( data ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertIn ( '<STR_LIT:class>' , data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ '<STR_LIT:class>' ] , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT:path>' , data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] , '<STR_LIT>' ) <EOL> def test_verb_head ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> book = self . Book ( ) <EOL> book . title = '<STR_LIT:title>' <EOL> self . db . session . add ( book ) <EOL> self . db . session . flush ( ) <EOL> response = self . client . head ( '<STR_LIT>' % book . id ) <EOL> self . assert_200 ( response ) <EOL> self . assertEqual ( response . data , '<STR_LIT>' ) <EOL> def test_verb_head_404 ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> response = self . client . head ( '<STR_LIT>' % '<STR_LIT>' ) <EOL> self . assert_404 ( response ) <EOL> self . assertEqual ( response . data , '<STR_LIT>' ) <EOL> def test_verb_put_existing ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> book = self . Book ( ) <EOL> book . title = '<STR_LIT:title>' <EOL> self . db . session . add ( book ) <EOL> self . db . session . flush ( ) <EOL> book_id = book . id <EOL> new_title = '<STR_LIT>' <EOL> data_in = json . dumps ( dict ( title = new_title ) ) <EOL> response = self . client . put ( '<STR_LIT>' % book . id , data = data_in ) <EOL> self . assert_200 ( response ) <EOL> book = self . Book . query . filter_by ( id = book_id ) . first ( ) <EOL> self . assertEqual ( book . title , new_title ) <EOL> def test_verb_put_not_existing ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> put_id = <NUM_LIT> <EOL> new_title = '<STR_LIT>' <EOL> data_in = json . dumps ( dict ( title = new_title ) ) <EOL> response = self . client . put ( '<STR_LIT>' % put_id , data = data_in ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> new_book_id = int ( re . sub ( '<STR_LIT>' , '<STR_LIT>' , response . headers [ '<STR_LIT>' ] ) ) <EOL> book = self . Book . query . filter_by ( id = put_id ) . first ( ) <EOL> self . assertEqual ( put_id , new_book_id ) <EOL> self . assertEqual ( book . title , new_title ) <EOL> def test_verb_patch_existing ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> book = self . Book ( ) <EOL> book . title = '<STR_LIT:title>' <EOL> self . db . session . add ( book ) <EOL> self . db . session . flush ( ) <EOL> book_id = book . id <EOL> new_title = '<STR_LIT>' <EOL> data_in = json . dumps ( dict ( title = new_title ) ) <EOL> response = self . client . patch ( '<STR_LIT>' % book . id , data = data_in ) <EOL> self . assert_200 ( response ) <EOL> book = self . Book . query . filter_by ( id = book_id ) . first ( ) <EOL> self . assertEqual ( book . title , new_title ) <EOL> def test_verb_patch_not_existing ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> put_id = <NUM_LIT> <EOL> new_title = '<STR_LIT>' <EOL> data_in = json . dumps ( dict ( title = new_title ) ) <EOL> response = self . client . patch ( '<STR_LIT>' % put_id , data = data_in ) <EOL> self . assert_404 ( response ) <EOL> def test_verb_delete_existing ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> book = self . Book ( ) <EOL> book . title = '<STR_LIT:title>' <EOL> self . db . session . add ( book ) <EOL> self . db . session . flush ( ) <EOL> book_id = book . id <EOL> response = self . client . delete ( '<STR_LIT>' % book . id ) <EOL> print_tb ( response ) <EOL> self . assert_200 ( response ) <EOL> book = self . Book . query . filter_by ( id = book_id ) . first ( ) <EOL> self . assertIs ( book , None ) <EOL> def test_verb_delete_not_existing ( self ) : <EOL> apimgr = self . create_mgr ( ) <EOL> apimgr . add ( SqlAlchemyEndpoint ( self . db , self . Book , [ '<STR_LIT:title>' ] ) ) <EOL> put_id = <NUM_LIT> <EOL> response = self . client . delete ( '<STR_LIT>' % put_id ) <EOL> self . assert_404 ( response ) </s>
