<s> from __future__ import absolute_import , division <EOL> import unittest <EOL> import json <EOL> import datetime <EOL> from flask import ( <EOL> Flask , <EOL> url_for , <EOL> ) <EOL> from flask_alchemyview import AlchemyView <EOL> from sqlalchemy import ( <EOL> create_engine , <EOL> Column , <EOL> Integer , <EOL> Unicode , <EOL> DateTime , <EOL> ) <EOL> from sqlalchemy . orm import sessionmaker <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> import colander as c <EOL> from dictalchemy import DictableModel <EOL> engine = create_engine ( '<STR_LIT>' ) <EOL> Base = declarative_base ( cls = DictableModel ) <EOL> class SimpleModel ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> name = Column ( Unicode ) <EOL> created = Column ( DateTime ) <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> class SimpleModelSchema ( c . MappingSchema ) : <EOL> name = c . SchemaNode ( c . String ( ) ) <EOL> class SimpleModelView ( AlchemyView ) : <EOL> model = SimpleModel <EOL> schema = SimpleModelSchema <EOL> session = None <EOL> max_page_limit = <NUM_LIT:20> <EOL> class TestSimpleModel ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> Base . metadata . create_all ( bind = engine ) <EOL> self . Session = sessionmaker ( bind = engine ) <EOL> self . session = self . Session ( ) <EOL> self . app = Flask ( '<STR_LIT>' , template_folder = "<STR_LIT>" ) <EOL> SimpleModelView . register ( self . app ) <EOL> SimpleModelView . session = self . session <EOL> self . session . query ( SimpleModel ) . delete ( ) <EOL> self . ctx = self . app . test_request_context ( ) <EOL> self . ctx . push ( ) <EOL> self . client = self . app . test_client ( ) <EOL> def tearDown ( self ) : <EOL> self . ctx . pop ( ) <EOL> def json_get ( self , url , follow_redirects = False ) : <EOL> """<STR_LIT>""" <EOL> return self . client . get ( url , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> headers = [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> follow_redirects = follow_redirects ) <EOL> def json_post ( self , url , data , follow_redirects = False ) : <EOL> """<STR_LIT>""" <EOL> return self . client . post ( url , <EOL> data = json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> headers = [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> follow_redirects = follow_redirects ) <EOL> def json_put ( self , url , data , follow_redirects = False ) : <EOL> """<STR_LIT>""" <EOL> return self . client . put ( url , <EOL> data = json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> headers = [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> follow_redirects = follow_redirects ) <EOL> def json_delete ( self , url , data = None , follow_redirects = False ) : <EOL> """<STR_LIT>""" <EOL> return self . client . delete ( url , <EOL> data = json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> headers = [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] , <EOL> follow_redirects = follow_redirects ) <EOL> def assert_redirects ( self , request , location , status_code = None ) : <EOL> """<STR_LIT>""" <EOL> if status_code is not None : <EOL> assert request . status_code == status_code <EOL> else : <EOL> assert request . status_code in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert request . location == location <EOL> def test_get_url ( self ) : <EOL> assert url_for ( '<STR_LIT>' , id = <NUM_LIT:1> ) == '<STR_LIT>' <EOL> def test_item_url ( self ) : <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> assert SimpleModelView ( ) . _item_url ( m ) == url_for ( '<STR_LIT>' , <EOL> id = m . id ) <EOL> def test_get ( self ) : <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> assert json . loads ( self . json_get ( url_for ( '<STR_LIT>' , <EOL> id = m . id ) ) . data . decode ( '<STR_LIT:utf-8>' ) ) == dict ( m ) <EOL> def test_jsonencoder_encodes_datetime ( self ) : <EOL> """<STR_LIT>""" <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> now = datetime . datetime . now ( ) <EOL> m . created = now <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> expected = dict ( m ) <EOL> expected [ '<STR_LIT>' ] = expected [ '<STR_LIT>' ] . isoformat ( ) <EOL> assert json . loads ( self . json_get ( url_for ( '<STR_LIT>' , <EOL> id = m . id ) ) . data . decode ( '<STR_LIT:utf-8>' ) ) == expected <EOL> def test_get_non_existing ( self ) : <EOL> model_id = <NUM_LIT> <EOL> model = self . session . query ( SimpleModel ) . get ( model_id ) <EOL> assert not model <EOL> response = self . json_get ( url_for ( '<STR_LIT>' , id = model_id ) ) <EOL> assert response . status_code == <NUM_LIT> <EOL> def test_get_invalid_id ( self ) : <EOL> response = self . json_get ( url_for ( '<STR_LIT>' , <EOL> id = u'<STR_LIT>' ) ) <EOL> assert response . status_code == <NUM_LIT> <EOL> def test_post ( self ) : <EOL> response = self . json_post ( url_for ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assert_redirects ( response , response . location ) <EOL> model_id = int ( response . location . split ( '<STR_LIT:/>' ) [ <NUM_LIT:4> ] ) <EOL> model = self . session . query ( SimpleModel ) . get ( model_id ) <EOL> assert model <EOL> assert model . asdict ( exclude = [ '<STR_LIT>' ] ) == { u'<STR_LIT:name>' : u'<STR_LIT>' , <EOL> u'<STR_LIT:id>' : model_id } <EOL> def test_post_with_missing_data ( self ) : <EOL> response = self . json_post ( url_for ( '<STR_LIT>' ) , { } ) <EOL> assert response . status_code == <NUM_LIT> <EOL> assert u'<STR_LIT:name>' in json . loads ( response . data . decode ( '<STR_LIT:utf-8>' ) ) [ '<STR_LIT>' ] <EOL> def test_put ( self ) : <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> model_id = m . id <EOL> response = self . json_put ( url_for ( '<STR_LIT>' , id = model_id ) , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assert_redirects ( response , response . location ) <EOL> m = self . session . query ( SimpleModel ) . get ( model_id ) <EOL> assert m . asdict ( exclude = [ '<STR_LIT>' ] ) == { u'<STR_LIT:name>' : u'<STR_LIT>' , <EOL> u'<STR_LIT:id>' : model_id } <EOL> def test_put_non_existing ( self ) : <EOL> model_id = <NUM_LIT> <EOL> model = self . session . query ( SimpleModel ) . get ( model_id ) <EOL> assert not model <EOL> response = self . json_put ( url_for ( '<STR_LIT>' , <EOL> id = model_id ) , { } ) <EOL> assert response . status_code == <NUM_LIT> <EOL> def test_delete_non_existing ( self ) : <EOL> model_id = <NUM_LIT> <EOL> model = self . session . query ( SimpleModel ) . get ( model_id ) <EOL> assert not model <EOL> response = self . json_get ( url_for ( '<STR_LIT>' , <EOL> id = model_id ) ) <EOL> assert response . status_code == <NUM_LIT> <EOL> def test_delete ( self ) : <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> model_id = m . id <EOL> response = self . json_delete ( url_for ( '<STR_LIT>' , <EOL> id = model_id ) ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> m = self . session . query ( SimpleModel ) . get ( model_id ) <EOL> assert not m <EOL> def get_with_limit ( self , limit ) : <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> m = SimpleModel ( u'<STR_LIT>' % i ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> return self . json_get ( url_for ( '<STR_LIT>' , limit = limit ) ) <EOL> def test_list_limit ( self ) : <EOL> response = self . get_with_limit ( <NUM_LIT:5> ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> assert len ( json . loads ( response . data . decode ( '<STR_LIT:utf-8>' ) ) [ '<STR_LIT>' ] ) == <NUM_LIT:5> <EOL> def test_list_limit_greater_than_max ( self ) : <EOL> response = self . get_with_limit ( <NUM_LIT:1000> ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> assert len ( json . loads ( response . data . decode ( '<STR_LIT:utf-8>' ) ) [ '<STR_LIT>' ] ) == <NUM_LIT:20> <EOL> def test_offset ( self ) : <EOL> self . session . query ( SimpleModel ) . delete ( ) <EOL> for i in range ( <NUM_LIT:20> ) : <EOL> m = SimpleModel ( u'<STR_LIT>' % i ) <EOL> m . id = i + <NUM_LIT:1> <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> response = self . json_get ( url_for ( '<STR_LIT>' , <EOL> sortby = '<STR_LIT:id>' , <EOL> offset = <NUM_LIT:10> ) ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> assert json . loads ( response . data . decode ( '<STR_LIT:utf-8>' ) ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] == <NUM_LIT:11> <EOL> def test_get_html ( self ) : <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> response = self . client . get ( url_for ( '<STR_LIT>' , <EOL> id = m . id ) ) <EOL> assert '<STR_LIT>' % m . id in response . data . decode ( '<STR_LIT:utf-8>' ) <EOL> def test_get_invalid_id_html ( self ) : <EOL> response = self . client . get ( url_for ( '<STR_LIT>' , <EOL> id = u'<STR_LIT>' ) ) <EOL> assert response . status_code == <NUM_LIT> <EOL> assert '<STR_LIT>' in response . data . decode ( '<STR_LIT:utf-8>' ) <EOL> assert '<STR_LIT>' in response . data . decode ( '<STR_LIT:utf-8>' ) <EOL> def test_invalid_offset_html ( self ) : <EOL> response = self . client . get ( url_for ( '<STR_LIT>' , <EOL> sortby = '<STR_LIT:id>' , <EOL> offset = '<STR_LIT>' ) ) <EOL> assert response . status_code == <NUM_LIT> <EOL> assert '<STR_LIT>' in response . data . decode ( '<STR_LIT:utf-8>' ) <EOL> assert '<STR_LIT>' in response . data . decode ( '<STR_LIT:utf-8>' ) <EOL> def test_missing_template_generates_406 ( self ) : <EOL> response = self . client . get ( url_for ( '<STR_LIT>' ) ) <EOL> assert response . status_code == <NUM_LIT> <EOL> def test_before_render_get ( self ) : <EOL> before_data = '<STR_LIT>' <EOL> def fn ( self , data ) : <EOL> return { '<STR_LIT>' : before_data } <EOL> setattr ( SimpleModelView , '<STR_LIT>' , fn ) <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> response = self . client . get ( url_for ( '<STR_LIT>' , id = m . id ) ) <EOL> assert before_data in response . data . decode ( '<STR_LIT:utf-8>' ) <EOL> delattr ( SimpleModelView , '<STR_LIT>' ) <EOL> def test_put_missing_data_not_accept_json ( self ) : <EOL> m = SimpleModel ( u'<STR_LIT:name>' ) <EOL> self . session . add ( m ) <EOL> self . session . flush ( ) <EOL> model_id = m . id <EOL> response = self . client . put ( <EOL> url_for ( '<STR_LIT>' , <EOL> id = model_id ) , <EOL> data = json . dumps ( { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) </s>
