<s> """<STR_LIT>""" <EOL> import json <EOL> import pytest <EOL> from flask import Response <EOL> from flask . testing import FlaskClient <EOL> from sqlalchemy . orm import sessionmaker <EOL> from app import db as db_ <EOL> from app import app , User , BlogPost , BlogComment , Log <EOL> from faker import Faker <EOL> Session = sessionmaker ( ) <EOL> fake = Faker ( ) <EOL> @ pytest . yield_fixture ( scope = '<STR_LIT>' ) <EOL> def flask_app ( ) : <EOL> """<STR_LIT>""" <EOL> ctx = app . app_context ( ) <EOL> ctx . push ( ) <EOL> yield app <EOL> ctx . pop ( ) <EOL> @ pytest . yield_fixture ( scope = '<STR_LIT>' ) <EOL> def db ( flask_app ) : <EOL> """<STR_LIT>""" <EOL> db_ . app = flask_app <EOL> db_ . drop_all ( ) <EOL> db_ . create_all ( ) <EOL> yield db_ <EOL> @ pytest . yield_fixture ( scope = '<STR_LIT>' ) <EOL> def session ( request , db ) : <EOL> """<STR_LIT>""" <EOL> connection = db . engine . connect ( ) <EOL> transaction = connection . begin ( ) <EOL> options = { '<STR_LIT>' : connection } <EOL> session = db . create_scoped_session ( options = options ) <EOL> yield session <EOL> transaction . rollback ( ) <EOL> connection . close ( ) <EOL> session . remove ( ) <EOL> class TestingResponse ( Response ) : <EOL> def validate ( self , status_code , error = None ) : <EOL> print ( self . data ) <EOL> assert self . status_code == status_code <EOL> assert self . headers [ '<STR_LIT:Content-Type>' ] == '<STR_LIT>' <EOL> if status_code != <NUM_LIT> : <EOL> self . json_data = json . loads ( self . data . decode ( ) ) <EOL> if error : <EOL> assert self . status_code == error . status_code <EOL> assert self . json_data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:code>' ] == error . code <EOL> assert self . json_data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:status>' <EOL> ] == error . status_code <EOL> return self <EOL> @ pytest . fixture <EOL> def client ( flask_app ) : <EOL> """<STR_LIT>""" <EOL> with FlaskClient ( flask_app , <EOL> use_cookies = True , <EOL> response_wrapper = TestingResponse ) as c : <EOL> return c <EOL> @ pytest . fixture <EOL> def user ( session ) : <EOL> new_user = User ( email = fake . email ( ) , <EOL> password = fake . sentence ( ) , <EOL> username = fake . user_name ( ) ) <EOL> session . add ( new_user ) <EOL> session . commit ( ) <EOL> return new_user <EOL> @ pytest . fixture <EOL> def post ( user , session ) : <EOL> new_post = BlogPost ( author = user , <EOL> title = fake . sentence ( ) , <EOL> content = fake . paragraph ( ) , <EOL> is_published = True ) <EOL> session . add ( new_post ) <EOL> session . commit ( ) <EOL> return new_post <EOL> @ pytest . fixture <EOL> def unpublished_post ( user , session ) : <EOL> new_post = BlogPost ( author = user , <EOL> title = fake . sentence ( ) , <EOL> content = fake . paragraph ( ) , <EOL> is_published = False ) <EOL> session . add ( new_post ) <EOL> session . commit ( ) <EOL> return new_post <EOL> @ pytest . fixture <EOL> def bunch_of_posts ( user , session ) : <EOL> for x in range ( <NUM_LIT:30> ) : <EOL> new_post = BlogPost ( author = user , <EOL> title = fake . sentence ( ) , <EOL> content = fake . paragraph ( ) , <EOL> is_published = fake . boolean ( ) ) <EOL> session . add ( new_post ) <EOL> new_post . comments . append ( BlogComment ( author = user , <EOL> content = fake . paragraph ( ) ) ) <EOL> session . commit ( ) <EOL> @ pytest . fixture <EOL> def comment ( user , post , session ) : <EOL> new_comment = BlogComment ( author = user , post = post , content = fake . paragraph ( ) ) <EOL> session . add ( new_comment ) <EOL> session . commit ( ) <EOL> return new_comment <EOL> @ pytest . fixture <EOL> def log ( user , post , session ) : <EOL> new_log = Log ( user = user , post = post ) <EOL> session . add ( new_log ) <EOL> session . commit ( ) <EOL> return new_log </s>
